blob: c0f0af1532f3f2ebb6b1a6074259712ea6ad90a2 [file] [log] [blame]
Bri Prebilic Cole96f26472015-03-31 13:07:05 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.ui.impl;
17
18import com.fasterxml.jackson.databind.node.ArrayNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import com.google.common.collect.ImmutableSet;
21import org.onosproject.core.ApplicationId;
22import org.onosproject.net.intent.Intent;
23import org.onosproject.net.intent.IntentService;
24
25import java.util.ArrayList;
26import java.util.Arrays;
27import java.util.List;
28
29/**
30 * Message handler for intent view related messages.
31 */
32public class IntentViewMessageHandler extends AbstractTabularViewMessageHandler {
33
34 /**
35 * Creates a new message handler for the intent messages.
36 */
37 protected IntentViewMessageHandler() {
38 super(ImmutableSet.of("intentDataRequest"));
39 }
40
41 @Override
42 public void process(ObjectNode message) {
43 ObjectNode payload = payload(message);
44 String sortCol = string(payload, "sortCol", "appId");
45 String sortDir = string(payload, "sortDir", "asc");
46
47 IntentService service = get(IntentService.class);
48 TableRow[] rows = generateTableRows(service);
49 RowComparator rc =
50 new RowComparator(sortCol, RowComparator.direction(sortDir));
51 Arrays.sort(rows, rc);
52 ArrayNode intents = generateArrayNode(rows);
53 ObjectNode rootNode = mapper.createObjectNode();
54 rootNode.set("intents", intents);
55
56 connection().sendMessage("intentDataResponse", 0, rootNode);
57 }
58
59 private TableRow[] generateTableRows(IntentService service) {
60 List<TableRow> list = new ArrayList<>();
61 for (Intent intent : service.getIntents()) {
62 list.add(new IntentTableRow(intent));
63 }
64 return list.toArray(new TableRow[list.size()]);
65 }
66
67 /**
68 * TableRow implementation for {@link Intent intents}.
69 */
70 private static class IntentTableRow extends AbstractTableRow {
71
72 private static final String APP_ID = "appId";
73 private static final String KEY = "key";
74 private static final String TYPE = "type";
75 private static final String PRIORITY = "priority";
76
77 private static final String[] COL_IDS = {
78 APP_ID, KEY, TYPE, PRIORITY
79 };
80
81 public IntentTableRow(Intent i) {
82 ApplicationId appid = i.appId();
83
84 add(APP_ID, String.valueOf(appid.id()) + " : " + appid.name());
85 add(KEY, i.key().toString());
86 add(TYPE, i.getClass().getSimpleName());
87 add(PRIORITY, Integer.toString(i.priority()));
88 }
89
90 @Override
91 protected String[] columnIds() {
92 return COL_IDS;
93 }
94 }
95
96}