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; |
| 22 | import org.joda.time.format.DateTimeFormat; |
| 23 | import org.onosproject.cluster.ClusterService; |
| 24 | import org.onosproject.cluster.ControllerNode; |
| 25 | import org.onosproject.cluster.NodeId; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 26 | import org.onosproject.ui.RequestHandler; |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 27 | import org.onosproject.ui.UiMessageHandler; |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 28 | import org.onosproject.ui.table.AbstractTableRow; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 29 | import org.onosproject.ui.table.TableRequestHandler; |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 30 | import org.onosproject.ui.table.TableRow; |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 31 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 32 | import java.util.Collection; |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 33 | import java.util.List; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 34 | import java.util.stream.Collectors; |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 35 | |
| 36 | |
| 37 | /** |
| 38 | * Message handler for cluster view related messages. |
| 39 | */ |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 40 | public class ClusterViewMessageHandler extends UiMessageHandler { |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 41 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 42 | private static final String CLUSTER_DATA_REQ = "clusterDataRequest"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 43 | 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 Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 51 | |
| 52 | @Override |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 53 | protected Collection<RequestHandler> getHandlers() { |
| 54 | return ImmutableSet.of(new ClusterDataRequest()); |
| 55 | } |
| 56 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 57 | // handler for cluster table requests |
| 58 | private final class ClusterDataRequest extends TableRequestHandler { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 59 | private ClusterDataRequest() { |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 60 | super(CLUSTER_DATA_REQ, CLUSTER_DATA_RESP, CLUSTERS); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | @Override |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 64 | protected TableRow[] generateTableRows(ObjectNode payload) { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 65 | ClusterService service = get(ClusterService.class); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 66 | 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 Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 73 | /** |
| 74 | * TableRow implementation for {@link ControllerNode controller nodes}. |
| 75 | */ |
| 76 | private static class ControllerNodeTableRow extends AbstractTableRow { |
| 77 | |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 78 | private static final String[] COL_IDS = { |
Bri Prebilic Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 79 | ID, IP, TCP_PORT, STATE_IID, UPDATED |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 80 | }; |
| 81 | |
Bri Prebilic Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 82 | private static final String ICON_ID_ONLINE = "active"; |
| 83 | private static final String ICON_ID_OFFLINE = "inactive"; |
| 84 | |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 85 | 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 Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 89 | String iconId = (service.getState(id) == ControllerNode.State.ACTIVE) ? |
| 90 | ICON_ID_ONLINE : ICON_ID_OFFLINE; |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 91 | |
| 92 | add(ID, id.toString()); |
| 93 | add(IP, n.ip().toString()); |
| 94 | add(TCP_PORT, Integer.toString(n.tcpPort())); |
Bri Prebilic Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 95 | add(STATE_IID, iconId); |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 96 | add(UPDATED, format.print(lastUpdated)); |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | protected String[] columnIds() { |
| 101 | return COL_IDS; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | } |