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