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