blob: eeded83567473e239a217d7a0a8aae151b9f79a2 [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.IntComparator;
30import org.onosproject.ui.table.cell.TimeFormatter;
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070031
Simon Huntd2747a02015-04-30 22:41:16 -070032import java.util.Collection;
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070033
34
35/**
36 * Message handler for cluster view related messages.
37 */
Simon Hunta0ddb022015-05-01 09:53:01 -070038public class ClusterViewMessageHandler extends UiMessageHandler {
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070039
Simon Huntd2747a02015-04-30 22:41:16 -070040 private static final String CLUSTER_DATA_REQ = "clusterDataRequest";
Simon Huntabd16f62015-05-01 13:14:40 -070041 private static final String CLUSTER_DATA_RESP = "clusterDataResponse";
42 private static final String CLUSTERS = "clusters";
43
44 private static final String ID = "id";
45 private static final String IP = "ip";
46 private static final String TCP_PORT = "tcp";
47 private static final String STATE_IID = "_iconid_state";
48 private static final String UPDATED = "updated";
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070049
Simon Hunt3d1b0652015-05-05 17:27:24 -070050 private static final String[] COL_IDS = {
51 ID, IP, TCP_PORT, STATE_IID, UPDATED
52 };
53
54 private static final String ICON_ID_ONLINE = "active";
55 private static final String ICON_ID_OFFLINE = "inactive";
56
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070057 @Override
Simon Huntd2747a02015-04-30 22:41:16 -070058 protected Collection<RequestHandler> getHandlers() {
59 return ImmutableSet.of(new ClusterDataRequest());
60 }
61
Simon Huntabd16f62015-05-01 13:14:40 -070062 // handler for cluster table requests
63 private final class ClusterDataRequest extends TableRequestHandler {
Simon Huntd2747a02015-04-30 22:41:16 -070064 private ClusterDataRequest() {
Simon Huntabd16f62015-05-01 13:14:40 -070065 super(CLUSTER_DATA_REQ, CLUSTER_DATA_RESP, CLUSTERS);
Simon Huntd2747a02015-04-30 22:41:16 -070066 }
67
68 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -070069 protected String[] getColumnIds() {
70 return COL_IDS;
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070071 }
72
73 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -070074 protected TableModel createTableModel() {
75 TableModel tm = super.createTableModel();
76 tm.setComparator(TCP_PORT, IntComparator.INSTANCE);
Simon Hunt5939e652015-05-06 16:20:23 -070077 tm.setFormatter(UPDATED, new TimeFormatter());
Simon Hunt3d1b0652015-05-05 17:27:24 -070078 return tm;
79 }
80
81 @Override
82 protected void populateTable(TableModel tm, ObjectNode payload) {
83 ClusterService cs = get(ClusterService.class);
84 for (ControllerNode node : cs.getNodes()) {
85 populateRow(tm.addRow(), node, cs);
86 }
87 }
88
89 private void populateRow(TableModel.Row row, ControllerNode node,
90 ClusterService cs) {
91 NodeId id = node.id();
92 DateTime lastUpdated = cs.getLastUpdated(id);
93 String iconId = (cs.getState(id) == ControllerNode.State.ACTIVE) ?
94 ICON_ID_ONLINE : ICON_ID_OFFLINE;
95
96 row.cell(ID, id)
97 .cell(IP, node.ip())
98 .cell(TCP_PORT, node.tcpPort())
99 .cell(STATE_IID, iconId)
100 .cell(UPDATED, lastUpdated);
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -0700101 }
102 }
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -0700103}