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