blob: 209b98e0412442f77e6385cb59be7a88fa120f5f [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;
22import org.joda.time.format.DateTimeFormat;
23import org.onosproject.cluster.ClusterService;
24import org.onosproject.cluster.ControllerNode;
25import org.onosproject.cluster.NodeId;
Simon Huntd2747a02015-04-30 22:41:16 -070026import org.onosproject.ui.RequestHandler;
Simon Hunta0ddb022015-05-01 09:53:01 -070027import org.onosproject.ui.UiMessageHandler;
Simon Hunt44aa2f82015-04-30 15:01:35 -070028import org.onosproject.ui.table.AbstractTableRow;
29import org.onosproject.ui.table.RowComparator;
30import org.onosproject.ui.table.TableRow;
31import org.onosproject.ui.table.TableUtils;
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070032
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070033import java.util.Arrays;
Simon Huntd2747a02015-04-30 22:41:16 -070034import java.util.Collection;
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070035import java.util.List;
Simon Huntd2747a02015-04-30 22:41:16 -070036import java.util.stream.Collectors;
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070037
38
39/**
40 * Message handler for cluster view related messages.
41 */
Simon Hunta0ddb022015-05-01 09:53:01 -070042public class ClusterViewMessageHandler extends UiMessageHandler {
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070043
Simon Huntd2747a02015-04-30 22:41:16 -070044 private static final String CLUSTER_DATA_REQ = "clusterDataRequest";
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070045
46 @Override
Simon Huntd2747a02015-04-30 22:41:16 -070047 protected Collection<RequestHandler> getHandlers() {
48 return ImmutableSet.of(new ClusterDataRequest());
49 }
50
51 // ======================================================================
52
53 private final class ClusterDataRequest extends RequestHandler {
54
55 private ClusterDataRequest() {
56 super(CLUSTER_DATA_REQ);
57 }
58
59 @Override
60 public void process(long sid, ObjectNode payload) {
61 RowComparator rc = TableUtils.createRowComparator(payload);
62
63 ClusterService service = get(ClusterService.class);
64 TableRow[] rows = generateTableRows(service);
65 Arrays.sort(rows, rc);
66 ObjectNode rootNode = MAPPER.createObjectNode();
67 rootNode.set("clusters", TableUtils.generateArrayNode(rows));
68
69 sendMessage("clusterDataResponse", 0, rootNode);
70 }
71
72 private TableRow[] generateTableRows(ClusterService service) {
73 List<TableRow> list = service.getNodes().stream()
74 .map(node -> new ControllerNodeTableRow(service, node))
75 .collect(Collectors.toList());
76 return list.toArray(new TableRow[list.size()]);
Simon Hunt44aa2f82015-04-30 15:01:35 -070077 }
78 }
79
Simon Huntd2747a02015-04-30 22:41:16 -070080 // ======================================================================
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070081
82 /**
83 * TableRow implementation for {@link ControllerNode controller nodes}.
84 */
85 private static class ControllerNodeTableRow extends AbstractTableRow {
86
87 private static final String ID = "id";
88 private static final String IP = "ip";
89 private static final String TCP_PORT = "tcp";
Bri Prebilic Coleab582b82015-04-14 15:08:22 -070090 private static final String STATE_IID = "_iconid_state";
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070091 private static final String UPDATED = "updated";
92
93 private static final String[] COL_IDS = {
Bri Prebilic Coleab582b82015-04-14 15:08:22 -070094 ID, IP, TCP_PORT, STATE_IID, UPDATED
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070095 };
96
Bri Prebilic Coleab582b82015-04-14 15:08:22 -070097 private static final String ICON_ID_ONLINE = "active";
98 private static final String ICON_ID_OFFLINE = "inactive";
99
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -0700100 public ControllerNodeTableRow(ClusterService service, ControllerNode n) {
101 NodeId id = n.id();
102 DateTime lastUpdated = service.getLastUpdated(id);
103 org.joda.time.format.DateTimeFormatter format = DateTimeFormat.longTime();
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700104 String iconId = (service.getState(id) == ControllerNode.State.ACTIVE) ?
105 ICON_ID_ONLINE : ICON_ID_OFFLINE;
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -0700106
107 add(ID, id.toString());
108 add(IP, n.ip().toString());
109 add(TCP_PORT, Integer.toString(n.tcpPort()));
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700110 add(STATE_IID, iconId);
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -0700111 add(UPDATED, format.print(lastUpdated));
112 }
113
114 @Override
115 protected String[] columnIds() {
116 return COL_IDS;
117 }
118 }
119
120}