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; |
| 29 | import org.onosproject.ui.table.RowComparator; |
| 30 | import org.onosproject.ui.table.TableRow; |
| 31 | import org.onosproject.ui.table.TableUtils; |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 32 | |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 33 | import java.util.Arrays; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 34 | import java.util.Collection; |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 35 | import java.util.List; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 36 | import java.util.stream.Collectors; |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 37 | |
| 38 | |
| 39 | /** |
| 40 | * Message handler for cluster view related messages. |
| 41 | */ |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame^] | 42 | public class ClusterViewMessageHandler extends UiMessageHandler { |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 43 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 44 | private static final String CLUSTER_DATA_REQ = "clusterDataRequest"; |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 45 | |
| 46 | @Override |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 47 | protected Collection<RequestHandler> getHandlers() { |
| 48 | return ImmutableSet.of(new ClusterDataRequest()); |
| 49 | } |
| 50 | |
| 51 | // ====================================================================== |
| 52 | |
| 53 | private final class ClusterDataRequest extends RequestHandler { |
| 54 | |
| 55 | private ClusterDataRequest() { |
| 56 | super(CLUSTER_DATA_REQ); |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public void process(long sid, ObjectNode payload) { |
| 61 | RowComparator rc = TableUtils.createRowComparator(payload); |
| 62 | |
| 63 | ClusterService service = get(ClusterService.class); |
| 64 | TableRow[] rows = generateTableRows(service); |
| 65 | Arrays.sort(rows, rc); |
| 66 | ObjectNode rootNode = MAPPER.createObjectNode(); |
| 67 | rootNode.set("clusters", TableUtils.generateArrayNode(rows)); |
| 68 | |
| 69 | sendMessage("clusterDataResponse", 0, rootNode); |
| 70 | } |
| 71 | |
| 72 | private TableRow[] generateTableRows(ClusterService service) { |
| 73 | List<TableRow> list = service.getNodes().stream() |
| 74 | .map(node -> new ControllerNodeTableRow(service, node)) |
| 75 | .collect(Collectors.toList()); |
| 76 | return list.toArray(new TableRow[list.size()]); |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 80 | // ====================================================================== |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 81 | |
| 82 | /** |
| 83 | * TableRow implementation for {@link ControllerNode controller nodes}. |
| 84 | */ |
| 85 | private static class ControllerNodeTableRow extends AbstractTableRow { |
| 86 | |
| 87 | private static final String ID = "id"; |
| 88 | private static final String IP = "ip"; |
| 89 | private static final String TCP_PORT = "tcp"; |
Bri Prebilic Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 90 | private static final String STATE_IID = "_iconid_state"; |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 91 | private static final String UPDATED = "updated"; |
| 92 | |
| 93 | private static final String[] COL_IDS = { |
Bri Prebilic Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 94 | ID, IP, TCP_PORT, STATE_IID, UPDATED |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 95 | }; |
| 96 | |
Bri Prebilic Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 97 | private static final String ICON_ID_ONLINE = "active"; |
| 98 | private static final String ICON_ID_OFFLINE = "inactive"; |
| 99 | |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 100 | public ControllerNodeTableRow(ClusterService service, ControllerNode n) { |
| 101 | NodeId id = n.id(); |
| 102 | DateTime lastUpdated = service.getLastUpdated(id); |
| 103 | org.joda.time.format.DateTimeFormatter format = DateTimeFormat.longTime(); |
Bri Prebilic Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 104 | String iconId = (service.getState(id) == ControllerNode.State.ACTIVE) ? |
| 105 | ICON_ID_ONLINE : ICON_ID_OFFLINE; |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 106 | |
| 107 | add(ID, id.toString()); |
| 108 | add(IP, n.ip().toString()); |
| 109 | add(TCP_PORT, Integer.toString(n.tcpPort())); |
Bri Prebilic Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 110 | add(STATE_IID, iconId); |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 111 | add(UPDATED, format.print(lastUpdated)); |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | protected String[] columnIds() { |
| 116 | return COL_IDS; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | } |