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