Manage ODU4 services over multiple OTU4
[transportpce.git] / renderer / src / main / java / org / opendaylight / transportpce / renderer / provisiondevice / DeviceRendererServiceImpl.java
1 /*
2  * Copyright © 2017 AT&T and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.transportpce.renderer.provisiondevice;
9
10 import com.google.common.collect.Sets;
11 import com.google.common.util.concurrent.FluentFuture;
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.LinkedList;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Optional;
18 import java.util.Set;
19 import java.util.concurrent.ConcurrentHashMap;
20 import java.util.concurrent.ConcurrentLinkedQueue;
21 import java.util.concurrent.CopyOnWriteArrayList;
22 import java.util.concurrent.ExecutionException;
23 import java.util.concurrent.ForkJoinPool;
24 import java.util.concurrent.ForkJoinTask;
25 import java.util.concurrent.Future;
26 import java.util.concurrent.TimeUnit;
27 import java.util.concurrent.TimeoutException;
28 import java.util.concurrent.atomic.AtomicBoolean;
29 import org.eclipse.jdt.annotation.NonNull;
30 import org.opendaylight.mdsal.binding.api.DataBroker;
31 import org.opendaylight.mdsal.binding.api.ReadTransaction;
32 import org.opendaylight.mdsal.binding.api.WriteTransaction;
33 import org.opendaylight.mdsal.common.api.CommitInfo;
34 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
35 import org.opendaylight.transportpce.common.StringConstants;
36 import org.opendaylight.transportpce.common.Timeouts;
37 import org.opendaylight.transportpce.common.crossconnect.CrossConnect;
38 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
39 import org.opendaylight.transportpce.common.fixedflex.GridConstant;
40 import org.opendaylight.transportpce.common.fixedflex.GridUtils;
41 import org.opendaylight.transportpce.common.fixedflex.SpectrumInformation;
42 import org.opendaylight.transportpce.common.mapping.PortMapping;
43 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaceException;
44 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaces;
45 import org.opendaylight.transportpce.networkmodel.service.NetworkModelService;
46 import org.opendaylight.transportpce.renderer.openroadminterface.OpenRoadmInterfaceFactory;
47 import org.opendaylight.transportpce.renderer.provisiondevice.servicepath.ServiceListTopology;
48 import org.opendaylight.transportpce.renderer.provisiondevice.servicepath.ServicePathDirection;
49 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.alarmsuppression.rev171102.ServiceNodelist;
50 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.alarmsuppression.rev171102.service.nodelist.NodelistBuilder;
51 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.alarmsuppression.rev171102.service.nodelist.NodelistKey;
52 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev211004.CreateOtsOmsInput;
53 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev211004.CreateOtsOmsOutput;
54 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev211004.CreateOtsOmsOutputBuilder;
55 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev211004.RendererRollbackInput;
56 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev211004.RendererRollbackOutput;
57 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev211004.RendererRollbackOutputBuilder;
58 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev211004.ServicePathInput;
59 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev211004.ServicePathOutput;
60 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev211004.ServicePathOutputBuilder;
61 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev211004.az.api.info.AEndApiInfo;
62 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev211004.az.api.info.ZEndApiInfo;
63 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev211004.renderer.rollback.output.FailedToRollback;
64 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev211004.renderer.rollback.output.FailedToRollbackBuilder;
65 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev211004.renderer.rollback.output.FailedToRollbackKey;
66 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210927.OpenroadmNodeVersion;
67 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210927.mapping.Mapping;
68 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.service.Topology;
69 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.ServiceList;
70 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.service.list.Services;
71 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.service.list.ServicesBuilder;
72 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.service.list.ServicesKey;
73 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev210618.link.tp.LinkTp;
74 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev210618.link.tp.LinkTpBuilder;
75 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev210618.node.interfaces.NodeInterface;
76 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev210618.node.interfaces.NodeInterfaceBuilder;
77 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev210618.node.interfaces.NodeInterfaceKey;
78 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev210618.optical.renderer.nodes.Nodes;
79 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
80 import org.slf4j.Logger;
81 import org.slf4j.LoggerFactory;
82
83
84 public class DeviceRendererServiceImpl implements DeviceRendererService {
85     private static final String IS_NOT_MOUNTED_ON_THE_CONTROLLER = " is not mounted on the controller";
86     private static final Logger LOG = LoggerFactory.getLogger(DeviceRendererServiceImpl.class);
87     private final DataBroker dataBroker;
88     private final DeviceTransactionManager deviceTransactionManager;
89     private final OpenRoadmInterfaceFactory openRoadmInterfaceFactory;
90     private final OpenRoadmInterfaces openRoadmInterfaces;
91     private final CrossConnect crossConnect;
92     private final PortMapping portMapping;
93     private final NetworkModelService networkModelService;
94
95     public DeviceRendererServiceImpl(DataBroker dataBroker, DeviceTransactionManager deviceTransactionManager,
96             OpenRoadmInterfaceFactory openRoadmInterfaceFactory, OpenRoadmInterfaces openRoadmInterfaces,
97             CrossConnect crossConnect, PortMapping portMapping, NetworkModelService networkModelService) {
98         this.dataBroker = dataBroker;
99         this.deviceTransactionManager = deviceTransactionManager;
100         this.openRoadmInterfaceFactory = openRoadmInterfaceFactory;
101         this.openRoadmInterfaces = openRoadmInterfaces;
102         this.crossConnect = crossConnect;
103         this.portMapping = portMapping;
104         this.networkModelService = networkModelService;
105     }
106
107     @SuppressWarnings("rawtypes")
108     // FIXME check if the ForkJoinTask raw type can be avoided
109     // Raw types use are discouraged since they lack type safety.
110     // Resulting Problems are observed at run time and not at compile time
111     @Override
112     public ServicePathOutput setupServicePath(ServicePathInput input, ServicePathDirection direction) {
113         LOG.info("setup service path for input {} and direction {}", input, direction);
114         List<Nodes> nodes = new ArrayList<>();
115         if (input.getNodes() != null) {
116             nodes.addAll(input.getNodes());
117         }
118         SpectrumInformation spectrumInformation = GridUtils.initSpectrumInformationFromServicePathInput(input);
119         // Register node for suppressing alarms
120         if (!alarmSuppressionNodeRegistration(input)) {
121             LOG.warn("Alarm suppresion node registration failed!!!!");
122         }
123         ConcurrentLinkedQueue<String> results = new ConcurrentLinkedQueue<>();
124         Map<NodeInterfaceKey,NodeInterface> nodeInterfaces = new ConcurrentHashMap<>();
125         Set<String> nodesProvisioned = Sets.newConcurrentHashSet();
126         CopyOnWriteArrayList<LinkTp> otnLinkTps = new CopyOnWriteArrayList<>();
127         ServiceListTopology topology = new ServiceListTopology();
128         AtomicBoolean success = new AtomicBoolean(true);
129         ForkJoinPool forkJoinPool = new ForkJoinPool();
130         ForkJoinTask forkJoinTask = forkJoinPool.submit(() -> nodes.parallelStream().forEach(node -> {
131             String nodeId = node.getNodeId();
132             // take the index of the node
133             int nodeIndex = nodes.indexOf(node);
134             LOG.info("Starting provisioning for node : {}", nodeId);
135             AEndApiInfo apiInfoA = null;
136             ZEndApiInfo apiInfoZ = null;
137             if (input.getAEndApiInfo() != null && input.getAEndApiInfo().getNodeId().contains(nodeId)) {
138                 apiInfoA = input.getAEndApiInfo();
139             }
140             if (input.getZEndApiInfo() != null && input.getZEndApiInfo().getNodeId().contains(nodeId)) {
141                 apiInfoZ = input.getZEndApiInfo();
142             }
143             List<String> createdEthInterfaces = new ArrayList<>();
144             List<String> createdOtuInterfaces = new ArrayList<>();
145             List<String> createdOduInterfaces = new ArrayList<>();
146             List<String> createdOchInterfaces = new ArrayList<>();
147             List<String> createdConnections = new ArrayList<>();
148             int crossConnectFlag = 0;
149             try {
150                 // if the node is currently mounted then proceed
151                 if (this.deviceTransactionManager.isDeviceMounted(nodeId)) {
152                     String srcTp = node.getSrcTp();
153                     String destTp = node.getDestTp();
154                     if ((destTp != null) && destTp.contains(StringConstants.NETWORK_TOKEN)) {
155                         LOG.info("Adding supporting OCH interface for node {}, dest tp {}, spectrumInformation {}",
156                                 nodeId, destTp, spectrumInformation);
157                         crossConnectFlag++;
158                         String supportingOchInterface = this.openRoadmInterfaceFactory.createOpenRoadmOchInterface(
159                                 nodeId, destTp, spectrumInformation);
160                         createdOchInterfaces.add(supportingOchInterface);
161                         String supportingOtuInterface = this.openRoadmInterfaceFactory.createOpenRoadmOtu4Interface(
162                                 nodeId, destTp, supportingOchInterface, apiInfoA, apiInfoZ);
163                         createdOtuInterfaces.add(supportingOtuInterface);
164                         if (srcTp == null) {
165                             otnLinkTps.add(new LinkTpBuilder().setNodeId(nodeId).setTpId(destTp).build());
166                         } else {
167                             createdOduInterfaces.add(this.openRoadmInterfaceFactory.createOpenRoadmOdu4HOInterface(
168                                     nodeId, destTp, false, apiInfoA, apiInfoZ, "07"));
169                         }
170                     }
171                     if ((srcTp != null) && srcTp.contains(StringConstants.CLIENT_TOKEN)) {
172                         LOG.info("Adding supporting EThernet interface for node {}, src tp {}", nodeId, srcTp);
173                         crossConnectFlag++;
174                         // create OpenRoadm Xponder Client Interfaces
175                         createdEthInterfaces.add(this.openRoadmInterfaceFactory.createOpenRoadmEthInterface(
176                                 nodeId, srcTp));
177                     }
178                     if ((srcTp != null) && srcTp.contains(StringConstants.NETWORK_TOKEN)) {
179                         LOG.info("Adding supporting OCH interface for node {}, src tp {}, spectrumInformation {}",
180                                 nodeId, srcTp, spectrumInformation);
181                         crossConnectFlag++;
182                         // create OpenRoadm Xponder Line Interfaces
183                         String supportingOchInterface = this.openRoadmInterfaceFactory.createOpenRoadmOchInterface(
184                                 nodeId, srcTp, spectrumInformation);
185                         createdOchInterfaces.add(supportingOchInterface);
186                         String supportingOtuInterface = this.openRoadmInterfaceFactory.createOpenRoadmOtu4Interface(
187                                 nodeId, srcTp, supportingOchInterface, apiInfoA, apiInfoZ);
188                         createdOtuInterfaces.add(supportingOtuInterface);
189                         createdOduInterfaces.add(this.openRoadmInterfaceFactory.createOpenRoadmOdu4Interface(
190                                 nodeId, srcTp, supportingOtuInterface));
191                         Mapping mapping = this.portMapping.getMapping(nodeId,srcTp);
192                         createdOduInterfaces.add(
193                             mapping != null
194                             && mapping.getXponderType() != null
195                             && (mapping.getXponderType().getIntValue() == 3
196                                 || mapping.getXponderType().getIntValue() == 2)
197                             ? this.openRoadmInterfaceFactory.createOpenRoadmOtnOdu4Interface(
198                                     nodeId, destTp, supportingOtuInterface)
199                             : this.openRoadmInterfaceFactory.createOpenRoadmOdu4Interface(
200                                     nodeId, destTp, supportingOtuInterface));
201                     }
202                     if ((destTp != null) && destTp.contains(StringConstants.CLIENT_TOKEN)) {
203                         LOG.info("Adding supporting EThernet interface for node {}, dest tp {}", nodeId, destTp);
204                         crossConnectFlag++;
205                         // create OpenRoadm Xponder Client Interfaces
206                         createdEthInterfaces.add(this.openRoadmInterfaceFactory.createOpenRoadmEthInterface(
207                                 nodeId, destTp));
208                     }
209                     if ((srcTp != null) && (srcTp.contains(StringConstants.TTP_TOKEN)
210                             || srcTp.contains(StringConstants.PP_TOKEN))) {
211                         LOG.info("Adding supporting OCH interface for node {}, src tp {}, spectrumInformation {}",
212                                 nodeId, srcTp, spectrumInformation);
213                         createdOchInterfaces.addAll(this.openRoadmInterfaceFactory.createOpenRoadmOchInterfaces(
214                                 nodeId, srcTp, spectrumInformation));
215                     }
216                     if ((destTp != null) && (destTp.contains(StringConstants.TTP_TOKEN)
217                             || destTp.contains(StringConstants.PP_TOKEN))) {
218                         LOG.info("Adding supporting OCH interface for node {}, dest tp {}, spectrumInformation {}",
219                                 nodeId, destTp, spectrumInformation);
220                         createdOchInterfaces.addAll(this.openRoadmInterfaceFactory.createOpenRoadmOchInterfaces(
221                                 nodeId, destTp, spectrumInformation));
222                     }
223                     if (crossConnectFlag < 1) {
224                         LOG.info("Creating cross connect between source {} and destination {} for node {}", srcTp,
225                                 destTp, nodeId);
226                         Optional<String> connectionNameOpt =
227                                 this.crossConnect.postCrossConnect(nodeId, srcTp, destTp, spectrumInformation);
228                         if (connectionNameOpt.isPresent()) {
229                             nodesProvisioned.add(nodeId);
230                             createdConnections.add(connectionNameOpt.get());
231                         } else {
232                             processErrorMessage("Unable to post Roadm-connection for node " + nodeId, forkJoinPool,
233                                     results);
234                             success.set(false);
235                         }
236                     }
237                 } else {
238                     processErrorMessage(nodeId + IS_NOT_MOUNTED_ON_THE_CONTROLLER, forkJoinPool, results);
239                     success.set(false);
240                 }
241             } catch (OpenRoadmInterfaceException ex) {
242                 processErrorMessage("Setup service path failed! Exception:" + ex.toString(), forkJoinPool, results);
243                 success.set(false);
244             }
245             NodeInterface nodeInterface = new NodeInterfaceBuilder()
246                 .withKey(new NodeInterfaceKey(nodeId))
247                 .setNodeId(nodeId)
248                 .setConnectionId(createdConnections)
249                 .setEthInterfaceId(createdEthInterfaces)
250                 .setOtuInterfaceId(createdOtuInterfaces)
251                 .setOduInterfaceId(createdOduInterfaces)
252                 .setOchInterfaceId(createdOchInterfaces)
253                 .build();
254             nodeInterfaces.put(nodeInterface.key(),nodeInterface);
255         }));
256         try {
257             forkJoinTask.get();
258         } catch (InterruptedException | ExecutionException e) {
259             LOG.error("Error while setting up service paths!", e);
260         }
261         forkJoinPool.shutdown();
262
263         if (success.get()) {
264             results.add("Roadm-connection successfully created for nodes: " + String.join(", ", nodesProvisioned));
265         }
266         // setting topology in the service list data store
267         try {
268             setTopologyForService(input.getServiceName(), topology.getTopology());
269         } catch (InterruptedException | TimeoutException | ExecutionException e) {
270             LOG.warn("Failed to write topologies for service {}.", input.getServiceName(), e);
271         }
272         if (!alarmSuppressionNodeRemoval(input.getServiceName())) {
273             LOG.error("Alarm suppresion node removal failed!!!!");
274         }
275         return new ServicePathOutputBuilder()
276                 .setNodeInterface(nodeInterfaces)
277                 .setSuccess(success.get())
278                 .setResult(String.join("\n", results))
279                 .setLinkTp(otnLinkTps)
280                 .build();
281     }
282
283     private ConcurrentLinkedQueue<String> processErrorMessage(String message, ForkJoinPool forkJoinPool,
284             ConcurrentLinkedQueue<String> messages) {
285         LOG.warn("Received error message {}", message);
286         messages.add(message);
287         forkJoinPool.shutdown();
288         return messages;
289     }
290
291     @SuppressWarnings("rawtypes")
292     // FIXME check if the ForkJoinTask raw type can be avoided
293     // Raw types use are discouraged since they lack type safety.
294     // Resulting Problems are observed at run time and not at compile time
295     @Override
296     public ServicePathOutput deleteServicePath(ServicePathInput input) {
297         if (!alarmSuppressionNodeRegistration(input)) {
298             LOG.warn("Alarm suppresion node registraion failed!!!!");
299         }
300         List<Nodes> nodes = input.getNodes();
301         AtomicBoolean success = new AtomicBoolean(true);
302         ConcurrentLinkedQueue<String> results = new ConcurrentLinkedQueue<>();
303         CopyOnWriteArrayList<LinkTp> otnLinkTps = new CopyOnWriteArrayList<>();
304         ForkJoinPool forkJoinPool = new ForkJoinPool();
305         ForkJoinTask forkJoinTask = forkJoinPool.submit(() -> nodes.parallelStream().forEach(node -> {
306             String nodeId = node.getNodeId();
307             LOG.info("Deleting service setup on node {}", nodeId);
308             if (node.getDestTp() == null) {
309                 LOG.error("Destination termination point must not be null.");
310                 return;
311             }
312             if (!this.deviceTransactionManager.isDeviceMounted(nodeId)) {
313                 String result = nodeId + IS_NOT_MOUNTED_ON_THE_CONTROLLER;
314                 results.add(result);
315                 success.set(false);
316                 LOG.warn(result);
317                 forkJoinPool.shutdown();
318                 return;
319                 //TODO should deletion end here?
320             }
321             // if the node is currently mounted then proceed.
322
323             String destTp = node.getDestTp();
324             String srcTp = "";
325             if (node.getSrcTp() == null) {
326                 otnLinkTps.add(new LinkTpBuilder()
327                         .setNodeId(nodeId)
328                         .setTpId(destTp)
329                         .build());
330             } else {
331                 srcTp = node.getSrcTp();
332             }
333             List<String> interfacesToDelete = new LinkedList<>();
334             interfacesToDelete.addAll(getInterfaces2delete(nodeId, srcTp, destTp,
335                     input.getLowerSpectralSlotNumber().intValue(),
336                     input.getHigherSpectralSlotNumber().intValue()));
337             for (String interfaceId : interfacesToDelete) {
338                 try {
339                     this.openRoadmInterfaces.deleteInterface(nodeId, interfaceId);
340                 } catch (OpenRoadmInterfaceException e) {
341                     String result = String.format("Failed to delete interface %s on node %s!", interfaceId, nodeId);
342                     success.set(false);
343                     LOG.error(result, e);
344                     results.add(result);
345                 }
346             }
347         }));
348         try {
349             forkJoinTask.get();
350         } catch (InterruptedException | ExecutionException e) {
351             LOG.error("Error while deleting service paths!", e);
352         }
353         forkJoinPool.shutdown();
354         if (!alarmSuppressionNodeRemoval(input.getServiceName())) {
355             LOG.error("Alarm suppresion node removal failed!!!!");
356         }
357         return new ServicePathOutputBuilder()
358                 .setSuccess(success.get())
359                 .setLinkTp(otnLinkTps)
360                 .setResult(
361                     results.isEmpty()
362                     ? "Request processed"
363                     : String.join("\n", results))
364                 .build();
365     }
366
367     private List<String> getInterfaces2delete(
368             String nodeId, String srcTp, String destTp, int lowerSpectralSlotNumber, int higherSpectralSlotNumber) {
369         String spectralSlotName = String.join(GridConstant.SPECTRAL_SLOT_SEPARATOR,
370                 String.valueOf(lowerSpectralSlotNumber),
371                 String.valueOf(higherSpectralSlotNumber));
372         if (destTp.contains(StringConstants.NETWORK_TOKEN)
373                 || srcTp.contains(StringConstants.CLIENT_TOKEN)
374                 || srcTp.contains(StringConstants.NETWORK_TOKEN)
375                 || destTp.contains(StringConstants.CLIENT_TOKEN)) {
376             return getInterfacesTodelete(nodeId, srcTp, destTp, spectralSlotName);
377         }
378
379         List<String> interfacesToDelete = new LinkedList<>();
380         String connectionNumber = String.join(GridConstant.NAME_PARAMETERS_SEPARATOR, srcTp, destTp, spectralSlotName);
381         List<String> intToDelete = this.crossConnect.deleteCrossConnect(nodeId, connectionNumber, false);
382         for (String interf : intToDelete == null ? new ArrayList<String>() : intToDelete) {
383             if (!this.openRoadmInterfaceFactory.isUsedByXc(
384                     nodeId, interf, connectionNumber, this.deviceTransactionManager)) {
385                 interfacesToDelete.add(interf);
386             }
387         }
388         return interfacesToDelete;
389     }
390
391     private List<String> getInterfacesTodelete(String nodeId, String srcTp, String destTp, String spectralSlotName) {
392
393         OpenroadmNodeVersion nodeOpenRoadmVersion =
394                 this.portMapping.getNode(nodeId).getNodeInfo().getOpenroadmVersion();
395         List<String> interfacesToDelete = new LinkedList<>();
396         Map<String, List<String>> suffixListMap =
397             nodeOpenRoadmVersion.equals(OpenroadmNodeVersion._71)
398                 ? Map.of(
399                     "ODU",  List.of("ODUC4","ODUFLEX"),
400                     "other", List.of("OTUC4", "OTSI-GROUP", spectralSlotName))
401                 : Map.of(
402                     "ODU", List.of("ODU", "ODU4"),
403                     "other", List.of("OTU", spectralSlotName));
404         // this last suffix used to be retrieved from openRoadmInterfaceFactory.createOpenRoadmOchInterfaceName
405         // i.e. String.join(GridConstant.NAME_PARAMETERS_SEPARATOR, destTp, spectralSlotName) with
406         // common GridConstant that states NAME_PARAMETERS_SEPARATOR = "-"
407
408         if (destTp.contains(StringConstants.NETWORK_TOKEN)) {
409             try {
410                 for (String suffix : suffixListMap.get("ODU")) {
411                     if (this.openRoadmInterfaces.getInterface(
412                             nodeId, String.join(GridConstant.NAME_PARAMETERS_SEPARATOR, destTp, suffix)).isPresent()) {
413                         interfacesToDelete.add(String.join(GridConstant.NAME_PARAMETERS_SEPARATOR, destTp, suffix));
414                     }
415                 }
416             }
417             catch (OpenRoadmInterfaceException e) {
418                 LOG.error("impossible to get one of the interfaces {}",
419                     destTp + GridConstant.NAME_PARAMETERS_SEPARATOR + String.join(
420                         " or " + destTp + GridConstant.NAME_PARAMETERS_SEPARATOR,
421                         suffixListMap.get("ODU")),
422                     e);
423             }
424             for (String suffix : suffixListMap.get("other")) {
425                 interfacesToDelete.add(String.join(GridConstant.NAME_PARAMETERS_SEPARATOR, destTp, suffix));
426             }
427         }
428         if (srcTp.contains(StringConstants.NETWORK_TOKEN)) {
429             interfacesToDelete.add(
430                     String.join(GridConstant.NAME_PARAMETERS_SEPARATOR, srcTp, suffixListMap.get("ODU").get(0)));
431             for (String suffix : suffixListMap.get("other")) {
432                 interfacesToDelete.add(String.join(GridConstant.NAME_PARAMETERS_SEPARATOR, srcTp, suffix));
433             }
434         }
435         if (srcTp.contains(StringConstants.CLIENT_TOKEN)) {
436             interfacesToDelete.add(String.join(GridConstant.NAME_PARAMETERS_SEPARATOR, srcTp, "ETHERNET"));
437         }
438         if (destTp.contains(StringConstants.CLIENT_TOKEN)) {
439             interfacesToDelete.add(String.join(GridConstant.NAME_PARAMETERS_SEPARATOR, destTp, "ETHERNET"));
440         }
441         return interfacesToDelete;
442     }
443
444
445
446     @Override
447     public RendererRollbackOutput rendererRollback(RendererRollbackInput input) {
448         boolean success = true;
449         Map<FailedToRollbackKey,FailedToRollback> failedToRollbackList = new HashMap<>();
450         for (NodeInterface nodeInterfaces : input.nonnullNodeInterface().values()) {
451             List<String> failedInterfaces = new ArrayList<>();
452             String nodeId = nodeInterfaces.getNodeId();
453             for (String connectionId : nodeInterfaces.getConnectionId()) {
454                 List<String> listInter = this.crossConnect.deleteCrossConnect(nodeId, connectionId, false);
455                 if (listInter != null) {
456                     LOG.info("Cross connect {} on node {} successfully deleted.", connectionId, nodeId);
457                 } else {
458                     LOG.error("Failed to delete cross connect {} on node {}!", connectionId, nodeId);
459                     success = false;
460                     failedInterfaces.add(connectionId);
461                 }
462             }
463             // Interfaces needs to be in specific order to delete. Order is:
464             // 1. ODU interfaces
465             // 2. OTU interfaces
466             // 3. OCH interfaces
467             // 4. ETH interfaces
468             LinkedList<String> interfacesToDelete = new LinkedList<>();
469             if (nodeInterfaces.getOduInterfaceId() != null) {
470                 interfacesToDelete.addAll(nodeInterfaces.getOduInterfaceId());
471             }
472             if (nodeInterfaces.getOtuInterfaceId() != null) {
473                 interfacesToDelete.addAll(nodeInterfaces.getOtuInterfaceId());
474             }
475             if (nodeInterfaces.getOchInterfaceId() != null) {
476                 interfacesToDelete.addAll(nodeInterfaces.getOchInterfaceId());
477             }
478             if (nodeInterfaces.getEthInterfaceId() != null) {
479                 interfacesToDelete.addAll(nodeInterfaces.getEthInterfaceId());
480             }
481             LOG.info("Going to execute rollback on node {}. Interfaces to rollback: {}", nodeId,
482                     String.join(", ", interfacesToDelete));
483             for (String interfaceId : interfacesToDelete) {
484                 try {
485                     this.openRoadmInterfaces.deleteInterface(nodeId, interfaceId);
486                     LOG.info("Interface {} on node {} successfully deleted.", interfaceId, nodeId);
487                 } catch (OpenRoadmInterfaceException e) {
488                     LOG.error("Failed to delete interface {} on node {}!", interfaceId, nodeId);
489                     success = false;
490                     failedInterfaces.add(interfaceId);
491                 }
492             }
493             FailedToRollback failedToRollack = new FailedToRollbackBuilder().withKey(new FailedToRollbackKey(nodeId))
494                     .setNodeId(nodeId).setInterface(failedInterfaces).build();
495             failedToRollbackList.put(failedToRollack.key(),failedToRollack);
496         }
497         return new RendererRollbackOutputBuilder()
498                 .setSuccess(success)
499                 .setFailedToRollback(failedToRollbackList)
500                 .build();
501     }
502
503     private boolean alarmSuppressionNodeRegistration(ServicePathInput input) {
504         Map<org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.alarmsuppression.rev171102.service
505                 .nodelist.nodelist.NodesKey,
506             org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.alarmsuppression.rev171102.service
507                 .nodelist.nodelist.Nodes> nodeList = new HashMap<>();
508         if (input.getNodes() != null) {
509             for (Nodes node : input.getNodes()) {
510                 org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.alarmsuppression.rev171102
511                     .service.nodelist.nodelist.Nodes nodes =
512                         new org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.alarmsuppression.rev171102
513                             .service.nodelist.nodelist.NodesBuilder().setNodeId(node.getNodeId()).build();
514                 nodeList.put(nodes.key(),nodes);
515             }
516         }
517         InstanceIdentifier<org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.alarmsuppression.rev171102
518             .service.nodelist.Nodelist> nodeListIID =
519                  InstanceIdentifier.create(ServiceNodelist.class)
520                      .child(org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.alarmsuppression.rev171102
521                          .service.nodelist.Nodelist.class, new NodelistKey(input.getServiceName()));
522         final WriteTransaction writeTransaction = this.dataBroker.newWriteOnlyTransaction();
523         writeTransaction.merge(LogicalDatastoreType.CONFIGURATION,
524                 nodeListIID,
525                 new NodelistBuilder()
526                     .withKey(new NodelistKey(input.getServiceName()))
527                     .setServiceName(input.getServiceName())
528                     .setNodes(nodeList)
529                     .build());
530         FluentFuture<? extends @NonNull CommitInfo> commit = writeTransaction.commit();
531         try {
532             commit.get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
533             LOG.info("Nodes are register for alarm suppression for service: {}", input.getServiceName());
534             return true;
535         } catch (ExecutionException | InterruptedException | TimeoutException e) {
536             LOG.warn("Failed to alarm suppresslist for service: {}", input.getServiceName(), e);
537             return false;
538         }
539     }
540
541     private boolean alarmSuppressionNodeRemoval(String serviceName) {
542         InstanceIdentifier<org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.alarmsuppression.rev171102
543             .service.nodelist.Nodelist> nodeListIID =
544                 InstanceIdentifier.create(ServiceNodelist.class)
545                     .child(org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.alarmsuppression.rev171102
546                         .service.nodelist.Nodelist.class, new NodelistKey(serviceName));
547         final WriteTransaction writeTransaction = this.dataBroker.newWriteOnlyTransaction();
548         writeTransaction.delete(LogicalDatastoreType.CONFIGURATION, nodeListIID);
549         FluentFuture<? extends @NonNull CommitInfo> commit = writeTransaction.commit();
550         try {
551             commit.get(Timeouts.DATASTORE_DELETE, TimeUnit.MILLISECONDS);
552             LOG.info("Nodes are unregister for alarm suppression for service: {}", serviceName);
553             return true;
554         } catch (InterruptedException | TimeoutException | ExecutionException e) {
555             LOG.warn("Failed to alarm suppresslist for service: {}", serviceName, e);
556             return false;
557         }
558     }
559
560     private void setTopologyForService(String name, Topology topo)
561             throws InterruptedException, ExecutionException, TimeoutException {
562         ServicesBuilder servicesBuilder;
563         // Get the service from the service list inventory
564         ServicesKey serviceKey = new ServicesKey(name);
565         InstanceIdentifier<Services> iid =
566                 InstanceIdentifier.create(ServiceList.class).child(Services.class, serviceKey);
567         Optional<Services> services;
568         try (ReadTransaction readTx = this.dataBroker.newReadOnlyTransaction()) {
569             Future<java.util.Optional<Services>> future =
570                     readTx.read(LogicalDatastoreType.OPERATIONAL, iid);
571             services = future.get(Timeouts.DATASTORE_READ, TimeUnit.MILLISECONDS);
572         } catch (InterruptedException | ExecutionException | TimeoutException e) {
573             throw e;
574         }
575         if (services.isPresent()) {
576             LOG.info("service {} already exists", name);
577             servicesBuilder = new ServicesBuilder(services.get()).setTopology(topo);
578             WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
579             writeTx.merge(LogicalDatastoreType.OPERATIONAL, iid, servicesBuilder.build());
580             writeTx.commit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
581         } else {
582             LOG.warn("Service {} does not exist - topology can not be updated", name);
583         }
584     }
585
586     @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
587         value = "SLF4J_FORMAT_SHOULD_BE_CONST",
588         justification = "Log messages content needs to be formatted before"
589             + "since they are used in the returned object")
590     @Override
591     public CreateOtsOmsOutput createOtsOms(CreateOtsOmsInput input) throws OpenRoadmInterfaceException {
592         if (!this.deviceTransactionManager.isDeviceMounted(input.getNodeId())) {
593             String result = input.getNodeId() + IS_NOT_MOUNTED_ON_THE_CONTROLLER;
594             LOG.warn(result);
595             return new CreateOtsOmsOutputBuilder().setResult(result).setSuccess(false).build();
596         }
597         // if the node is currently mounted then proceed.
598         Mapping oldMapping = this.portMapping.getMapping(input.getNodeId(), input.getLogicalConnectionPoint());
599         if (oldMapping == null) {
600             return new CreateOtsOmsOutputBuilder()
601                     .setResult(String.format("Logical Connection point %s does not exist for %s",
602                             input.getLogicalConnectionPoint(), input.getNodeId()))
603                     .setSuccess(false)
604                     .build();
605         }
606
607         String otsInterface = this.openRoadmInterfaceFactory.createOpenRoadmOtsInterface(input.getNodeId(), oldMapping);
608         int count = 0;
609         Mapping newMapping = this.portMapping.getMapping(input.getNodeId(), input.getLogicalConnectionPoint());
610         while (!isSupportingOtsPresent(newMapping)) {
611             LOG.info("waiting for post interface operation on node '{}'...", input.getNodeId());
612             try {
613                 Thread.sleep(10000);
614                 this.portMapping.updateMapping(input.getNodeId(), oldMapping);
615             } catch (InterruptedException e) {
616                 LOG.error("Failed to wait for post interface operation");
617             }
618             count++;
619             if (count >= 6) {
620                 String result = String.format("Unable to get ots interface from mapping %s - %s",
621                         oldMapping.getLogicalConnectionPoint(), input.getNodeId());
622                 LOG.error(result);
623                 return new CreateOtsOmsOutputBuilder().setResult(result).setSuccess(false).build();
624             }
625             newMapping = this.portMapping.getMapping(input.getNodeId(), input.getLogicalConnectionPoint());
626         }
627
628         String omsInterface = this.openRoadmInterfaceFactory.createOpenRoadmOmsInterface(input.getNodeId(), newMapping);
629         if (omsInterface == null) {
630             String result = String.format("Fail to create OpenRoadmOms Interface for node : %s", input.getNodeId());
631             LOG.error(result);
632             return new CreateOtsOmsOutputBuilder().setResult(result).setSuccess(false).build();
633         }
634         return new CreateOtsOmsOutputBuilder()
635                 .setResult(String.format("Interfaces %s - %s successfully created on node %s",
636                     otsInterface, omsInterface, input.getNodeId()))
637                 .setSuccess(true)
638                 .build();
639     }
640
641     private Boolean isSupportingOtsPresent(Mapping mapping) {
642         if (mapping == null) {
643             return false;
644         }
645         if (mapping.getSupportingOts() == null) {
646             LOG.warn("SupportingOts info is not present in mapping {}", mapping);
647             return false;
648         }
649         LOG.info("SupportingOts info is present in mapping {}", mapping);
650         return true;
651     }
652 }