blob: 6b1ac7b1014fa2b7265eef07cb9a0257462f5bc8 [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;
23import org.onosproject.net.device.DeviceService;
24
25import java.util.ArrayList;
26import java.util.Arrays;
27import java.util.List;
28
29/**
30 * Message handler for device view related messages.
31 */
32public class DeviceViewMessageHandler extends AbstractTabularViewMessageHandler {
33
34 /**
35 * Creates a new message handler for the device messages.
36 */
37 protected DeviceViewMessageHandler() {
38 super(ImmutableSet.of("deviceDataRequest"));
39 }
40
41 @Override
42 public void process(ObjectNode message) {
43 ObjectNode payload = payload(message);
44 String sortCol = string(payload, "sortCol", "id");
45 String sortDir = string(payload, "sortDir", "asc");
46
47 DeviceService service = get(DeviceService.class);
Bri Prebilic Coleae65c962015-04-02 16:24:49 -070048 MastershipService mastershipService = get(MastershipService.class);
49 TableRow[] rows = generateTableRows(service, mastershipService);
50 RowComparator rc =
51 new RowComparator(sortCol, RowComparator.direction(sortDir));
Thomas Vachuskae586b792015-03-26 13:59:38 -070052 Arrays.sort(rows, rc);
53 ArrayNode devices = generateArrayNode(rows);
54 ObjectNode rootNode = mapper.createObjectNode();
55 rootNode.set("devices", devices);
56
57 connection().sendMessage("deviceDataResponse", 0, rootNode);
58 }
59
Bri Prebilic Coleae65c962015-04-02 16:24:49 -070060 private TableRow[] generateTableRows(DeviceService service,
61 MastershipService mastershipService) {
Thomas Vachuskae586b792015-03-26 13:59:38 -070062 List<TableRow> list = new ArrayList<>();
63 for (Device dev : service.getDevices()) {
Bri Prebilic Coleae65c962015-04-02 16:24:49 -070064 list.add(new DeviceTableRow(service, mastershipService, dev));
Thomas Vachuskae586b792015-03-26 13:59:38 -070065 }
66 return list.toArray(new TableRow[list.size()]);
67 }
68
69 /**
70 * TableRow implementation for {@link Device devices}.
71 */
72 private static class DeviceTableRow extends AbstractTableRow {
73
74 private static final String ID = "id";
75 private static final String AVAILABLE = "available";
76 private static final String AVAILABLE_IID = "_iconid_available";
77 private static final String TYPE_IID = "_iconid_type";
78 private static final String DEV_ICON_PREFIX = "devIcon_";
79 private static final String ROLE = "role";
80 private static final String MFR = "mfr";
81 private static final String HW = "hw";
82 private static final String SW = "sw";
83 private static final String SERIAL = "serial";
84 private static final String PROTOCOL = "protocol";
85 private static final String CHASSISID = "chassisid";
Bri Prebilic Coleae65c962015-04-02 16:24:49 -070086 private static final String MASTERID = "masterid";
Thomas Vachuskae586b792015-03-26 13:59:38 -070087
88 private static final String[] COL_IDS = {
89 ID, AVAILABLE, AVAILABLE_IID, TYPE_IID, ROLE,
Bri Prebilic Coleae65c962015-04-02 16:24:49 -070090 MFR, HW, SW, SERIAL, PROTOCOL, CHASSISID, MASTERID
Thomas Vachuskae586b792015-03-26 13:59:38 -070091 };
92
93 private static final String ICON_ID_ONLINE = "deviceOnline";
94 private static final String ICON_ID_OFFLINE = "deviceOffline";
95
Bri Prebilic Coleae65c962015-04-02 16:24:49 -070096 public DeviceTableRow(DeviceService service,
97 MastershipService ms,
98 Device d) {
Thomas Vachuskae586b792015-03-26 13:59:38 -070099 boolean available = service.isAvailable(d.id());
100 String iconId = available ? ICON_ID_ONLINE : ICON_ID_OFFLINE;
101
102 add(ID, d.id().toString());
103 add(AVAILABLE, Boolean.toString(available));
104 add(AVAILABLE_IID, iconId);
105 add(TYPE_IID, getTypeIconId(d));
106 add(ROLE, service.getRole(d.id()).toString());
107 add(MFR, d.manufacturer());
108 add(HW, d.hwVersion());
109 add(SW, d.swVersion());
110 add(SERIAL, d.serialNumber());
111 add(PROTOCOL, d.annotations().value(PROTOCOL));
112 add(CHASSISID, d.chassisId().toString());
Bri Prebilic Coleae65c962015-04-02 16:24:49 -0700113 add(MASTERID, ms.getMasterFor(d.id()).toString());
Thomas Vachuskae586b792015-03-26 13:59:38 -0700114 }
115
116 private String getTypeIconId(Device d) {
117 return DEV_ICON_PREFIX + d.type().toString();
118 }
119
120 @Override
121 protected String[] columnIds() {
122 return COL_IDS;
123 }
124 }
125
126}