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