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