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