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