Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 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 | |
| 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 Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 48 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 49 | 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 Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 56 | @Override |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 57 | protected Collection<RequestHandler> createRequestHandlers() { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 58 | return ImmutableSet.of(new ClusterDataRequest()); |
| 59 | } |
| 60 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 61 | // handler for cluster table requests |
| 62 | private final class ClusterDataRequest extends TableRequestHandler { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 63 | private ClusterDataRequest() { |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 64 | super(CLUSTER_DATA_REQ, CLUSTER_DATA_RESP, CLUSTERS); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | @Override |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 68 | protected String[] getColumnIds() { |
| 69 | return COL_IDS; |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | @Override |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 73 | protected TableModel createTableModel() { |
| 74 | TableModel tm = super.createTableModel(); |
Simon Hunt | 5939e65 | 2015-05-06 16:20:23 -0700 | [diff] [blame] | 75 | tm.setFormatter(UPDATED, new TimeFormatter()); |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 76 | return tm; |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | protected void populateTable(TableModel tm, ObjectNode payload) { |
| 81 | ClusterService cs = get(ClusterService.class); |
| 82 | for (ControllerNode node : cs.getNodes()) { |
| 83 | populateRow(tm.addRow(), node, cs); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | private void populateRow(TableModel.Row row, ControllerNode node, |
| 88 | ClusterService cs) { |
| 89 | NodeId id = node.id(); |
| 90 | DateTime lastUpdated = cs.getLastUpdated(id); |
| 91 | String iconId = (cs.getState(id) == ControllerNode.State.ACTIVE) ? |
| 92 | ICON_ID_ONLINE : ICON_ID_OFFLINE; |
| 93 | |
| 94 | row.cell(ID, id) |
| 95 | .cell(IP, node.ip()) |
| 96 | .cell(TCP_PORT, node.tcpPort()) |
| 97 | .cell(STATE_IID, iconId) |
| 98 | .cell(UPDATED, lastUpdated); |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 99 | } |
| 100 | } |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 101 | } |