8bac0a20993faf532ceebdb35c40dbe6977033c3
[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.rev240205.PathComputationRequestInput;
33 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev240205.path.computation.reroute.request.input.Endpoints;
34 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev231221.mc.capabilities.McCapabilities;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev230526.Link1;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev230526.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.rev230526.OpenroadmLinkType;
40 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev230526.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.orElseThrow();
277                 LOG.debug("readMdSal: network nodes: nwOptional.isPresent = true {}", nw);
278             }
279         } catch (InterruptedException | ExecutionException e) {
280             LOG.error("readMdSal: Error reading topology {}", nwInstanceIdentifier);
281             returnStructure.setRC(ResponseCodes.RESPONSE_FAILED);
282         }
283         return nw;
284     }
285
286     private boolean analyzeNw() {
287
288         LOG.debug("analyzeNw: allNodes size {}, allLinks size {}", allNodes.size(), allLinks.size());
289         switch (serviceType) {
290             case StringConstants.SERVICE_TYPE_100GE_T:
291             case  StringConstants.SERVICE_TYPE_OTU4:
292             case  StringConstants.SERVICE_TYPE_400GE:
293             case StringConstants.SERVICE_TYPE_OTUC2:
294             case StringConstants.SERVICE_TYPE_OTUC3:
295             case  StringConstants.SERVICE_TYPE_OTUC4:
296                 // 100GE service and OTU4 service are handled at the openroadm-topology layer
297                 for (Node node : allNodes) {
298                     validateNode(node);
299                 }
300
301                 LOG.debug("analyzeNw: allPceNodes size {}", allPceNodes.size());
302
303                 if (aendPceNode == null || zendPceNode == null) {
304                     LOG.error("analyzeNw: Error in reading nodes: A or Z do not present in the network");
305                     return false;
306                 }
307                 for (Link link : allLinks) {
308                     validateLink(link);
309                 }
310                 // debug prints
311                 LOG.debug("analyzeNw: addLinks size {}, dropLinks size {}", addLinks.size(), dropLinks.size());
312                 // debug prints
313                 LOG.debug("analyzeNw: azSrgs size = {}", azSrgs.size());
314                 for (NodeId srg : azSrgs) {
315                     LOG.debug("analyzeNw: A/Z Srgs SRG = {}", srg.getValue());
316                 }
317                 // debug prints
318                 for (PceLink link : addLinks) {
319                     filteraddLinks(link);
320                 }
321                 for (PceLink link : dropLinks) {
322                     filterdropLinks(link);
323                 }
324                 break;
325
326             default:
327                 // ODU4, 10GE/ODU2e or 1GE/ODU0 services are handled at openroadm-otn layer
328
329                 for (Node node : allNodes) {
330                     validateOtnNode(node);
331                 }
332
333                 LOG.info("analyzeNw: allPceNodes {}", allPceNodes);
334
335                 if (aendPceNode == null || zendPceNode == null) {
336                     LOG.error("analyzeNw: Error in reading nodes: A or Z do not present in the network");
337                     return false;
338                 }
339                 for (Link link : allLinks) {
340                     validateLink(link);
341                 }
342                 break;
343         }
344
345         LOG.info("analyzeNw: allPceNodes size {}, allPceLinks size {}", allPceNodes.size(), allPceLinks.size());
346
347         if ((allPceNodes.size() == 0) || (allPceLinks.size() == 0)) {
348             return false;
349         }
350
351         LOG.debug("analyzeNw: allPceNodes {}", allPceNodes);
352         LOG.debug("analyzeNw: allPceLinks {}", allPceLinks);
353
354         return true;
355     }
356
357     private boolean filteraddLinks(PceLink pcelink) {
358
359         NodeId nodeId = pcelink.getSourceId();
360
361         if (azSrgs.contains(nodeId)) {
362             allPceLinks.put(pcelink.getLinkId(), pcelink);
363             allPceNodes.get(nodeId).addOutgoingLink(pcelink);
364             LOG.debug("analyzeNw: Add_LINK added to source and to allPceLinks {}", pcelink.getLinkId());
365             return true;
366         }
367
368         // remove the SRG from PceNodes, as it is not directly connected to A/Z
369         allPceNodes.remove(nodeId);
370         LOG.debug("analyzeNw: SRG removed {}", nodeId.getValue());
371
372         return false;
373     }
374
375     private boolean filterdropLinks(PceLink pcelink) {
376
377         NodeId nodeId = pcelink.getDestId();
378
379         if (azSrgs.contains(nodeId)) {
380             allPceLinks.put(pcelink.getLinkId(), pcelink);
381             allPceNodes.get(nodeId).addOutgoingLink(pcelink);
382             LOG.debug("analyzeNw: Drop_LINK added to dest and to allPceLinks {}", pcelink.getLinkId());
383             return true;
384         }
385
386         // remove the SRG from PceNodes, as it is not directly connected to A/Z
387         allPceNodes.remove(pcelink.getDestId());
388         LOG.debug("analyzeNw: SRG removed {}", nodeId.getValue());
389
390         return false;
391     }
392
393     private boolean validateLink(Link link) {
394         LOG.debug("validateLink: link {} ", link);
395
396         NodeId sourceId = link.getSource().getSourceNode();
397         NodeId destId = link.getDestination().getDestNode();
398         PceNode source = allPceNodes.get(sourceId);
399         PceNode dest = allPceNodes.get(destId);
400         State state = link.augmentation(Link1.class).getOperationalState();
401
402         if (source == null) {
403             LOG.debug("validateLink: Link is ignored due source node is rejected by node validation - {}",
404                 link.getSource().getSourceNode().getValue());
405             return false;
406         }
407         if (dest == null) {
408             LOG.debug("validateLink: Link is ignored due dest node is rejected by node validation - {}",
409                 link.getDestination().getDestNode().getValue());
410             return false;
411         }
412
413         if (State.OutOfService.equals(state)) {
414             LOG.debug("validateLink: Link is ignored due operational state - {}",
415                     state.getName());
416             return false;
417         }
418
419         switch (serviceType) {
420             case StringConstants.SERVICE_TYPE_100GE_T:
421             case StringConstants.SERVICE_TYPE_OTU4:
422             case StringConstants.SERVICE_TYPE_OTUC2:
423             case StringConstants.SERVICE_TYPE_OTUC3:
424             case StringConstants.SERVICE_TYPE_OTUC4:
425             case StringConstants.SERVICE_TYPE_400GE:
426                 return processPceLink(link, sourceId, destId, source, dest);
427             case StringConstants.SERVICE_TYPE_ODU4:
428             case StringConstants.SERVICE_TYPE_10GE:
429             case StringConstants.SERVICE_TYPE_100GE_M:
430             case StringConstants.SERVICE_TYPE_100GE_S:
431             case StringConstants.SERVICE_TYPE_ODUC2:
432             case StringConstants.SERVICE_TYPE_ODUC3:
433             case StringConstants.SERVICE_TYPE_ODUC4:
434             case StringConstants.SERVICE_TYPE_1GE:
435                 return processPceOtnLink(link, source, dest);
436             default:
437                 LOG.error(" validateLink: Unmanaged service type {}", serviceType);
438                 return false;
439         }
440     }
441
442     private void validateNode(Node node) {
443         LOG.debug("validateNode: node {} ", node);
444         // PceNode will be used in Graph algorithm
445         Node1 node1 = node.augmentation(Node1.class);
446         if (node1 == null) {
447             LOG.error("getNodeType: no Node1 (type) Augmentation for node: [{}]. Node is ignored", node.getNodeId());
448             return;
449         }
450         if (State.OutOfService.equals(node1.getOperationalState())) {
451             LOG.error("getNodeType: node is ignored due to operational state - {}", node1.getOperationalState()
452                     .getName());
453             return;
454         }
455         OpenroadmNodeType nodeType = node1.getNodeType();
456         String deviceNodeId = MapUtils.getSupNetworkNode(node);
457         // Should never happen but because of existing topology test files
458         // we have to manage this case
459         if (deviceNodeId == null || deviceNodeId.isBlank()) {
460             deviceNodeId = node.getNodeId().getValue();
461         }
462
463         LOG.debug("Device node id {} for {}", deviceNodeId, node);
464         PceOpticalNode pceNode = new PceOpticalNode(deviceNodeId, this.serviceType, portMapping, node, nodeType,
465             mappingUtils.getOpenRoadmVersion(deviceNodeId), getSlotWidthGranularity(deviceNodeId, node.getNodeId()),
466             getCentralFreqGranularity(deviceNodeId, node.getNodeId()));
467         if (endpoints != null) {
468             pceNode.setEndpoints(endpoints);
469         }
470         pceNode.validateAZxponder(anodeId, znodeId, input.getServiceAEnd().getServiceFormat());
471         pceNode.initFrequenciesBitSet();
472
473         if (!pceNode.isValid()) {
474             LOG.debug(" validateNode: Node {} is ignored", node.getNodeId().getValue());
475             return;
476         }
477         if (validateNodeConstraints(pceNode).equals(ConstraintTypes.HARD_EXCLUDE)) {
478             return;
479         }
480
481         if (endPceNode(nodeType, pceNode.getNodeId(), pceNode)) {
482             if (this.aendPceNode == null && isAZendPceNode(this.serviceFormatA, pceNode, anodeId, "A")) {
483                 // Added to ensure A-node has a addlink in the topology
484                 List<Link> links = this.allLinks.stream()
485                     .filter(x -> x.getSource().getSourceNode().getValue().contains(pceNode.getNodeId().getValue()))
486                     .collect(Collectors.toList());
487                 if (!links.isEmpty()) {
488                     this.aendPceNode = pceNode;
489                 }
490             }
491             if (this.zendPceNode == null && isAZendPceNode(this.serviceFormatZ, pceNode, znodeId, "Z")) {
492                 // Added to ensure Z-node has a droplink in the topology
493                 List<Link> links = this.allLinks.stream()
494                     .filter(x -> x.getDestination().getDestNode().getValue().contains(pceNode.getNodeId().getValue()))
495                     .collect(Collectors.toList());
496                 if (!links.isEmpty()) {
497                     this.zendPceNode = pceNode;
498                 }
499             }
500         }
501
502         allPceNodes.put(pceNode.getNodeId(), pceNode);
503         LOG.debug("validateNode: node is saved {}", pceNode.getNodeId().getValue());
504     }
505
506     @SuppressWarnings("fallthrough")
507     @SuppressFBWarnings(
508         value = "SF_SWITCH_FALLTHROUGH",
509         justification = "intentional fallthrough")
510     private boolean isAZendPceNode(String serviceFormat, PceOpticalNode pceNode, String azNodeId, String azEndPoint) {
511         switch (serviceFormat) {
512             case "Ethernet":
513             case "OC":
514                 if (pceNode.getSupNetworkNodeId().equals(azNodeId)) {
515                     return true;
516                 }
517             //fallthrough
518             case "OTU":
519                 switch (azEndPoint) {
520                     case "A":
521                         return checkAendInputTxPortDeviceName()
522                             && pceNode.getNodeId().getValue()
523                                 .equals(this.input.getServiceAEnd().getTxDirection().getPort().getPortDeviceName());
524                     case "Z":
525                         return checkZendInputTxPortDeviceName()
526                             && pceNode.getNodeId().getValue()
527                                 .equals(this.input.getServiceZEnd().getTxDirection().getPort().getPortDeviceName());
528                     default:
529                         return false;
530                 }
531             default:
532                 LOG.debug("Unsupported service Format {} for node {}", serviceFormat, pceNode.getNodeId().getValue());
533                 return false;
534         }
535     }
536
537     private void validateOtnNode(Node node) {
538         LOG.info("validateOtnNode: {} ", node.getNodeId().getValue());
539         // PceOtnNode will be used in Graph algorithm
540         if (node.augmentation(Node1.class) == null) {
541             LOG.error("ValidateOtnNode: no node-type augmentation. Node {} is ignored", node.getNodeId().getValue());
542             return;
543         }
544
545         OpenroadmNodeType nodeType = node.augmentation(Node1.class).getNodeType();
546         String clientPort = null;
547         if (node.getNodeId().getValue().equals(anodeId)
548                 && this.aendPceNode == null
549                 && input.getServiceAEnd() != null
550                 && input.getServiceAEnd().getRxDirection() != null
551                 && input.getServiceAEnd().getRxDirection().getPort() != null
552                 && input.getServiceAEnd().getRxDirection().getPort().getPortName() != null) {
553             clientPort = input.getServiceAEnd().getRxDirection().getPort().getPortName();
554         } else if (node.getNodeId().getValue().equals(znodeId)
555                 && this.zendPceNode == null
556                 && input.getServiceZEnd() != null
557                 && input.getServiceZEnd().getRxDirection() != null
558                 && input.getServiceZEnd().getRxDirection().getPort() != null
559                 && input.getServiceZEnd().getRxDirection().getPort().getPortName() != null) {
560             clientPort = input.getServiceZEnd().getRxDirection().getPort().getPortName();
561         }
562
563         PceOtnNode pceOtnNode = new PceOtnNode(node, nodeType, node.getNodeId(), "otn", serviceType, clientPort);
564         pceOtnNode.validateXponder(anodeId, znodeId);
565
566         if (!pceOtnNode.isValid()) {
567             LOG.warn(" validateOtnNode: Node {} is ignored", node.getNodeId().getValue());
568             return;
569         }
570         if (validateNodeConstraints(pceOtnNode).equals(ConstraintTypes.HARD_EXCLUDE)) {
571             return;
572         }
573         if (pceOtnNode.getNodeId().getValue().equals(anodeId) && this.aendPceNode == null) {
574             this.aendPceNode = pceOtnNode;
575         }
576         if (pceOtnNode.getNodeId().getValue().equals(znodeId) && this.zendPceNode == null) {
577             this.zendPceNode = pceOtnNode;
578         }
579         allPceNodes.put(pceOtnNode.getNodeId(), pceOtnNode);
580         LOG.info("validateOtnNode: node {} is saved", node.getNodeId().getValue());
581     }
582
583     private ConstraintTypes validateNodeConstraints(PceNode pcenode) {
584         if (pceHardConstraints.getExcludeSupNodes().isEmpty() && pceHardConstraints.getExcludeCLLI().isEmpty()) {
585             return ConstraintTypes.NONE;
586         }
587         if (pceHardConstraints.getExcludeSupNodes().contains(pcenode.getSupNetworkNodeId())) {
588             LOG.debug("validateNodeConstraints: {}", pcenode.getNodeId().getValue());
589             return ConstraintTypes.HARD_EXCLUDE;
590         }
591         if (pceHardConstraints.getExcludeCLLI().contains(pcenode.getSupClliNodeId())) {
592             LOG.debug("validateNodeConstraints: {}", pcenode.getNodeId().getValue());
593             return ConstraintTypes.HARD_EXCLUDE;
594         }
595         return ConstraintTypes.NONE;
596     }
597
598     private ConstraintTypes validateLinkConstraints(PceLink link) {
599         if (pceHardConstraints.getExcludeSRLG().isEmpty()) {
600             return ConstraintTypes.NONE;
601         }
602
603         // for now SRLG is the only constraint for link
604         if (link.getlinkType() != OpenroadmLinkType.ROADMTOROADM) {
605             return ConstraintTypes.NONE;
606         }
607
608         List<Long> constraints = new ArrayList<>(pceHardConstraints.getExcludeSRLG());
609         constraints.retainAll(link.getsrlgList());
610         if (!constraints.isEmpty()) {
611             LOG.debug("validateLinkConstraints: {}", link.getLinkId().getValue());
612             return ConstraintTypes.HARD_EXCLUDE;
613         }
614
615         return ConstraintTypes.NONE;
616     }
617
618     private void dropOppositeLink(Link link) {
619         LinkId opplink = MapUtils.extractOppositeLink(link);
620
621         if (allPceLinks.containsKey(opplink)) {
622             allPceLinks.remove(opplink);
623         } else {
624             linksToExclude.add(opplink);
625         }
626     }
627
628     private Boolean endPceNode(OpenroadmNodeType openroadmNodeType, NodeId nodeId, PceOpticalNode pceNode) {
629         switch (openroadmNodeType) {
630             case SRG:
631                 pceNode.initSrgTps();
632                 this.azSrgs.add(nodeId);
633                 break;
634             case XPONDER:
635                 pceNode.initXndrTps(input.getServiceAEnd().getServiceFormat());
636                 break;
637             default:
638                 LOG.debug("endPceNode: Node {} is not SRG or XPONDER !", nodeId);
639                 return false;
640         }
641
642         if (!pceNode.isValid()) {
643             LOG.error("validateNode : there are no available frequencies in node {}", pceNode.getNodeId().getValue());
644             return false;
645         }
646         return true;
647     }
648
649     private boolean processPceLink(Link link, NodeId sourceId, NodeId destId, PceNode source, PceNode dest) {
650         PceLink pcelink = new PceLink(link, source, dest);
651         if (!pcelink.isValid()) {
652             dropOppositeLink(link);
653             LOG.error(" validateLink: Link is ignored due errors in network data or in opposite link");
654             return false;
655         }
656         LinkId linkId = pcelink.getLinkId();
657         if (validateLinkConstraints(pcelink).equals(ConstraintTypes.HARD_EXCLUDE)) {
658             dropOppositeLink(link);
659             LOG.debug("validateLink: constraints : link is ignored == {}", linkId.getValue());
660             return false;
661         }
662         switch (pcelink.getlinkType()) {
663             case ROADMTOROADM:
664             case EXPRESSLINK:
665                 allPceLinks.put(linkId, pcelink);
666                 source.addOutgoingLink(pcelink);
667                 LOG.debug("validateLink: {}-LINK added to allPceLinks {}",
668                     pcelink.getlinkType(), pcelink);
669                 break;
670             case ADDLINK:
671                 pcelink.setClientA(
672                     source.getRdmSrgClient(pcelink.getSourceTP().getValue(), StringConstants.SERVICE_DIRECTION_AZ));
673                 addLinks.add(pcelink);
674                 LOG.debug("validateLink: ADD-LINK saved  {}", pcelink);
675                 break;
676             case DROPLINK:
677                 pcelink.setClientZ(
678                     dest.getRdmSrgClient(pcelink.getDestTP().getValue(), StringConstants.SERVICE_DIRECTION_ZA));
679                 dropLinks.add(pcelink);
680                 LOG.debug("validateLink: DROP-LINK saved  {}", pcelink);
681                 break;
682             case XPONDERINPUT:
683                 // store separately all SRG links directly
684                 azSrgs.add(sourceId);
685                 // connected to A/Z
686                 if (!dest.checkTP(pcelink.getDestTP().getValue())) {
687                     LOG.debug(
688                         "validateLink: XPONDER-INPUT is rejected as NW port is busy - {} ", pcelink);
689                     return false;
690                 }
691                 if (dest.getXpdrClient(pcelink.getDestTP().getValue()) != null) {
692                     pcelink.setClientZ(dest.getXpdrClient(pcelink.getDestTP().getValue()));
693                 }
694                 allPceLinks.put(linkId, pcelink);
695                 source.addOutgoingLink(pcelink);
696                 LOG.debug("validateLink: XPONDER-INPUT link added to allPceLinks {}", pcelink);
697                 break;
698             // does it mean XPONDER==>>SRG ?
699             case XPONDEROUTPUT:
700                 // store separately all SRG links directly
701                 azSrgs.add(destId);
702                 // connected to A/Z
703                 if (!source.checkTP(pcelink.getSourceTP().getValue())) {
704                     LOG.debug(
705                         "validateLink: XPONDER-OUTPUT is rejected as NW port is busy - {} ", pcelink);
706                     return false;
707                 }
708                 if (source.getXpdrClient(pcelink.getSourceTP().getValue()) != null) {
709                     pcelink.setClientA(source.getXpdrClient(pcelink.getSourceTP().getValue()));
710                 }
711                 allPceLinks.put(linkId, pcelink);
712                 source.addOutgoingLink(pcelink);
713                 LOG.debug("validateLink: XPONDER-OUTPUT link added to allPceLinks {}", pcelink);
714                 break;
715             default:
716                 LOG.warn("validateLink: link type is not supported {}", pcelink);
717         }
718         return true;
719     }
720
721     private boolean processPceOtnLink(Link link, PceNode source, PceNode dest) {
722         PceLink pceOtnLink = new PceLink(link, source, dest);
723
724         if (!pceOtnLink.isOtnValid(link, serviceType)) {
725             dropOppositeLink(link);
726             LOG.error(" validateLink: Link is ignored due errors in network data or in opposite link");
727             return false;
728         }
729
730         LinkId linkId = pceOtnLink.getLinkId();
731         if (validateLinkConstraints(pceOtnLink).equals(ConstraintTypes.HARD_EXCLUDE)) {
732             dropOppositeLink(link);
733             LOG.debug("validateLink: constraints : link is ignored == {}", linkId.getValue());
734             return false;
735         }
736
737         switch (pceOtnLink.getlinkType()) {
738             case OTNLINK:
739                 if (source.getXpdrClient(pceOtnLink.getSourceTP().getValue()) != null) {
740                     pceOtnLink.setClientA(source.getXpdrClient(pceOtnLink.getSourceTP().getValue()));
741                 }
742                 if (dest.getXpdrClient(pceOtnLink.getDestTP().getValue()) != null) {
743                     pceOtnLink.setClientZ(dest.getXpdrClient(pceOtnLink.getDestTP().getValue()));
744                 }
745                 allPceLinks.put(linkId, pceOtnLink);
746                 source.addOutgoingLink(pceOtnLink);
747                 LOG.debug("validateLink: OTN-LINK added to allPceLinks {}", pceOtnLink);
748                 break;
749             default:
750                 LOG.warn("validateLink: link type is not supported {}", pceOtnLink);
751         }
752         return true;
753     }
754
755     public PceNode getaendPceNode() {
756         return aendPceNode;
757     }
758
759     public PceNode getzendPceNode() {
760         return zendPceNode;
761     }
762
763     public Map<NodeId, PceNode> getAllPceNodes() {
764         return this.allPceNodes;
765     }
766
767     public Map<LinkId, PceLink> getAllPceLinks() {
768         return this.allPceLinks;
769     }
770
771     public String getServiceType() {
772         return serviceType;
773     }
774
775     public PceResult getReturnStructure() {
776         return returnStructure;
777     }
778
779     private static void printNodesInfo(Map<NodeId, PceNode> allPceNodes) {
780         allPceNodes.forEach(((nodeId, pceNode) -> {
781             LOG.debug("In printNodes in node {} : outgoing links {} ", pceNode.getNodeId().getValue(),
782                     pceNode.getOutgoingLinks());
783         }));
784     }
785
786     /**
787      * Get mc capability slot width granularity for device.
788      * @param deviceNodeId String
789      * @param nodeId NodeId
790      * @return slot width granularity
791      */
792     private BigDecimal getSlotWidthGranularity(String deviceNodeId, NodeId nodeId) {
793         // nodeId: openroadm-topology level node
794         // deviceNodeId: openroadm-network level node
795         List<McCapabilities> mcCapabilities = mappingUtils.getMcCapabilitiesForNode(deviceNodeId);
796         String[] params = nodeId.getValue().split("-");
797         // DEGx or SRGx or XPDRx
798         String moduleName = params[params.length - 1];
799         for (McCapabilities mcCapabitility : mcCapabilities) {
800             if (mcCapabitility.getMcNodeName().contains("XPDR")
801                     && mcCapabitility.getSlotWidthGranularity() != null) {
802                 return mcCapabitility.getSlotWidthGranularity().getValue().decimalValue();
803             }
804             if (mcCapabitility.getMcNodeName().contains(moduleName)
805                     && mcCapabitility.getSlotWidthGranularity() != null) {
806                 return mcCapabitility.getSlotWidthGranularity().getValue().decimalValue();
807             }
808         }
809         return GridConstant.SLOT_WIDTH_50;
810     }
811
812     /**
813      * Get mc capability central-width granularity for device.
814      * @param deviceNodeId String
815      * @param nodeId NodeId
816      * @return center-freq granularity
817      */
818     private BigDecimal getCentralFreqGranularity(String deviceNodeId, NodeId nodeId) {
819         // nodeId: openroadm-topology level node
820         // deviceNodeId: openroadm-network level node
821         List<McCapabilities> mcCapabilities = mappingUtils.getMcCapabilitiesForNode(deviceNodeId);
822         String[] params = nodeId.getValue().split("-");
823         // DEGx or SRGx or XPDRx
824         String moduleName = params[params.length - 1];
825         for (McCapabilities mcCapabitility : mcCapabilities) {
826             if (mcCapabitility.getMcNodeName().contains("XPDR")
827                     && mcCapabitility.getCenterFreqGranularity() != null) {
828                 return mcCapabitility.getCenterFreqGranularity().getValue().decimalValue();
829             }
830             if (mcCapabitility.getMcNodeName().contains(moduleName)
831                     && mcCapabitility.getCenterFreqGranularity() != null) {
832                 return mcCapabitility.getCenterFreqGranularity().getValue().decimalValue();
833             }
834         }
835         return GridConstant.SLOT_WIDTH_50;
836     }
837 }