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