blob: a3c7e2f49723bbd9e805d9f12ce49dffefd570e7 [file] [log] [blame]
Thomas Vachuskae586b792015-03-26 13:59:38 -07001/*
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 */
16package org.onosproject.ui.impl;
17
18import com.fasterxml.jackson.databind.node.ArrayNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import com.google.common.collect.ImmutableSet;
Bri Prebilic Coleae65c962015-04-02 16:24:49 -070021import org.onosproject.mastership.MastershipService;
Thomas Vachuskae586b792015-03-26 13:59:38 -070022import org.onosproject.net.Device;
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -070023import org.onosproject.net.DeviceId;
24import org.onosproject.net.Port;
Thomas Vachuskae586b792015-03-26 13:59:38 -070025import org.onosproject.net.device.DeviceService;
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -070026import org.onosproject.net.link.LinkService;
Thomas Vachuskae586b792015-03-26 13:59:38 -070027
28import java.util.ArrayList;
29import java.util.Arrays;
30import java.util.List;
31
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -070032//import org.onosproject.net.Link;
33//import java.util.Set;
34
Thomas Vachuskae586b792015-03-26 13:59:38 -070035/**
36 * Message handler for device view related messages.
37 */
38public class DeviceViewMessageHandler extends AbstractTabularViewMessageHandler {
39
40 /**
41 * Creates a new message handler for the device messages.
42 */
43 protected DeviceViewMessageHandler() {
44 super(ImmutableSet.of("deviceDataRequest"));
45 }
46
47 @Override
48 public void process(ObjectNode message) {
49 ObjectNode payload = payload(message);
50 String sortCol = string(payload, "sortCol", "id");
51 String sortDir = string(payload, "sortDir", "asc");
52
53 DeviceService service = get(DeviceService.class);
Bri Prebilic Coleae65c962015-04-02 16:24:49 -070054 MastershipService mastershipService = get(MastershipService.class);
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -070055 LinkService linkService = get(LinkService.class);
56 TableRow[] rows = generateTableRows(service,
57 mastershipService,
58 linkService);
Bri Prebilic Coleae65c962015-04-02 16:24:49 -070059 RowComparator rc =
60 new RowComparator(sortCol, RowComparator.direction(sortDir));
Thomas Vachuskae586b792015-03-26 13:59:38 -070061 Arrays.sort(rows, rc);
62 ArrayNode devices = generateArrayNode(rows);
63 ObjectNode rootNode = mapper.createObjectNode();
64 rootNode.set("devices", devices);
65
66 connection().sendMessage("deviceDataResponse", 0, rootNode);
67 }
68
Bri Prebilic Coleae65c962015-04-02 16:24:49 -070069 private TableRow[] generateTableRows(DeviceService service,
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -070070 MastershipService mastershipService,
71 LinkService linkService) {
Thomas Vachuskae586b792015-03-26 13:59:38 -070072 List<TableRow> list = new ArrayList<>();
73 for (Device dev : service.getDevices()) {
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -070074 list.add(new DeviceTableRow(service,
75 mastershipService,
76 linkService,
77 dev));
Thomas Vachuskae586b792015-03-26 13:59:38 -070078 }
79 return list.toArray(new TableRow[list.size()]);
80 }
81
82 /**
83 * TableRow implementation for {@link Device devices}.
84 */
85 private static class DeviceTableRow extends AbstractTableRow {
86
87 private static final String ID = "id";
88 private static final String AVAILABLE = "available";
89 private static final String AVAILABLE_IID = "_iconid_available";
90 private static final String TYPE_IID = "_iconid_type";
91 private static final String DEV_ICON_PREFIX = "devIcon_";
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -070092 private static final String NUM_PORTS = "num_ports";
93 private static final String NUM_EGRESS_LINKS = "num_elinks";
Thomas Vachuskae586b792015-03-26 13:59:38 -070094 private static final String MFR = "mfr";
95 private static final String HW = "hw";
96 private static final String SW = "sw";
Thomas Vachuskae586b792015-03-26 13:59:38 -070097 private static final String PROTOCOL = "protocol";
Bri Prebilic Coleae65c962015-04-02 16:24:49 -070098 private static final String MASTERID = "masterid";
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -070099 private static final String CHASSISID = "chassisid";
100 private static final String SERIAL = "serial";
Thomas Vachuskae586b792015-03-26 13:59:38 -0700101
102 private static final String[] COL_IDS = {
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -0700103 AVAILABLE, AVAILABLE_IID, TYPE_IID, ID,
104 NUM_PORTS, NUM_EGRESS_LINKS, MASTERID, MFR, HW, SW,
105 PROTOCOL, CHASSISID, SERIAL
Thomas Vachuskae586b792015-03-26 13:59:38 -0700106 };
107
108 private static final String ICON_ID_ONLINE = "deviceOnline";
109 private static final String ICON_ID_OFFLINE = "deviceOffline";
110
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -0700111 // TODO: use in details pane
112// private String getPorts(List<Port> ports) {
113// String formattedString = "";
114// int numPorts = 0;
115//
116// for (Port p : ports) {
117// numPorts++;
118// formattedString += p.number().toString() + ", ";
119// }
120// return formattedString + "Total: " + numPorts;
121// }
122
123 // TODO: use in details pane
124// private String getEgressLinks(Set<Link> links) {
125// String formattedString = "";
126//
127// for (Link l : links) {
128// formattedString += l.dst().port().toString() + ", ";
129// }
130// return formattedString;
131// }
132
133 // TODO: include "extra" backend information in device details pane
Bri Prebilic Coleae65c962015-04-02 16:24:49 -0700134 public DeviceTableRow(DeviceService service,
135 MastershipService ms,
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -0700136 LinkService ls,
Bri Prebilic Coleae65c962015-04-02 16:24:49 -0700137 Device d) {
Thomas Vachuskae586b792015-03-26 13:59:38 -0700138 boolean available = service.isAvailable(d.id());
139 String iconId = available ? ICON_ID_ONLINE : ICON_ID_OFFLINE;
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -0700140 DeviceId id = d.id();
141 List<Port> ports = service.getPorts(id);
142// Set<Link> links = ls.getDeviceEgressLinks(id);
Thomas Vachuskae586b792015-03-26 13:59:38 -0700143
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -0700144 add(ID, id.toString());
Thomas Vachuskae586b792015-03-26 13:59:38 -0700145 add(AVAILABLE, Boolean.toString(available));
146 add(AVAILABLE_IID, iconId);
147 add(TYPE_IID, getTypeIconId(d));
Thomas Vachuskae586b792015-03-26 13:59:38 -0700148 add(MFR, d.manufacturer());
149 add(HW, d.hwVersion());
150 add(SW, d.swVersion());
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -0700151// add(SERIAL, d.serialNumber());
Thomas Vachuskae586b792015-03-26 13:59:38 -0700152 add(PROTOCOL, d.annotations().value(PROTOCOL));
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -0700153 add(NUM_PORTS, Integer.toString(ports.size()));
154// add(NUM_EGRESS_LINKS, Integer.toString(links.size()));
155// add(CHASSISID, d.chassisId().toString());
Bri Prebilic Coleae65c962015-04-02 16:24:49 -0700156 add(MASTERID, ms.getMasterFor(d.id()).toString());
Thomas Vachuskae586b792015-03-26 13:59:38 -0700157 }
158
159 private String getTypeIconId(Device d) {
160 return DEV_ICON_PREFIX + d.type().toString();
161 }
162
163 @Override
164 protected String[] columnIds() {
165 return COL_IDS;
166 }
167 }
168
169}