blob: 1199b13edcb5526e258f5f97a9e07701e32f2ebd [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;
Simon Huntabd16f62015-05-01 13:14:40 -070029import org.onosproject.ui.table.TableRequestHandler;
Simon Hunt44aa2f82015-04-30 15:01:35 -070030import org.onosproject.ui.table.TableRow;
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 -070033import java.util.List;
Simon Huntd2747a02015-04-30 22:41:16 -070034import java.util.stream.Collectors;
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070035
36
37/**
38 * Message handler for cluster view related messages.
39 */
Simon Hunta0ddb022015-05-01 09:53:01 -070040public class ClusterViewMessageHandler extends UiMessageHandler {
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070041
Simon Huntd2747a02015-04-30 22:41:16 -070042 private static final String CLUSTER_DATA_REQ = "clusterDataRequest";
Simon Huntabd16f62015-05-01 13:14:40 -070043 private static final String CLUSTER_DATA_RESP = "clusterDataResponse";
44 private static final String CLUSTERS = "clusters";
45
46 private static final String ID = "id";
47 private static final String IP = "ip";
48 private static final String TCP_PORT = "tcp";
49 private static final String STATE_IID = "_iconid_state";
50 private static final String UPDATED = "updated";
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070051
52 @Override
Simon Huntd2747a02015-04-30 22:41:16 -070053 protected Collection<RequestHandler> getHandlers() {
54 return ImmutableSet.of(new ClusterDataRequest());
55 }
56
Simon Huntabd16f62015-05-01 13:14:40 -070057 // handler for cluster table requests
58 private final class ClusterDataRequest extends TableRequestHandler {
Simon Huntd2747a02015-04-30 22:41:16 -070059 private ClusterDataRequest() {
Simon Huntabd16f62015-05-01 13:14:40 -070060 super(CLUSTER_DATA_REQ, CLUSTER_DATA_RESP, CLUSTERS);
Simon Huntd2747a02015-04-30 22:41:16 -070061 }
62
63 @Override
Simon Huntabd16f62015-05-01 13:14:40 -070064 protected TableRow[] generateTableRows(ObjectNode payload) {
Simon Huntd2747a02015-04-30 22:41:16 -070065 ClusterService service = get(ClusterService.class);
Simon Huntd2747a02015-04-30 22:41:16 -070066 List<TableRow> list = service.getNodes().stream()
67 .map(node -> new ControllerNodeTableRow(service, node))
68 .collect(Collectors.toList());
69 return list.toArray(new TableRow[list.size()]);
Simon Hunt44aa2f82015-04-30 15:01:35 -070070 }
71 }
72
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070073 /**
74 * TableRow implementation for {@link ControllerNode controller nodes}.
75 */
76 private static class ControllerNodeTableRow extends AbstractTableRow {
77
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070078 private static final String[] COL_IDS = {
Bri Prebilic Coleab582b82015-04-14 15:08:22 -070079 ID, IP, TCP_PORT, STATE_IID, UPDATED
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070080 };
81
Bri Prebilic Coleab582b82015-04-14 15:08:22 -070082 private static final String ICON_ID_ONLINE = "active";
83 private static final String ICON_ID_OFFLINE = "inactive";
84
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070085 public ControllerNodeTableRow(ClusterService service, ControllerNode n) {
86 NodeId id = n.id();
87 DateTime lastUpdated = service.getLastUpdated(id);
88 org.joda.time.format.DateTimeFormatter format = DateTimeFormat.longTime();
Bri Prebilic Coleab582b82015-04-14 15:08:22 -070089 String iconId = (service.getState(id) == ControllerNode.State.ACTIVE) ?
90 ICON_ID_ONLINE : ICON_ID_OFFLINE;
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070091
92 add(ID, id.toString());
93 add(IP, n.ip().toString());
94 add(TCP_PORT, Integer.toString(n.tcpPort()));
Bri Prebilic Coleab582b82015-04-14 15:08:22 -070095 add(STATE_IID, iconId);
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070096 add(UPDATED, format.print(lastUpdated));
97 }
98
99 @Override
100 protected String[] columnIds() {
101 return COL_IDS;
102 }
103 }
104
105}