Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -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 | package org.onosproject.ui.impl; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import com.google.common.collect.ImmutableSet; |
Bri Prebilic Cole | ae65c96 | 2015-04-02 16:24:49 -0700 | [diff] [blame^] | 21 | import org.onosproject.mastership.MastershipService; |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 22 | import org.onosproject.net.Device; |
| 23 | import org.onosproject.net.device.DeviceService; |
| 24 | |
| 25 | import java.util.ArrayList; |
| 26 | import java.util.Arrays; |
| 27 | import java.util.List; |
| 28 | |
| 29 | /** |
| 30 | * Message handler for device view related messages. |
| 31 | */ |
| 32 | public 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 Cole | ae65c96 | 2015-04-02 16:24:49 -0700 | [diff] [blame^] | 48 | MastershipService mastershipService = get(MastershipService.class); |
| 49 | TableRow[] rows = generateTableRows(service, mastershipService); |
| 50 | RowComparator rc = |
| 51 | new RowComparator(sortCol, RowComparator.direction(sortDir)); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 52 | 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 Cole | ae65c96 | 2015-04-02 16:24:49 -0700 | [diff] [blame^] | 60 | private TableRow[] generateTableRows(DeviceService service, |
| 61 | MastershipService mastershipService) { |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 62 | List<TableRow> list = new ArrayList<>(); |
| 63 | for (Device dev : service.getDevices()) { |
Bri Prebilic Cole | ae65c96 | 2015-04-02 16:24:49 -0700 | [diff] [blame^] | 64 | list.add(new DeviceTableRow(service, mastershipService, dev)); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 65 | } |
| 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 Cole | ae65c96 | 2015-04-02 16:24:49 -0700 | [diff] [blame^] | 86 | private static final String MASTERID = "masterid"; |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 87 | |
| 88 | private static final String[] COL_IDS = { |
| 89 | ID, AVAILABLE, AVAILABLE_IID, TYPE_IID, ROLE, |
Bri Prebilic Cole | ae65c96 | 2015-04-02 16:24:49 -0700 | [diff] [blame^] | 90 | MFR, HW, SW, SERIAL, PROTOCOL, CHASSISID, MASTERID |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | private static final String ICON_ID_ONLINE = "deviceOnline"; |
| 94 | private static final String ICON_ID_OFFLINE = "deviceOffline"; |
| 95 | |
Bri Prebilic Cole | ae65c96 | 2015-04-02 16:24:49 -0700 | [diff] [blame^] | 96 | public DeviceTableRow(DeviceService service, |
| 97 | MastershipService ms, |
| 98 | Device d) { |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 99 | 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 Cole | ae65c96 | 2015-04-02 16:24:49 -0700 | [diff] [blame^] | 113 | add(MASTERID, ms.getMasterFor(d.id()).toString()); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 114 | } |
| 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 | } |