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