blob: b1d4c98f981889209e820653618d5dfc1d5d9bb4 [file] [log] [blame]
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -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 */
16
17package org.onosproject.ui.impl;
18
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import com.google.common.collect.ImmutableSet;
22import org.joda.time.DateTime;
23import org.joda.time.format.DateTimeFormat;
24import org.onosproject.cluster.ClusterService;
25import org.onosproject.cluster.ControllerNode;
26import org.onosproject.cluster.NodeId;
27
28import java.util.ArrayList;
29import java.util.Arrays;
30import java.util.List;
31
32
33/**
34 * Message handler for cluster view related messages.
35 */
36public class ClusterViewMessageHandler extends AbstractTabularViewMessageHandler {
37
38 /**
39 * Creates a new message handler for the cluster messages.
40 */
41 protected ClusterViewMessageHandler() {
42 super(ImmutableSet.of("clusterDataRequest"));
43 }
44
45 @Override
46 public void process(ObjectNode message) {
47 ObjectNode payload = payload(message);
48 String sortCol = string(payload, "sortCol", "id");
49 String sortDir = string(payload, "sortDir", "asc");
50
51 ClusterService service = get(ClusterService.class);
52 TableRow[] rows = generateTableRows(service);
53 RowComparator rc =
54 new RowComparator(sortCol, RowComparator.direction(sortDir));
55 Arrays.sort(rows, rc);
56 ArrayNode clusterNodes = generateArrayNode(rows);
57 ObjectNode rootNode = mapper.createObjectNode();
58 rootNode.set("clusters", clusterNodes);
59
60 connection().sendMessage("clusterDataResponse", 0, rootNode);
61 }
62
63 private TableRow[] generateTableRows(ClusterService service) {
64 List<TableRow> list = new ArrayList<>();
65 for (ControllerNode node : service.getNodes()) {
66 list.add(new ControllerNodeTableRow(service, node));
67 }
68 return list.toArray(new TableRow[list.size()]);
69 }
70
71 /**
72 * TableRow implementation for {@link ControllerNode controller nodes}.
73 */
74 private static class ControllerNodeTableRow extends AbstractTableRow {
75
76 private static final String ID = "id";
77 private static final String IP = "ip";
78 private static final String TCP_PORT = "tcp";
79 private static final String STATE = "state";
80 private static final String UPDATED = "updated";
81
82 private static final String[] COL_IDS = {
83 ID, IP, TCP_PORT, STATE, UPDATED
84 };
85
86 public ControllerNodeTableRow(ClusterService service, ControllerNode n) {
87 NodeId id = n.id();
88 DateTime lastUpdated = service.getLastUpdated(id);
89 org.joda.time.format.DateTimeFormatter format = DateTimeFormat.longTime();
90
91 add(ID, id.toString());
92 add(IP, n.ip().toString());
93 add(TCP_PORT, Integer.toString(n.tcpPort()));
94 add(STATE, service.getState(id).toString());
95 add(UPDATED, format.print(lastUpdated));
96 }
97
98 @Override
99 protected String[] columnIds() {
100 return COL_IDS;
101 }
102 }
103
104}