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