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