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 org.opendaylight.mdsal.common.api.LogicalDatastoreType;
21 import org.opendaylight.transportpce.common.NetworkUtils;
22 import org.opendaylight.transportpce.common.ResponseCodes;
23 import org.opendaylight.transportpce.common.network.NetworkTransactionService;
24 import org.opendaylight.transportpce.pce.constraints.PceConstraints;
25 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev190624.PathComputationRequestInput;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev181130.Node1;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev181130.OpenroadmLinkType;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev181130.OpenroadmNodeType;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NetworkId;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.Networks;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NodeId;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.Network;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.NetworkKey;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.network.Node;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.LinkId;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.Network1;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network.Link;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 public class PceCalculation {
44 private static final Logger LOG = LoggerFactory.getLogger(PceCalculation.class);
45 private NetworkTransactionService networkTransactionService = null;
47 ///////////// data parsed from Input/////////////////
48 private PathComputationRequestInput input;
49 private String anodeId = "";
50 private String znodeId = "";
52 private PceConstraints pceHardConstraints;
54 ///////////// Intermediate data/////////////////
55 private List<PceLink> addLinks = new ArrayList<PceLink>();
56 private List<PceLink> dropLinks = new ArrayList<PceLink>();
57 private HashSet<NodeId> azSrgs = new HashSet<NodeId>();
59 private PceNode aendPceNode = null;
60 private PceNode zendPceNode = null;
62 private List<Link> allLinks = null;
63 private List<Node> allNodes = null;
65 // this List serves graph calculation
66 private Map<NodeId, PceNode> allPceNodes = new HashMap<NodeId, PceNode>();
67 // this List serves calculation of ZtoA path descritopn
68 // TODO maybe better solution is possible
69 private Map<LinkId, PceLink> allPceLinks = new HashMap<LinkId, PceLink>();
70 private Set<LinkId> linksToExclude = new HashSet<LinkId>();
71 private PceResult returnStructure;
73 private enum ConstraintTypes {
74 NONE, HARD_EXCLUDE, HARD_INCLUDE, HARD_DIVERSITY, SOFT_EXCLUDE, SOFT_INCLUDE, SOFT_DIVERSITY;
77 public PceCalculation(PathComputationRequestInput input, NetworkTransactionService networkTransactionService,
78 PceConstraints pceHardConstraints, PceConstraints pceSoftConstraints, PceResult rc) {
80 this.networkTransactionService = networkTransactionService;
81 this.returnStructure = rc;
83 this.pceHardConstraints = pceHardConstraints;
87 public void calcPath() {
89 LOG.info("In PceCalculation calcPath: ");
92 returnStructure.setRC(ResponseCodes.RESPONSE_FAILED);
96 MapUtils.mapDiversityConstraints(allNodes, allLinks, pceHardConstraints);
99 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();
143 Network1 nw1 = nw.augmentation(Network1.class);
145 allLinks = nw1.getLink();
146 if (allNodes == null || allNodes.isEmpty()) {
147 LOG.error("readMdSal: no nodes ");
150 LOG.info("readMdSal: network nodes: {} nodes added", allNodes.size());
151 LOG.debug("readMdSal: network nodes: {} nodes added", allNodes.toString());
153 if (allLinks == null || allLinks.isEmpty()) {
154 LOG.error("readMdSal: no links ");
157 LOG.info("readMdSal: network links: {} links added", allLinks.size());
158 LOG.debug("readMdSal: network links: {} links added", allLinks.toString());
163 private boolean analyzeNw() {
165 LOG.debug("analyzeNw: allNodes size {}, allLinks size {}", allNodes.size(), allLinks.size());
167 for (Node node : allNodes) {
170 LOG.debug("analyzeNw: allPceNodes size {}", allPceNodes.size());
172 if (aendPceNode == null || zendPceNode == null) {
173 LOG.error("analyzeNw: Error in reading nodes: A or Z do not present in the network");
177 for (Link link : allLinks) {
181 LOG.debug("analyzeNw: addLinks size {}, dropLinks size {}", addLinks.size(), dropLinks.size());
184 LOG.debug("analyzeNw: azSrgs size = {}", azSrgs.size());
185 for (NodeId srg : azSrgs) {
186 LOG.debug("analyzeNw: A/Z Srgs SRG = {}", srg.getValue());
190 for (PceLink link : addLinks) {
191 filteraddLinks(link);
193 for (PceLink link : dropLinks) {
194 filterdropLinks(link);
197 LOG.info("analyzeNw: allPceNodes size {}, allPceLinks size {}", allPceNodes.size(), allPceLinks.size());
199 if ((allPceNodes.size() == 0) || (allPceLinks.size() == 0)) {
203 LOG.debug("analyzeNw: allPceNodes {}", allPceNodes.toString());
204 LOG.debug("analyzeNw: allPceLinks {}", allPceLinks.toString());
209 private boolean filteraddLinks(PceLink pcelink) {
211 NodeId nodeId = pcelink.getSourceId();
213 if (azSrgs.contains(nodeId)) {
214 allPceLinks.put(pcelink.getLinkId(), pcelink);
215 allPceNodes.get(nodeId).addOutgoingLink(pcelink);
216 LOG.debug("analyzeNw: Add_LINK added to source and to allPceLinks {}", pcelink.getLinkId().toString());
220 // remove the SRG from PceNodes, as it is not directly connected to A/Z
221 allPceNodes.remove(nodeId);
222 LOG.debug("analyzeNw: SRG removed {}", nodeId.getValue());
227 private boolean filterdropLinks(PceLink pcelink) {
229 NodeId nodeId = pcelink.getDestId();
231 if (azSrgs.contains(nodeId)) {
232 allPceLinks.put(pcelink.getLinkId(), pcelink);
233 allPceNodes.get(nodeId).addOutgoingLink(pcelink);
234 LOG.debug("analyzeNw: Drop_LINK added to dest and to allPceLinks {}", pcelink.getLinkId().toString());
238 // remove the SRG from PceNodes, as it is not directly connected to A/Z
239 allPceNodes.remove(pcelink.getDestId());
240 LOG.debug("analyzeNw: SRG removed {}", nodeId.getValue());
245 private boolean validateLink(Link link) {
247 LOG.debug("validateLink: link {} ", link.toString());
249 if (linksToExclude.contains(link.getLinkId())) {
250 LOG.info("validateLink: Link is ignored due opposite link problem - {}", link.getLinkId().getValue());
254 NodeId sourceId = link.getSource().getSourceNode();
255 NodeId destId = link.getDestination().getDestNode();
256 PceNode source = allPceNodes.get(sourceId);
257 PceNode dest = allPceNodes.get(destId);
259 if (source == null) {
260 LOG.debug("validateLink: Link is ignored due source node is rejected by node validation - {}",
261 link.getSource().getSourceNode().getValue());
265 LOG.debug("validateLink: Link is ignored due dest node is rejected by node validation - {}",
266 link.getDestination().getDestNode().getValue());
270 PceLink pcelink = new PceLink(link, source, dest);
271 if (!pcelink.isValid()) {
272 dropOppositeLink(link);
273 LOG.error(" validateLink: Link is ignored due errors in network data or in opposite link");
277 LinkId linkId = pcelink.getLinkId();
279 switch (validateLinkConstraints(pcelink)) {
281 dropOppositeLink(link);
282 LOG.debug("validateLink: constraints : link is ignored == {}", linkId.getValue());
288 switch (pcelink.getlinkType()) {
290 allPceLinks.put(linkId, pcelink);
291 source.addOutgoingLink(pcelink);
292 LOG.debug("validateLink: ROADMTOROADM-LINK added to allPceLinks {}", pcelink.toString());
295 allPceLinks.put(linkId, pcelink);
296 source.addOutgoingLink(pcelink);
297 LOG.debug("validateLink: EXPRESS-LINK added to allPceLinks {}", pcelink.toString());
300 pcelink.setClient(source.getRdmSrgClient(pcelink.getSourceTP().toString(), true));
301 addLinks.add(pcelink);
302 LOG.debug("validateLink: ADD-LINK saved {}", pcelink.toString());
305 pcelink.setClient(dest.getRdmSrgClient(pcelink.getDestTP().toString(), false));
306 dropLinks.add(pcelink);
307 LOG.debug("validateLink: DROP-LINK saved {}", pcelink.toString());
310 // store separately all SRG links directly
311 azSrgs.add(sourceId);
313 if (!dest.checkTP(pcelink.getDestTP().toString())) {
314 LOG.debug("validateLink: XPONDER-INPUT is rejected as NW port is busy - {} ", pcelink.toString());
317 pcelink.setClient(dest.getClient(pcelink.getDestTP().toString()));
318 allPceLinks.put(linkId, pcelink);
319 source.addOutgoingLink(pcelink);
320 LOG.debug("validateLink: XPONDER-INPUT link added to allPceLinks {}", pcelink.toString());
322 // does it mean XPONDER==>>SRG ?
324 // store separately all SRG links directly
327 if (!source.checkTP(pcelink.getSourceTP().toString())) {
328 LOG.debug("validateLink: XPONDER-OUTPUT is rejected as NW port is busy - {} ", pcelink.toString());
331 pcelink.setClient(source.getClient(pcelink.getSourceTP().toString()));
332 allPceLinks.put(linkId, pcelink);
333 source.addOutgoingLink(pcelink);
334 LOG.debug("validateLink: XPONDER-OUTPUT link added to allPceLinks {}", pcelink.toString());
337 LOG.warn("validateLink: link type is not supported {}", pcelink.toString());
344 private boolean validateNode(Node node) {
345 LOG.debug("validateNode: node {} ", node.toString());
347 // PceNode will be used in Graph algorithm
348 Node1 node1 = node.augmentation(Node1.class);
350 LOG.error("getNodeType: no Node1 (type) Augmentation for node: [{}]. Node is ignored", node.getNodeId());
352 OpenroadmNodeType nodeType = node1.getNodeType();
354 PceNode pceNode = new PceNode(node,nodeType,node.getNodeId());
355 pceNode.validateAZxponder(anodeId, znodeId);
356 pceNode.initWLlist();
358 if (!pceNode.isValid()) {
359 LOG.warn(" validateNode: Node is ignored");
363 switch (validateNodeConstraints(pceNode)) {
371 if ((pceNode.getSupNodeIdPceNode().equals(anodeId)) && (endPceNode(nodeType,pceNode.getNodeId(), pceNode))) {
372 this.aendPceNode = pceNode;
374 if ((pceNode.getSupNodeIdPceNode().equals(znodeId)) && (endPceNode(nodeType,pceNode.getNodeId(), pceNode))) {
375 this.zendPceNode = pceNode;
378 allPceNodes.put(pceNode.getNodeId(), pceNode);
379 LOG.debug("validateNode: node is saved {}", pceNode.getNodeId().getValue());
383 private ConstraintTypes validateNodeConstraints(PceNode pcenode) {
385 if (pceHardConstraints.getExcludeSupNodes().isEmpty() && pceHardConstraints.getExcludeCLLI().isEmpty()) {
386 return ConstraintTypes.NONE;
389 if (pceHardConstraints.getExcludeSupNodes().contains(pcenode.getSupNodeIdPceNode())) {
390 LOG.info("validateNodeConstraints: {}", pcenode.getNodeId().getValue());
391 return ConstraintTypes.HARD_EXCLUDE;
393 if (pceHardConstraints.getExcludeCLLI().contains(pcenode.getCLLI())) {
394 LOG.info("validateNodeConstraints: {}", pcenode.getNodeId().getValue());
395 return ConstraintTypes.HARD_EXCLUDE;
398 return ConstraintTypes.NONE;
401 private ConstraintTypes validateLinkConstraints(PceLink link) {
402 if (pceHardConstraints.getExcludeSRLG().isEmpty()) {
403 return ConstraintTypes.NONE;
406 // for now SRLG is the only constraint for link
407 if (link.getlinkType() != OpenroadmLinkType.ROADMTOROADM) {
408 return ConstraintTypes.NONE;
411 List<Long> constraints = new ArrayList<Long>(pceHardConstraints.getExcludeSRLG());
412 constraints.retainAll(link.getsrlgList());
413 if (!constraints.isEmpty()) {
414 LOG.info("validateLinkConstraints: {}", link.getLinkId().getValue());
415 return ConstraintTypes.HARD_EXCLUDE;
418 return ConstraintTypes.NONE;
421 private void dropOppositeLink(Link link) {
422 LinkId opplink = MapUtils.extractOppositeLink(link);
424 PceLink oppPceLink = allPceLinks.get(opplink);
425 if (oppPceLink != null) {
426 allPceLinks.remove(oppPceLink);
428 linksToExclude.add(opplink);
432 private Boolean endPceNode(OpenroadmNodeType openroadmNodeType, NodeId nodeId, PceNode pceNode) {
434 switch (openroadmNodeType) {
436 pceNode.initSrgTps();
437 this.azSrgs.add(nodeId);
440 pceNode.initXndrTps();
444 LOG.warn("endPceNode: Node {} is not SRG or XPONDER !", nodeId);
450 public PceNode getaendPceNode() {
454 public PceNode getzendPceNode() {
458 public Map<NodeId, PceNode> getAllPceNodes() {
459 return this.allPceNodes;
462 public Map<LinkId, PceLink> getAllPceLinks() {
463 return this.allPceLinks;
466 public PceResult getReturnStructure() {
467 return returnStructure;
470 private static void printNodesInfo(Map<NodeId, PceNode> allpcenodes) {
471 Iterator<Map.Entry<NodeId, PceNode>> nodes = allpcenodes.entrySet().iterator();
472 while (nodes.hasNext()) {
473 PceNode pcenode = nodes.next().getValue();
474 List<PceLink> links = pcenode.getOutgoingLinks();
475 LOG.info("In printNodes in node {} : outgoing links {} ", pcenode.getNodeId().getValue(), links.toString());
479 /*private static void printLinksInfo(Map<LinkId, PceLink> allpcelinks) {
480 Iterator<Map.Entry<LinkId, PceLink>> links = allpcelinks.entrySet().iterator();
481 while (links.hasNext()) {
482 LOG.info("In printLinksInfo link {} : ", links.next().getValue().toString());