blob: 8c6c1eb72a5cc80e0731085026540326c821979f [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
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070019import com.fasterxml.jackson.databind.node.ObjectNode;
20import com.google.common.collect.ImmutableSet;
21import org.joda.time.DateTime;
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070022import org.onosproject.cluster.ClusterService;
23import org.onosproject.cluster.ControllerNode;
24import org.onosproject.cluster.NodeId;
Simon Huntd2747a02015-04-30 22:41:16 -070025import org.onosproject.ui.RequestHandler;
Simon Hunta0ddb022015-05-01 09:53:01 -070026import org.onosproject.ui.UiMessageHandler;
Simon Hunt3d1b0652015-05-05 17:27:24 -070027import org.onosproject.ui.table.TableModel;
Simon Huntabd16f62015-05-01 13:14:40 -070028import org.onosproject.ui.table.TableRequestHandler;
Simon Hunt3d1b0652015-05-05 17:27:24 -070029import org.onosproject.ui.table.cell.TimeFormatter;
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070030
Simon Huntd2747a02015-04-30 22:41:16 -070031import java.util.Collection;
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070032
33
34/**
35 * Message handler for cluster view related messages.
36 */
Simon Hunta0ddb022015-05-01 09:53:01 -070037public class ClusterViewMessageHandler extends UiMessageHandler {
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070038
Simon Huntd2747a02015-04-30 22:41:16 -070039 private static final String CLUSTER_DATA_REQ = "clusterDataRequest";
Simon Huntabd16f62015-05-01 13:14:40 -070040 private static final String CLUSTER_DATA_RESP = "clusterDataResponse";
41 private static final String CLUSTERS = "clusters";
42
43 private static final String ID = "id";
44 private static final String IP = "ip";
45 private static final String TCP_PORT = "tcp";
46 private static final String STATE_IID = "_iconid_state";
47 private static final String UPDATED = "updated";
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070048
Simon Hunt3d1b0652015-05-05 17:27:24 -070049 private static final String[] COL_IDS = {
50 ID, IP, TCP_PORT, STATE_IID, UPDATED
51 };
52
53 private static final String ICON_ID_ONLINE = "active";
54 private static final String ICON_ID_OFFLINE = "inactive";
55
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070056 @Override
Simon Huntda580882015-05-12 20:58:18 -070057 protected Collection<RequestHandler> createRequestHandlers() {
Simon Huntd2747a02015-04-30 22:41:16 -070058 return ImmutableSet.of(new ClusterDataRequest());
59 }
60
Simon Huntabd16f62015-05-01 13:14:40 -070061 // handler for cluster table requests
62 private final class ClusterDataRequest extends TableRequestHandler {
Jian Li69f66632016-01-15 12:27:42 -080063 private static final String NO_ROWS_MESSAGE = "No cluster nodes found";
64
Simon Huntd2747a02015-04-30 22:41:16 -070065 private ClusterDataRequest() {
Simon Huntabd16f62015-05-01 13:14:40 -070066 super(CLUSTER_DATA_REQ, CLUSTER_DATA_RESP, CLUSTERS);
Simon Huntd2747a02015-04-30 22:41:16 -070067 }
68
69 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -070070 protected String[] getColumnIds() {
71 return COL_IDS;
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070072 }
73
74 @Override
Jian Li69f66632016-01-15 12:27:42 -080075 protected String noRowsMessage() {
76 return NO_ROWS_MESSAGE;
77 }
78
79 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -070080 protected TableModel createTableModel() {
81 TableModel tm = super.createTableModel();
Simon Hunt5939e652015-05-06 16:20:23 -070082 tm.setFormatter(UPDATED, new TimeFormatter());
Simon Hunt3d1b0652015-05-05 17:27:24 -070083 return tm;
84 }
85
86 @Override
87 protected void populateTable(TableModel tm, ObjectNode payload) {
88 ClusterService cs = get(ClusterService.class);
89 for (ControllerNode node : cs.getNodes()) {
90 populateRow(tm.addRow(), node, cs);
91 }
92 }
93
94 private void populateRow(TableModel.Row row, ControllerNode node,
95 ClusterService cs) {
96 NodeId id = node.id();
97 DateTime lastUpdated = cs.getLastUpdated(id);
98 String iconId = (cs.getState(id) == ControllerNode.State.ACTIVE) ?
99 ICON_ID_ONLINE : ICON_ID_OFFLINE;
100
101 row.cell(ID, id)
102 .cell(IP, node.ip())
103 .cell(TCP_PORT, node.tcpPort())
104 .cell(STATE_IID, iconId)
105 .cell(UPDATED, lastUpdated);
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -0700106 }
107 }
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -0700108}