blob: ac3fb92484e063fa1d4d9e1fdd35bea821ada4e7 [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";
Bri Prebilic Coleab582b82015-04-14 15:08:22 -070079 private static final String STATE_IID = "_iconid_state";
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070080 private static final String UPDATED = "updated";
81
82 private static final String[] COL_IDS = {
Bri Prebilic Coleab582b82015-04-14 15:08:22 -070083 ID, IP, TCP_PORT, STATE_IID, UPDATED
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070084 };
85
Bri Prebilic Coleab582b82015-04-14 15:08:22 -070086 private static final String ICON_ID_ONLINE = "active";
87 private static final String ICON_ID_OFFLINE = "inactive";
88
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070089 public ControllerNodeTableRow(ClusterService service, ControllerNode n) {
90 NodeId id = n.id();
91 DateTime lastUpdated = service.getLastUpdated(id);
92 org.joda.time.format.DateTimeFormatter format = DateTimeFormat.longTime();
Bri Prebilic Coleab582b82015-04-14 15:08:22 -070093 String iconId = (service.getState(id) == ControllerNode.State.ACTIVE) ?
94 ICON_ID_ONLINE : ICON_ID_OFFLINE;
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070095
96 add(ID, id.toString());
97 add(IP, n.ip().toString());
98 add(TCP_PORT, Integer.toString(n.tcpPort()));
Bri Prebilic Coleab582b82015-04-14 15:08:22 -070099 add(STATE_IID, iconId);
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -0700100 add(UPDATED, format.print(lastUpdated));
101 }
102
103 @Override
104 protected String[] columnIds() {
105 return COL_IDS;
106 }
107 }
108
109}