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; |
| 21 | import org.onosproject.net.Device; |
| 22 | import org.onosproject.net.device.DeviceService; |
| 23 | |
| 24 | import java.util.ArrayList; |
| 25 | import java.util.Arrays; |
| 26 | import java.util.List; |
| 27 | |
| 28 | /** |
| 29 | * Message handler for host view related messages. |
| 30 | */ |
| 31 | public class HostViewMessageHandler extends AbstractTabularViewMessageHandler { |
| 32 | |
| 33 | /** |
| 34 | * Creates a new message handler for the host messages. |
| 35 | */ |
| 36 | protected HostViewMessageHandler() { |
| 37 | super(ImmutableSet.of("hostDataRequest")); |
| 38 | } |
| 39 | |
| 40 | @Override |
| 41 | public void process(ObjectNode message) { |
| 42 | ObjectNode payload = payload(message); |
| 43 | String sortCol = string(payload, "sortCol", "id"); |
| 44 | String sortDir = string(payload, "sortDir", "asc"); |
| 45 | |
| 46 | DeviceService service = get(DeviceService.class); |
| 47 | TableRow[] rows = generateTableRows(service); |
| 48 | RowComparator rc = new RowComparator(sortCol, RowComparator.direction(sortDir)); |
| 49 | Arrays.sort(rows, rc); |
| 50 | ArrayNode devices = generateArrayNode(rows); |
| 51 | ObjectNode rootNode = mapper.createObjectNode(); |
| 52 | rootNode.set("devices", devices); |
| 53 | |
| 54 | connection().sendMessage("hostDataResponse", 0, rootNode); |
| 55 | } |
| 56 | |
| 57 | private TableRow[] generateTableRows(DeviceService service) { |
| 58 | List<TableRow> list = new ArrayList<>(); |
| 59 | for (Device dev : service.getDevices()) { |
| 60 | list.add(new HostTableRow(service, dev)); |
| 61 | } |
| 62 | return list.toArray(new TableRow[list.size()]); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * TableRow implementation for {@link Device devices}. |
| 67 | */ |
| 68 | private static class HostTableRow extends AbstractTableRow { |
| 69 | |
| 70 | private static final String ID = "id"; |
| 71 | private static final String AVAILABLE = "available"; |
| 72 | private static final String AVAILABLE_IID = "_iconid_available"; |
| 73 | private static final String TYPE_IID = "_iconid_type"; |
| 74 | private static final String DEV_ICON_PREFIX = "devIcon_"; |
| 75 | private static final String ROLE = "role"; |
| 76 | private static final String MFR = "mfr"; |
| 77 | private static final String HW = "hw"; |
| 78 | private static final String SW = "sw"; |
| 79 | private static final String SERIAL = "serial"; |
| 80 | private static final String PROTOCOL = "protocol"; |
| 81 | private static final String CHASSISID = "chassisid"; |
| 82 | |
| 83 | private static final String[] COL_IDS = { |
| 84 | ID, AVAILABLE, AVAILABLE_IID, TYPE_IID, ROLE, |
| 85 | MFR, HW, SW, SERIAL, PROTOCOL, CHASSISID |
| 86 | }; |
| 87 | |
| 88 | private static final String ICON_ID_ONLINE = "deviceOnline"; |
| 89 | private static final String ICON_ID_OFFLINE = "deviceOffline"; |
| 90 | |
| 91 | public HostTableRow(DeviceService service, Device d) { |
| 92 | boolean available = service.isAvailable(d.id()); |
| 93 | String iconId = available ? ICON_ID_ONLINE : ICON_ID_OFFLINE; |
| 94 | |
| 95 | add(ID, d.id().toString()); |
| 96 | add(AVAILABLE, Boolean.toString(available)); |
| 97 | add(AVAILABLE_IID, iconId); |
| 98 | add(TYPE_IID, getTypeIconId(d)); |
| 99 | add(ROLE, service.getRole(d.id()).toString()); |
| 100 | add(MFR, d.manufacturer()); |
| 101 | add(HW, d.hwVersion()); |
| 102 | add(SW, d.swVersion()); |
| 103 | add(SERIAL, d.serialNumber()); |
| 104 | add(PROTOCOL, d.annotations().value(PROTOCOL)); |
| 105 | add(CHASSISID, d.chassisId().toString()); |
| 106 | } |
| 107 | |
| 108 | private String getTypeIconId(Device d) { |
| 109 | return DEV_ICON_PREFIX + d.type().toString(); |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | protected String[] columnIds() { |
| 114 | return COL_IDS; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | } |