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