Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.ui.impl; |
| 18 | |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import com.google.common.collect.ImmutableSet; |
| 21 | import org.joda.time.DateTime; |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 22 | import org.onosproject.cluster.ClusterService; |
| 23 | import org.onosproject.cluster.ControllerNode; |
| 24 | import org.onosproject.cluster.NodeId; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 25 | import org.onosproject.ui.RequestHandler; |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 26 | import org.onosproject.ui.UiMessageHandler; |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 27 | import org.onosproject.ui.table.TableModel; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 28 | import org.onosproject.ui.table.TableRequestHandler; |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 29 | import org.onosproject.ui.table.cell.TimeFormatter; |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 30 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 31 | import java.util.Collection; |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 32 | |
| 33 | |
| 34 | /** |
| 35 | * Message handler for cluster view related messages. |
| 36 | */ |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 37 | public class ClusterViewMessageHandler extends UiMessageHandler { |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 38 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 39 | private static final String CLUSTER_DATA_REQ = "clusterDataRequest"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 40 | private static final String CLUSTER_DATA_RESP = "clusterDataResponse"; |
| 41 | private static final String CLUSTERS = "clusters"; |
| 42 | |
Simon Hunt | a271e0a | 2016-10-26 10:59:14 -0700 | [diff] [blame] | 43 | private static final String CLUSTER_DETAILS_REQ = "clusterDetailsRequest"; |
| 44 | private static final String CLUSTER_DETAILS_RESP = "clusterDetailsResponse"; |
Viswanath KSP | df11ea8 | 2016-10-25 01:15:16 +0530 | [diff] [blame] | 45 | private static final String DETAILS = "details"; |
| 46 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 47 | private static final String ID = "id"; |
| 48 | private static final String IP = "ip"; |
| 49 | private static final String TCP_PORT = "tcp"; |
| 50 | private static final String STATE_IID = "_iconid_state"; |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 51 | private static final String STARTED_IID = "_iconid_started"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 52 | private static final String UPDATED = "updated"; |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 53 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 54 | private static final String[] COL_IDS = { |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 55 | ID, IP, TCP_PORT, STATE_IID, STARTED_IID, UPDATED |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | private static final String ICON_ID_ONLINE = "active"; |
| 59 | private static final String ICON_ID_OFFLINE = "inactive"; |
| 60 | |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 61 | @Override |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 62 | protected Collection<RequestHandler> createRequestHandlers() { |
Viswanath KSP | df11ea8 | 2016-10-25 01:15:16 +0530 | [diff] [blame] | 63 | return ImmutableSet.of( |
| 64 | new ClusterDataRequest(), |
| 65 | new DetailRequestHandler()); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 68 | // handler for cluster table requests |
| 69 | private final class ClusterDataRequest extends TableRequestHandler { |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 70 | private static final String NO_ROWS_MESSAGE = "No cluster nodes found"; |
| 71 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 72 | private ClusterDataRequest() { |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 73 | super(CLUSTER_DATA_REQ, CLUSTER_DATA_RESP, CLUSTERS); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | @Override |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 77 | protected String[] getColumnIds() { |
| 78 | return COL_IDS; |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | @Override |
Jian Li | 8baf447 | 2016-01-15 15:08:09 -0800 | [diff] [blame] | 82 | protected String noRowsMessage(ObjectNode payload) { |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 83 | return NO_ROWS_MESSAGE; |
| 84 | } |
| 85 | |
| 86 | @Override |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 87 | protected TableModel createTableModel() { |
| 88 | TableModel tm = super.createTableModel(); |
Simon Hunt | 5939e65 | 2015-05-06 16:20:23 -0700 | [diff] [blame] | 89 | tm.setFormatter(UPDATED, new TimeFormatter()); |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 90 | return tm; |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | protected void populateTable(TableModel tm, ObjectNode payload) { |
| 95 | ClusterService cs = get(ClusterService.class); |
| 96 | for (ControllerNode node : cs.getNodes()) { |
| 97 | populateRow(tm.addRow(), node, cs); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | private void populateRow(TableModel.Row row, ControllerNode node, |
| 102 | ClusterService cs) { |
| 103 | NodeId id = node.id(); |
| 104 | DateTime lastUpdated = cs.getLastUpdated(id); |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 105 | ControllerNode.State state = cs.getState(id); |
| 106 | String iconId = state.isActive() ? ICON_ID_ONLINE : ICON_ID_OFFLINE; |
| 107 | String startedId = state.isReady() ? ICON_ID_ONLINE : ICON_ID_OFFLINE; |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 108 | |
| 109 | row.cell(ID, id) |
| 110 | .cell(IP, node.ip()) |
| 111 | .cell(TCP_PORT, node.tcpPort()) |
| 112 | .cell(STATE_IID, iconId) |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 113 | .cell(STARTED_IID, startedId) |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 114 | .cell(UPDATED, lastUpdated); |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 115 | } |
| 116 | } |
Viswanath KSP | df11ea8 | 2016-10-25 01:15:16 +0530 | [diff] [blame] | 117 | |
| 118 | private final class DetailRequestHandler extends RequestHandler { |
| 119 | |
| 120 | public DetailRequestHandler() { |
| 121 | super(CLUSTER_DETAILS_REQ); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public void process(long sid, ObjectNode payload) { |
| 126 | |
| 127 | String id = string(payload, ID); |
| 128 | ClusterService cs = get(ClusterService.class); |
| 129 | ControllerNode node = cs.getNode(new NodeId(id)); |
| 130 | |
| 131 | ObjectNode data = objectNode(); |
| 132 | data.put(ID, node.id().toString()); |
| 133 | data.put(IP, node.ip().toString()); |
| 134 | |
| 135 | //TODO put more detail info to data |
| 136 | |
| 137 | ObjectNode rootNode = objectNode(); |
| 138 | rootNode.set(DETAILS, data); |
| 139 | sendMessage(CLUSTER_DETAILS_RESP, rootNode); |
| 140 | } |
| 141 | } |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 142 | } |