blob: 54d1a59e905589f188d2f23851ec93267dc4092a [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;
Andrea Campanellabdeeda12019-08-02 16:12:05 +020028import org.onosproject.net.ModulationScheme;
MaoLu937cf422017-03-03 23:31:46 -080029import org.onosproject.net.OchSignal;
Jimmy Yanda878fc2016-09-02 16:32:01 -070030import org.onosproject.net.Port;
31import org.onosproject.net.PortNumber;
Andrea Campanellabdeeda12019-08-02 16:12:05 +020032import org.onosproject.net.behaviour.protection.ProtectedTransportEndpointState;
33import org.onosproject.net.behaviour.protection.TransportEndpointState;
34import org.onosproject.net.device.DeviceService;
alessiod4a2b842019-04-30 18:43:17 +020035import org.onosproject.net.intent.OpticalPathIntent;
MaoLu2846b112017-05-15 17:18:55 -070036import org.onosproject.net.optical.OpticalAnnotations;
Jimmy Yanda878fc2016-09-02 16:32:01 -070037import org.onosproject.ui.RequestHandler;
38import org.onosproject.ui.UiConnection;
39import org.onosproject.ui.UiMessageHandler;
40import org.onosproject.ui.table.TableModel;
41import org.onosproject.ui.table.TableRequestHandler;
42import org.slf4j.Logger;
43import org.slf4j.LoggerFactory;
44
45import java.util.Collection;
MaoLu937cf422017-03-03 23:31:46 -080046import java.util.Collections;
47import java.util.Comparator;
Jimmy Yanda878fc2016-09-02 16:32:01 -070048import java.util.List;
MaoLu2846b112017-05-15 17:18:55 -070049import java.util.Map;
MaoLu937cf422017-03-03 23:31:46 -080050import java.util.Set;
51
52import static org.onosproject.net.Device.Type;
MaoLu2846b112017-05-15 17:18:55 -070053import static org.onosproject.net.behaviour.protection.ProtectedTransportEndpointState.ACTIVE_UNKNOWN;
54import static org.onosproject.roadm.RoadmUtil.OPS_OPT_AUTO;
55import static org.onosproject.roadm.RoadmUtil.OPS_OPT_FORCE;
56import static org.onosproject.roadm.RoadmUtil.OPS_OPT_MANUAL;
Jimmy Yanda878fc2016-09-02 16:32:01 -070057
58/**
59 * Table-View message handler for ROADM port view.
60 */
61public class RoadmPortViewMessageHandler extends UiMessageHandler {
62
63 private static final String ROADM_PORT_DATA_REQ = "roadmPortDataRequest";
64 private static final String ROADM_PORT_DATA_RESP = "roadmPortDataResponse";
65 private static final String ROADM_PORTS = "roadmPorts";
Jimmy Yanda878fc2016-09-02 16:32:01 -070066 private static final String ROADM_SET_TARGET_POWER_REQ = "roadmSetTargetPowerRequest";
67 private static final String ROADM_SET_TARGET_POWER_RESP = "roadmSetTargetPowerResponse";
Andrea Campanellabdeeda12019-08-02 16:12:05 +020068 private static final String ROADM_SET_MODULATION_REQ = "roadmSetModulationRequest";
69 private static final String ROADM_SET_MODULATION_RESP = "roadmSetModulationResponse";
Boyuan Yanf3f6a8d2019-05-26 18:35:54 -070070 private static final String ROADM_SYNC_TARGET_POWER_REQ = "roadmSyncTargetPowerRequest";
71 private static final String ROADM_SYNC_TARGET_POWER_RESP = "roadmSyncTargetPowerResp";
MaoLu937cf422017-03-03 23:31:46 -080072 private static final String ROADM_SHOW_ITEMS_REQ = "roadmShowPortItemsRequest";
73 private static final String ROADM_SHOW_ITEMS_RESP = "roadmShowPortItemsResponse";
74 private static final String ROADM_SET_OPS_MODE_REQ = "roadmSetOpsModeRequest";
75 private static final String ROADM_SET_OPS_MODE_RESP = "roadmSetOpsModeResponse";
Jimmy Yanda878fc2016-09-02 16:32:01 -070076
77 private static final String ID = "id";
alessiod4a2b842019-04-30 18:43:17 +020078 private static final String REVERSE_PORT = "reversePort";
Jimmy Yanda878fc2016-09-02 16:32:01 -070079 private static final String NAME = "name";
MaoLu937cf422017-03-03 23:31:46 -080080 private static final String TYPE = "type";
Jimmy Yanda878fc2016-09-02 16:32:01 -070081 private static final String ENABLED = "enabled";
82 private static final String MIN_FREQ = "minFreq";
83 private static final String MAX_FREQ = "maxFreq";
84 private static final String GRID = "grid";
MaoLu2f7eadb2017-05-02 15:38:43 -070085 private static final String POWER_RANGE = "powerRange";
Jimmy Yanda878fc2016-09-02 16:32:01 -070086 private static final String CURRENT_POWER = "currentPower";
87 private static final String TARGET_POWER = "targetPower";
Andrea Campanellabdeeda12019-08-02 16:12:05 +020088 private static final String MODULATION = "modulation";
Jimmy Yanda878fc2016-09-02 16:32:01 -070089 private static final String HAS_TARGET_POWER = "hasTargetPower";
MaoLu937cf422017-03-03 23:31:46 -080090 private static final String SERVICE_STATE = "serviceState";
Jimmy Yanda878fc2016-09-02 16:32:01 -070091
92 private static final String[] COLUMN_IDS = {
alessiod4a2b842019-04-30 18:43:17 +020093 ID, REVERSE_PORT, TYPE, NAME, ENABLED, MIN_FREQ, MAX_FREQ, GRID, POWER_RANGE,
Andrea Campanellabdeeda12019-08-02 16:12:05 +020094 CURRENT_POWER, SERVICE_STATE, TARGET_POWER, MODULATION, HAS_TARGET_POWER
Jimmy Yanda878fc2016-09-02 16:32:01 -070095 };
96
Jimmy Yanda878fc2016-09-02 16:32:01 -070097 private RoadmService roadmService;
MaoLu937cf422017-03-03 23:31:46 -080098 private DeviceService deviceService;
Jimmy Yanda878fc2016-09-02 16:32:01 -070099
100 private final Logger log = LoggerFactory.getLogger(getClass());
101
102 @Override
103 public void init(UiConnection connection, ServiceDirectory directory) {
104 super.init(connection, directory);
Jimmy Yanda878fc2016-09-02 16:32:01 -0700105 roadmService = get(RoadmService.class);
MaoLu937cf422017-03-03 23:31:46 -0800106 deviceService = get(DeviceService.class);
Jimmy Yanda878fc2016-09-02 16:32:01 -0700107 }
108
109 @Override
110 protected Collection<RequestHandler> createRequestHandlers() {
MaoLu937cf422017-03-03 23:31:46 -0800111 return ImmutableSet.of(new PortTableDataRequestHandler(),
112 new SetTargetPowerRequestHandler(),
113 new CreateShowItemsRequestHandler(),
Boyuan Yanf3f6a8d2019-05-26 18:35:54 -0700114 new CreateOpsModeSetRequestHandler(),
Andrea Campanellabdeeda12019-08-02 16:12:05 +0200115 new SyncTargetPowerRequestHandler(),
116 new SetModulationRequestHandler()
117 );
Jimmy Yanda878fc2016-09-02 16:32:01 -0700118 }
119
120 // Handler for sample table requests
121 private final class PortTableDataRequestHandler extends TableRequestHandler {
122
123 private PortTableDataRequestHandler() {
124 super(ROADM_PORT_DATA_REQ, ROADM_PORT_DATA_RESP, ROADM_PORTS);
125 }
126
127 @Override
128 protected String[] getColumnIds() {
129 return COLUMN_IDS;
130 }
131
132 @Override
133 protected String noRowsMessage(ObjectNode payload) {
MaoLu937cf422017-03-03 23:31:46 -0800134 return RoadmUtil.NO_ROWS_MESSAGE;
Jimmy Yanda878fc2016-09-02 16:32:01 -0700135 }
136
137 @Override
138 protected void populateTable(TableModel tm, ObjectNode payload) {
MaoLu937cf422017-03-03 23:31:46 -0800139 DeviceId deviceId = DeviceId.deviceId(string(payload, RoadmUtil.DEV_ID));
Jimmy Yanda878fc2016-09-02 16:32:01 -0700140 if (deviceService.isAvailable(deviceId)) {
141 List<Port> ports = deviceService.getPorts(deviceId);
142 for (Port port : ports) {
143 populateRow(tm.addRow(), port, deviceId);
144 }
145 }
146 }
147
148 private void populateRow(TableModel.Row row, Port port, DeviceId deviceId) {
MaoLu937cf422017-03-03 23:31:46 -0800149 PortNumber portNum = port.number();
150 getFrequencyLimit(deviceId, portNum);
151 row.cell(ID, portNum.toLong())
alessiod4a2b842019-04-30 18:43:17 +0200152 .cell(REVERSE_PORT, RoadmUtil.getAnnotation(port.annotations(),
153 OpticalPathIntent.REVERSE_PORT_ANNOTATION_KEY))
Jimmy Yanda878fc2016-09-02 16:32:01 -0700154 .cell(TYPE, port.type())
155 .cell(ENABLED, port.isEnabled())
MaoLu937cf422017-03-03 23:31:46 -0800156 .cell(NAME, RoadmUtil.getAnnotation(port.annotations(), AnnotationKeys.PORT_NAME))
157 .cell(MIN_FREQ, RoadmUtil.asTHz(minFreq))
158 .cell(MAX_FREQ, RoadmUtil.asTHz(maxFreq))
159 .cell(GRID, RoadmUtil.asGHz(channelSpacing))
MaoLu2f7eadb2017-05-02 15:38:43 -0700160 .cell(POWER_RANGE, getPowerRange(deviceId, portNum))
MaoLu937cf422017-03-03 23:31:46 -0800161 .cell(CURRENT_POWER, getCurrentPower(deviceId, portNum))
162 .cell(SERVICE_STATE, getPortServiceState(deviceId, portNum))
Andrea Campanellabdeeda12019-08-02 16:12:05 +0200163 .cell(MODULATION, getModulation(deviceId, portNum))
MaoLu937cf422017-03-03 23:31:46 -0800164 .cell(TARGET_POWER, getTargetPower(deviceId, portNum))
165 .cell(HAS_TARGET_POWER, roadmService.hasPortTargetPower(deviceId, portNum));
166 }
167
168 private String getPortServiceState(DeviceId deviceId, PortNumber portNumber) {
MaoLu2846b112017-05-15 17:18:55 -0700169 if (deviceService.getDevice(deviceId).type() != Type.FIBER_SWITCH) {
170 return RoadmUtil.NA;
171 }
172 Map<ConnectPoint, ProtectedTransportEndpointState> map =
173 roadmService.getProtectionSwitchStates(deviceId);
174 for (ProtectedTransportEndpointState state : map.values()) {
175 for (TransportEndpointState element : state.pathStates()) {
176 if (element.description().output().connectPoint().port().equals(portNumber)) {
177 return RoadmUtil.defaultString(element.attributes()
Andrea Campanellabdeeda12019-08-02 16:12:05 +0200178 .get(OpticalAnnotations.INPUT_PORT_STATUS), RoadmUtil.UNKNOWN);
MaoLu2846b112017-05-15 17:18:55 -0700179 }
180 }
181 }
182 return RoadmUtil.UNKNOWN;
MaoLu937cf422017-03-03 23:31:46 -0800183 }
184
185 private Frequency minFreq = null, maxFreq = null, channelSpacing = null;
Andrea Campanellabdeeda12019-08-02 16:12:05 +0200186
MaoLu937cf422017-03-03 23:31:46 -0800187 // Gets min frequency, max frequency, channel spacing
188 private void getFrequencyLimit(DeviceId deviceId, PortNumber portNumber) {
189 Set<OchSignal> signals = roadmService.queryLambdas(deviceId, portNumber);
190 if (signals.isEmpty()) {
191 return;
192 }
193 Comparator<OchSignal> compare =
194 (OchSignal a, OchSignal b) -> a.spacingMultiplier() - b.spacingMultiplier();
195 OchSignal minOch = Collections.min(signals, compare);
196 OchSignal maxOch = Collections.max(signals, compare);
197 minFreq = minOch.centralFrequency();
198 maxFreq = maxOch.centralFrequency();
199 channelSpacing = minOch.channelSpacing().frequency();
Jimmy Yanda878fc2016-09-02 16:32:01 -0700200 }
201
MaoLu2f7eadb2017-05-02 15:38:43 -0700202 // Returns the power range as a string, N/A if the power range not exists.
203 // The power range would be input power range or target power range determined by port property.
204 // If the port is RX direction then acquire the input power range from driver.
205 // Otherwise there will be a TX direction port, thus acquire the target power range.
206 private String getPowerRange(DeviceId deviceId, PortNumber portNumber) {
Andrea Campanelladadf6402019-08-07 15:24:11 +0200207 Range<Double> range = roadmService.inputPortPowerRange(deviceId, portNumber);
MaoLu2f7eadb2017-05-02 15:38:43 -0700208 if (range == null) {
209 range = roadmService.targetPortPowerRange(deviceId, portNumber);
210 }
MaoLu937cf422017-03-03 23:31:46 -0800211 return RoadmUtil.objectToString(range, RoadmUtil.NA);
Jimmy Yanda878fc2016-09-02 16:32:01 -0700212 }
213
214 // Returns the current power as a string, Unknown if no value can be found.
215 private String getCurrentPower(DeviceId deviceId, PortNumber portNumber) {
Andrea Campanelladadf6402019-08-07 15:24:11 +0200216 Double currentPower = roadmService.getCurrentPortPower(deviceId, portNumber);
MaoLu937cf422017-03-03 23:31:46 -0800217 return RoadmUtil.objectToString(currentPower, RoadmUtil.UNKNOWN);
Jimmy Yanda878fc2016-09-02 16:32:01 -0700218 }
219
220 // Returns target power as a string, Unknown if target power is expected but
221 // cannot be found, N/A if port does not have configurable target power
222 private String getTargetPower(DeviceId deviceId, PortNumber portNumber) {
MaoLu937cf422017-03-03 23:31:46 -0800223 if (!roadmService.hasPortTargetPower(deviceId, portNumber)) {
224 return RoadmUtil.NA;
Jimmy Yanda878fc2016-09-02 16:32:01 -0700225 }
Andrea Campanelladadf6402019-08-07 15:24:11 +0200226 Double targetPower = roadmService.getTargetPortPower(deviceId, portNumber);
MaoLu937cf422017-03-03 23:31:46 -0800227 return RoadmUtil.objectToString(targetPower, RoadmUtil.UNKNOWN);
Jimmy Yanda878fc2016-09-02 16:32:01 -0700228 }
Andrea Campanellabdeeda12019-08-02 16:12:05 +0200229
230 // Returns modulation as a string, Unknown if modulation is expected but
231 // cannot be found
232 private String getModulation(DeviceId deviceId, PortNumber portNumber) {
233 Port port = deviceService.getPort(deviceId, portNumber);
234 ModulationScheme modulation = null;
235 if (port.type().equals(Port.Type.OCH)) {
236 modulation = roadmService.getModulation(deviceId, portNumber);
237 }
238 return RoadmUtil.objectToString(modulation, RoadmUtil.UNKNOWN);
239 }
Jimmy Yanda878fc2016-09-02 16:32:01 -0700240 }
241
242
243 // Handler for setting port target power
244 private final class SetTargetPowerRequestHandler extends RequestHandler {
245
Jimmy Yanda878fc2016-09-02 16:32:01 -0700246 private static final String TARGET_POWER_ERR_MSG = "Target power range is %s.";
247
248 private SetTargetPowerRequestHandler() {
249 super(ROADM_SET_TARGET_POWER_REQ);
250 }
251
252 @Override
253 public void process(ObjectNode payload) {
MaoLu937cf422017-03-03 23:31:46 -0800254 DeviceId deviceId = DeviceId.deviceId(string(payload, RoadmUtil.DEV_ID));
Jimmy Yanda878fc2016-09-02 16:32:01 -0700255 PortNumber portNumber = PortNumber.portNumber(payload.get(ID).asLong());
Andrea Campanelladadf6402019-08-07 15:24:11 +0200256 Range<Double> range = roadmService.targetPortPowerRange(deviceId, portNumber);
MaoLu937cf422017-03-03 23:31:46 -0800257 if (range == null) {
Jimmy Yanda878fc2016-09-02 16:32:01 -0700258 log.warn("Unable to determine target power range for device {}", deviceId);
MaoLu937cf422017-03-03 23:31:46 -0800259 return;
Jimmy Yanda878fc2016-09-02 16:32:01 -0700260 }
Andrea Campanelladadf6402019-08-07 15:24:11 +0200261 Double targetPower = payload.get(TARGET_POWER).asDouble();
MaoLu937cf422017-03-03 23:31:46 -0800262 boolean validTargetPower = range.contains(targetPower);
263 if (validTargetPower) {
264 roadmService.setTargetPortPower(deviceId, portNumber, targetPower);
265 }
266 ObjectNode rootNode = objectNode();
267 rootNode.put(ID, payload.get(ID).asText());
268 rootNode.put(RoadmUtil.VALID, validTargetPower);
269 rootNode.put(RoadmUtil.MESSAGE, String.format(TARGET_POWER_ERR_MSG, range.toString()));
270 sendMessage(ROADM_SET_TARGET_POWER_RESP, rootNode);
271 }
272 }
273
Boyuan Yanf3f6a8d2019-05-26 18:35:54 -0700274 // Handler for sync-up port target power
275 private final class SyncTargetPowerRequestHandler extends RequestHandler {
276
277 private static final String SYNCED_TARGET_POWER = "Synced target power is %s.";
Andrea Campanellabdeeda12019-08-02 16:12:05 +0200278
Boyuan Yanf3f6a8d2019-05-26 18:35:54 -0700279 private SyncTargetPowerRequestHandler() {
280 super(ROADM_SYNC_TARGET_POWER_REQ);
281 }
282
283 @Override
284 public void process(ObjectNode payload) {
285 DeviceId deviceId = DeviceId.deviceId(string(payload, RoadmUtil.DEV_ID));
286 PortNumber portNumber = PortNumber.portNumber(payload.get(ID).asLong());
Andrea Campanelladadf6402019-08-07 15:24:11 +0200287 Double targetPower = roadmService.syncTargetPortPower(deviceId, portNumber);
Boyuan Yanf3f6a8d2019-05-26 18:35:54 -0700288 String power = RoadmUtil.objectToString(targetPower, RoadmUtil.UNKNOWN);
289 ObjectNode rootNode = objectNode();
290 rootNode.put(ID, payload.get(ID).asText())
291 .put(RoadmUtil.VALID, true)
292 .put(RoadmUtil.MESSAGE, String.format(SYNCED_TARGET_POWER, power));
293 sendMessage(ROADM_SYNC_TARGET_POWER_RESP, rootNode);
294 }
295 }
296
Andrea Campanellabdeeda12019-08-02 16:12:05 +0200297 // Handler for setting port modulation
298 private final class SetModulationRequestHandler extends RequestHandler {
299
300 private static final String TARGET_MODULATION_MSG = "Target modulation is %s.";
301
302 private SetModulationRequestHandler() {
303 super(ROADM_SET_MODULATION_REQ);
304 }
305
306 @Override
307 public void process(ObjectNode payload) {
308 DeviceId deviceId = DeviceId.deviceId(string(payload, RoadmUtil.DEV_ID));
309 PortNumber portNumber = PortNumber.portNumber(payload.get(ID).asLong());
310 String modulation = payload.get(MODULATION).asText();
311 roadmService.setModulation(deviceId, portNumber, modulation);
312 ObjectNode rootNode = objectNode();
313 rootNode.put(ID, payload.get(ID).asText());
314 rootNode.put(RoadmUtil.VALID, modulation);
315 rootNode.put(RoadmUtil.MESSAGE, String.format(TARGET_MODULATION_MSG, modulation));
316 sendMessage(ROADM_SET_MODULATION_RESP, rootNode);
317 }
318 }
319
MaoLu2846b112017-05-15 17:18:55 -0700320 // Protection switch operation type and path index
321 private static final String OPS_ARRAY_INDEX = "index";
322 private static final String OPS_ARRAY_OPERATION = "operation";
323 private static final String[] OPS_NON_AUTO_OPTS = {OPS_OPT_FORCE, OPS_OPT_MANUAL};
324
MaoLu937cf422017-03-03 23:31:46 -0800325 private final class CreateShowItemsRequestHandler extends RequestHandler {
326 private static final String SHOW_TARGET_POWER = "showTargetPower";
327 private static final String SHOW_SERVICE_STATE = "showServiceState";
328 private static final String SHOW_FLOW_ICON = "showFlowIcon";
MaoLu2846b112017-05-15 17:18:55 -0700329 private static final String OPS_PATHS = "opsOperations";
330 private static final String OPS_ARRAY_NAME = "name";
331 private static final String OPS_GROUP_FMT = "GROUP%d ";
MaoLu937cf422017-03-03 23:31:46 -0800332
333 private CreateShowItemsRequestHandler() {
334 super(ROADM_SHOW_ITEMS_REQ);
335 }
336
337 @Override
338 public void process(ObjectNode payload) {
339 DeviceId did = DeviceId.deviceId(string(payload, RoadmUtil.DEV_ID));
340 Type devType = deviceService.getDevice(did).type();
341 // Build response
342 ObjectNode node = objectNode();
MaoLu937cf422017-03-03 23:31:46 -0800343 node.put(SHOW_FLOW_ICON, devType == Type.ROADM);
MaoLu2846b112017-05-15 17:18:55 -0700344 if (devType == Type.FIBER_SWITCH) {
345 node.put(SHOW_TARGET_POWER, false);
346 node.put(SHOW_SERVICE_STATE, true);
347 // add protection switch paths
348 putProtectionSwitchPaths(did, node);
349 } else {
350 node.put(SHOW_TARGET_POWER, true);
351 node.put(SHOW_SERVICE_STATE, false);
352 }
MaoLu937cf422017-03-03 23:31:46 -0800353 sendMessage(ROADM_SHOW_ITEMS_RESP, node);
354 }
MaoLu2846b112017-05-15 17:18:55 -0700355
356 private void putProtectionSwitchPaths(DeviceId deviceId, ObjectNode node) {
357 Map<ConnectPoint, ProtectedTransportEndpointState> states =
358 roadmService.getProtectionSwitchStates(deviceId);
359 ArrayNode nodes = node.putArray(OPS_PATHS);
360 // Add path names for every identifier.
361 int groupIndex = 0;
362 for (ConnectPoint identifier : states.keySet()) {
363 // No group name needed if there is only one connection point identifier.
364 String groupName = states.keySet().size() == 1 ? "" : String.format(OPS_GROUP_FMT, ++groupIndex);
365 // Add AUTOMATIC operation.
366 nodes.add(new ObjectNode(JsonNodeFactory.instance)
Andrea Campanellabdeeda12019-08-02 16:12:05 +0200367 .put(OPS_ARRAY_INDEX, ACTIVE_UNKNOWN)
368 .put(OPS_ARRAY_OPERATION, OPS_OPT_AUTO)
369 .put(OPS_ARRAY_NAME, String.format("%s%s", groupName, OPS_OPT_AUTO)));
MaoLu2846b112017-05-15 17:18:55 -0700370 // Add FORCE and MANUAL operations for every path.
371 for (String opt : OPS_NON_AUTO_OPTS) {
372 int pathIndex = 0;
373 for (TransportEndpointState state : states.get(identifier).pathStates()) {
374 nodes.add(new ObjectNode(JsonNodeFactory.instance)
Andrea Campanellabdeeda12019-08-02 16:12:05 +0200375 .put(OPS_ARRAY_INDEX, pathIndex++)
376 .put(OPS_ARRAY_OPERATION, opt)
377 .put(OPS_ARRAY_NAME,
378 String.format("%s%s %s", groupName, opt, state.id().id().toUpperCase())));
MaoLu2846b112017-05-15 17:18:55 -0700379 }
380 }
381 }
382
383
384 }
MaoLu937cf422017-03-03 23:31:46 -0800385 }
386
387 private final class CreateOpsModeSetRequestHandler extends RequestHandler {
MaoLu937cf422017-03-03 23:31:46 -0800388 private static final String DEVICE_INVALID_ERR_MSG = "Apply failed: device is offline or unavailable.";
389 private static final String TYPE_INVALID_ERR_MSG = "Apply failed: invalid device type.";
390
391 private CreateOpsModeSetRequestHandler() {
392 super(ROADM_SET_OPS_MODE_REQ);
393 }
394
395 @Override
396 public void process(ObjectNode payload) {
397 DeviceId did = DeviceId.deviceId(string(payload, RoadmUtil.DEV_ID));
398 ObjectNode node = objectNode();
399 if (!deviceService.isAvailable(did)) {
400 node.put(RoadmUtil.VALID, false);
401 node.put(RoadmUtil.MESSAGE, DEVICE_INVALID_ERR_MSG);
402 sendMessage(ROADM_SET_OPS_MODE_RESP, node);
403 return;
404 }
405 Type devType = deviceService.getDevice(did).type();
406 if (devType != Type.FIBER_SWITCH) {
407 node.put(RoadmUtil.VALID, false);
408 node.put(RoadmUtil.MESSAGE, TYPE_INVALID_ERR_MSG);
409 sendMessage(ROADM_SET_OPS_MODE_RESP, node);
410 return;
411 }
MaoLu2846b112017-05-15 17:18:55 -0700412 // get switch configuration from payload, and then switch the device.
413 roadmService.configProtectionSwitch(did, string(payload, OPS_ARRAY_OPERATION),
414 roadmService.getProtectionSwitchStates(did).keySet().toArray(new ConnectPoint[0])[0],
415 (int) number(payload, OPS_ARRAY_INDEX));
MaoLu937cf422017-03-03 23:31:46 -0800416 node.put(RoadmUtil.VALID, true);
417 sendMessage(ROADM_SET_OPS_MODE_RESP, node);
Jimmy Yanda878fc2016-09-02 16:32:01 -0700418 }
419 }
420}