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