Jimmy Yan | da878fc | 2016-09-02 16:32:01 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016-present 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.roadm; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import org.onosproject.mastership.MastershipService; |
| 21 | import org.onosproject.net.AnnotationKeys; |
| 22 | import org.onosproject.net.Device; |
| 23 | import org.onosproject.net.device.DeviceService; |
| 24 | import org.onosproject.ui.RequestHandler; |
| 25 | import org.onosproject.ui.UiMessageHandler; |
| 26 | import org.onosproject.ui.table.TableModel; |
| 27 | import org.onosproject.ui.table.TableRequestHandler; |
| 28 | |
| 29 | import java.util.Collection; |
| 30 | |
| 31 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 32 | |
| 33 | /** |
| 34 | * Table-View message handler for ROADM device view. |
| 35 | */ |
| 36 | public class RoadmDeviceViewMessageHandler extends UiMessageHandler { |
| 37 | |
| 38 | private static final String ROADM_DEVICE_DATA_REQ = "roadmDeviceDataRequest"; |
| 39 | private static final String ROADM_DEVICE_DATA_RESP = "roadmDeviceDataResponse"; |
| 40 | private static final String ROADM_DEVICES = "roadmDevices"; |
| 41 | |
| 42 | private static final String NO_ROWS_MESSAGE = "No items found"; |
| 43 | |
| 44 | private static final String ID = "id"; |
| 45 | private static final String FRIENDLY_NAME = "name"; |
| 46 | private static final String MASTER = "master"; |
| 47 | private static final String PORTS = "ports"; |
| 48 | private static final String VENDOR = "vendor"; |
| 49 | private static final String HW_VERSION = "hwVersion"; |
| 50 | private static final String SW_VERSION = "swVersion"; |
| 51 | private static final String PROTOCOL = "protocol"; |
| 52 | |
| 53 | private static final String[] COLUMN_IDS = { |
| 54 | ID, FRIENDLY_NAME, MASTER, PORTS, VENDOR, HW_VERSION, SW_VERSION, |
| 55 | PROTOCOL |
| 56 | }; |
| 57 | |
| 58 | @Override |
| 59 | protected Collection<RequestHandler> createRequestHandlers() { |
| 60 | return ImmutableSet.of( |
| 61 | new DeviceTableDataRequestHandler() |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | // Returns friendly name of the device from the annotations |
| 66 | private static String deviceName(Device device) { |
| 67 | String name = device.annotations().value(AnnotationKeys.NAME); |
| 68 | return isNullOrEmpty(name) ? device.id().toString() : name; |
| 69 | } |
| 70 | |
| 71 | // Returns the device protocol from annotations |
| 72 | private static String deviceProtocol(Device device) { |
| 73 | String protocol = device.annotations().value(PROTOCOL); |
| 74 | return protocol != null ? protocol : "N/A"; |
| 75 | } |
| 76 | |
| 77 | // Handler for sample table requests |
| 78 | private final class DeviceTableDataRequestHandler extends TableRequestHandler { |
| 79 | |
| 80 | private DeviceTableDataRequestHandler() { |
| 81 | super(ROADM_DEVICE_DATA_REQ, ROADM_DEVICE_DATA_RESP, ROADM_DEVICES); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | protected String[] getColumnIds() { |
| 86 | return COLUMN_IDS; |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | protected String noRowsMessage(ObjectNode payload) { |
| 91 | return NO_ROWS_MESSAGE; |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | protected void populateTable(TableModel tm, ObjectNode payload) { |
| 96 | DeviceService ds = get(DeviceService.class); |
| 97 | MastershipService ms = get(MastershipService.class); |
| 98 | for (Device device : ds.getDevices(Device.Type.ROADM)) { |
| 99 | populateRow(tm.addRow(), device, ds, ms); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | private void populateRow(TableModel.Row row, Device device, DeviceService ds, |
| 104 | MastershipService ms) { |
| 105 | row.cell(ID, device.id().toString()) |
| 106 | .cell(FRIENDLY_NAME, deviceName(device)) |
| 107 | .cell(MASTER, ms.getMasterFor(device.id())) |
| 108 | .cell(PORTS, ds.getPorts(device.id()).size()) |
| 109 | .cell(VENDOR, device.manufacturer()) |
| 110 | .cell(HW_VERSION, device.hwVersion()) |
| 111 | .cell(SW_VERSION, device.swVersion()) |
| 112 | .cell(PROTOCOL, deviceProtocol(device)); |
| 113 | } |
| 114 | } |
| 115 | } |