065eab4a5918743c23081ff2ee4ed3cc6a5248ae
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / networkanalyzer / PceCalculation.java
1 /*
2  * Copyright © 2017 AT&T, Inc. 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
9 package org.opendaylight.transportpce.pce.networkanalyzer;
10
11 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
12 import java.math.BigDecimal;
13 import java.util.ArrayList;
14 import java.util.HashMap;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Optional;
18 import java.util.concurrent.ExecutionException;
19 import java.util.stream.Collectors;
20 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
21 import org.opendaylight.transportpce.common.NetworkUtils;
22 import org.opendaylight.transportpce.common.ResponseCodes;
23 import org.opendaylight.transportpce.common.StringConstants;
24 import org.opendaylight.transportpce.common.fixedflex.GridConstant;
25 import org.opendaylight.transportpce.common.mapping.MappingUtils;
26 import org.opendaylight.transportpce.common.mapping.MappingUtilsImpl;
27 import org.opendaylight.transportpce.common.mapping.PortMapping;
28 import org.opendaylight.transportpce.common.network.NetworkTransactionService;
29 import org.opendaylight.transportpce.common.service.ServiceTypes;
30 import org.opendaylight.transportpce.pce.PceComplianceCheck;
31 import org.opendaylight.transportpce.pce.constraints.PceConstraints;
32 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220808.PathComputationRequestInput;
33 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220808.path.computation.reroute.request.input.Endpoints;
34 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev220316.mc.capabilities.McCapabilities;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev211210.Link1;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev211210.Node1;
37 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.state.types.rev191129.State;
38 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.types.rev191129.NodeTypes;
39 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev211210.OpenroadmLinkType;
40 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev211210.OpenroadmNodeType;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NetworkId;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.Networks;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NodeId;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.Network;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.NetworkKey;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.network.Node;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.LinkId;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.Network1;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network.Link;
50 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
51 import org.opendaylight.yangtools.yang.common.Uint32;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55 public class PceCalculation {
56     /* Logging. */
57     private static final Logger LOG = LoggerFactory.getLogger(PceCalculation.class);
58     private NetworkTransactionService networkTransactionService;
59
60     ///////////// data parsed from Input/////////////////
61     private PathComputationRequestInput input;
62     private String anodeId = "";
63     private String znodeId = "";
64     private String serviceFormatA = "";
65     private String serviceFormatZ = "";
66     private String serviceType = "";
67     private Uint32 serviceRate = Uint32.valueOf(0);
68
69     private PceConstraints pceHardConstraints;
70
71     ///////////// Intermediate data/////////////////
72     private List<PceLink> addLinks = new ArrayList<>();
73     private List<PceLink> dropLinks = new ArrayList<>();
74     private List<NodeId> azSrgs = new ArrayList<>();
75
76     private PceNode aendPceNode = null;
77     private PceNode zendPceNode = null;
78
79     private List<Link> allLinks = null;
80     private List<Node> allNodes = null;
81
82     // this List serves graph calculation
83     private Map<NodeId, PceNode> allPceNodes = new HashMap<>();
84     // this List serves calculation of ZtoA path description
85     // TODO maybe better solution is possible
86     private Map<LinkId, PceLink> allPceLinks = new HashMap<>();
87     private List<LinkId> linksToExclude = new ArrayList<>();
88     private PceResult returnStructure;
89     private PortMapping portMapping;
90     // Define the termination points whose reservation status is not taken into account during the pruning process
91     private Endpoints endpoints;
92
93     private enum ConstraintTypes {
94         NONE, HARD_EXCLUDE, HARD_INCLUDE, HARD_DIVERSITY, SOFT_EXCLUDE, SOFT_INCLUDE, SOFT_DIVERSITY;
95     }
96
97     private MappingUtils mappingUtils;
98
99     public PceCalculation(PathComputationRequestInput input, NetworkTransactionService networkTransactionService,
100                           PceConstraints pceHardConstraints, PceConstraints pceSoftConstraints, PceResult rc,
101                           PortMapping portMapping) {
102         this.input = input;
103         this.networkTransactionService = networkTransactionService;
104         this.returnStructure = rc;
105         this.pceHardConstraints = pceHardConstraints;
106         this.mappingUtils = new MappingUtilsImpl(networkTransactionService.getDataBroker());
107         this.portMapping = portMapping;
108         this.endpoints = null;
109         parseInput();
110     }
111
112     public PceCalculation(PathComputationRequestInput input, NetworkTransactionService networkTransactionService,
113                           PceConstraints pceHardConstraints, PceConstraints pceSoftConstraints, PceResult rc,
114                           PortMapping portMapping, Endpoints endpoints) {
115         this.input = input;
116         this.networkTransactionService = networkTransactionService;
117         this.returnStructure = rc;
118         this.pceHardConstraints = pceHardConstraints;
119         this.mappingUtils = new MappingUtilsImpl(networkTransactionService.getDataBroker());
120         this.portMapping = portMapping;
121         this.endpoints = endpoints;
122         parseInput();
123     }
124
125     public void retrievePceNetwork() {
126
127         LOG.debug("In PceCalculation retrieveNetwork");
128
129         if (!readMdSal()) {
130             returnStructure.setRC(ResponseCodes.RESPONSE_FAILED);
131             return;
132         }
133         MapUtils.mapDiversityConstraints(allNodes, allLinks, pceHardConstraints);
134
135         if (!analyzeNw()) {
136             returnStructure.setRC(ResponseCodes.RESPONSE_FAILED);
137             return;
138         }
139         printNodesInfo(allPceNodes);
140
141         returnStructure.setRC(ResponseCodes.RESPONSE_OK);
142     }
143
144     private boolean parseInput() {
145         if (!PceComplianceCheck.checkString(input.getServiceAEnd().getServiceFormat().getName())
146                 || !PceComplianceCheck.checkString(input.getServiceZEnd().getServiceFormat().getName())
147                 || !PceComplianceCheck.checkString(input.getServiceAEnd().getServiceRate().toString())) {
148             LOG.error("Service Format and Service Rate are required for a path calculation");
149             return false;
150         }
151         serviceFormatA = input.getServiceAEnd().getServiceFormat().getName();
152         serviceFormatZ = input.getServiceZEnd().getServiceFormat().getName();
153         serviceRate = input.getServiceAEnd().getServiceRate();
154         serviceType = ServiceTypes.getServiceType(
155             serviceFormatA,
156             serviceRate,
157             NodeTypes.Xpdr.equals(portMapping.getNode(input.getServiceAEnd().getNodeId()).getNodeInfo().getNodeType())
158                     && checkAendInputTxPortName()
159                 ? portMapping.getMapping(
160                     input.getServiceAEnd().getNodeId(),
161                     input.getServiceAEnd().getTxDirection().getPort().getPortName())
162                 : null);
163
164         LOG.debug("parseInput: A and Z :[{}] and [{}]", anodeId, znodeId);
165
166         getAZnodeId();
167
168         returnStructure.setRate(input.getServiceAEnd().getServiceRate().toJava());
169         returnStructure.setServiceFormat(input.getServiceAEnd().getServiceFormat());
170         return true;
171     }
172
173     private boolean checkAendInputTxPortName() {
174         return checkAendInputTxPort()
175             && input.getServiceAEnd().getTxDirection().getPort().getPortName() != null;
176     }
177
178     private boolean checkAendInputTxPortDeviceName() {
179         return checkAendInputTxPort()
180             && input.getServiceAEnd().getTxDirection().getPort().getPortDeviceName() != null;
181     }
182
183     private boolean checkAendInputTxPort() {
184         return input.getServiceAEnd() != null
185             && input.getServiceAEnd().getTxDirection() != null
186             && input.getServiceAEnd().getTxDirection().getPort() != null;
187     }
188
189     private boolean checkZendInputTxPortDeviceName() {
190         return input.getServiceZEnd() != null
191             && input.getServiceZEnd().getTxDirection() != null
192             && input.getServiceZEnd().getTxDirection().getPort() != null
193             && input.getServiceZEnd().getTxDirection().getPort().getPortDeviceName() != null;
194     }
195
196     private void getAZnodeId() {
197         anodeId =
198             checkAendInputTxPortDeviceName()
199                 ? input.getServiceAEnd().getTxDirection().getPort().getPortDeviceName()
200                 : input.getServiceAEnd().getNodeId();
201         znodeId =
202             checkZendInputTxPortDeviceName()
203                 ? input.getServiceZEnd().getTxDirection().getPort().getPortDeviceName()
204                 : input.getServiceZEnd().getNodeId();
205     }
206
207     private boolean readMdSal() {
208         InstanceIdentifier<Network> nwInstanceIdentifier = null;
209         switch (serviceType) {
210             case StringConstants.SERVICE_TYPE_100GE_T:
211             case StringConstants.SERVICE_TYPE_400GE:
212             case StringConstants.SERVICE_TYPE_OTU4:
213             case StringConstants.SERVICE_TYPE_OTUC2:
214             case StringConstants.SERVICE_TYPE_OTUC3:
215             case StringConstants.SERVICE_TYPE_OTUC4:
216                 LOG.debug("readMdSal: network {}", NetworkUtils.OVERLAY_NETWORK_ID);
217                 nwInstanceIdentifier = InstanceIdentifier.builder(Networks.class)
218                     .child(Network.class, new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID))).build();
219                 break;
220             case StringConstants.SERVICE_TYPE_100GE_M:
221             case StringConstants.SERVICE_TYPE_100GE_S:
222             case StringConstants.SERVICE_TYPE_ODU4:
223             case StringConstants.SERVICE_TYPE_ODUC2:
224             case StringConstants.SERVICE_TYPE_ODUC3:
225             case StringConstants.SERVICE_TYPE_ODUC4:
226             case StringConstants.SERVICE_TYPE_10GE:
227             case StringConstants.SERVICE_TYPE_1GE:
228                 LOG.debug("readMdSal: network {}", NetworkUtils.OTN_NETWORK_ID);
229                 nwInstanceIdentifier = InstanceIdentifier.builder(Networks.class)
230                     .child(Network.class, new NetworkKey(new NetworkId(NetworkUtils.OTN_NETWORK_ID))).build();
231                 break;
232             default:
233                 LOG.warn("readMdSal: unknown service-type for service-rate {} and service-format {}", serviceRate,
234                     serviceFormatA);
235                 break;
236         }
237
238         if (readTopology(nwInstanceIdentifier) == null) {
239             LOG.error("readMdSal: network is null: {}", nwInstanceIdentifier);
240             return false;
241         }
242
243         allNodes = readTopology(nwInstanceIdentifier).nonnullNode().values().stream().sorted((n1, n2)
244             -> n1.getNodeId().getValue().compareTo(n2.getNodeId().getValue())).collect(Collectors.toList());
245         Network1 nw1 = readTopology(nwInstanceIdentifier).augmentation(Network1.class);
246         if (nw1 == null) {
247             LOG.warn("no otn links in otn-topology");
248         } else {
249             allLinks = nw1.nonnullLink().values().stream().sorted((l1, l2)
250                 -> l1.getSource().getSourceTp().getValue().compareTo(l2.getSource().getSourceTp().getValue()))
251                     .collect(Collectors.toList());
252         }
253         if (allNodes == null || allNodes.isEmpty()) {
254             LOG.error("readMdSal: no nodes ");
255             return false;
256         }
257         LOG.info("readMdSal: network nodes: {} nodes added", allNodes.size());
258         LOG.debug("readMdSal: network nodes: {} nodes added", allNodes);
259
260         if (allLinks == null || allLinks.isEmpty()) {
261             LOG.error("readMdSal: no links ");
262             return false;
263         }
264         LOG.info("readMdSal: network links: {} links added", allLinks.size());
265         LOG.debug("readMdSal: network links: {} links added", allLinks);
266
267         return true;
268     }
269
270     private Network readTopology(InstanceIdentifier<Network> nwInstanceIdentifier) {
271         Network nw = null;
272         try {
273             Optional<Network> nwOptional =
274                 networkTransactionService.read(LogicalDatastoreType.CONFIGURATION, nwInstanceIdentifier).get();
275             if (nwOptional.isPresent()) {
276                 nw = nwOptional.get();
277                 LOG.debug("readMdSal: network nodes: nwOptional.isPresent = true {}", nw);
278                 networkTransactionService.close();
279             }
280         } catch (InterruptedException | ExecutionException e) {
281             LOG.error("readMdSal: Error reading topology {}", nwInstanceIdentifier);
282             networkTransactionService.close();
283             returnStructure.setRC(ResponseCodes.RESPONSE_FAILED);
284         }
285         return nw;
286     }
287
288     private boolean analyzeNw() {
289
290         LOG.debug("analyzeNw: allNodes size {}, allLinks size {}", allNodes.size(), allLinks.size());
291         switch (serviceType) {
292             case StringConstants.SERVICE_TYPE_100GE_T:
293             case  StringConstants.SERVICE_TYPE_OTU4:
294             case  StringConstants.SERVICE_TYPE_400GE:
295             case StringConstants.SERVICE_TYPE_OTUC2:
296             case StringConstants.SERVICE_TYPE_OTUC3:
297             case  StringConstants.SERVICE_TYPE_OTUC4:
298                 // 100GE service and OTU4 service are handled at the openroadm-topology layer
299                 for (Node node : allNodes) {
300                     validateNode(node);
301                 }
302
303                 LOG.debug("analyzeNw: allPceNodes size {}", allPceNodes.size());
304
305                 if (aendPceNode == null || zendPceNode == null) {
306                     LOG.error("analyzeNw: Error in reading nodes: A or Z do not present in the network");
307                     return false;
308                 }
309                 for (Link link : allLinks) {
310                     validateLink(link);
311                 }
312                 // debug prints
313                 LOG.debug("analyzeNw: addLinks size {}, dropLinks size {}", addLinks.size(), dropLinks.size());
314                 // debug prints
315                 LOG.debug("analyzeNw: azSrgs size = {}", azSrgs.size());
316                 for (NodeId srg : azSrgs) {
317                     LOG.debug("analyzeNw: A/Z Srgs SRG = {}", srg.getValue());
318                 }
319                 // debug prints
320                 for (PceLink link : addLinks) {
321                     filteraddLinks(link);
322                 }
323                 for (PceLink link : dropLinks) {
324                     filterdropLinks(link);
325                 }
326                 break;
327
328             default:
329                 // ODU4, 10GE/ODU2e or 1GE/ODU0 services are handled at openroadm-otn layer
330
331                 for (Node node : allNodes) {
332                     validateOtnNode(node);
333                 }
334
335                 LOG.info("analyzeNw: allPceNodes {}", allPceNodes);
336
337                 if (aendPceNode == null || zendPceNode == null) {
338                     LOG.error("analyzeNw: Error in reading nodes: A or Z do not present in the network");
339                     return false;
340                 }
341                 for (Link link : allLinks) {
342                     validateLink(link);
343                 }
344                 break;
345         }
346
347         LOG.info("analyzeNw: allPceNodes size {}, allPceLinks size {}", allPceNodes.size(), allPceLinks.size());
348
349         if ((allPceNodes.size() == 0) || (allPceLinks.size() == 0)) {
350             return false;
351         }
352
353         LOG.debug("analyzeNw: allPceNodes {}", allPceNodes);
354         LOG.debug("analyzeNw: allPceLinks {}", allPceLinks);
355
356         return true;
357     }
358
359     private boolean filteraddLinks(PceLink pcelink) {
360
361         NodeId nodeId = pcelink.getSourceId();
362
363         if (azSrgs.contains(nodeId)) {
364             allPceLinks.put(pcelink.getLinkId(), pcelink);
365             allPceNodes.get(nodeId).addOutgoingLink(pcelink);
366             LOG.debug("analyzeNw: Add_LINK added to source and to allPceLinks {}", pcelink.getLinkId());
367             return true;
368         }
369
370         // remove the SRG from PceNodes, as it is not directly connected to A/Z
371         allPceNodes.remove(nodeId);
372         LOG.debug("analyzeNw: SRG removed {}", nodeId.getValue());
373
374         return false;
375     }
376
377     private boolean filterdropLinks(PceLink pcelink) {
378
379         NodeId nodeId = pcelink.getDestId();
380
381         if (azSrgs.contains(nodeId)) {
382             allPceLinks.put(pcelink.getLinkId(), pcelink);
383             allPceNodes.get(nodeId).addOutgoingLink(pcelink);
384             LOG.debug("analyzeNw: Drop_LINK added to dest and to allPceLinks {}", pcelink.getLinkId());
385             return true;
386         }
387
388         // remove the SRG from PceNodes, as it is not directly connected to A/Z
389         allPceNodes.remove(pcelink.getDestId());
390         LOG.debug("analyzeNw: SRG removed {}", nodeId.getValue());
391
392         return false;
393     }
394
395     private boolean validateLink(Link link) {
396         LOG.debug("validateLink: link {} ", link);
397
398         NodeId sourceId = link.getSource().getSourceNode();
399         NodeId destId = link.getDestination().getDestNode();
400         PceNode source = allPceNodes.get(sourceId);
401         PceNode dest = allPceNodes.get(destId);
402         State state = link.augmentation(Link1.class).getOperationalState();
403
404         if (source == null) {
405             LOG.debug("validateLink: Link is ignored due source node is rejected by node validation - {}",
406                 link.getSource().getSourceNode().getValue());
407             return false;
408         }
409         if (dest == null) {
410             LOG.debug("validateLink: Link is ignored due dest node is rejected by node validation - {}",
411                 link.getDestination().getDestNode().getValue());
412             return false;
413         }
414
415         if (State.OutOfService.equals(state)) {
416             LOG.debug("validateLink: Link is ignored due operational state - {}",
417                     state.getName());
418             return false;
419         }
420
421         switch (serviceType) {
422             case StringConstants.SERVICE_TYPE_100GE_T:
423             case StringConstants.SERVICE_TYPE_OTU4:
424             case StringConstants.SERVICE_TYPE_OTUC2:
425             case StringConstants.SERVICE_TYPE_OTUC3:
426             case StringConstants.SERVICE_TYPE_OTUC4:
427             case StringConstants.SERVICE_TYPE_400GE:
428                 return processPceLink(link, sourceId, destId, source, dest);
429             case StringConstants.SERVICE_TYPE_ODU4:
430             case StringConstants.SERVICE_TYPE_10GE:
431             case StringConstants.SERVICE_TYPE_100GE_M:
432             case StringConstants.SERVICE_TYPE_100GE_S:
433             case StringConstants.SERVICE_TYPE_ODUC2:
434             case StringConstants.SERVICE_TYPE_ODUC3:
435             case StringConstants.SERVICE_TYPE_ODUC4:
436             case StringConstants.SERVICE_TYPE_1GE:
437                 return processPceOtnLink(link, source, dest);
438             default:
439                 LOG.error(" validateLink: Unmanaged service type {}", serviceType);
440                 return false;
441         }
442     }
443
444     private void validateNode(Node node) {
445         LOG.debug("validateNode: node {} ", node);
446         // PceNode will be used in Graph algorithm
447         Node1 node1 = node.augmentation(Node1.class);
448         if (node1 == null) {
449             LOG.error("getNodeType: no Node1 (type) Augmentation for node: [{}]. Node is ignored", node.getNodeId());
450             return;
451         }
452         if (State.OutOfService.equals(node1.getOperationalState())) {
453             LOG.error("getNodeType: node is ignored due to operational state - {}", node1.getOperationalState()
454                     .getName());
455             return;
456         }
457         OpenroadmNodeType nodeType = node1.getNodeType();
458         String deviceNodeId = MapUtils.getSupNetworkNode(node);
459         // Should never happen but because of existing topology test files
460         // we have to manage this case
461         if (deviceNodeId == null || deviceNodeId.isBlank()) {
462             deviceNodeId = node.getNodeId().getValue();
463         }
464
465         LOG.debug("Device node id {} for {}", deviceNodeId, node);
466         PceOpticalNode pceNode = new PceOpticalNode(deviceNodeId, this.serviceType, portMapping, node, nodeType,
467             mappingUtils.getOpenRoadmVersion(deviceNodeId), getSlotWidthGranularity(deviceNodeId, node.getNodeId()),
468             getCentralFreqGranularity(deviceNodeId, node.getNodeId()));
469         if (endpoints != null) {
470             pceNode.setEndpoints(endpoints);
471         }
472         pceNode.validateAZxponder(anodeId, znodeId, input.getServiceAEnd().getServiceFormat());
473         pceNode.initFrequenciesBitSet();
474
475         if (!pceNode.isValid()) {
476             LOG.debug(" validateNode: Node {} is ignored", node.getNodeId().getValue());
477             return;
478         }
479         if (validateNodeConstraints(pceNode).equals(ConstraintTypes.HARD_EXCLUDE)) {
480             return;
481         }
482
483         if (endPceNode(nodeType, pceNode.getNodeId(), pceNode)) {
484             if (this.aendPceNode == null && isAZendPceNode(this.serviceFormatA, pceNode, anodeId, "A")) {
485                 // Added to ensure A-node has a addlink in the topology
486                 List<Link> links = this.allLinks.stream()
487                     .filter(x -> x.getSource().getSourceNode().getValue().contains(pceNode.getNodeId().getValue()))
488                     .collect(Collectors.toList());
489                 if (!links.isEmpty()) {
490                     this.aendPceNode = pceNode;
491                 }
492             }
493             if (this.zendPceNode == null && isAZendPceNode(this.serviceFormatZ, pceNode, znodeId, "Z")) {
494                 // Added to ensure Z-node has a droplink in the topology
495                 List<Link> links = this.allLinks.stream()
496                     .filter(x -> x.getDestination().getDestNode().getValue().contains(pceNode.getNodeId().getValue()))
497                     .collect(Collectors.toList());
498                 if (!links.isEmpty()) {
499                     this.zendPceNode = pceNode;
500                 }
501             }
502         }
503
504         allPceNodes.put(pceNode.getNodeId(), pceNode);
505         LOG.debug("validateNode: node is saved {}", pceNode.getNodeId().getValue());
506     }
507
508     @SuppressWarnings("fallthrough")
509     @SuppressFBWarnings(
510         value = "SF_SWITCH_FALLTHROUGH",
511         justification = "intentional fallthrough")
512     private boolean isAZendPceNode(String serviceFormat, PceOpticalNode pceNode, String azNodeId, String azEndPoint) {
513         switch (serviceFormat) {
514             case "Ethernet":
515             case "OC":
516                 if (pceNode.getSupNetworkNodeId().equals(azNodeId)) {
517                     return true;
518                 }
519             //fallthrough
520             case "OTU":
521                 switch (azEndPoint) {
522                     case "A":
523                         return checkAendInputTxPortDeviceName()
524                             && pceNode.getNodeId().getValue()
525                                 .equals(this.input.getServiceAEnd().getTxDirection().getPort().getPortDeviceName());
526                     case "Z":
527                         return checkZendInputTxPortDeviceName()
528                             && pceNode.getNodeId().getValue()
529                                 .equals(this.input.getServiceZEnd().getTxDirection().getPort().getPortDeviceName());
530                     default:
531                         return false;
532                 }
533             default:
534                 LOG.debug("Unsupported service Format {} for node {}", serviceFormat, pceNode.getNodeId().getValue());
535                 return false;
536         }
537     }
538
539     private void validateOtnNode(Node node) {
540         LOG.info("validateOtnNode: {} ", node.getNodeId().getValue());
541         // PceOtnNode will be used in Graph algorithm
542         if (node.augmentation(Node1.class) == null) {
543             LOG.error("ValidateOtnNode: no node-type augmentation. Node {} is ignored", node.getNodeId().getValue());
544             return;
545         }
546
547         OpenroadmNodeType nodeType = node.augmentation(Node1.class).getNodeType();
548         String clientPort = null;
549         if (node.getNodeId().getValue().equals(anodeId)
550                 && this.aendPceNode == null
551                 && input.getServiceAEnd() != null
552                 && input.getServiceAEnd().getRxDirection() != null
553                 && input.getServiceAEnd().getRxDirection().getPort() != null
554                 && input.getServiceAEnd().getRxDirection().getPort().getPortName() != null) {
555             clientPort = input.getServiceAEnd().getRxDirection().getPort().getPortName();
556         } else if (node.getNodeId().getValue().equals(znodeId)
557                 && this.zendPceNode == null
558                 && input.getServiceZEnd() != null
559                 && input.getServiceZEnd().getRxDirection() != null
560                 && input.getServiceZEnd().getRxDirection().getPort() != null
561                 && input.getServiceZEnd().getRxDirection().getPort().getPortName() != null) {
562             clientPort = input.getServiceZEnd().getRxDirection().getPort().getPortName();
563         }
564
565         PceOtnNode pceOtnNode = new PceOtnNode(node, nodeType, node.getNodeId(), "otn", serviceType, clientPort);
566         pceOtnNode.validateXponder(anodeId, znodeId);
567
568         if (!pceOtnNode.isValid()) {
569             LOG.warn(" validateOtnNode: Node {} is ignored", node.getNodeId().getValue());
570             return;
571         }
572         if (validateNodeConstraints(pceOtnNode).equals(ConstraintTypes.HARD_EXCLUDE)) {
573             return;
574         }
575         if (pceOtnNode.getNodeId().getValue().equals(anodeId) && this.aendPceNode == null) {
576             this.aendPceNode = pceOtnNode;
577         }
578         if (pceOtnNode.getNodeId().getValue().equals(znodeId) && this.zendPceNode == null) {
579             this.zendPceNode = pceOtnNode;
580         }
581         allPceNodes.put(pceOtnNode.getNodeId(), pceOtnNode);
582         LOG.info("validateOtnNode: node {} is saved", node.getNodeId().getValue());
583     }
584
585     private ConstraintTypes validateNodeConstraints(PceNode pcenode) {
586         if (pceHardConstraints.getExcludeSupNodes().isEmpty() && pceHardConstraints.getExcludeCLLI().isEmpty()) {
587             return ConstraintTypes.NONE;
588         }
589         if (pceHardConstraints.getExcludeSupNodes().contains(pcenode.getSupNetworkNodeId())) {
590             LOG.debug("validateNodeConstraints: {}", pcenode.getNodeId().getValue());
591             return ConstraintTypes.HARD_EXCLUDE;
592         }
593         if (pceHardConstraints.getExcludeCLLI().contains(pcenode.getSupClliNodeId())) {
594             LOG.debug("validateNodeConstraints: {}", pcenode.getNodeId().getValue());
595             return ConstraintTypes.HARD_EXCLUDE;
596         }
597         return ConstraintTypes.NONE;
598     }
599
600     private ConstraintTypes validateLinkConstraints(PceLink link) {
601         if (pceHardConstraints.getExcludeSRLG().isEmpty()) {
602             return ConstraintTypes.NONE;
603         }
604
605         // for now SRLG is the only constraint for link
606         if (link.getlinkType() != OpenroadmLinkType.ROADMTOROADM) {
607             return ConstraintTypes.NONE;
608         }
609
610         List<Long> constraints = new ArrayList<>(pceHardConstraints.getExcludeSRLG());
611         constraints.retainAll(link.getsrlgList());
612         if (!constraints.isEmpty()) {
613             LOG.debug("validateLinkConstraints: {}", link.getLinkId().getValue());
614             return ConstraintTypes.HARD_EXCLUDE;
615         }
616
617         return ConstraintTypes.NONE;
618     }
619
620     private void dropOppositeLink(Link link) {
621         LinkId opplink = MapUtils.extractOppositeLink(link);
622
623         if (allPceLinks.containsKey(opplink)) {
624             allPceLinks.remove(opplink);
625         } else {
626             linksToExclude.add(opplink);
627         }
628     }
629
630     private Boolean endPceNode(OpenroadmNodeType openroadmNodeType, NodeId nodeId, PceOpticalNode pceNode) {
631         switch (openroadmNodeType) {
632             case SRG:
633                 pceNode.initSrgTps();
634                 this.azSrgs.add(nodeId);
635                 break;
636             case XPONDER:
637                 pceNode.initXndrTps(input.getServiceAEnd().getServiceFormat());
638                 break;
639             default:
640                 LOG.debug("endPceNode: Node {} is not SRG or XPONDER !", nodeId);
641                 return false;
642         }
643
644         if (!pceNode.isValid()) {
645             LOG.error("validateNode : there are no available frequencies in node {}", pceNode.getNodeId().getValue());
646             return false;
647         }
648         return true;
649     }
650
651     private boolean processPceLink(Link link, NodeId sourceId, NodeId destId, PceNode source, PceNode dest) {
652         PceLink pcelink = new PceLink(link, source, dest);
653         if (!pcelink.isValid()) {
654             dropOppositeLink(link);
655             LOG.error(" validateLink: Link is ignored due errors in network data or in opposite link");
656             return false;
657         }
658         LinkId linkId = pcelink.getLinkId();
659         if (validateLinkConstraints(pcelink).equals(ConstraintTypes.HARD_EXCLUDE)) {
660             dropOppositeLink(link);
661             LOG.debug("validateLink: constraints : link is ignored == {}", linkId.getValue());
662             return false;
663         }
664         switch (pcelink.getlinkType()) {
665             case ROADMTOROADM:
666             case EXPRESSLINK:
667                 allPceLinks.put(linkId, pcelink);
668                 source.addOutgoingLink(pcelink);
669                 LOG.debug("validateLink: {}-LINK added to allPceLinks {}",
670                     pcelink.getlinkType(), pcelink);
671                 break;
672             case ADDLINK:
673                 pcelink.setClient(
674                     source.getRdmSrgClient(pcelink.getSourceTP().getValue(), StringConstants.SERVICE_DIRECTION_AZ));
675                 addLinks.add(pcelink);
676                 LOG.debug("validateLink: ADD-LINK saved  {}", pcelink);
677                 break;
678             case DROPLINK:
679                 pcelink.setClient(
680                     dest.getRdmSrgClient(pcelink.getDestTP().getValue(), StringConstants.SERVICE_DIRECTION_ZA));
681                 dropLinks.add(pcelink);
682                 LOG.debug("validateLink: DROP-LINK saved  {}", pcelink);
683                 break;
684             case XPONDERINPUT:
685                 // store separately all SRG links directly
686                 azSrgs.add(sourceId);
687                 // connected to A/Z
688                 if (!dest.checkTP(pcelink.getDestTP().getValue())) {
689                     LOG.debug(
690                         "validateLink: XPONDER-INPUT is rejected as NW port is busy - {} ", pcelink);
691                     return false;
692                 }
693                 if (dest.getXpdrClient(pcelink.getDestTP().getValue()) != null) {
694                     pcelink.setClient(dest.getXpdrClient(pcelink.getDestTP().getValue()));
695                 }
696                 allPceLinks.put(linkId, pcelink);
697                 source.addOutgoingLink(pcelink);
698                 LOG.debug("validateLink: XPONDER-INPUT link added to allPceLinks {}", pcelink);
699                 break;
700             // does it mean XPONDER==>>SRG ?
701             case XPONDEROUTPUT:
702                 // store separately all SRG links directly
703                 azSrgs.add(destId);
704                 // connected to A/Z
705                 if (!source.checkTP(pcelink.getSourceTP().getValue())) {
706                     LOG.debug(
707                         "validateLink: XPONDER-OUTPUT is rejected as NW port is busy - {} ", pcelink);
708                     return false;
709                 }
710                 if (source.getXpdrClient(pcelink.getSourceTP().getValue()) != null) {
711                     pcelink.setClient(source.getXpdrClient(pcelink.getSourceTP().getValue()));
712                 }
713                 allPceLinks.put(linkId, pcelink);
714                 source.addOutgoingLink(pcelink);
715                 LOG.debug("validateLink: XPONDER-OUTPUT link added to allPceLinks {}", pcelink);
716                 break;
717             default:
718                 LOG.warn("validateLink: link type is not supported {}", pcelink);
719         }
720         return true;
721     }
722
723     private boolean processPceOtnLink(Link link, PceNode source, PceNode dest) {
724         PceLink pceOtnLink = new PceLink(link, source, dest);
725
726         if (!pceOtnLink.isOtnValid(link, serviceType)) {
727             dropOppositeLink(link);
728             LOG.error(" validateLink: Link is ignored due errors in network data or in opposite link");
729             return false;
730         }
731
732         LinkId linkId = pceOtnLink.getLinkId();
733         if (validateLinkConstraints(pceOtnLink).equals(ConstraintTypes.HARD_EXCLUDE)) {
734             dropOppositeLink(link);
735             LOG.debug("validateLink: constraints : link is ignored == {}", linkId.getValue());
736             return false;
737         }
738
739         switch (pceOtnLink.getlinkType()) {
740             case OTNLINK:
741                 if (source.getXpdrClient(pceOtnLink.getSourceTP().getValue()) != null) {
742                     pceOtnLink.setClient(source.getXpdrClient(pceOtnLink.getSourceTP().getValue()));
743                 }
744                 if (dest.getXpdrClient(pceOtnLink.getDestTP().getValue()) != null) {
745                     pceOtnLink.setClient(dest.getXpdrClient(pceOtnLink.getDestTP().getValue()));
746                 }
747                 allPceLinks.put(linkId, pceOtnLink);
748                 source.addOutgoingLink(pceOtnLink);
749                 LOG.debug("validateLink: OTN-LINK added to allPceLinks {}", pceOtnLink);
750                 break;
751             default:
752                 LOG.warn("validateLink: link type is not supported {}", pceOtnLink);
753         }
754         return true;
755     }
756
757     public PceNode getaendPceNode() {
758         return aendPceNode;
759     }
760
761     public PceNode getzendPceNode() {
762         return zendPceNode;
763     }
764
765     public Map<NodeId, PceNode> getAllPceNodes() {
766         return this.allPceNodes;
767     }
768
769     public Map<LinkId, PceLink> getAllPceLinks() {
770         return this.allPceLinks;
771     }
772
773     public String getServiceType() {
774         return serviceType;
775     }
776
777     public PceResult getReturnStructure() {
778         return returnStructure;
779     }
780
781     private static void printNodesInfo(Map<NodeId, PceNode> allPceNodes) {
782         allPceNodes.forEach(((nodeId, pceNode) -> {
783             LOG.debug("In printNodes in node {} : outgoing links {} ", pceNode.getNodeId().getValue(),
784                     pceNode.getOutgoingLinks());
785         }));
786     }
787
788     /**
789      * Get mc capability slot width granularity for device.
790      * @param deviceNodeId String
791      * @param nodeId NodeId
792      * @return slot width granularity
793      */
794     private BigDecimal getSlotWidthGranularity(String deviceNodeId, NodeId nodeId) {
795         // nodeId: openroadm-topology level node
796         // deviceNodeId: openroadm-network level node
797         List<McCapabilities> mcCapabilities = mappingUtils.getMcCapabilitiesForNode(deviceNodeId);
798         String[] params = nodeId.getValue().split("-");
799         // DEGx or SRGx or XPDRx
800         String moduleName = params[params.length - 1];
801         for (McCapabilities mcCapabitility : mcCapabilities) {
802             if (mcCapabitility.getMcNodeName().contains("XPDR")
803                     && mcCapabitility.getSlotWidthGranularity() != null) {
804                 return mcCapabitility.getSlotWidthGranularity().getValue().decimalValue();
805             }
806             if (mcCapabitility.getMcNodeName().contains(moduleName)
807                     && mcCapabitility.getSlotWidthGranularity() != null) {
808                 return mcCapabitility.getSlotWidthGranularity().getValue().decimalValue();
809             }
810         }
811         return GridConstant.SLOT_WIDTH_50;
812     }
813
814     /**
815      * Get mc capability central-width granularity for device.
816      * @param deviceNodeId String
817      * @param nodeId NodeId
818      * @return center-freq granularity
819      */
820     private BigDecimal getCentralFreqGranularity(String deviceNodeId, NodeId nodeId) {
821         // nodeId: openroadm-topology level node
822         // deviceNodeId: openroadm-network level node
823         List<McCapabilities> mcCapabilities = mappingUtils.getMcCapabilitiesForNode(deviceNodeId);
824         String[] params = nodeId.getValue().split("-");
825         // DEGx or SRGx or XPDRx
826         String moduleName = params[params.length - 1];
827         for (McCapabilities mcCapabitility : mcCapabilities) {
828             if (mcCapabitility.getMcNodeName().contains("XPDR")
829                     && mcCapabitility.getCenterFreqGranularity() != null) {
830                 return mcCapabitility.getCenterFreqGranularity().getValue().decimalValue();
831             }
832             if (mcCapabitility.getMcNodeName().contains(moduleName)
833                     && mcCapabitility.getCenterFreqGranularity() != null) {
834                 return mcCapabitility.getCenterFreqGranularity().getValue().decimalValue();
835             }
836         }
837         return GridConstant.SLOT_WIDTH_50;
838     }
839 }