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