blob: 3a648c409e5c28f86a321ff6340bec3bc34a94d6 [file] [log] [blame]
Jimmy Yanda878fc2016-09-02 16:32:01 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Jimmy Yanda878fc2016-09-02 16:32:01 -07003 *
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 */
16package org.onosproject.roadm;
17
MaoLu2846b112017-05-15 17:18:55 -070018import com.fasterxml.jackson.databind.node.ArrayNode;
19import com.fasterxml.jackson.databind.node.JsonNodeFactory;
Jimmy Yanda878fc2016-09-02 16:32:01 -070020import com.fasterxml.jackson.databind.node.ObjectNode;
21import com.google.common.collect.ImmutableSet;
22import com.google.common.collect.Range;
23import org.onlab.osgi.ServiceDirectory;
MaoLu937cf422017-03-03 23:31:46 -080024import org.onlab.util.Frequency;
Jimmy Yanda878fc2016-09-02 16:32:01 -070025import org.onosproject.net.AnnotationKeys;
MaoLu2846b112017-05-15 17:18:55 -070026import org.onosproject.net.ConnectPoint;
Jimmy Yanda878fc2016-09-02 16:32:01 -070027import org.onosproject.net.DeviceId;
Sean Condonf20b8ef2019-08-13 16:45:52 +010028import org.onosproject.net.Direction;
Andrea Campanellabdeeda12019-08-02 16:12:05 +020029import org.onosproject.net.ModulationScheme;
MaoLu937cf422017-03-03 23:31:46 -080030import org.onosproject.net.OchSignal;
Jimmy Yanda878fc2016-09-02 16:32:01 -070031import org.onosproject.net.Port;
32import org.onosproject.net.PortNumber;
Sean Condonf20b8ef2019-08-13 16:45:52 +010033import org.onosproject.net.behaviour.PowerConfig;
Andrea Campanellabdeeda12019-08-02 16:12:05 +020034import org.onosproject.net.behaviour.protection.ProtectedTransportEndpointState;
35import org.onosproject.net.behaviour.protection.TransportEndpointState;
36import org.onosproject.net.device.DeviceService;
alessiod4a2b842019-04-30 18:43:17 +020037import org.onosproject.net.intent.OpticalPathIntent;
MaoLu2846b112017-05-15 17:18:55 -070038import org.onosproject.net.optical.OpticalAnnotations;
Jimmy Yanda878fc2016-09-02 16:32:01 -070039import org.onosproject.ui.RequestHandler;
40import org.onosproject.ui.UiConnection;
41import org.onosproject.ui.UiMessageHandler;
42import org.onosproject.ui.table.TableModel;
43import org.onosproject.ui.table.TableRequestHandler;
44import org.slf4j.Logger;
45import org.slf4j.LoggerFactory;
46
47import java.util.Collection;
MaoLu937cf422017-03-03 23:31:46 -080048import java.util.Collections;
49import java.util.Comparator;
Jimmy Yanda878fc2016-09-02 16:32:01 -070050import java.util.List;
MaoLu2846b112017-05-15 17:18:55 -070051import java.util.Map;
Sean Condonf20b8ef2019-08-13 16:45:52 +010052import java.util.Optional;
MaoLu937cf422017-03-03 23:31:46 -080053import java.util.Set;
54
55import static org.onosproject.net.Device.Type;
MaoLu2846b112017-05-15 17:18:55 -070056import static org.onosproject.net.behaviour.protection.ProtectedTransportEndpointState.ACTIVE_UNKNOWN;
57import static org.onosproject.roadm.RoadmUtil.OPS_OPT_AUTO;
58import static org.onosproject.roadm.RoadmUtil.OPS_OPT_FORCE;
59import static org.onosproject.roadm.RoadmUtil.OPS_OPT_MANUAL;
Jimmy Yanda878fc2016-09-02 16:32:01 -070060
61/**
62 * Table-View message handler for ROADM port view.
63 */
64public class RoadmPortViewMessageHandler extends UiMessageHandler {
65
66 private static final String ROADM_PORT_DATA_REQ = "roadmPortDataRequest";
67 private static final String ROADM_PORT_DATA_RESP = "roadmPortDataResponse";
68 private static final String ROADM_PORTS = "roadmPorts";
Jimmy Yanda878fc2016-09-02 16:32:01 -070069 private static final String ROADM_SET_TARGET_POWER_REQ = "roadmSetTargetPowerRequest";
70 private static final String ROADM_SET_TARGET_POWER_RESP = "roadmSetTargetPowerResponse";
Andrea Campanellabdeeda12019-08-02 16:12:05 +020071 private static final String ROADM_SET_MODULATION_REQ = "roadmSetModulationRequest";
72 private static final String ROADM_SET_MODULATION_RESP = "roadmSetModulationResponse";
Boyuan Yanf3f6a8d2019-05-26 18:35:54 -070073 private static final String ROADM_SYNC_TARGET_POWER_REQ = "roadmSyncTargetPowerRequest";
74 private static final String ROADM_SYNC_TARGET_POWER_RESP = "roadmSyncTargetPowerResp";
MaoLu937cf422017-03-03 23:31:46 -080075 private static final String ROADM_SHOW_ITEMS_REQ = "roadmShowPortItemsRequest";
76 private static final String ROADM_SHOW_ITEMS_RESP = "roadmShowPortItemsResponse";
77 private static final String ROADM_SET_OPS_MODE_REQ = "roadmSetOpsModeRequest";
78 private static final String ROADM_SET_OPS_MODE_RESP = "roadmSetOpsModeResponse";
Jimmy Yanda878fc2016-09-02 16:32:01 -070079
80 private static final String ID = "id";
alessiod4a2b842019-04-30 18:43:17 +020081 private static final String REVERSE_PORT = "reversePort";
Jimmy Yanda878fc2016-09-02 16:32:01 -070082 private static final String NAME = "name";
MaoLu937cf422017-03-03 23:31:46 -080083 private static final String TYPE = "type";
Jimmy Yanda878fc2016-09-02 16:32:01 -070084 private static final String ENABLED = "enabled";
85 private static final String MIN_FREQ = "minFreq";
86 private static final String MAX_FREQ = "maxFreq";
87 private static final String GRID = "grid";
Andrea Campanella7ebfe322019-08-29 11:46:57 -070088 private static final String CURR_FREQ = "currFreq";
MaoLu2f7eadb2017-05-02 15:38:43 -070089 private static final String POWER_RANGE = "powerRange";
Jimmy Yanda878fc2016-09-02 16:32:01 -070090 private static final String CURRENT_POWER = "currentPower";
Sean Condonf20b8ef2019-08-13 16:45:52 +010091 private static final String CURRENT_INPUT_POWER = "currentInputPower";
Jimmy Yanda878fc2016-09-02 16:32:01 -070092 private static final String TARGET_POWER = "targetPower";
Andrea Campanellabdeeda12019-08-02 16:12:05 +020093 private static final String MODULATION = "modulation";
Jimmy Yanda878fc2016-09-02 16:32:01 -070094 private static final String HAS_TARGET_POWER = "hasTargetPower";
MaoLu937cf422017-03-03 23:31:46 -080095 private static final String SERVICE_STATE = "serviceState";
Jimmy Yanda878fc2016-09-02 16:32:01 -070096
97 private static final String[] COLUMN_IDS = {
Andrea Campanella7ebfe322019-08-29 11:46:57 -070098 ID, REVERSE_PORT, TYPE, NAME, ENABLED, MIN_FREQ, MAX_FREQ, GRID, CURR_FREQ, POWER_RANGE,
Sean Condonf20b8ef2019-08-13 16:45:52 +010099 CURRENT_POWER, CURRENT_INPUT_POWER, SERVICE_STATE, TARGET_POWER, MODULATION, HAS_TARGET_POWER
Jimmy Yanda878fc2016-09-02 16:32:01 -0700100 };
101
Jimmy Yanda878fc2016-09-02 16:32:01 -0700102 private RoadmService roadmService;
MaoLu937cf422017-03-03 23:31:46 -0800103 private DeviceService deviceService;
Jimmy Yanda878fc2016-09-02 16:32:01 -0700104
105 private final Logger log = LoggerFactory.getLogger(getClass());
106
107 @Override
108 public void init(UiConnection connection, ServiceDirectory directory) {
109 super.init(connection, directory);
Jimmy Yanda878fc2016-09-02 16:32:01 -0700110 roadmService = get(RoadmService.class);
MaoLu937cf422017-03-03 23:31:46 -0800111 deviceService = get(DeviceService.class);
Jimmy Yanda878fc2016-09-02 16:32:01 -0700112 }
113
114 @Override
115 protected Collection<RequestHandler> createRequestHandlers() {
MaoLu937cf422017-03-03 23:31:46 -0800116 return ImmutableSet.of(new PortTableDataRequestHandler(),
117 new SetTargetPowerRequestHandler(),
118 new CreateShowItemsRequestHandler(),
Boyuan Yanf3f6a8d2019-05-26 18:35:54 -0700119 new CreateOpsModeSetRequestHandler(),
Andrea Campanellabdeeda12019-08-02 16:12:05 +0200120 new SyncTargetPowerRequestHandler(),
121 new SetModulationRequestHandler()
122 );
Jimmy Yanda878fc2016-09-02 16:32:01 -0700123 }
124
125 // Handler for sample table requests
126 private final class PortTableDataRequestHandler extends TableRequestHandler {
127
128 private PortTableDataRequestHandler() {
129 super(ROADM_PORT_DATA_REQ, ROADM_PORT_DATA_RESP, ROADM_PORTS);
130 }
131
132 @Override
133 protected String[] getColumnIds() {
134 return COLUMN_IDS;
135 }
136
137 @Override
138 protected String noRowsMessage(ObjectNode payload) {
MaoLu937cf422017-03-03 23:31:46 -0800139 return RoadmUtil.NO_ROWS_MESSAGE;
Jimmy Yanda878fc2016-09-02 16:32:01 -0700140 }
141
142 @Override
143 protected void populateTable(TableModel tm, ObjectNode payload) {
MaoLu937cf422017-03-03 23:31:46 -0800144 DeviceId deviceId = DeviceId.deviceId(string(payload, RoadmUtil.DEV_ID));
Jimmy Yanda878fc2016-09-02 16:32:01 -0700145 if (deviceService.isAvailable(deviceId)) {
146 List<Port> ports = deviceService.getPorts(deviceId);
147 for (Port port : ports) {
148 populateRow(tm.addRow(), port, deviceId);
149 }
150 }
151 }
152
153 private void populateRow(TableModel.Row row, Port port, DeviceId deviceId) {
MaoLu937cf422017-03-03 23:31:46 -0800154 PortNumber portNum = port.number();
155 getFrequencyLimit(deviceId, portNum);
156 row.cell(ID, portNum.toLong())
alessiod4a2b842019-04-30 18:43:17 +0200157 .cell(REVERSE_PORT, RoadmUtil.getAnnotation(port.annotations(),
158 OpticalPathIntent.REVERSE_PORT_ANNOTATION_KEY))
Jimmy Yanda878fc2016-09-02 16:32:01 -0700159 .cell(TYPE, port.type())
160 .cell(ENABLED, port.isEnabled())
MaoLu937cf422017-03-03 23:31:46 -0800161 .cell(NAME, RoadmUtil.getAnnotation(port.annotations(), AnnotationKeys.PORT_NAME))
162 .cell(MIN_FREQ, RoadmUtil.asTHz(minFreq))
163 .cell(MAX_FREQ, RoadmUtil.asTHz(maxFreq))
164 .cell(GRID, RoadmUtil.asGHz(channelSpacing))
Andrea Campanella7ebfe322019-08-29 11:46:57 -0700165 .cell(CURR_FREQ, getWavelength(deviceId, portNum))
MaoLu2f7eadb2017-05-02 15:38:43 -0700166 .cell(POWER_RANGE, getPowerRange(deviceId, portNum))
MaoLu937cf422017-03-03 23:31:46 -0800167 .cell(CURRENT_POWER, getCurrentPower(deviceId, portNum))
Sean Condonf20b8ef2019-08-13 16:45:52 +0100168 .cell(CURRENT_INPUT_POWER, getCurrentInputPower(deviceId, portNum))
MaoLu937cf422017-03-03 23:31:46 -0800169 .cell(SERVICE_STATE, getPortServiceState(deviceId, portNum))
Andrea Campanellabdeeda12019-08-02 16:12:05 +0200170 .cell(MODULATION, getModulation(deviceId, portNum))
MaoLu937cf422017-03-03 23:31:46 -0800171 .cell(TARGET_POWER, getTargetPower(deviceId, portNum))
172 .cell(HAS_TARGET_POWER, roadmService.hasPortTargetPower(deviceId, portNum));
173 }
174
175 private String getPortServiceState(DeviceId deviceId, PortNumber portNumber) {
MaoLu2846b112017-05-15 17:18:55 -0700176 if (deviceService.getDevice(deviceId).type() != Type.FIBER_SWITCH) {
177 return RoadmUtil.NA;
178 }
179 Map<ConnectPoint, ProtectedTransportEndpointState> map =
180 roadmService.getProtectionSwitchStates(deviceId);
181 for (ProtectedTransportEndpointState state : map.values()) {
182 for (TransportEndpointState element : state.pathStates()) {
183 if (element.description().output().connectPoint().port().equals(portNumber)) {
184 return RoadmUtil.defaultString(element.attributes()
Andrea Campanellabdeeda12019-08-02 16:12:05 +0200185 .get(OpticalAnnotations.INPUT_PORT_STATUS), RoadmUtil.UNKNOWN);
MaoLu2846b112017-05-15 17:18:55 -0700186 }
187 }
188 }
189 return RoadmUtil.UNKNOWN;
MaoLu937cf422017-03-03 23:31:46 -0800190 }
191
Andrea Campanella7ebfe322019-08-29 11:46:57 -0700192 private Frequency minFreq = null, maxFreq = null, channelSpacing = null, currFreq = null;
Andrea Campanellabdeeda12019-08-02 16:12:05 +0200193
MaoLu937cf422017-03-03 23:31:46 -0800194 // Gets min frequency, max frequency, channel spacing
195 private void getFrequencyLimit(DeviceId deviceId, PortNumber portNumber) {
196 Set<OchSignal> signals = roadmService.queryLambdas(deviceId, portNumber);
197 if (signals.isEmpty()) {
198 return;
199 }
200 Comparator<OchSignal> compare =
201 (OchSignal a, OchSignal b) -> a.spacingMultiplier() - b.spacingMultiplier();
202 OchSignal minOch = Collections.min(signals, compare);
203 OchSignal maxOch = Collections.max(signals, compare);
204 minFreq = minOch.centralFrequency();
205 maxFreq = maxOch.centralFrequency();
206 channelSpacing = minOch.channelSpacing().frequency();
Jimmy Yanda878fc2016-09-02 16:32:01 -0700207 }
208
MaoLu2f7eadb2017-05-02 15:38:43 -0700209 // Returns the power range as a string, N/A if the power range not exists.
210 // The power range would be input power range or target power range determined by port property.
211 // If the port is RX direction then acquire the input power range from driver.
212 // Otherwise there will be a TX direction port, thus acquire the target power range.
213 private String getPowerRange(DeviceId deviceId, PortNumber portNumber) {
Andrea Campanelladadf6402019-08-07 15:24:11 +0200214 Range<Double> range = roadmService.inputPortPowerRange(deviceId, portNumber);
MaoLu2f7eadb2017-05-02 15:38:43 -0700215 if (range == null) {
216 range = roadmService.targetPortPowerRange(deviceId, portNumber);
217 }
MaoLu937cf422017-03-03 23:31:46 -0800218 return RoadmUtil.objectToString(range, RoadmUtil.NA);
Jimmy Yanda878fc2016-09-02 16:32:01 -0700219 }
220
221 // Returns the current power as a string, Unknown if no value can be found.
222 private String getCurrentPower(DeviceId deviceId, PortNumber portNumber) {
Andrea Campanelladadf6402019-08-07 15:24:11 +0200223 Double currentPower = roadmService.getCurrentPortPower(deviceId, portNumber);
MaoLu937cf422017-03-03 23:31:46 -0800224 return RoadmUtil.objectToString(currentPower, RoadmUtil.UNKNOWN);
Jimmy Yanda878fc2016-09-02 16:32:01 -0700225 }
226
Sean Condonf20b8ef2019-08-13 16:45:52 +0100227 // Returns the current input power as a string, Unknown if no value can be found.
228 private String getCurrentInputPower(DeviceId deviceId, PortNumber portNumber) {
229 PowerConfig powerConfig = deviceService.getDevice(deviceId).as(PowerConfig.class);
230 Optional<Double> currentInputPower = powerConfig.currentInputPower(portNumber, Direction.ALL);
231 Double inputPowerVal = null;
232 if (currentInputPower.isPresent()) {
233 inputPowerVal = currentInputPower.orElse(Double.MIN_VALUE);
234 }
235 return RoadmUtil.objectToString(inputPowerVal, RoadmUtil.UNKNOWN);
236 }
237
Jimmy Yanda878fc2016-09-02 16:32:01 -0700238 // Returns target power as a string, Unknown if target power is expected but
239 // cannot be found, N/A if port does not have configurable target power
240 private String getTargetPower(DeviceId deviceId, PortNumber portNumber) {
MaoLu937cf422017-03-03 23:31:46 -0800241 if (!roadmService.hasPortTargetPower(deviceId, portNumber)) {
242 return RoadmUtil.NA;
Jimmy Yanda878fc2016-09-02 16:32:01 -0700243 }
Andrea Campanelladadf6402019-08-07 15:24:11 +0200244 Double targetPower = roadmService.getTargetPortPower(deviceId, portNumber);
MaoLu937cf422017-03-03 23:31:46 -0800245 return RoadmUtil.objectToString(targetPower, RoadmUtil.UNKNOWN);
Jimmy Yanda878fc2016-09-02 16:32:01 -0700246 }
Andrea Campanellabdeeda12019-08-02 16:12:05 +0200247
248 // Returns modulation as a string, Unknown if modulation is expected but
249 // cannot be found
250 private String getModulation(DeviceId deviceId, PortNumber portNumber) {
251 Port port = deviceService.getPort(deviceId, portNumber);
252 ModulationScheme modulation = null;
253 if (port.type().equals(Port.Type.OCH)) {
254 modulation = roadmService.getModulation(deviceId, portNumber);
255 }
256 return RoadmUtil.objectToString(modulation, RoadmUtil.UNKNOWN);
257 }
Andrea Campanella7ebfe322019-08-29 11:46:57 -0700258
259 // Returns modulation as a string, Unknown if modulation is expected but
260 // cannot be found
261 private String getWavelength(DeviceId deviceId, PortNumber portNumber) {
262 Frequency currentFrequency = roadmService.getWavelength(deviceId, portNumber);
263 if (currentFrequency == null) {
264 return "0";
265 } else {
266 return String.valueOf(currentFrequency.asTHz());
267 }
268 }
Jimmy Yanda878fc2016-09-02 16:32:01 -0700269 }
270
271
272 // Handler for setting port target power
273 private final class SetTargetPowerRequestHandler extends RequestHandler {
274
Jimmy Yanda878fc2016-09-02 16:32:01 -0700275 private static final String TARGET_POWER_ERR_MSG = "Target power range is %s.";
276
277 private SetTargetPowerRequestHandler() {
278 super(ROADM_SET_TARGET_POWER_REQ);
279 }
280
281 @Override
282 public void process(ObjectNode payload) {
MaoLu937cf422017-03-03 23:31:46 -0800283 DeviceId deviceId = DeviceId.deviceId(string(payload, RoadmUtil.DEV_ID));
Jimmy Yanda878fc2016-09-02 16:32:01 -0700284 PortNumber portNumber = PortNumber.portNumber(payload.get(ID).asLong());
Andrea Campanelladadf6402019-08-07 15:24:11 +0200285 Range<Double> range = roadmService.targetPortPowerRange(deviceId, portNumber);
MaoLu937cf422017-03-03 23:31:46 -0800286 if (range == null) {
Jimmy Yanda878fc2016-09-02 16:32:01 -0700287 log.warn("Unable to determine target power range for device {}", deviceId);
MaoLu937cf422017-03-03 23:31:46 -0800288 return;
Jimmy Yanda878fc2016-09-02 16:32:01 -0700289 }
Andrea Campanelladadf6402019-08-07 15:24:11 +0200290 Double targetPower = payload.get(TARGET_POWER).asDouble();
MaoLu937cf422017-03-03 23:31:46 -0800291 boolean validTargetPower = range.contains(targetPower);
292 if (validTargetPower) {
293 roadmService.setTargetPortPower(deviceId, portNumber, targetPower);
294 }
295 ObjectNode rootNode = objectNode();
296 rootNode.put(ID, payload.get(ID).asText());
297 rootNode.put(RoadmUtil.VALID, validTargetPower);
298 rootNode.put(RoadmUtil.MESSAGE, String.format(TARGET_POWER_ERR_MSG, range.toString()));
299 sendMessage(ROADM_SET_TARGET_POWER_RESP, rootNode);
300 }
301 }
302
Boyuan Yanf3f6a8d2019-05-26 18:35:54 -0700303 // Handler for sync-up port target power
304 private final class SyncTargetPowerRequestHandler extends RequestHandler {
305
306 private static final String SYNCED_TARGET_POWER = "Synced target power is %s.";
Andrea Campanellabdeeda12019-08-02 16:12:05 +0200307
Boyuan Yanf3f6a8d2019-05-26 18:35:54 -0700308 private SyncTargetPowerRequestHandler() {
309 super(ROADM_SYNC_TARGET_POWER_REQ);
310 }
311
312 @Override
313 public void process(ObjectNode payload) {
314 DeviceId deviceId = DeviceId.deviceId(string(payload, RoadmUtil.DEV_ID));
315 PortNumber portNumber = PortNumber.portNumber(payload.get(ID).asLong());
Andrea Campanelladadf6402019-08-07 15:24:11 +0200316 Double targetPower = roadmService.syncTargetPortPower(deviceId, portNumber);
Boyuan Yanf3f6a8d2019-05-26 18:35:54 -0700317 String power = RoadmUtil.objectToString(targetPower, RoadmUtil.UNKNOWN);
318 ObjectNode rootNode = objectNode();
319 rootNode.put(ID, payload.get(ID).asText())
320 .put(RoadmUtil.VALID, true)
321 .put(RoadmUtil.MESSAGE, String.format(SYNCED_TARGET_POWER, power));
322 sendMessage(ROADM_SYNC_TARGET_POWER_RESP, rootNode);
323 }
324 }
325
Andrea Campanellabdeeda12019-08-02 16:12:05 +0200326 // Handler for setting port modulation
327 private final class SetModulationRequestHandler extends RequestHandler {
328
329 private static final String TARGET_MODULATION_MSG = "Target modulation is %s.";
330
331 private SetModulationRequestHandler() {
332 super(ROADM_SET_MODULATION_REQ);
333 }
334
335 @Override
336 public void process(ObjectNode payload) {
337 DeviceId deviceId = DeviceId.deviceId(string(payload, RoadmUtil.DEV_ID));
338 PortNumber portNumber = PortNumber.portNumber(payload.get(ID).asLong());
339 String modulation = payload.get(MODULATION).asText();
340 roadmService.setModulation(deviceId, portNumber, modulation);
341 ObjectNode rootNode = objectNode();
342 rootNode.put(ID, payload.get(ID).asText());
343 rootNode.put(RoadmUtil.VALID, modulation);
344 rootNode.put(RoadmUtil.MESSAGE, String.format(TARGET_MODULATION_MSG, modulation));
345 sendMessage(ROADM_SET_MODULATION_RESP, rootNode);
346 }
347 }
348
MaoLu2846b112017-05-15 17:18:55 -0700349 // Protection switch operation type and path index
350 private static final String OPS_ARRAY_INDEX = "index";
351 private static final String OPS_ARRAY_OPERATION = "operation";
352 private static final String[] OPS_NON_AUTO_OPTS = {OPS_OPT_FORCE, OPS_OPT_MANUAL};
353
MaoLu937cf422017-03-03 23:31:46 -0800354 private final class CreateShowItemsRequestHandler extends RequestHandler {
355 private static final String SHOW_TARGET_POWER = "showTargetPower";
356 private static final String SHOW_SERVICE_STATE = "showServiceState";
357 private static final String SHOW_FLOW_ICON = "showFlowIcon";
MaoLu2846b112017-05-15 17:18:55 -0700358 private static final String OPS_PATHS = "opsOperations";
359 private static final String OPS_ARRAY_NAME = "name";
360 private static final String OPS_GROUP_FMT = "GROUP%d ";
MaoLu937cf422017-03-03 23:31:46 -0800361
362 private CreateShowItemsRequestHandler() {
363 super(ROADM_SHOW_ITEMS_REQ);
364 }
365
366 @Override
367 public void process(ObjectNode payload) {
368 DeviceId did = DeviceId.deviceId(string(payload, RoadmUtil.DEV_ID));
369 Type devType = deviceService.getDevice(did).type();
370 // Build response
371 ObjectNode node = objectNode();
MaoLu937cf422017-03-03 23:31:46 -0800372 node.put(SHOW_FLOW_ICON, devType == Type.ROADM);
MaoLu2846b112017-05-15 17:18:55 -0700373 if (devType == Type.FIBER_SWITCH) {
374 node.put(SHOW_TARGET_POWER, false);
375 node.put(SHOW_SERVICE_STATE, true);
376 // add protection switch paths
377 putProtectionSwitchPaths(did, node);
378 } else {
379 node.put(SHOW_TARGET_POWER, true);
380 node.put(SHOW_SERVICE_STATE, false);
381 }
MaoLu937cf422017-03-03 23:31:46 -0800382 sendMessage(ROADM_SHOW_ITEMS_RESP, node);
383 }
MaoLu2846b112017-05-15 17:18:55 -0700384
385 private void putProtectionSwitchPaths(DeviceId deviceId, ObjectNode node) {
386 Map<ConnectPoint, ProtectedTransportEndpointState> states =
387 roadmService.getProtectionSwitchStates(deviceId);
388 ArrayNode nodes = node.putArray(OPS_PATHS);
389 // Add path names for every identifier.
390 int groupIndex = 0;
391 for (ConnectPoint identifier : states.keySet()) {
392 // No group name needed if there is only one connection point identifier.
393 String groupName = states.keySet().size() == 1 ? "" : String.format(OPS_GROUP_FMT, ++groupIndex);
394 // Add AUTOMATIC operation.
395 nodes.add(new ObjectNode(JsonNodeFactory.instance)
Andrea Campanellabdeeda12019-08-02 16:12:05 +0200396 .put(OPS_ARRAY_INDEX, ACTIVE_UNKNOWN)
397 .put(OPS_ARRAY_OPERATION, OPS_OPT_AUTO)
398 .put(OPS_ARRAY_NAME, String.format("%s%s", groupName, OPS_OPT_AUTO)));
MaoLu2846b112017-05-15 17:18:55 -0700399 // Add FORCE and MANUAL operations for every path.
400 for (String opt : OPS_NON_AUTO_OPTS) {
401 int pathIndex = 0;
402 for (TransportEndpointState state : states.get(identifier).pathStates()) {
403 nodes.add(new ObjectNode(JsonNodeFactory.instance)
Andrea Campanellabdeeda12019-08-02 16:12:05 +0200404 .put(OPS_ARRAY_INDEX, pathIndex++)
405 .put(OPS_ARRAY_OPERATION, opt)
406 .put(OPS_ARRAY_NAME,
407 String.format("%s%s %s", groupName, opt, state.id().id().toUpperCase())));
MaoLu2846b112017-05-15 17:18:55 -0700408 }
409 }
410 }
411
412
413 }
MaoLu937cf422017-03-03 23:31:46 -0800414 }
415
416 private final class CreateOpsModeSetRequestHandler extends RequestHandler {
MaoLu937cf422017-03-03 23:31:46 -0800417 private static final String DEVICE_INVALID_ERR_MSG = "Apply failed: device is offline or unavailable.";
418 private static final String TYPE_INVALID_ERR_MSG = "Apply failed: invalid device type.";
419
420 private CreateOpsModeSetRequestHandler() {
421 super(ROADM_SET_OPS_MODE_REQ);
422 }
423
424 @Override
425 public void process(ObjectNode payload) {
426 DeviceId did = DeviceId.deviceId(string(payload, RoadmUtil.DEV_ID));
427 ObjectNode node = objectNode();
428 if (!deviceService.isAvailable(did)) {
429 node.put(RoadmUtil.VALID, false);
430 node.put(RoadmUtil.MESSAGE, DEVICE_INVALID_ERR_MSG);
431 sendMessage(ROADM_SET_OPS_MODE_RESP, node);
432 return;
433 }
434 Type devType = deviceService.getDevice(did).type();
435 if (devType != Type.FIBER_SWITCH) {
436 node.put(RoadmUtil.VALID, false);
437 node.put(RoadmUtil.MESSAGE, TYPE_INVALID_ERR_MSG);
438 sendMessage(ROADM_SET_OPS_MODE_RESP, node);
439 return;
440 }
MaoLu2846b112017-05-15 17:18:55 -0700441 // get switch configuration from payload, and then switch the device.
442 roadmService.configProtectionSwitch(did, string(payload, OPS_ARRAY_OPERATION),
443 roadmService.getProtectionSwitchStates(did).keySet().toArray(new ConnectPoint[0])[0],
444 (int) number(payload, OPS_ARRAY_INDEX));
MaoLu937cf422017-03-03 23:31:46 -0800445 node.put(RoadmUtil.VALID, true);
446 sendMessage(ROADM_SET_OPS_MODE_RESP, node);
Jimmy Yanda878fc2016-09-02 16:32:01 -0700447 }
448 }
449}