blob: 7243818bbe5a71a9463dc8f3009b7dfba6fcb7a2 [file] [log] [blame]
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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;
pierventrefa5dc3c2021-11-05 15:37:32 +010022import org.onlab.packet.IpAddress;
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
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -070035import java.time.Instant;
Simon Huntd2747a02015-04-30 22:41:16 -070036import java.util.Collection;
Viswanath KSP116f0c12016-11-21 00:02:51 +053037import java.util.List;
38import java.util.stream.Collectors;
39
40import static com.google.common.collect.ImmutableList.copyOf;
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070041
42
43/**
44 * Message handler for cluster view related messages.
45 */
Simon Hunta0ddb022015-05-01 09:53:01 -070046public class ClusterViewMessageHandler extends UiMessageHandler {
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070047
Simon Huntd2747a02015-04-30 22:41:16 -070048 private static final String CLUSTER_DATA_REQ = "clusterDataRequest";
Simon Huntabd16f62015-05-01 13:14:40 -070049 private static final String CLUSTER_DATA_RESP = "clusterDataResponse";
50 private static final String CLUSTERS = "clusters";
51
Simon Hunta271e0a2016-10-26 10:59:14 -070052 private static final String CLUSTER_DETAILS_REQ = "clusterDetailsRequest";
53 private static final String CLUSTER_DETAILS_RESP = "clusterDetailsResponse";
Viswanath KSPdf11ea82016-10-25 01:15:16 +053054 private static final String DETAILS = "details";
55
Viswanath KSP116f0c12016-11-21 00:02:51 +053056 private static final String DEVICES = "devices";
Simon Huntabd16f62015-05-01 13:14:40 -070057 private static final String ID = "id";
58 private static final String IP = "ip";
59 private static final String TCP_PORT = "tcp";
60 private static final String STATE_IID = "_iconid_state";
Thomas Vachuska7a8de842016-03-07 20:56:35 -080061 private static final String STARTED_IID = "_iconid_started";
Simon Huntabd16f62015-05-01 13:14:40 -070062 private static final String UPDATED = "updated";
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070063
Simon Hunt3d1b0652015-05-05 17:27:24 -070064 private static final String[] COL_IDS = {
Thomas Vachuska7a8de842016-03-07 20:56:35 -080065 ID, IP, TCP_PORT, STATE_IID, STARTED_IID, UPDATED
Simon Hunt3d1b0652015-05-05 17:27:24 -070066 };
67
Viswanath KSP116f0c12016-11-21 00:02:51 +053068 private static final String URI = "id";
69 private static final String TYPE = "type";
70 private static final String CHASSIS_ID = "chassisid";
71 private static final String HW = "hw";
72 private static final String SW = "sw";
73 private static final String MFR = "mfr";
74 private static final String PROTOCOL = "protocol";
75 private static final String SERIAL = "serial";
76
77
Simon Hunt3d1b0652015-05-05 17:27:24 -070078 private static final String ICON_ID_ONLINE = "active";
79 private static final String ICON_ID_OFFLINE = "inactive";
80
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070081 @Override
Simon Huntda580882015-05-12 20:58:18 -070082 protected Collection<RequestHandler> createRequestHandlers() {
Viswanath KSPdf11ea82016-10-25 01:15:16 +053083 return ImmutableSet.of(
84 new ClusterDataRequest(),
85 new DetailRequestHandler());
Simon Huntd2747a02015-04-30 22:41:16 -070086 }
87
Simon Huntabd16f62015-05-01 13:14:40 -070088 // handler for cluster table requests
89 private final class ClusterDataRequest extends TableRequestHandler {
Jian Li69f66632016-01-15 12:27:42 -080090 private static final String NO_ROWS_MESSAGE = "No cluster nodes found";
91
Simon Huntd2747a02015-04-30 22:41:16 -070092 private ClusterDataRequest() {
Simon Huntabd16f62015-05-01 13:14:40 -070093 super(CLUSTER_DATA_REQ, CLUSTER_DATA_RESP, CLUSTERS);
Simon Huntd2747a02015-04-30 22:41:16 -070094 }
95
96 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -070097 protected String[] getColumnIds() {
98 return COL_IDS;
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070099 }
100
101 @Override
Jian Li8baf4472016-01-15 15:08:09 -0800102 protected String noRowsMessage(ObjectNode payload) {
Jian Li69f66632016-01-15 12:27:42 -0800103 return NO_ROWS_MESSAGE;
104 }
105
106 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -0700107 protected TableModel createTableModel() {
108 TableModel tm = super.createTableModel();
Simon Hunt5939e652015-05-06 16:20:23 -0700109 tm.setFormatter(UPDATED, new TimeFormatter());
Simon Hunt3d1b0652015-05-05 17:27:24 -0700110 return tm;
111 }
112
113 @Override
114 protected void populateTable(TableModel tm, ObjectNode payload) {
115 ClusterService cs = get(ClusterService.class);
116 for (ControllerNode node : cs.getNodes()) {
117 populateRow(tm.addRow(), node, cs);
118 }
119 }
120
121 private void populateRow(TableModel.Row row, ControllerNode node,
122 ClusterService cs) {
123 NodeId id = node.id();
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -0700124 Instant lastUpdated = cs.getLastUpdatedInstant(id);
Thomas Vachuska7a8de842016-03-07 20:56:35 -0800125 ControllerNode.State state = cs.getState(id);
126 String iconId = state.isActive() ? ICON_ID_ONLINE : ICON_ID_OFFLINE;
127 String startedId = state.isReady() ? ICON_ID_ONLINE : ICON_ID_OFFLINE;
Simon Hunt3d1b0652015-05-05 17:27:24 -0700128
129 row.cell(ID, id)
130 .cell(IP, node.ip())
131 .cell(TCP_PORT, node.tcpPort())
132 .cell(STATE_IID, iconId)
Thomas Vachuska7a8de842016-03-07 20:56:35 -0800133 .cell(STARTED_IID, startedId)
Simon Hunt3d1b0652015-05-05 17:27:24 -0700134 .cell(UPDATED, lastUpdated);
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -0700135 }
136 }
Viswanath KSPdf11ea82016-10-25 01:15:16 +0530137
138 private final class DetailRequestHandler extends RequestHandler {
139
140 public DetailRequestHandler() {
141 super(CLUSTER_DETAILS_REQ);
142 }
143
Viswanath KSP32322862017-01-16 18:33:05 +0530144 private List<Device> populateDevices(ControllerNode node) {
Viswanath KSP116f0c12016-11-21 00:02:51 +0530145 DeviceService ds = get(DeviceService.class);
Viswanath KSP32322862017-01-16 18:33:05 +0530146 MastershipService ms = get(MastershipService.class);
Viswanath KSP116f0c12016-11-21 00:02:51 +0530147 return copyOf(ds.getDevices()).stream()
Viswanath KSP32322862017-01-16 18:33:05 +0530148 .filter(d -> ms.getMasterFor(d.id()).equals(node.id()))
Viswanath KSP116f0c12016-11-21 00:02:51 +0530149 .collect(Collectors.toList());
150 }
151
152 private String deviceProtocol(Device device) {
153 String protocol = device.annotations().value(PROTOCOL);
154 return protocol != null ? protocol : "";
155 }
156
157 private ObjectNode deviceData(Device d) {
158 ObjectNode device = objectNode();
159
160 device.put(URI, d.id().toString());
161 device.put(TYPE, d.type().toString());
162 device.put(CHASSIS_ID, d.chassisId().toString());
163 device.put(MFR, d.manufacturer());
164 device.put(HW, d.hwVersion());
165 device.put(SW, d.swVersion());
166 device.put(PROTOCOL, deviceProtocol(d));
167 device.put(SERIAL, d.serialNumber());
168
169 return device;
170 }
171
Viswanath KSPdf11ea82016-10-25 01:15:16 +0530172 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800173 public void process(ObjectNode payload) {
pierventrefa5dc3c2021-11-05 15:37:32 +0100174 ObjectNode rootNode = objectNode();
175 ObjectNode data = objectNode();
176 ArrayNode devices = arrayNode();
Viswanath KSPdf11ea82016-10-25 01:15:16 +0530177
178 String id = string(payload, ID);
179 ClusterService cs = get(ClusterService.class);
180 ControllerNode node = cs.getNode(new NodeId(id));
pierventrefa5dc3c2021-11-05 15:37:32 +0100181 if (node != null) {
182 IpAddress nodeIp = node.ip();
183 List<Device> deviceList = populateDevices(node);
Viswanath KSPdf11ea82016-10-25 01:15:16 +0530184
pierventrefa5dc3c2021-11-05 15:37:32 +0100185 data.put(ID, node.id().toString());
186 data.put(IP, nodeIp != null ? nodeIp.toString() : node.host());
Viswanath KSP116f0c12016-11-21 00:02:51 +0530187
pierventrefa5dc3c2021-11-05 15:37:32 +0100188 for (Device d : deviceList) {
189 devices.add(deviceData(d));
190 }
Viswanath KSPdf11ea82016-10-25 01:15:16 +0530191
pierventrefa5dc3c2021-11-05 15:37:32 +0100192 } else {
193 data.put(ID, "NONE");
194 data.put(IP, "NONE");
Viswanath KSP116f0c12016-11-21 00:02:51 +0530195 }
Viswanath KSP116f0c12016-11-21 00:02:51 +0530196 data.set(DEVICES, devices);
197
Viswanath KSPdf11ea82016-10-25 01:15:16 +0530198 //TODO put more detail info to data
Viswanath KSPdf11ea82016-10-25 01:15:16 +0530199 rootNode.set(DETAILS, data);
200 sendMessage(CLUSTER_DETAILS_RESP, rootNode);
201 }
202 }
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -0700203}