blob: b88364ef4c4d5aa53e20b9e057fc3dd53bb179bf [file] [log] [blame]
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -07003 *
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
Viswanath KSP116f0c12016-11-21 00:02:51 +053019import com.fasterxml.jackson.databind.node.ArrayNode;
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070020import com.fasterxml.jackson.databind.node.ObjectNode;
21import com.google.common.collect.ImmutableSet;
22import org.joda.time.DateTime;
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070023import org.onosproject.cluster.ClusterService;
24import org.onosproject.cluster.ControllerNode;
25import org.onosproject.cluster.NodeId;
Viswanath KSP32322862017-01-16 18:33:05 +053026import org.onosproject.mastership.MastershipService;
Viswanath KSP116f0c12016-11-21 00:02:51 +053027import org.onosproject.net.Device;
28import org.onosproject.net.device.DeviceService;
Simon Huntd2747a02015-04-30 22:41:16 -070029import org.onosproject.ui.RequestHandler;
Simon Hunta0ddb022015-05-01 09:53:01 -070030import org.onosproject.ui.UiMessageHandler;
Simon Hunt3d1b0652015-05-05 17:27:24 -070031import org.onosproject.ui.table.TableModel;
Simon Huntabd16f62015-05-01 13:14:40 -070032import org.onosproject.ui.table.TableRequestHandler;
Simon Hunt3d1b0652015-05-05 17:27:24 -070033import org.onosproject.ui.table.cell.TimeFormatter;
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070034
Simon Huntd2747a02015-04-30 22:41:16 -070035import java.util.Collection;
Viswanath KSP116f0c12016-11-21 00:02:51 +053036import java.util.List;
37import java.util.stream.Collectors;
38
39import static com.google.common.collect.ImmutableList.copyOf;
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070040
41
42/**
43 * Message handler for cluster view related messages.
44 */
Simon Hunta0ddb022015-05-01 09:53:01 -070045public class ClusterViewMessageHandler extends UiMessageHandler {
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070046
Simon Huntd2747a02015-04-30 22:41:16 -070047 private static final String CLUSTER_DATA_REQ = "clusterDataRequest";
Simon Huntabd16f62015-05-01 13:14:40 -070048 private static final String CLUSTER_DATA_RESP = "clusterDataResponse";
49 private static final String CLUSTERS = "clusters";
50
Simon Hunta271e0a2016-10-26 10:59:14 -070051 private static final String CLUSTER_DETAILS_REQ = "clusterDetailsRequest";
52 private static final String CLUSTER_DETAILS_RESP = "clusterDetailsResponse";
Viswanath KSPdf11ea82016-10-25 01:15:16 +053053 private static final String DETAILS = "details";
54
Viswanath KSP116f0c12016-11-21 00:02:51 +053055 private static final String DEVICES = "devices";
Simon Huntabd16f62015-05-01 13:14:40 -070056 private static final String ID = "id";
57 private static final String IP = "ip";
58 private static final String TCP_PORT = "tcp";
59 private static final String STATE_IID = "_iconid_state";
Thomas Vachuska7a8de842016-03-07 20:56:35 -080060 private static final String STARTED_IID = "_iconid_started";
Simon Huntabd16f62015-05-01 13:14:40 -070061 private static final String UPDATED = "updated";
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070062
Simon Hunt3d1b0652015-05-05 17:27:24 -070063 private static final String[] COL_IDS = {
Thomas Vachuska7a8de842016-03-07 20:56:35 -080064 ID, IP, TCP_PORT, STATE_IID, STARTED_IID, UPDATED
Simon Hunt3d1b0652015-05-05 17:27:24 -070065 };
66
Viswanath KSP116f0c12016-11-21 00:02:51 +053067 private static final String URI = "id";
68 private static final String TYPE = "type";
69 private static final String CHASSIS_ID = "chassisid";
70 private static final String HW = "hw";
71 private static final String SW = "sw";
72 private static final String MFR = "mfr";
73 private static final String PROTOCOL = "protocol";
74 private static final String SERIAL = "serial";
75
76
Simon Hunt3d1b0652015-05-05 17:27:24 -070077 private static final String ICON_ID_ONLINE = "active";
78 private static final String ICON_ID_OFFLINE = "inactive";
79
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070080 @Override
Simon Huntda580882015-05-12 20:58:18 -070081 protected Collection<RequestHandler> createRequestHandlers() {
Viswanath KSPdf11ea82016-10-25 01:15:16 +053082 return ImmutableSet.of(
83 new ClusterDataRequest(),
84 new DetailRequestHandler());
Simon Huntd2747a02015-04-30 22:41:16 -070085 }
86
Simon Huntabd16f62015-05-01 13:14:40 -070087 // handler for cluster table requests
88 private final class ClusterDataRequest extends TableRequestHandler {
Jian Li69f66632016-01-15 12:27:42 -080089 private static final String NO_ROWS_MESSAGE = "No cluster nodes found";
90
Simon Huntd2747a02015-04-30 22:41:16 -070091 private ClusterDataRequest() {
Simon Huntabd16f62015-05-01 13:14:40 -070092 super(CLUSTER_DATA_REQ, CLUSTER_DATA_RESP, CLUSTERS);
Simon Huntd2747a02015-04-30 22:41:16 -070093 }
94
95 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -070096 protected String[] getColumnIds() {
97 return COL_IDS;
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070098 }
99
100 @Override
Jian Li8baf4472016-01-15 15:08:09 -0800101 protected String noRowsMessage(ObjectNode payload) {
Jian Li69f66632016-01-15 12:27:42 -0800102 return NO_ROWS_MESSAGE;
103 }
104
105 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -0700106 protected TableModel createTableModel() {
107 TableModel tm = super.createTableModel();
Simon Hunt5939e652015-05-06 16:20:23 -0700108 tm.setFormatter(UPDATED, new TimeFormatter());
Simon Hunt3d1b0652015-05-05 17:27:24 -0700109 return tm;
110 }
111
112 @Override
113 protected void populateTable(TableModel tm, ObjectNode payload) {
114 ClusterService cs = get(ClusterService.class);
115 for (ControllerNode node : cs.getNodes()) {
116 populateRow(tm.addRow(), node, cs);
117 }
118 }
119
120 private void populateRow(TableModel.Row row, ControllerNode node,
121 ClusterService cs) {
122 NodeId id = node.id();
123 DateTime lastUpdated = cs.getLastUpdated(id);
Thomas Vachuska7a8de842016-03-07 20:56:35 -0800124 ControllerNode.State state = cs.getState(id);
125 String iconId = state.isActive() ? ICON_ID_ONLINE : ICON_ID_OFFLINE;
126 String startedId = state.isReady() ? ICON_ID_ONLINE : ICON_ID_OFFLINE;
Simon Hunt3d1b0652015-05-05 17:27:24 -0700127
128 row.cell(ID, id)
129 .cell(IP, node.ip())
130 .cell(TCP_PORT, node.tcpPort())
131 .cell(STATE_IID, iconId)
Thomas Vachuska7a8de842016-03-07 20:56:35 -0800132 .cell(STARTED_IID, startedId)
Simon Hunt3d1b0652015-05-05 17:27:24 -0700133 .cell(UPDATED, lastUpdated);
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -0700134 }
135 }
Viswanath KSPdf11ea82016-10-25 01:15:16 +0530136
137 private final class DetailRequestHandler extends RequestHandler {
138
139 public DetailRequestHandler() {
140 super(CLUSTER_DETAILS_REQ);
141 }
142
Viswanath KSP32322862017-01-16 18:33:05 +0530143 private List<Device> populateDevices(ControllerNode node) {
Viswanath KSP116f0c12016-11-21 00:02:51 +0530144 DeviceService ds = get(DeviceService.class);
Viswanath KSP32322862017-01-16 18:33:05 +0530145 MastershipService ms = get(MastershipService.class);
Viswanath KSP116f0c12016-11-21 00:02:51 +0530146 return copyOf(ds.getDevices()).stream()
Viswanath KSP32322862017-01-16 18:33:05 +0530147 .filter(d -> ms.getMasterFor(d.id()).equals(node.id()))
Viswanath KSP116f0c12016-11-21 00:02:51 +0530148 .collect(Collectors.toList());
149 }
150
151 private String deviceProtocol(Device device) {
152 String protocol = device.annotations().value(PROTOCOL);
153 return protocol != null ? protocol : "";
154 }
155
156 private ObjectNode deviceData(Device d) {
157 ObjectNode device = objectNode();
158
159 device.put(URI, d.id().toString());
160 device.put(TYPE, d.type().toString());
161 device.put(CHASSIS_ID, d.chassisId().toString());
162 device.put(MFR, d.manufacturer());
163 device.put(HW, d.hwVersion());
164 device.put(SW, d.swVersion());
165 device.put(PROTOCOL, deviceProtocol(d));
166 device.put(SERIAL, d.serialNumber());
167
168 return device;
169 }
170
Viswanath KSPdf11ea82016-10-25 01:15:16 +0530171 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800172 public void process(ObjectNode payload) {
Viswanath KSPdf11ea82016-10-25 01:15:16 +0530173
174 String id = string(payload, ID);
175 ClusterService cs = get(ClusterService.class);
176 ControllerNode node = cs.getNode(new NodeId(id));
177
178 ObjectNode data = objectNode();
Viswanath KSP116f0c12016-11-21 00:02:51 +0530179 ArrayNode devices = arrayNode();
Viswanath KSP32322862017-01-16 18:33:05 +0530180 List<Device> deviceList = populateDevices(node);
Viswanath KSP116f0c12016-11-21 00:02:51 +0530181
Viswanath KSPdf11ea82016-10-25 01:15:16 +0530182 data.put(ID, node.id().toString());
183 data.put(IP, node.ip().toString());
184
Viswanath KSP116f0c12016-11-21 00:02:51 +0530185 for (Device d : deviceList) {
186 devices.add(deviceData(d));
187 }
188
189 data.set(DEVICES, devices);
190
Viswanath KSPdf11ea82016-10-25 01:15:16 +0530191 //TODO put more detail info to data
192
193 ObjectNode rootNode = objectNode();
194 rootNode.set(DETAILS, data);
195 sendMessage(CLUSTER_DETAILS_RESP, rootNode);
196 }
197 }
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -0700198}