Refactor to remove useless wavelength reference
[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.network.NetworkTransactionService;
29 import org.opendaylight.transportpce.pce.constraints.PceConstraints;
30 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.PathComputationRequestInput;
31 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev201012.network.nodes.McCapabilities;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev200529.Node1;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev200529.OpenroadmLinkType;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev200529.OpenroadmNodeType;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NetworkId;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.Networks;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NodeId;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.Network;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.NetworkKey;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.network.Node;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.LinkId;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.Network1;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network.Link;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 public class PceCalculation {
49     /* Logging. */
50     private static final Logger LOG = LoggerFactory.getLogger(PceCalculation.class);
51     private NetworkTransactionService networkTransactionService = null;
52
53     ///////////// data parsed from Input/////////////////
54     private PathComputationRequestInput input;
55     private String anodeId = "";
56     private String znodeId = "";
57     private String serviceFormatA = "";
58     private String serviceFormatZ = "";
59     private String serviceType = "";
60     private Long serviceRate = 0L;
61
62     private PceConstraints pceHardConstraints;
63
64     ///////////// Intermediate data/////////////////
65     private List<PceLink> addLinks = new ArrayList<>();
66     private List<PceLink> dropLinks = new ArrayList<>();
67     private HashSet<NodeId> azSrgs = new HashSet<>();
68
69     private PceNode aendPceNode = null;
70     private PceNode zendPceNode = null;
71
72     private List<Link> allLinks = null;
73     private List<Node> allNodes = null;
74
75     // this List serves graph calculation
76     private Map<NodeId, PceNode> allPceNodes = new HashMap<>();
77     // this List serves calculation of ZtoA path description
78     // TODO maybe better solution is possible
79     private Map<LinkId, PceLink> allPceLinks = new HashMap<>();
80     private Set<LinkId> linksToExclude = new HashSet<>();
81     private PceResult returnStructure;
82
83     private enum ConstraintTypes {
84         NONE, HARD_EXCLUDE, HARD_INCLUDE, HARD_DIVERSITY, SOFT_EXCLUDE, SOFT_INCLUDE, SOFT_DIVERSITY;
85     }
86
87     private MappingUtils mappingUtils;
88
89     public PceCalculation(PathComputationRequestInput input, NetworkTransactionService networkTransactionService,
90             PceConstraints pceHardConstraints, PceConstraints pceSoftConstraints, PceResult rc) {
91         this.input = input;
92         this.networkTransactionService = networkTransactionService;
93         this.returnStructure = rc;
94
95         this.pceHardConstraints = pceHardConstraints;
96         this.mappingUtils = new MappingUtilsImpl(networkTransactionService.getDataBroker());
97         parseInput();
98     }
99
100     public void retrievePceNetwork() {
101
102         LOG.info("In PceCalculation retrieveNetwork: ");
103
104         if (!readMdSal()) {
105             returnStructure.setRC(ResponseCodes.RESPONSE_FAILED);
106             return;
107         }
108         MapUtils.mapDiversityConstraints(allNodes, allLinks, pceHardConstraints);
109
110         if (!analyzeNw()) {
111             returnStructure.setRC(ResponseCodes.RESPONSE_FAILED);
112             return;
113         }
114         printNodesInfo(allPceNodes);
115
116         returnStructure.setRC(ResponseCodes.RESPONSE_OK);
117         return;
118     }
119
120     private boolean parseInput() {
121         if (input.getServiceAEnd().getServiceFormat() == null || input.getServiceZEnd().getServiceFormat() == null
122             || input.getServiceAEnd().getServiceRate() == null) {
123             LOG.error("Service Format and Service Rate are required for a path calculation");
124             return false;
125         }
126         serviceFormatA = input.getServiceAEnd().getServiceFormat().getName();
127         serviceFormatZ = input.getServiceZEnd().getServiceFormat().getName();
128         serviceRate = input.getServiceAEnd().getServiceRate().toJava();
129
130         LOG.info("parseInput: A and Z :[{}] and [{}]", anodeId, znodeId);
131         if (!(serviceFormatA.equals(serviceFormatZ))) {
132             LOG.info("parseInput: different service format for A and Z not handled, will use service format from Aend");
133         } else if (serviceRate == 100L) {
134             switch (serviceFormatA) {
135                 case "Ethernet":
136                 case "OC":
137                     serviceType = StringConstants.SERVICE_TYPE_100GE;
138                     break;
139                 case "OTU":
140                     serviceType = StringConstants.SERVICE_TYPE_OTU4;
141                     break;
142                 case "ODU":
143                     serviceType = StringConstants.SERVICE_TYPE_ODU4;
144                     break;
145                 default:
146                     LOG.debug("parseInput: unsupported service type: Format {} Rate 100L", serviceFormatA);
147                     break;
148             }
149             //switch(serviceRate) may seem a better option at first glance.
150             //But switching on Long or long is not directly possible in Java.
151             //And casting to int bumps the limit here.
152             //Passing by ENUM or String are possible alternatives.
153             //Maybe HashMap and similar options should also be considered here.
154         } else if ("Ethernet".equals(serviceFormatA)) {
155         //only rate 100L is currently supported except in Ethernet
156             if (serviceRate == 10L) {
157                 serviceType = StringConstants.SERVICE_TYPE_10GE;
158             } else if (serviceRate == 1L) {
159                 serviceType = StringConstants.SERVICE_TYPE_1GE;
160             } else {
161                 LOG.debug("parseInput: unsupported service type: Format Ethernet Rate {}", serviceRate);
162             }
163         } else {
164             LOG.debug("parseInput: unsupported service type: Format {} Rate {}",
165                 serviceFormatA, serviceRate);
166         }
167         if (StringConstants.SERVICE_TYPE_ODU4.equals(serviceType)
168                 || StringConstants.SERVICE_TYPE_10GE.equals(serviceType)
169                 || StringConstants.SERVICE_TYPE_1GE.equals(serviceType)) {
170             anodeId = input.getServiceAEnd().getTxDirection().getPort().getPortDeviceName();
171             znodeId = input.getServiceZEnd().getTxDirection().getPort().getPortDeviceName();
172         } else {
173             anodeId = input.getServiceAEnd().getNodeId();
174             znodeId = input.getServiceZEnd().getNodeId();
175         }
176
177         returnStructure.setRate(input.getServiceAEnd().getServiceRate().toJava());
178         returnStructure.setServiceFormat(input.getServiceAEnd().getServiceFormat());
179         return true;
180     }
181
182     private boolean readMdSal() {
183         InstanceIdentifier<Network> nwInstanceIdentifier = null;
184         Network nw = null;
185         if (("OC".equals(serviceFormatA)) || ("OTU".equals(serviceFormatA)) || (("Ethernet".equals(serviceFormatA))
186             && (serviceRate == 100L))) {
187
188             LOG.info("readMdSal: network {}", NetworkUtils.OVERLAY_NETWORK_ID);
189             nwInstanceIdentifier = InstanceIdentifier.builder(Networks.class)
190                 .child(Network.class, new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID))).build();
191         } else if ("ODU".equals(serviceFormatA) || ("Ethernet".equals(serviceFormatA) && serviceRate == 10L)
192             || ("Ethernet".equals(serviceFormatA) && serviceRate == 1L)) {
193             LOG.info("readMdSal: network {}", NetworkUtils.OTN_NETWORK_ID);
194             nwInstanceIdentifier = InstanceIdentifier.builder(Networks.class)
195                 .child(Network.class, new NetworkKey(new NetworkId(NetworkUtils.OTN_NETWORK_ID))).build();
196         } else {
197             LOG.info("readMdSal: service-rate {} / service-format not handled {}", serviceRate, serviceFormatA);
198             return false;
199         }
200
201         try {
202             Optional<Network> nwOptional =
203                 networkTransactionService.read(LogicalDatastoreType.CONFIGURATION, nwInstanceIdentifier).get();
204             if (nwOptional.isPresent()) {
205                 nw = nwOptional.get();
206                 LOG.debug("readMdSal: network nodes: nwOptional.isPresent = true {}", nw);
207             }
208         } catch (InterruptedException | ExecutionException e) {
209             LOG.error("readMdSal: Error reading topology {}", nwInstanceIdentifier);
210             networkTransactionService.close();
211             returnStructure.setRC(ResponseCodes.RESPONSE_FAILED);
212             throw new RuntimeException(
213                 "readMdSal: Error reading from operational store, topology : " + nwInstanceIdentifier + " :" + e);
214         }
215         networkTransactionService.close();
216
217         if (nw == null) {
218             LOG.error("readMdSal: network is null: {}", nwInstanceIdentifier);
219             return false;
220         }
221         allNodes = nw.nonnullNode().values().stream().sorted((n1, n2)
222             -> n1.getNodeId().getValue().compareTo(n2.getNodeId().getValue())).collect(Collectors.toList());
223         Network1 nw1 = nw.augmentation(Network1.class);
224         if (nw1 != null) {
225             allLinks = nw1.nonnullLink().values().stream().sorted((l1, l2)
226                 -> l1.getSource().getSourceTp().toString().compareTo(l2.getSource().getSourceTp().toString()))
227                     .collect(Collectors.toList());
228         } else {
229             LOG.warn("no otn links in otn-topology");
230         }
231         if (allNodes == null || allNodes.isEmpty()) {
232             LOG.error("readMdSal: no nodes ");
233             return false;
234         }
235         LOG.info("readMdSal: network nodes: {} nodes added", allNodes.size());
236         LOG.debug("readMdSal: network nodes: {} nodes added", allNodes);
237
238         if (allLinks == null || allLinks.isEmpty()) {
239             LOG.error("readMdSal: no links ");
240             return false;
241         }
242         LOG.info("readMdSal: network links: {} links added", allLinks.size());
243         LOG.debug("readMdSal: network links: {} links added", allLinks);
244
245         return true;
246     }
247
248     private boolean analyzeNw() {
249
250         LOG.debug("analyzeNw: allNodes size {}, allLinks size {}", allNodes.size(), allLinks.size());
251
252         if ((StringConstants.SERVICE_TYPE_100GE.equals(serviceType))
253                 || (StringConstants.SERVICE_TYPE_OTU4.equals(serviceType))) {
254             // 100GE service and OTU4 service are handled at the openroadm-topology layer
255             for (Node node : allNodes) {
256                 validateNode(node);
257             }
258
259             LOG.debug("analyzeNw: allPceNodes size {}", allPceNodes.size());
260
261             if (aendPceNode == null || zendPceNode == null) {
262                 LOG.error("analyzeNw: Error in reading nodes: A or Z do not present in the network");
263                 return false;
264             }
265             for (Link link : allLinks) {
266                 validateLink(link);
267             }
268             // debug prints
269             LOG.debug("analyzeNw: addLinks size {}, dropLinks size {}", addLinks.size(), dropLinks.size());
270             // debug prints
271             LOG.debug("analyzeNw: azSrgs size = {}", azSrgs.size());
272             for (NodeId srg : azSrgs) {
273                 LOG.debug("analyzeNw: A/Z Srgs SRG = {}", srg.getValue());
274             }
275             // debug prints
276             for (PceLink link : addLinks) {
277                 filteraddLinks(link);
278             }
279             for (PceLink link : dropLinks) {
280                 filterdropLinks(link);
281             }
282
283         } else {
284             // ODU4, 10GE/ODU2e or 1GE/ODU0 services are handled at openroadm-otn layer
285
286             for (Node node : allNodes) {
287                 validateOtnNode(node);
288             }
289
290             LOG.info("analyzeNw: allPceNodes {}", allPceNodes);
291
292             if (aendPceNode == null || zendPceNode == null) {
293                 LOG.error("analyzeNw: Error in reading nodes: A or Z do not present in the network");
294                 return false;
295             }
296             for (Link link : allLinks) {
297                 validateLink(link);
298             }
299         }
300
301         LOG.info("analyzeNw: allPceNodes size {}, allPceLinks size {}", allPceNodes.size(), allPceLinks.size());
302
303         if ((allPceNodes.size() == 0) || (allPceLinks.size() == 0)) {
304             return false;
305         }
306
307         LOG.debug("analyzeNw: allPceNodes {}", allPceNodes);
308         LOG.debug("analyzeNw: allPceLinks {}", allPceLinks);
309
310         return true;
311     }
312
313     private boolean filteraddLinks(PceLink pcelink) {
314
315         NodeId nodeId = pcelink.getSourceId();
316
317         if (azSrgs.contains(nodeId)) {
318             allPceLinks.put(pcelink.getLinkId(), pcelink);
319             allPceNodes.get(nodeId).addOutgoingLink(pcelink);
320             LOG.debug("analyzeNw: Add_LINK added to source and to allPceLinks {}", pcelink.getLinkId());
321             return true;
322         }
323
324         // remove the SRG from PceNodes, as it is not directly connected to A/Z
325         allPceNodes.remove(nodeId);
326         LOG.debug("analyzeNw: SRG removed {}", nodeId.getValue());
327
328         return false;
329     }
330
331     private boolean filterdropLinks(PceLink pcelink) {
332
333         NodeId nodeId = pcelink.getDestId();
334
335         if (azSrgs.contains(nodeId)) {
336             allPceLinks.put(pcelink.getLinkId(), pcelink);
337             allPceNodes.get(nodeId).addOutgoingLink(pcelink);
338             LOG.debug("analyzeNw: Drop_LINK added to dest and to allPceLinks {}", pcelink.getLinkId());
339             return true;
340         }
341
342         // remove the SRG from PceNodes, as it is not directly connected to A/Z
343         allPceNodes.remove(pcelink.getDestId());
344         LOG.debug("analyzeNw: SRG removed {}", nodeId.getValue());
345
346         return false;
347     }
348
349     private boolean validateLink(Link link) {
350         LOG.info("validateLink: link {} ", link);
351
352         NodeId sourceId = link.getSource().getSourceNode();
353         NodeId destId = link.getDestination().getDestNode();
354         PceNode source = allPceNodes.get(sourceId);
355         PceNode dest = allPceNodes.get(destId);
356
357         if (source == null) {
358             LOG.debug("validateLink: Link is ignored due source node is rejected by node validation - {}",
359                 link.getSource().getSourceNode().getValue());
360             return false;
361         }
362         if (dest == null) {
363             LOG.debug("validateLink: Link is ignored due dest node is rejected by node validation - {}",
364                 link.getDestination().getDestNode().getValue());
365             return false;
366         }
367
368         if ((StringConstants.SERVICE_TYPE_100GE.equals(serviceType))
369                 || (StringConstants.SERVICE_TYPE_OTU4.equals(serviceType))) {
370             // 100GE or OTU4 services are handled at WDM Layer
371             PceLink pcelink = new PceLink(link, source, dest);
372             if (!pcelink.isValid()) {
373                 dropOppositeLink(link);
374                 LOG.error(" validateLink: Link is ignored due errors in network data or in opposite link");
375                 return false;
376             }
377             LinkId linkId = pcelink.getLinkId();
378             if (validateLinkConstraints(pcelink).equals(ConstraintTypes.HARD_EXCLUDE)) {
379                 dropOppositeLink(link);
380                 LOG.debug("validateLink: constraints : link is ignored == {}", linkId.getValue());
381                 return false;
382             }
383             switch (pcelink.getlinkType()) {
384                 case ROADMTOROADM:
385                 case EXPRESSLINK:
386                     allPceLinks.put(linkId, pcelink);
387                     source.addOutgoingLink(pcelink);
388                     LOG.debug("validateLink: {}-LINK added to allPceLinks {}",
389                         pcelink.getlinkType(), pcelink);
390                     break;
391                 case ADDLINK:
392                     pcelink.setClient(source.getRdmSrgClient(pcelink.getSourceTP().toString()));
393                     addLinks.add(pcelink);
394                     LOG.debug("validateLink: ADD-LINK saved  {}", pcelink);
395                     break;
396                 case DROPLINK:
397                     pcelink.setClient(dest.getRdmSrgClient(pcelink.getDestTP().toString()));
398                     dropLinks.add(pcelink);
399                     LOG.debug("validateLink: DROP-LINK saved  {}", pcelink);
400                     break;
401                 case XPONDERINPUT:
402                     // store separately all SRG links directly
403                     azSrgs.add(sourceId);
404                     // connected to A/Z
405                     if (!dest.checkTP(pcelink.getDestTP().toString())) {
406                         LOG.debug(
407                             "validateLink: XPONDER-INPUT is rejected as NW port is busy - {} ", pcelink);
408                         return false;
409                     }
410                     if (dest.getXpdrClient(pcelink.getDestTP().toString()) != null) {
411                         pcelink.setClient(dest.getXpdrClient(pcelink.getDestTP().toString()));
412                     }
413                     allPceLinks.put(linkId, pcelink);
414                     source.addOutgoingLink(pcelink);
415                     LOG.debug("validateLink: XPONDER-INPUT link added to allPceLinks {}", pcelink);
416                     break;
417                 // does it mean XPONDER==>>SRG ?
418                 case XPONDEROUTPUT:
419                     // store separately all SRG links directly
420                     azSrgs.add(destId);
421                     // connected to A/Z
422                     if (!source.checkTP(pcelink.getSourceTP().toString())) {
423                         LOG.debug(
424                             "validateLink: XPONDER-OUTPUT is rejected as NW port is busy - {} ", pcelink);
425                         return false;
426                     }
427                     if (source.getXpdrClient(pcelink.getSourceTP().toString()) != null) {
428                         pcelink.setClient(source.getXpdrClient(pcelink.getSourceTP().toString()));
429                     }
430                     allPceLinks.put(linkId, pcelink);
431                     source.addOutgoingLink(pcelink);
432                     LOG.debug("validateLink: XPONDER-OUTPUT link added to allPceLinks {}", pcelink);
433                     break;
434                 default:
435                     LOG.warn("validateLink: link type is not supported {}", pcelink);
436             }
437             return true;
438
439         } else if ((StringConstants.SERVICE_TYPE_ODU4.equals(serviceType))
440                 || (StringConstants.SERVICE_TYPE_10GE.equals(serviceType))
441                 || (StringConstants.SERVICE_TYPE_1GE.equals(serviceType))) {
442             // ODU4, 1GE and 10GE services relying on ODU2, ODU2e or ODU0 services are handled at OTN layer
443             PceLink pceOtnLink = new PceLink(link, source, dest);
444
445             if (!pceOtnLink.isOtnValid(link, serviceType)) {
446                 dropOppositeLink(link);
447                 LOG.error(" validateLink: Link is ignored due errors in network data or in opposite link");
448                 return false;
449             }
450
451             LinkId linkId = pceOtnLink.getLinkId();
452             if (validateLinkConstraints(pceOtnLink).equals(ConstraintTypes.HARD_EXCLUDE)) {
453                 dropOppositeLink(link);
454                 LOG.debug("validateLink: constraints : link is ignored == {}", linkId.getValue());
455                 return false;
456             }
457
458             switch (pceOtnLink.getlinkType()) {
459                 case OTNLINK:
460                     if (dest.getXpdrClient(pceOtnLink.getDestTP().toString()) != null) {
461                         pceOtnLink.setClient(dest.getXpdrClient(pceOtnLink.getDestTP().toString()));
462                     }
463
464                     allPceLinks.put(linkId, pceOtnLink);
465                     source.addOutgoingLink(pceOtnLink);
466                     LOG.info("validateLink: OTN-LINK added to allPceLinks {}", pceOtnLink);
467                     break;
468                 default:
469                     LOG.warn("validateLink: link type is not supported {}", pceOtnLink);
470             }
471             return true;
472
473         } else {
474             LOG.error(" validateLink: Unmanaged service type {}", serviceType);
475             return false;
476         }
477
478     }
479
480     private boolean validateNode(Node node) {
481         LOG.debug("validateNode: node {} ", node);
482         // PceNode will be used in Graph algorithm
483         Node1 node1 = node.augmentation(Node1.class);
484         if (node1 == null) {
485             LOG.error("getNodeType: no Node1 (type) Augmentation for node: [{}]. Node is ignored", node.getNodeId());
486             return false;
487         }
488         OpenroadmNodeType nodeType = node1.getNodeType();
489         String deviceNodeId = MapUtils.getSupNetworkNode(node);
490         // Should never happen but because of existing topology test files
491         // we have to manage this case
492         if (deviceNodeId == null || deviceNodeId.isBlank()) {
493             deviceNodeId = node.getNodeId().getValue();
494         }
495         LOG.info("Device node id {} for {}", deviceNodeId, node);
496         PceOpticalNode pceNode = new PceOpticalNode(node, nodeType, mappingUtils.getOpenRoadmVersion(deviceNodeId),
497                 getSlotWidthGranularity(deviceNodeId, node.getNodeId()));
498         pceNode.validateAZxponder(anodeId, znodeId, input.getServiceAEnd().getServiceFormat());
499         pceNode.initFrequenciesBitSet();
500
501         if (!pceNode.isValid()) {
502             LOG.warn(" validateNode: Node is ignored");
503             return false;
504         }
505         if (validateNodeConstraints(pceNode).equals(ConstraintTypes.HARD_EXCLUDE)) {
506             return false;
507         }
508         if (endPceNode(nodeType, pceNode.getNodeId(), pceNode) && this.aendPceNode == null
509             && isAZendPceNode(this.serviceFormatA, pceNode, anodeId, "A")) {
510             this.aendPceNode = pceNode;
511         }
512         if (endPceNode(nodeType, pceNode.getNodeId(), pceNode) && this.zendPceNode == null
513             && isAZendPceNode(this.serviceFormatZ, pceNode, znodeId, "Z")) {
514             this.zendPceNode = pceNode;
515         }
516
517         allPceNodes.put(pceNode.getNodeId(), pceNode);
518         LOG.debug("validateNode: node is saved {}", pceNode.getNodeId().getValue());
519         return true;
520     }
521
522     private boolean isAZendPceNode(String serviceFormat, PceOpticalNode pceNode, String azNodeId, String azEndPoint) {
523         switch (serviceFormat) {
524             case "Ethernet":
525             case "OC":
526                 if (pceNode.getSupNetworkNodeId().equals(azNodeId)) {
527                     return true;
528                 }
529                 return false;
530             case "OTU":
531                 if ("A".equals(azEndPoint) && pceNode.getNodeId().getValue()
532                     .equals(this.input.getServiceAEnd().getRxDirection().getPort().getPortDeviceName())) {
533                     return true;
534                 }
535                 if ("Z".equals(azEndPoint) && pceNode.getNodeId().getValue()
536                     .equals(this.input.getServiceZEnd().getRxDirection().getPort().getPortDeviceName())) {
537                     return true;
538                 }
539                 return false;
540             default:
541                 LOG.debug("Unsupported service Format {} for node {}", serviceFormat, pceNode.getNodeId().getValue());
542                 return false;
543         }
544     }
545
546     private boolean validateOtnNode(Node node) {
547         LOG.info("validateOtnNode: {} ", node.getNodeId().getValue());
548         // PceOtnNode will be used in Graph algorithm
549         if (node.augmentation(Node1.class) != null) {
550             OpenroadmNodeType nodeType = node.augmentation(Node1.class).getNodeType();
551
552             PceOtnNode pceOtnNode = new PceOtnNode(node, nodeType, node.getNodeId(), "otn", serviceType);
553             pceOtnNode.validateXponder(anodeId, znodeId);
554
555             if (!pceOtnNode.isValid()) {
556                 LOG.warn(" validateOtnNode: Node {} is ignored", node.getNodeId().getValue());
557                 return false;
558             }
559             if (validateNodeConstraints(pceOtnNode).equals(ConstraintTypes.HARD_EXCLUDE)) {
560                 return false;
561             }
562             if (pceOtnNode.getNodeId().getValue().equals(anodeId) && this.aendPceNode == null) {
563                 this.aendPceNode = pceOtnNode;
564             }
565             if (pceOtnNode.getNodeId().getValue().equals(znodeId) && this.zendPceNode == null) {
566                 this.zendPceNode = pceOtnNode;
567             }
568             allPceNodes.put(pceOtnNode.getNodeId(), pceOtnNode);
569             LOG.info("validateOtnNode: node {} is saved", node.getNodeId().getValue());
570             return true;
571         } else {
572             LOG.error("ValidateOtnNode: no node-type augmentation. Node {} is ignored", node.getNodeId().getValue());
573             return false;
574         }
575     }
576
577     private ConstraintTypes validateNodeConstraints(PceNode pcenode) {
578         if (pceHardConstraints.getExcludeSupNodes().isEmpty() && pceHardConstraints.getExcludeCLLI().isEmpty()) {
579             return ConstraintTypes.NONE;
580         }
581         if (pceHardConstraints.getExcludeSupNodes().contains(pcenode.getSupNetworkNodeId())) {
582             LOG.info("validateNodeConstraints: {}", pcenode.getNodeId().getValue());
583             return ConstraintTypes.HARD_EXCLUDE;
584         }
585         if (pceHardConstraints.getExcludeCLLI().contains(pcenode.getSupClliNodeId())) {
586             LOG.info("validateNodeConstraints: {}", pcenode.getNodeId().getValue());
587             return ConstraintTypes.HARD_EXCLUDE;
588         }
589         return ConstraintTypes.NONE;
590     }
591
592     private ConstraintTypes validateLinkConstraints(PceLink link) {
593         if (pceHardConstraints.getExcludeSRLG().isEmpty()) {
594             return ConstraintTypes.NONE;
595         }
596
597         // for now SRLG is the only constraint for link
598         if (link.getlinkType() != OpenroadmLinkType.ROADMTOROADM) {
599             return ConstraintTypes.NONE;
600         }
601
602         List<Long> constraints = new ArrayList<>(pceHardConstraints.getExcludeSRLG());
603         constraints.retainAll(link.getsrlgList());
604         if (!constraints.isEmpty()) {
605             LOG.info("validateLinkConstraints: {}", link.getLinkId().getValue());
606             return ConstraintTypes.HARD_EXCLUDE;
607         }
608
609         return ConstraintTypes.NONE;
610     }
611
612     private void dropOppositeLink(Link link) {
613         LinkId opplink = MapUtils.extractOppositeLink(link);
614
615         if (allPceLinks.containsKey(opplink)) {
616             allPceLinks.remove(opplink);
617         } else {
618             linksToExclude.add(opplink);
619         }
620     }
621
622     private Boolean endPceNode(OpenroadmNodeType openroadmNodeType, NodeId nodeId, PceOpticalNode pceNode) {
623         switch (openroadmNodeType) {
624             case SRG:
625                 pceNode.initSrgTps();
626                 this.azSrgs.add(nodeId);
627                 break;
628             case XPONDER:
629                 pceNode.initXndrTps(input.getServiceAEnd().getServiceFormat());
630                 break;
631             default:
632                 LOG.warn("endPceNode: Node {} is not SRG or XPONDER !", nodeId);
633                 return false;
634         }
635
636         if (!pceNode.isValid()) {
637             LOG.error("validateNode : there are no available frequencies in node {}", pceNode.getNodeId().getValue());
638             return false;
639         }
640         return true;
641     }
642
643     public PceNode getaendPceNode() {
644         return aendPceNode;
645     }
646
647     public PceNode getzendPceNode() {
648         return zendPceNode;
649     }
650
651     public Map<NodeId, PceNode> getAllPceNodes() {
652         return this.allPceNodes;
653     }
654
655     public Map<LinkId, PceLink> getAllPceLinks() {
656         return this.allPceLinks;
657     }
658
659     public String getServiceType() {
660         return serviceType;
661     }
662
663     public PceResult getReturnStructure() {
664         return returnStructure;
665     }
666
667     private static void printNodesInfo(Map<NodeId, PceNode> allPceNodes) {
668         allPceNodes.forEach(((nodeId, pceNode) -> {
669             LOG.info("In printNodes in node {} : outgoing links {} ", pceNode.getNodeId().getValue(),
670                     pceNode.getOutgoingLinks());
671         }));
672     }
673
674     /**
675      * Get mc capability slot width granularity for device.
676      * @param deviceNodeId String
677      * @param nodeId NodeId
678      * @return slot width granularity
679      */
680     private BigDecimal getSlotWidthGranularity(String deviceNodeId, NodeId nodeId) {
681         // nodeId: openroadm-topology level node
682         // deviceNodeId: openroadm-network level node
683         List<McCapabilities> mcCapabilities = mappingUtils.getMcCapabilitiesForNode(deviceNodeId);
684         String[] params = nodeId.getValue().split("-");
685         // DEGX or SRGX
686         String rdmModuleName = params[params.length - 1];
687         for (McCapabilities mcCapabitility : mcCapabilities) {
688             if (mcCapabitility.getMcNodeName().contains(rdmModuleName)
689                     && mcCapabitility.getSlotWidthGranularity() != null) {
690                 return mcCapabitility.getSlotWidthGranularity().getValue();
691             }
692         }
693         return GridConstant.SLOT_WIDTH_50;
694     }
695 }