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.rev190624.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.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
44 public class PceCalculation {
46 private static final Logger LOG = LoggerFactory.getLogger(PceCalculation.class);
47 private NetworkTransactionService networkTransactionService = null;
49 ///////////// data parsed from Input/////////////////
50 private PathComputationRequestInput input;
51 private String anodeId = "";
52 private String znodeId = "";
54 private PceConstraints pceHardConstraints;
56 ///////////// Intermediate data/////////////////
57 private List<PceLink> addLinks = new ArrayList<PceLink>();
58 private List<PceLink> dropLinks = new ArrayList<PceLink>();
59 private HashSet<NodeId> azSrgs = new HashSet<NodeId>();
61 private PceNode aendPceNode = null;
62 private PceNode zendPceNode = null;
64 private List<Link> allLinks = null;
65 private List<Node> allNodes = null;
67 // this List serves graph calculation
68 private Map<NodeId, PceNode> allPceNodes = new HashMap<NodeId, PceNode>();
69 // this List serves calculation of ZtoA path description
70 // TODO maybe better solution is possible
71 private Map<LinkId, PceLink> allPceLinks = new HashMap<LinkId, PceLink>();
72 private Set<LinkId> linksToExclude = new HashSet<LinkId>();
73 private PceResult returnStructure;
75 private enum ConstraintTypes {
76 NONE, HARD_EXCLUDE, HARD_INCLUDE, HARD_DIVERSITY, SOFT_EXCLUDE, SOFT_INCLUDE, SOFT_DIVERSITY;
79 public PceCalculation(PathComputationRequestInput input, NetworkTransactionService networkTransactionService,
80 PceConstraints pceHardConstraints, PceConstraints pceSoftConstraints, PceResult rc) {
82 this.networkTransactionService = networkTransactionService;
83 this.returnStructure = rc;
85 this.pceHardConstraints = pceHardConstraints;
89 public void calcPath() {
91 LOG.info("In PceCalculation calcPath: ");
94 returnStructure.setRC(ResponseCodes.RESPONSE_FAILED);
97 MapUtils.mapDiversityConstraints(allNodes, allLinks, pceHardConstraints);
100 returnStructure.setRC(ResponseCodes.RESPONSE_FAILED);
103 printNodesInfo(allPceNodes);
105 returnStructure.setRC(ResponseCodes.RESPONSE_OK);
109 private boolean parseInput() {
110 anodeId = input.getServiceAEnd().getNodeId();
111 znodeId = input.getServiceZEnd().getNodeId();
112 LOG.info("parseInput: A and Z :[{}] and [{}]", anodeId, znodeId);
113 returnStructure.setRate(input.getServiceAEnd().getServiceRate());
117 private boolean readMdSal() {
118 LOG.info("readMdSal: network {}", NetworkUtils.OVERLAY_NETWORK_ID);
119 InstanceIdentifier<Network> nwInstanceIdentifier = InstanceIdentifier.builder(Networks.class)
120 .child(Network.class, new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID))).build();
123 Optional<Network> nwOptional =
124 networkTransactionService.read(LogicalDatastoreType.CONFIGURATION, nwInstanceIdentifier).get();
125 if (nwOptional.isPresent()) {
126 nw = nwOptional.get();
127 LOG.debug("readMdSal: network nodes: nwOptional.isPresent = true {}", nw.toString());
129 } catch (InterruptedException | ExecutionException e) {
130 LOG.error("readMdSal: Error reading topology {}", nwInstanceIdentifier);
131 networkTransactionService.close();
132 returnStructure.setRC(ResponseCodes.RESPONSE_FAILED);
133 throw new RuntimeException(
134 "readMdSal: Error reading from operational store, topology : " + nwInstanceIdentifier + " :" + e);
136 networkTransactionService.close();
139 LOG.error("readMdSal: network is null: {}", nwInstanceIdentifier);
142 allNodes = nw.getNode().stream().sorted((n1, n2) -> n1.getNodeId().getValue().compareTo(n2.getNodeId()
143 .getValue())).collect(Collectors.toList());
144 Network1 nw1 = nw.augmentation(Network1.class);
146 allLinks = nw1.getLink().stream().sorted((l1, l2) -> l1.getSource().getSourceTp().toString().compareTo(l2
147 .getSource().getSourceTp().toString())).collect(Collectors.toList());
148 if (allNodes == null || allNodes.isEmpty()) {
149 LOG.error("readMdSal: no nodes ");
152 LOG.info("readMdSal: network nodes: {} nodes added", allNodes.size());
153 LOG.debug("readMdSal: network nodes: {} nodes added", allNodes.toString());
155 if (allLinks == null || allLinks.isEmpty()) {
156 LOG.error("readMdSal: no links ");
159 LOG.info("readMdSal: network links: {} links added", allLinks.size());
160 LOG.debug("readMdSal: network links: {} links added", allLinks.toString());
165 private boolean analyzeNw() {
167 LOG.debug("analyzeNw: allNodes size {}, allLinks size {}", allNodes.size(), allLinks.size());
169 for (Node node : allNodes) {
172 LOG.debug("analyzeNw: allPceNodes size {}", allPceNodes.size());
174 if (aendPceNode == null || zendPceNode == null) {
175 LOG.error("analyzeNw: Error in reading nodes: A or Z do not present in the network");
179 for (Link link : allLinks) {
183 LOG.debug("analyzeNw: addLinks size {}, dropLinks size {}", addLinks.size(), dropLinks.size());
186 LOG.debug("analyzeNw: azSrgs size = {}", azSrgs.size());
187 for (NodeId srg : azSrgs) {
188 LOG.debug("analyzeNw: A/Z Srgs SRG = {}", srg.getValue());
192 for (PceLink link : addLinks) {
193 filteraddLinks(link);
195 for (PceLink link : dropLinks) {
196 filterdropLinks(link);
199 LOG.info("analyzeNw: allPceNodes size {}, allPceLinks size {}", allPceNodes.size(), allPceLinks.size());
201 if ((allPceNodes.size() == 0) || (allPceLinks.size() == 0)) {
205 LOG.debug("analyzeNw: allPceNodes {}", allPceNodes.toString());
206 LOG.debug("analyzeNw: allPceLinks {}", allPceLinks.toString());
211 private boolean filteraddLinks(PceLink pcelink) {
213 NodeId nodeId = pcelink.getSourceId();
215 if (azSrgs.contains(nodeId)) {
216 allPceLinks.put(pcelink.getLinkId(), pcelink);
217 allPceNodes.get(nodeId).addOutgoingLink(pcelink);
218 LOG.debug("analyzeNw: Add_LINK added to source and to allPceLinks {}", pcelink.getLinkId().toString());
222 // remove the SRG from PceNodes, as it is not directly connected to A/Z
223 allPceNodes.remove(nodeId);
224 LOG.debug("analyzeNw: SRG removed {}", nodeId.getValue());
229 private boolean filterdropLinks(PceLink pcelink) {
231 NodeId nodeId = pcelink.getDestId();
233 if (azSrgs.contains(nodeId)) {
234 allPceLinks.put(pcelink.getLinkId(), pcelink);
235 allPceNodes.get(nodeId).addOutgoingLink(pcelink);
236 LOG.debug("analyzeNw: Drop_LINK added to dest and to allPceLinks {}", pcelink.getLinkId().toString());
240 // remove the SRG from PceNodes, as it is not directly connected to A/Z
241 allPceNodes.remove(pcelink.getDestId());
242 LOG.debug("analyzeNw: SRG removed {}", nodeId.getValue());
247 private boolean validateLink(Link link) {
249 LOG.debug("validateLink: link {} ", link.toString());
251 if (linksToExclude.contains(link.getLinkId())) {
252 LOG.info("validateLink: Link is ignored due opposite link problem - {}", link.getLinkId().getValue());
256 NodeId sourceId = link.getSource().getSourceNode();
257 NodeId destId = link.getDestination().getDestNode();
258 PceNode source = allPceNodes.get(sourceId);
259 PceNode dest = allPceNodes.get(destId);
261 if (source == null) {
262 LOG.debug("validateLink: Link is ignored due source node is rejected by node validation - {}",
263 link.getSource().getSourceNode().getValue());
267 LOG.debug("validateLink: Link is ignored due dest node is rejected by node validation - {}",
268 link.getDestination().getDestNode().getValue());
272 PceLink pcelink = new PceLink(link, source, dest);
273 if (!pcelink.isValid()) {
274 dropOppositeLink(link);
275 LOG.error(" validateLink: Link is ignored due errors in network data or in opposite link");
279 LinkId linkId = pcelink.getLinkId();
281 switch (validateLinkConstraints(pcelink)) {
283 dropOppositeLink(link);
284 LOG.debug("validateLink: constraints : link is ignored == {}", linkId.getValue());
290 switch (pcelink.getlinkType()) {
292 allPceLinks.put(linkId, pcelink);
293 source.addOutgoingLink(pcelink);
294 LOG.debug("validateLink: ROADMTOROADM-LINK added to allPceLinks {}", pcelink.toString());
297 allPceLinks.put(linkId, pcelink);
298 source.addOutgoingLink(pcelink);
299 LOG.debug("validateLink: EXPRESS-LINK added to allPceLinks {}", pcelink.toString());
302 pcelink.setClient(source.getRdmSrgClient(pcelink.getSourceTP().toString(), true));
303 addLinks.add(pcelink);
304 LOG.debug("validateLink: ADD-LINK saved {}", pcelink.toString());
307 pcelink.setClient(dest.getRdmSrgClient(pcelink.getDestTP().toString(), false));
308 dropLinks.add(pcelink);
309 LOG.debug("validateLink: DROP-LINK saved {}", pcelink.toString());
312 // store separately all SRG links directly
313 azSrgs.add(sourceId);
315 if (!dest.checkTP(pcelink.getDestTP().toString())) {
316 LOG.debug("validateLink: XPONDER-INPUT is rejected as NW port is busy - {} ", pcelink.toString());
319 pcelink.setClient(dest.getClient(pcelink.getDestTP().toString()));
320 allPceLinks.put(linkId, pcelink);
321 source.addOutgoingLink(pcelink);
322 LOG.debug("validateLink: XPONDER-INPUT link added to allPceLinks {}", pcelink.toString());
324 // does it mean XPONDER==>>SRG ?
326 // store separately all SRG links directly
329 if (!source.checkTP(pcelink.getSourceTP().toString())) {
330 LOG.debug("validateLink: XPONDER-OUTPUT is rejected as NW port is busy - {} ", pcelink.toString());
333 pcelink.setClient(source.getClient(pcelink.getSourceTP().toString()));
334 allPceLinks.put(linkId, pcelink);
335 source.addOutgoingLink(pcelink);
336 LOG.debug("validateLink: XPONDER-OUTPUT link added to allPceLinks {}", pcelink.toString());
339 LOG.warn("validateLink: link type is not supported {}", pcelink.toString());
346 private boolean validateNode(Node node) {
347 LOG.debug("validateNode: node {} ", node.toString());
349 // PceNode will be used in Graph algorithm
350 Node1 node1 = node.augmentation(Node1.class);
352 LOG.error("getNodeType: no Node1 (type) Augmentation for node: [{}]. Node is ignored", node.getNodeId());
354 OpenroadmNodeType nodeType = node1.getNodeType();
356 PceNode pceNode = new PceNode(node,nodeType,node.getNodeId());
357 pceNode.validateAZxponder(anodeId, znodeId);
358 pceNode.initWLlist();
360 if (!pceNode.isValid()) {
361 LOG.warn(" validateNode: Node is ignored");
365 switch (validateNodeConstraints(pceNode)) {
373 if (pceNode.getSupNodeIdPceNode().equals(this.anodeId)) {
374 if (this.aendPceNode != null) {
375 LOG.debug("aendPceNode already gets: {}", this.aendPceNode);
376 } else if (endPceNode(nodeType,pceNode.getNodeId(), pceNode)) {
377 this.aendPceNode = pceNode;
379 // returning false otherwise would break E2E test
381 if (pceNode.getSupNodeIdPceNode().equals(this.znodeId)) {
382 if (this.zendPceNode != null) {
383 LOG.debug("zendPceNode already gets: {}", this.zendPceNode);
384 } else if (endPceNode(nodeType,pceNode.getNodeId(), pceNode)) {
385 this.zendPceNode = pceNode;
387 // returning false otherwise would break E2E test
390 allPceNodes.put(pceNode.getNodeId(), pceNode);
391 LOG.debug("validateNode: node is saved {}", pceNode.getNodeId().getValue());
395 private ConstraintTypes validateNodeConstraints(PceNode pcenode) {
397 if (pceHardConstraints.getExcludeSupNodes().isEmpty() && pceHardConstraints.getExcludeCLLI().isEmpty()) {
398 return ConstraintTypes.NONE;
401 if (pceHardConstraints.getExcludeSupNodes().contains(pcenode.getSupNodeIdPceNode())) {
402 LOG.info("validateNodeConstraints: {}", pcenode.getNodeId().getValue());
403 return ConstraintTypes.HARD_EXCLUDE;
405 if (pceHardConstraints.getExcludeCLLI().contains(pcenode.getCLLI())) {
406 LOG.info("validateNodeConstraints: {}", pcenode.getNodeId().getValue());
407 return ConstraintTypes.HARD_EXCLUDE;
410 return ConstraintTypes.NONE;
413 private ConstraintTypes validateLinkConstraints(PceLink link) {
414 if (pceHardConstraints.getExcludeSRLG().isEmpty()) {
415 return ConstraintTypes.NONE;
418 // for now SRLG is the only constraint for link
419 if (link.getlinkType() != OpenroadmLinkType.ROADMTOROADM) {
420 return ConstraintTypes.NONE;
423 List<Long> constraints = new ArrayList<Long>(pceHardConstraints.getExcludeSRLG());
424 constraints.retainAll(link.getsrlgList());
425 if (!constraints.isEmpty()) {
426 LOG.info("validateLinkConstraints: {}", link.getLinkId().getValue());
427 return ConstraintTypes.HARD_EXCLUDE;
430 return ConstraintTypes.NONE;
433 private void dropOppositeLink(Link link) {
434 LinkId opplink = MapUtils.extractOppositeLink(link);
436 PceLink oppPceLink = allPceLinks.get(opplink);
437 if (oppPceLink != null) {
438 allPceLinks.remove(oppPceLink);
440 linksToExclude.add(opplink);
444 private Boolean endPceNode(OpenroadmNodeType openroadmNodeType, NodeId nodeId, PceNode pceNode) {
445 switch (openroadmNodeType) {
447 pceNode.initSrgTps();
448 this.azSrgs.add(nodeId);
451 pceNode.initXndrTps();
454 LOG.warn("endPceNode: Node {} is not SRG or XPONDER !", nodeId);
458 if (!pceNode.isValid()) {
459 LOG.error("validateNode : there are no availaible wavelengths in node {}", pceNode.getNodeId().getValue());
465 public PceNode getaendPceNode() {
469 public PceNode getzendPceNode() {
473 public Map<NodeId, PceNode> getAllPceNodes() {
474 return this.allPceNodes;
477 public Map<LinkId, PceLink> getAllPceLinks() {
478 return this.allPceLinks;
481 public PceResult getReturnStructure() {
482 return returnStructure;
485 private static void printNodesInfo(Map<NodeId, PceNode> allpcenodes) {
486 Iterator<Map.Entry<NodeId, PceNode>> nodes = allpcenodes.entrySet().iterator();
487 while (nodes.hasNext()) {
488 PceNode pcenode = nodes.next().getValue();
489 List<PceLink> links = pcenode.getOutgoingLinks();
490 LOG.info("In printNodes in node {} : outgoing links {} ", pcenode.getNodeId().getValue(), links.toString());
494 /*private static void printLinksInfo(Map<LinkId, PceLink> allpcelinks) {
495 Iterator<Map.Entry<LinkId, PceLink>> links = allpcelinks.entrySet().iterator();
496 while (links.hasNext()) {
497 LOG.info("In printLinksInfo link {} : ", links.next().getValue().toString());