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