2 * Copyright © 2017 AT&T, Inc. and others. All rights reserved.
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
9 package org.opendaylight.transportpce.pce.networkanalyzer;
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.HashSet;
14 import java.util.Iterator;
15 import java.util.List;
17 import java.util.Optional;
19 import java.util.concurrent.ExecutionException;
20 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.network.NetworkTransactionService;
26 import org.opendaylight.transportpce.pce.constraints.PceConstraints;
27 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.PathComputationRequestInput;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev181130.Node1;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev181130.OpenroadmLinkType;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev181130.OpenroadmNodeType;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NetworkId;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.Networks;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NodeId;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.Network;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.NetworkKey;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.network.Node;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.LinkId;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.Network1;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network.Link;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 //import org.opendaylight.yangtools.yang.common.Decimal64;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 public class PceCalculation {
47 private static final Logger LOG = LoggerFactory.getLogger(PceCalculation.class);
48 private NetworkTransactionService networkTransactionService = null;
50 ///////////// data parsed from Input/////////////////
51 private PathComputationRequestInput input;
52 private String anodeId = "";
53 private String znodeId = "";
54 private String serviceFormatA = "";
55 private String serviceFormatZ = "";
56 private String serviceType = "";
57 private Long serviceRate = 0L;
59 private PceConstraints pceHardConstraints;
61 ///////////// Intermediate data/////////////////
62 private List<PceLink> addLinks = new ArrayList<>();
63 private List<PceLink> dropLinks = new ArrayList<>();
64 private HashSet<NodeId> azSrgs = new HashSet<>();
66 private PceNode aendPceNode = null;
67 private PceNode zendPceNode = null;
69 private List<Link> allLinks = null;
70 private List<Node> allNodes = null;
72 // this List serves graph calculation
73 private Map<NodeId, PceNode> allPceNodes = new HashMap<>();
74 // this List serves calculation of ZtoA path description
75 // TODO maybe better solution is possible
76 private Map<LinkId, PceLink> allPceLinks = new HashMap<>();
77 private Set<LinkId> linksToExclude = new HashSet<>();
78 private PceResult returnStructure;
80 private enum ConstraintTypes {
81 NONE, HARD_EXCLUDE, HARD_INCLUDE, HARD_DIVERSITY, SOFT_EXCLUDE, SOFT_INCLUDE, SOFT_DIVERSITY;
84 public PceCalculation(PathComputationRequestInput input, NetworkTransactionService networkTransactionService,
85 PceConstraints pceHardConstraints, PceConstraints pceSoftConstraints, PceResult rc) {
87 this.networkTransactionService = networkTransactionService;
88 this.returnStructure = rc;
90 this.pceHardConstraints = pceHardConstraints;
94 public void retrievePceNetwork() {
96 LOG.info("In PceCalculation retrieveNetwork: ");
99 returnStructure.setRC(ResponseCodes.RESPONSE_FAILED);
102 MapUtils.mapDiversityConstraints(allNodes, allLinks, pceHardConstraints);
105 returnStructure.setRC(ResponseCodes.RESPONSE_FAILED);
108 printNodesInfo(allPceNodes);
110 returnStructure.setRC(ResponseCodes.RESPONSE_OK);
114 private boolean parseInput() {
115 anodeId = input.getServiceAEnd().getNodeId();
116 znodeId = input.getServiceZEnd().getNodeId();
117 if (input.getServiceAEnd().getServiceFormat() == null || input.getServiceZEnd().getServiceFormat() == null
118 || input.getServiceAEnd().getServiceRate() == null) {
119 LOG.error("Service Format and Service Rate are required for a path calculation");
122 serviceFormatA = input.getServiceAEnd().getServiceFormat().getName();
123 serviceFormatZ = input.getServiceZEnd().getServiceFormat().getName();
124 serviceRate = input.getServiceAEnd().getServiceRate().toJava();
126 LOG.info("parseInput: A and Z :[{}] and [{}]", anodeId, znodeId);
127 if (!(serviceFormatA.equals(serviceFormatZ))) {
128 LOG.info("parseInput: different service format for A and Z not handled, will use service format from Aend");
129 } else if (serviceRate == 100L) {
130 switch (serviceFormatA) {
133 serviceType = "100GE";
136 serviceType = "OTU4";
139 serviceType = "ODU4";
142 LOG.debug("parseInput: unsupported service type: Format {} Rate 100L", serviceFormatA);
145 //switch(serviceRate) may seem a better option at first glance.
146 //But switching on Long or long is not directly possible in Java.
147 //And casting to int bumps the limit here.
148 //Passing by ENUM or String are possible alternatives.
149 //Maybe HashMap and similar options should also be considered here.
150 } else if ("Ethernet".equals(serviceFormatA)) {
151 //only rate 100L is currently supported except in Ethernet
152 if (serviceRate == 10L) {
153 serviceType = "10GE";
154 } else if (serviceRate == 1L) {
157 LOG.debug("parseInput: unsupported service type: Format Ethernet Rate {}", serviceRate);
160 LOG.debug("parseInput: unsupported service type: Format {} Rate {}",
161 serviceFormatA, serviceRate);
164 returnStructure.setRate(input.getServiceAEnd().getServiceRate().toJava());
165 returnStructure.setServiceFormat(input.getServiceAEnd().getServiceFormat());
169 private boolean readMdSal() {
170 InstanceIdentifier<Network> nwInstanceIdentifier = null;
172 if (("OC".equals(serviceFormatA)) || ("OTU".equals(serviceFormatA)) || (("Ethernet".equals(serviceFormatA))
173 && (serviceRate == 100L))) {
175 LOG.info("readMdSal: network {}", NetworkUtils.OVERLAY_NETWORK_ID);
176 nwInstanceIdentifier = InstanceIdentifier.builder(Networks.class)
177 .child(Network.class, new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID))).build();
178 } else if ("ODU".equals(serviceFormatA) || ("Ethernet".equals(serviceFormatA) && serviceRate == 10L)
179 || ("Ethernet".equals(serviceFormatA) && serviceRate == 1L)) {
180 LOG.info("readMdSal: network {}", NetworkUtils.OTN_NETWORK_ID);
181 nwInstanceIdentifier = InstanceIdentifier.builder(Networks.class)
182 .child(Network.class, new NetworkKey(new NetworkId(NetworkUtils.OTN_NETWORK_ID))).build();
184 LOG.info("readMdSal: service-rate {} / service-format not handled {}", serviceRate, serviceFormatA);
189 Optional<Network> nwOptional =
190 networkTransactionService.read(LogicalDatastoreType.CONFIGURATION, nwInstanceIdentifier).get();
191 if (nwOptional.isPresent()) {
192 nw = nwOptional.get();
193 LOG.debug("readMdSal: network nodes: nwOptional.isPresent = true {}", nw);
195 } catch (InterruptedException | ExecutionException e) {
196 LOG.error("readMdSal: Error reading topology {}", nwInstanceIdentifier);
197 networkTransactionService.close();
198 returnStructure.setRC(ResponseCodes.RESPONSE_FAILED);
199 throw new RuntimeException(
200 "readMdSal: Error reading from operational store, topology : " + nwInstanceIdentifier + " :" + e);
202 networkTransactionService.close();
205 LOG.error("readMdSal: network is null: {}", nwInstanceIdentifier);
208 allNodes = nw.getNode().stream().sorted((n1, n2) -> n1.getNodeId().getValue().compareTo(n2.getNodeId()
209 .getValue())).collect(Collectors.toList());
210 Network1 nw1 = nw.augmentation(Network1.class);
212 allLinks = nw1.getLink().stream().sorted((l1, l2) -> l1.getSource().getSourceTp().toString().compareTo(l2
213 .getSource().getSourceTp().toString())).collect(Collectors.toList());
215 LOG.warn("no otn links in otn-topology");
217 if (allNodes == null || allNodes.isEmpty()) {
218 LOG.error("readMdSal: no nodes ");
221 LOG.info("readMdSal: network nodes: {} nodes added", allNodes.size());
222 LOG.debug("readMdSal: network nodes: {} nodes added", allNodes);
224 if (allLinks == null || allLinks.isEmpty()) {
225 LOG.error("readMdSal: no links ");
228 LOG.info("readMdSal: network links: {} links added", allLinks.size());
229 LOG.debug("readMdSal: network links: {} links added", allLinks);
234 private boolean analyzeNw() {
236 LOG.debug("analyzeNw: allNodes size {}, allLinks size {}", allNodes.size(), allLinks.size());
238 if (("100GE".equals(serviceType)) || ("OTU4".equals(serviceType))) {
239 // 100GE service and OTU4 service are handled at the openroadm-topology layer
240 for (Node node : allNodes) {
244 LOG.debug("analyzeNw: allPceNodes size {}", allPceNodes.size());
246 if (aendPceNode == null || zendPceNode == null) {
247 LOG.error("analyzeNw: Error in reading nodes: A or Z do not present in the network");
250 for (Link link : allLinks) {
254 LOG.debug("analyzeNw: addLinks size {}, dropLinks size {}", addLinks.size(), dropLinks.size());
256 LOG.debug("analyzeNw: azSrgs size = {}", azSrgs.size());
257 for (NodeId srg : azSrgs) {
258 LOG.debug("analyzeNw: A/Z Srgs SRG = {}", srg.getValue());
261 for (PceLink link : addLinks) {
262 filteraddLinks(link);
264 for (PceLink link : dropLinks) {
265 filterdropLinks(link);
269 // ODU4, 10GE/ODU2e or 1GE/ODU0 services are handled at openroadm-otn layer
271 for (Node node : allNodes) {
272 validateOtnNode(node);
275 LOG.info("analyzeNw: allPceNodes {}", allPceNodes);
277 if (aendPceNode == null || zendPceNode == null) {
278 LOG.error("analyzeNw: Error in reading nodes: A or Z do not present in the network");
281 for (Link link : allLinks) {
286 LOG.info("analyzeNw: allPceNodes size {}, allPceLinks size {}", allPceNodes.size(), allPceLinks.size());
288 if ((allPceNodes.size() == 0) || (allPceLinks.size() == 0)) {
292 LOG.debug("analyzeNw: allPceNodes {}", allPceNodes);
293 LOG.debug("analyzeNw: allPceLinks {}", allPceLinks);
298 private boolean filteraddLinks(PceLink pcelink) {
300 NodeId nodeId = pcelink.getSourceId();
302 if (azSrgs.contains(nodeId)) {
303 allPceLinks.put(pcelink.getLinkId(), pcelink);
304 allPceNodes.get(nodeId).addOutgoingLink(pcelink);
305 LOG.debug("analyzeNw: Add_LINK added to source and to allPceLinks {}", pcelink.getLinkId());
309 // remove the SRG from PceNodes, as it is not directly connected to A/Z
310 allPceNodes.remove(nodeId);
311 LOG.debug("analyzeNw: SRG removed {}", nodeId.getValue());
316 private boolean filterdropLinks(PceLink pcelink) {
318 NodeId nodeId = pcelink.getDestId();
320 if (azSrgs.contains(nodeId)) {
321 allPceLinks.put(pcelink.getLinkId(), pcelink);
322 allPceNodes.get(nodeId).addOutgoingLink(pcelink);
323 LOG.debug("analyzeNw: Drop_LINK added to dest and to allPceLinks {}", pcelink.getLinkId());
327 // remove the SRG from PceNodes, as it is not directly connected to A/Z
328 allPceNodes.remove(pcelink.getDestId());
329 LOG.debug("analyzeNw: SRG removed {}", nodeId.getValue());
334 private boolean validateLink(Link link) {
335 LOG.info("validateLink: link {} ", link);
337 NodeId sourceId = link.getSource().getSourceNode();
338 NodeId destId = link.getDestination().getDestNode();
339 PceNode source = allPceNodes.get(sourceId);
340 PceNode dest = allPceNodes.get(destId);
342 if (source == null) {
343 LOG.debug("validateLink: Link is ignored due source node is rejected by node validation - {}",
344 link.getSource().getSourceNode().getValue());
348 LOG.debug("validateLink: Link is ignored due dest node is rejected by node validation - {}",
349 link.getDestination().getDestNode().getValue());
353 if (("100GE".equals(serviceType)) || ("OTU4".equals(serviceType))) {
354 // 100GE or OTU4 services are handled at WDM Layer
355 PceLink pcelink = new PceLink(link, source, dest);
356 if (!pcelink.isValid()) {
357 dropOppositeLink(link);
358 LOG.error(" validateLink: Link is ignored due errors in network data or in opposite link");
361 LinkId linkId = pcelink.getLinkId();
362 if (validateLinkConstraints(pcelink).equals(ConstraintTypes.HARD_EXCLUDE)) {
363 dropOppositeLink(link);
364 LOG.debug("validateLink: constraints : link is ignored == {}", linkId.getValue());
367 switch (pcelink.getlinkType()) {
370 allPceLinks.put(linkId, pcelink);
371 source.addOutgoingLink(pcelink);
372 LOG.debug("validateLink: {}-LINK added to allPceLinks {}",
373 pcelink.getlinkType(), pcelink);
376 pcelink.setClient(source.getRdmSrgClient(pcelink.getSourceTP().toString()));
377 addLinks.add(pcelink);
378 LOG.debug("validateLink: ADD-LINK saved {}", pcelink);
381 pcelink.setClient(dest.getRdmSrgClient(pcelink.getDestTP().toString()));
382 dropLinks.add(pcelink);
383 LOG.debug("validateLink: DROP-LINK saved {}", pcelink);
386 // store separately all SRG links directly
387 azSrgs.add(sourceId);
389 if (!dest.checkTP(pcelink.getDestTP().toString())) {
391 "validateLink: XPONDER-INPUT is rejected as NW port is busy - {} ", pcelink);
394 if (dest.getXpdrClient(pcelink.getDestTP().toString()) != null) {
395 pcelink.setClient(dest.getXpdrClient(pcelink.getDestTP().toString()));
397 allPceLinks.put(linkId, pcelink);
398 source.addOutgoingLink(pcelink);
399 LOG.debug("validateLink: XPONDER-INPUT link added to allPceLinks {}", pcelink);
401 // does it mean XPONDER==>>SRG ?
403 // store separately all SRG links directly
406 if (!source.checkTP(pcelink.getSourceTP().toString())) {
408 "validateLink: XPONDER-OUTPUT is rejected as NW port is busy - {} ", pcelink);
411 if (source.getXpdrClient(pcelink.getSourceTP().toString()) != null) {
412 pcelink.setClient(source.getXpdrClient(pcelink.getSourceTP().toString()));
414 allPceLinks.put(linkId, pcelink);
415 source.addOutgoingLink(pcelink);
416 LOG.debug("validateLink: XPONDER-OUTPUT link added to allPceLinks {}", pcelink);
419 LOG.warn("validateLink: link type is not supported {}", pcelink);
423 } else if (("ODU4".equals(serviceType)) || ("10GE".equals(serviceType)) || ("1GE".equals(serviceType))) {
424 // ODU4, 1GE and 10GE services relying on ODU2, ODU2e or ODU0 services are handled at OTN layer
425 PceLink pceOtnLink = new PceLink(link, source, dest);
427 if (!pceOtnLink.isOtnValid(link, serviceType)) {
428 dropOppositeLink(link);
429 LOG.error(" validateLink: Link is ignored due errors in network data or in opposite link");
433 LinkId linkId = pceOtnLink.getLinkId();
434 if (validateLinkConstraints(pceOtnLink).equals(ConstraintTypes.HARD_EXCLUDE)) {
435 dropOppositeLink(link);
436 LOG.debug("validateLink: constraints : link is ignored == {}", linkId.getValue());
440 switch (pceOtnLink.getlinkType()) {
442 if (dest.getXpdrClient(pceOtnLink.getDestTP().toString()) != null) {
443 pceOtnLink.setClient(dest.getXpdrClient(pceOtnLink.getDestTP().toString()));
446 allPceLinks.put(linkId, pceOtnLink);
447 source.addOutgoingLink(pceOtnLink);
448 LOG.info("validateLink: OTN-LINK added to allPceLinks {}", pceOtnLink);
451 LOG.warn("validateLink: link type is not supported {}", pceOtnLink);
456 LOG.error(" validateLink: Unmanaged service type {}", serviceType);
462 private boolean validateNode(Node node) {
463 LOG.debug("validateNode: node {} ", node);
465 // PceNode will be used in Graph algorithm
466 Node1 node1 = node.augmentation(Node1.class);
468 LOG.error("getNodeType: no Node1 (type) Augmentation for node: [{}]. Node is ignored", node.getNodeId());
471 OpenroadmNodeType nodeType = node1.getNodeType();
473 PceOpticalNode pceNode = new PceOpticalNode(node, nodeType, node.getNodeId(),
474 input.getServiceAEnd().getServiceFormat(), "optical");
475 pceNode.validateAZxponder(anodeId, znodeId);
476 pceNode.initWLlist();
478 if (!pceNode.isValid()) {
479 LOG.warn(" validateNode: Node is ignored");
483 if (validateNodeConstraints(pceNode).equals(ConstraintTypes.HARD_EXCLUDE)) {
486 if ((pceNode.getSupNetworkNodeId().equals(anodeId) && (this.aendPceNode == null))
487 && (Boolean.TRUE.equals(endPceNode(nodeType, pceNode.getNodeId(), pceNode)))) {
488 this.aendPceNode = pceNode;
490 if ((pceNode.getSupNetworkNodeId().equals(znodeId) && (this.zendPceNode == null))
491 && (Boolean.TRUE.equals(endPceNode(nodeType, pceNode.getNodeId(), pceNode)))) {
492 this.zendPceNode = pceNode;
495 allPceNodes.put(pceNode.getNodeId(), pceNode);
496 LOG.debug("validateNode: node is saved {}", pceNode.getNodeId().getValue());
500 private boolean validateOtnNode(Node node) {
502 LOG.info("validateOtnNode: {} ", node.getNodeId().getValue());
503 // PceOtnNode will be used in Graph algorithm
504 if (node.augmentation(Node1.class) != null) {
505 OpenroadmNodeType nodeType = node.augmentation(Node1.class).getNodeType();
507 PceOtnNode pceOtnNode = new PceOtnNode(node, nodeType, node.getNodeId(), "otn", serviceType);
508 pceOtnNode.validateXponder(anodeId, znodeId);
510 if (!pceOtnNode.isValid()) {
511 LOG.warn(" validateOtnNode: Node {} is ignored", node.getNodeId().getValue());
514 if (validateNodeConstraints(pceOtnNode).equals(ConstraintTypes.HARD_EXCLUDE)) {
517 if (pceOtnNode.getNodeId().getValue().equals(anodeId) && this.aendPceNode == null) {
518 this.aendPceNode = pceOtnNode;
520 if (pceOtnNode.getNodeId().getValue().equals(znodeId) && this.zendPceNode == null) {
521 this.zendPceNode = pceOtnNode;
523 allPceNodes.put(pceOtnNode.getNodeId(), pceOtnNode);
524 LOG.info("validateOtnNode: node {} is saved", node.getNodeId().getValue());
527 LOG.error("ValidateOtnNode: no node-type augmentation. Node {} is ignored", node.getNodeId().getValue());
531 // if (mode == "AZ") {
532 // pceOtnNode.validateAZxponder(anodeId, znodeId);
533 // } else if (mode == "intermediate") {
534 // pceOtnNode.validateIntermediateSwitch();
536 // LOG.error("validateOtnNode: unproper mode passed to the method : {} not supported", mode);
541 private ConstraintTypes validateNodeConstraints(PceNode pcenode) {
542 if (pceHardConstraints.getExcludeSupNodes().isEmpty() && pceHardConstraints.getExcludeCLLI().isEmpty()) {
543 return ConstraintTypes.NONE;
545 if (pceHardConstraints.getExcludeSupNodes().contains(pcenode.getSupNetworkNodeId())) {
546 LOG.info("validateNodeConstraints: {}", pcenode.getNodeId().getValue());
547 return ConstraintTypes.HARD_EXCLUDE;
549 if (pceHardConstraints.getExcludeCLLI().contains(pcenode.getSupClliNodeId())) {
550 LOG.info("validateNodeConstraints: {}", pcenode.getNodeId().getValue());
551 return ConstraintTypes.HARD_EXCLUDE;
553 return ConstraintTypes.NONE;
556 private ConstraintTypes validateLinkConstraints(PceLink link) {
557 if (pceHardConstraints.getExcludeSRLG().isEmpty()) {
558 return ConstraintTypes.NONE;
561 // for now SRLG is the only constraint for link
562 if (link.getlinkType() != OpenroadmLinkType.ROADMTOROADM) {
563 return ConstraintTypes.NONE;
566 List<Long> constraints = new ArrayList<>(pceHardConstraints.getExcludeSRLG());
567 constraints.retainAll(link.getsrlgList());
568 if (!constraints.isEmpty()) {
569 LOG.info("validateLinkConstraints: {}", link.getLinkId().getValue());
570 return ConstraintTypes.HARD_EXCLUDE;
573 return ConstraintTypes.NONE;
576 private void dropOppositeLink(Link link) {
577 LinkId opplink = MapUtils.extractOppositeLink(link);
579 if (allPceLinks.containsKey(opplink)) {
580 allPceLinks.remove(opplink);
582 linksToExclude.add(opplink);
586 private Boolean endPceNode(OpenroadmNodeType openroadmNodeType, NodeId nodeId, PceOpticalNode pceNode) {
587 switch (openroadmNodeType) {
589 pceNode.initSrgTps();
590 this.azSrgs.add(nodeId);
593 pceNode.initXndrTps();
596 LOG.warn("endPceNode: Node {} is not SRG or XPONDER !", nodeId);
600 if (!pceNode.isValid()) {
601 LOG.error("validateNode : there are no availaible wavelengths in node {}", pceNode.getNodeId().getValue());
607 public PceNode getaendPceNode() {
611 public PceNode getzendPceNode() {
615 public Map<NodeId, PceNode> getAllPceNodes() {
616 return this.allPceNodes;
619 public Map<LinkId, PceLink> getAllPceLinks() {
620 return this.allPceLinks;
623 public String getServiceType() {
627 public PceResult getReturnStructure() {
628 return returnStructure;
631 private static void printNodesInfo(Map<NodeId, PceNode> allpcenodes) {
632 Iterator<Map.Entry<NodeId, PceNode>> nodes = allpcenodes.entrySet().iterator();
633 while (nodes.hasNext()) {
634 PceNode pcenode = nodes.next().getValue();
635 List<PceLink> links = pcenode.getOutgoingLinks();
636 LOG.info("In printNodes in node {} : outgoing links {} ", pcenode.getNodeId().getValue(), links);