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; |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 23 | import org.onosproject.net.DeviceId; |
| 24 | import org.onosproject.net.Port; |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 25 | import org.onosproject.net.device.DeviceService; |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 26 | import org.onosproject.net.link.LinkService; |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 27 | |
| 28 | import java.util.ArrayList; |
| 29 | import java.util.Arrays; |
| 30 | import java.util.List; |
| 31 | |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 32 | //import org.onosproject.net.Link; |
| 33 | //import java.util.Set; |
| 34 | |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 35 | /** |
| 36 | * Message handler for device view related messages. |
| 37 | */ |
| 38 | public 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 Cole | ae65c96 | 2015-04-02 16:24:49 -0700 | [diff] [blame] | 54 | MastershipService mastershipService = get(MastershipService.class); |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 55 | LinkService linkService = get(LinkService.class); |
| 56 | TableRow[] rows = generateTableRows(service, |
| 57 | mastershipService, |
| 58 | linkService); |
Bri Prebilic Cole | ae65c96 | 2015-04-02 16:24:49 -0700 | [diff] [blame] | 59 | RowComparator rc = |
| 60 | new RowComparator(sortCol, RowComparator.direction(sortDir)); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 61 | 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 Cole | ae65c96 | 2015-04-02 16:24:49 -0700 | [diff] [blame] | 69 | private TableRow[] generateTableRows(DeviceService service, |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 70 | MastershipService mastershipService, |
| 71 | LinkService linkService) { |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 72 | List<TableRow> list = new ArrayList<>(); |
| 73 | for (Device dev : service.getDevices()) { |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 74 | list.add(new DeviceTableRow(service, |
| 75 | mastershipService, |
| 76 | linkService, |
| 77 | dev)); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 78 | } |
| 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 Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 92 | private static final String NUM_PORTS = "num_ports"; |
| 93 | private static final String NUM_EGRESS_LINKS = "num_elinks"; |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 94 | private static final String MFR = "mfr"; |
| 95 | private static final String HW = "hw"; |
| 96 | private static final String SW = "sw"; |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 97 | private static final String PROTOCOL = "protocol"; |
Bri Prebilic Cole | ae65c96 | 2015-04-02 16:24:49 -0700 | [diff] [blame] | 98 | private static final String MASTERID = "masterid"; |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 99 | private static final String CHASSISID = "chassisid"; |
| 100 | private static final String SERIAL = "serial"; |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 101 | |
| 102 | private static final String[] COL_IDS = { |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 103 | AVAILABLE, AVAILABLE_IID, TYPE_IID, ID, |
| 104 | NUM_PORTS, NUM_EGRESS_LINKS, MASTERID, MFR, HW, SW, |
| 105 | PROTOCOL, CHASSISID, SERIAL |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | private static final String ICON_ID_ONLINE = "deviceOnline"; |
| 109 | private static final String ICON_ID_OFFLINE = "deviceOffline"; |
| 110 | |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 111 | // 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 Cole | ae65c96 | 2015-04-02 16:24:49 -0700 | [diff] [blame] | 134 | public DeviceTableRow(DeviceService service, |
| 135 | MastershipService ms, |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 136 | LinkService ls, |
Bri Prebilic Cole | ae65c96 | 2015-04-02 16:24:49 -0700 | [diff] [blame] | 137 | Device d) { |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 138 | boolean available = service.isAvailable(d.id()); |
| 139 | String iconId = available ? ICON_ID_ONLINE : ICON_ID_OFFLINE; |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 140 | DeviceId id = d.id(); |
| 141 | List<Port> ports = service.getPorts(id); |
| 142 | // Set<Link> links = ls.getDeviceEgressLinks(id); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 143 | |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 144 | add(ID, id.toString()); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 145 | add(AVAILABLE, Boolean.toString(available)); |
| 146 | add(AVAILABLE_IID, iconId); |
| 147 | add(TYPE_IID, getTypeIconId(d)); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 148 | add(MFR, d.manufacturer()); |
| 149 | add(HW, d.hwVersion()); |
| 150 | add(SW, d.swVersion()); |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 151 | // add(SERIAL, d.serialNumber()); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 152 | add(PROTOCOL, d.annotations().value(PROTOCOL)); |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 153 | add(NUM_PORTS, Integer.toString(ports.size())); |
| 154 | // add(NUM_EGRESS_LINKS, Integer.toString(links.size())); |
| 155 | // add(CHASSISID, d.chassisId().toString()); |
Bri Prebilic Cole | ae65c96 | 2015-04-02 16:24:49 -0700 | [diff] [blame] | 156 | add(MASTERID, ms.getMasterFor(d.id()).toString()); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 157 | } |
| 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 | } |