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