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