Jimmy Yan | da878fc | 2016-09-02 16:32:01 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Jimmy Yan | da878fc | 2016-09-02 16:32:01 -0700 | [diff] [blame] | 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; |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 20 | import org.onlab.osgi.ServiceDirectory; |
Jimmy Yan | da878fc | 2016-09-02 16:32:01 -0700 | [diff] [blame] | 21 | import org.onosproject.mastership.MastershipService; |
| 22 | import org.onosproject.net.AnnotationKeys; |
| 23 | import org.onosproject.net.Device; |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 24 | import org.onosproject.net.DeviceId; |
Jimmy Yan | da878fc | 2016-09-02 16:32:01 -0700 | [diff] [blame] | 25 | import org.onosproject.net.device.DeviceService; |
| 26 | import org.onosproject.ui.RequestHandler; |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 27 | import org.onosproject.ui.UiConnection; |
Jimmy Yan | da878fc | 2016-09-02 16:32:01 -0700 | [diff] [blame] | 28 | import org.onosproject.ui.UiMessageHandler; |
| 29 | import org.onosproject.ui.table.TableModel; |
| 30 | import org.onosproject.ui.table.TableRequestHandler; |
| 31 | |
| 32 | import java.util.Collection; |
| 33 | |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 34 | import static org.onosproject.net.Device.Type; |
Jimmy Yan | da878fc | 2016-09-02 16:32:01 -0700 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * Table-View message handler for ROADM device view. |
| 38 | */ |
| 39 | public class RoadmDeviceViewMessageHandler extends UiMessageHandler { |
| 40 | |
Boyuan Yan | 6b23b38 | 2019-06-04 11:59:35 -0700 | [diff] [blame] | 41 | private static final String ROADM_DEVICE_DATA_REQ = "roadmDataRequest"; |
| 42 | private static final String ROADM_DEVICE_DATA_RESP = "roadmDataResponse"; |
| 43 | private static final String ROADM_DEVICES = "roadms"; |
Jimmy Yan | da878fc | 2016-09-02 16:32:01 -0700 | [diff] [blame] | 44 | |
Jimmy Yan | da878fc | 2016-09-02 16:32:01 -0700 | [diff] [blame] | 45 | private static final String ID = "id"; |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 46 | private static final String NAME = "name"; |
| 47 | private static final String TYPE = "type"; |
Jimmy Yan | da878fc | 2016-09-02 16:32:01 -0700 | [diff] [blame] | 48 | private static final String MASTER = "master"; |
| 49 | private static final String PORTS = "ports"; |
| 50 | private static final String VENDOR = "vendor"; |
| 51 | private static final String HW_VERSION = "hwVersion"; |
| 52 | private static final String SW_VERSION = "swVersion"; |
| 53 | private static final String PROTOCOL = "protocol"; |
| 54 | |
| 55 | private static final String[] COLUMN_IDS = { |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 56 | ID, NAME, TYPE, MASTER, PORTS, VENDOR, HW_VERSION, SW_VERSION, PROTOCOL |
Jimmy Yan | da878fc | 2016-09-02 16:32:01 -0700 | [diff] [blame] | 57 | }; |
| 58 | |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 59 | private DeviceService deviceService; |
| 60 | private MastershipService mastershipService; |
| 61 | |
| 62 | @Override |
| 63 | public void init(UiConnection connection, ServiceDirectory directory) { |
| 64 | super.init(connection, directory); |
| 65 | deviceService = get(DeviceService.class); |
| 66 | mastershipService = get(MastershipService.class); |
| 67 | } |
| 68 | |
Jimmy Yan | da878fc | 2016-09-02 16:32:01 -0700 | [diff] [blame] | 69 | @Override |
| 70 | protected Collection<RequestHandler> createRequestHandlers() { |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 71 | return ImmutableSet.of(new DeviceTableDataRequestHandler()); |
Jimmy Yan | da878fc | 2016-09-02 16:32:01 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | // Handler for sample table requests |
| 75 | private final class DeviceTableDataRequestHandler extends TableRequestHandler { |
| 76 | |
| 77 | private DeviceTableDataRequestHandler() { |
| 78 | super(ROADM_DEVICE_DATA_REQ, ROADM_DEVICE_DATA_RESP, ROADM_DEVICES); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | protected String[] getColumnIds() { |
| 83 | return COLUMN_IDS; |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | protected String noRowsMessage(ObjectNode payload) { |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 88 | return RoadmUtil.NO_ROWS_MESSAGE; |
Jimmy Yan | da878fc | 2016-09-02 16:32:01 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | @Override |
| 92 | protected void populateTable(TableModel tm, ObjectNode payload) { |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 93 | for (Device device : deviceService.getDevices()) { |
| 94 | Type type = device.type(); |
alessio | d4a2b84 | 2019-04-30 18:43:17 +0200 | [diff] [blame] | 95 | if (type == Type.ROADM || type == Type.TERMINAL_DEVICE |
Boyuan Yan | 6b23b38 | 2019-06-04 11:59:35 -0700 | [diff] [blame] | 96 | || type == Type.OPTICAL_AMPLIFIER || type == Type.FIBER_SWITCH |
Eroshkin Ivan | f5c0a09 | 2019-08-27 10:50:48 +0200 | [diff] [blame] | 97 | || type == Type.ROADM_OTN |
| 98 | || type == Type.OTN |
Boyuan Yan | 6b23b38 | 2019-06-04 11:59:35 -0700 | [diff] [blame] | 99 | || type == Type.OLS) { |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 100 | populateRow(tm.addRow(), device); |
| 101 | } |
Jimmy Yan | da878fc | 2016-09-02 16:32:01 -0700 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 105 | private void populateRow(TableModel.Row row, Device device) { |
| 106 | DeviceId devId = device.id(); |
| 107 | String id = devId.toString(); |
| 108 | row.cell(ID, id) |
Laszlo Papp | d4aaa35 | 2017-10-10 23:55:52 +0100 | [diff] [blame] | 109 | .cell(NAME, RoadmUtil.getAnnotation(device.annotations(), AnnotationKeys.NAME, id)) |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 110 | .cell(TYPE, RoadmUtil.objectToString(device.type(), RoadmUtil.UNKNOWN)) |
| 111 | .cell(MASTER, mastershipService.getMasterFor(devId)) |
| 112 | .cell(PORTS, deviceService.getPorts(devId).size()) |
Jimmy Yan | da878fc | 2016-09-02 16:32:01 -0700 | [diff] [blame] | 113 | .cell(VENDOR, device.manufacturer()) |
| 114 | .cell(HW_VERSION, device.hwVersion()) |
| 115 | .cell(SW_VERSION, device.swVersion()) |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 116 | .cell(PROTOCOL, RoadmUtil.getAnnotation(device.annotations(), PROTOCOL)); |
Jimmy Yan | da878fc | 2016-09-02 16:32:01 -0700 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | } |