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 | |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import com.google.common.collect.ImmutableSet; |
| 22 | import org.joda.time.DateTime; |
| 23 | import org.joda.time.format.DateTimeFormat; |
| 24 | import org.onosproject.cluster.ClusterService; |
| 25 | import org.onosproject.cluster.ControllerNode; |
| 26 | import org.onosproject.cluster.NodeId; |
| 27 | |
| 28 | import java.util.ArrayList; |
| 29 | import java.util.Arrays; |
| 30 | import java.util.List; |
| 31 | |
| 32 | |
| 33 | /** |
| 34 | * Message handler for cluster view related messages. |
| 35 | */ |
| 36 | public class ClusterViewMessageHandler extends AbstractTabularViewMessageHandler { |
| 37 | |
| 38 | /** |
| 39 | * Creates a new message handler for the cluster messages. |
| 40 | */ |
| 41 | protected ClusterViewMessageHandler() { |
| 42 | super(ImmutableSet.of("clusterDataRequest")); |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public void process(ObjectNode message) { |
| 47 | ObjectNode payload = payload(message); |
| 48 | String sortCol = string(payload, "sortCol", "id"); |
| 49 | String sortDir = string(payload, "sortDir", "asc"); |
| 50 | |
| 51 | ClusterService service = get(ClusterService.class); |
| 52 | TableRow[] rows = generateTableRows(service); |
| 53 | RowComparator rc = |
| 54 | new RowComparator(sortCol, RowComparator.direction(sortDir)); |
| 55 | Arrays.sort(rows, rc); |
| 56 | ArrayNode clusterNodes = generateArrayNode(rows); |
| 57 | ObjectNode rootNode = mapper.createObjectNode(); |
| 58 | rootNode.set("clusters", clusterNodes); |
| 59 | |
| 60 | connection().sendMessage("clusterDataResponse", 0, rootNode); |
| 61 | } |
| 62 | |
| 63 | private TableRow[] generateTableRows(ClusterService service) { |
| 64 | List<TableRow> list = new ArrayList<>(); |
| 65 | for (ControllerNode node : service.getNodes()) { |
| 66 | list.add(new ControllerNodeTableRow(service, node)); |
| 67 | } |
| 68 | return list.toArray(new TableRow[list.size()]); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * TableRow implementation for {@link ControllerNode controller nodes}. |
| 73 | */ |
| 74 | private static class ControllerNodeTableRow extends AbstractTableRow { |
| 75 | |
| 76 | private static final String ID = "id"; |
| 77 | private static final String IP = "ip"; |
| 78 | private static final String TCP_PORT = "tcp"; |
Bri Prebilic Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 79 | private static final String STATE_IID = "_iconid_state"; |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 80 | private static final String UPDATED = "updated"; |
| 81 | |
| 82 | private static final String[] COL_IDS = { |
Bri Prebilic Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 83 | ID, IP, TCP_PORT, STATE_IID, UPDATED |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 84 | }; |
| 85 | |
Bri Prebilic Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 86 | private static final String ICON_ID_ONLINE = "active"; |
| 87 | private static final String ICON_ID_OFFLINE = "inactive"; |
| 88 | |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 89 | public ControllerNodeTableRow(ClusterService service, ControllerNode n) { |
| 90 | NodeId id = n.id(); |
| 91 | DateTime lastUpdated = service.getLastUpdated(id); |
| 92 | org.joda.time.format.DateTimeFormatter format = DateTimeFormat.longTime(); |
Bri Prebilic Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 93 | String iconId = (service.getState(id) == ControllerNode.State.ACTIVE) ? |
| 94 | ICON_ID_ONLINE : ICON_ID_OFFLINE; |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 95 | |
| 96 | add(ID, id.toString()); |
| 97 | add(IP, n.ip().toString()); |
| 98 | add(TCP_PORT, Integer.toString(n.tcpPort())); |
Bri Prebilic Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 99 | add(STATE_IID, iconId); |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 100 | add(UPDATED, format.print(lastUpdated)); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | protected String[] columnIds() { |
| 105 | return COL_IDS; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | } |