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
8 package org.opendaylight.transportpce.pce;
10 import com.google.common.base.Optional;
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.HashSet;
15 import java.util.List;
17 import java.util.concurrent.ExecutionException;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.transportpce.common.NetworkUtils;
23 import org.opendaylight.transportpce.common.ResponseCodes;
24 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev171017.PathComputationRequestInput;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev170929.Node1;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev170929.OpenroadmNodeType;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev150608.Network;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev150608.NetworkId;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev150608.NetworkKey;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev150608.NodeId;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev150608.network.Node;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev150608.LinkId;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev150608.Network1;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev150608.network.Link;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 public class PceCalculation {
41 private static final Logger LOG = LoggerFactory.getLogger(PceCalculation.class);
42 private DataBroker dataBroker = null;
43 ///////////// data parsed from Input/////////////////
44 private PathComputationRequestInput input;
45 private String anodeId = "";
46 private String znodeId = "";
47 private PceConstraints pceHardConstraints;
48 private PceConstraints pceSoftConstraints;
49 ///////////// Intermediate data/////////////////
50 private List<PceLink> addLinks = new ArrayList<PceLink>();
51 private List<PceLink> dropLinks = new ArrayList<PceLink>();
52 private HashSet<NodeId> azSrgs = new HashSet<NodeId>();
53 private PceNode aendPceNode = null;
54 private PceNode zendPceNode = null;
55 private List<Link> allLinks = null;
56 private List<Node> allNodes = null;
57 // this List serves graph calculation
58 private Map<NodeId, PceNode> allPceNodes = new HashMap<NodeId, PceNode>();
59 // this List serves calculation of ZtoA path descritopn
60 // TODO maybe better solution is possible
61 private Map<LinkId, PceLink> allPceLinks = new HashMap<LinkId, PceLink>();
62 private PceResult returnStructure;
64 public PceResult getReturnStructure() {
65 return this.returnStructure;
68 public enum NodeConstraint {
69 NONE, HARD_EXCLUDE, HARD_INCLUDE, HARD_DIVERSITY, SOFT_EXCLUDE, SOFT_INCLUDE, SOFT_DIVERSITY;
72 // private static final String NETWORK_ID = "Transport Overlay";
73 public PceCalculation(PathComputationRequestInput input, DataBroker dataBroker, PceConstraints pceHardConstraints,
74 PceConstraints pceSoftConstraints, PceResult rc) {
76 this.dataBroker = dataBroker;
77 this.returnStructure = rc;
78 this.pceHardConstraints = pceHardConstraints;
79 this.pceSoftConstraints = pceSoftConstraints;
83 // apply constraints to get applicable result
84 public void calcPath() {
85 LOG.info("In PceCalculation calcPath: ");
87 this.returnStructure.setRC(ResponseCodes.RESPONSE_FAILED);
91 this.returnStructure.setRC(ResponseCodes.RESPONSE_FAILED);
94 this.returnStructure.setRC(ResponseCodes.RESPONSE_OK);
98 private boolean parseInput() {
99 this.anodeId = this.input.getServiceAEnd().getNodeId();
100 this.znodeId = this.input.getServiceZEnd().getNodeId();
101 LOG.info("parseInput: A and Z :[{}] and [{}]", this.anodeId, this.znodeId);
102 this.returnStructure.setRate(this.input.getServiceAEnd().getServiceRate());
106 private boolean readMdSal() {
107 LOG.info("readMdSal: network {}", NetworkUtils.OVERLAY_NETWORK_ID);
108 InstanceIdentifier<Network> nwInstanceIdentifier = InstanceIdentifier
109 .builder(Network.class, new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID))).build();
110 ReadOnlyTransaction readOnlyTransaction = this.dataBroker.newReadOnlyTransaction();
113 Optional<Network> nwOptional =
114 readOnlyTransaction.read(LogicalDatastoreType.CONFIGURATION, nwInstanceIdentifier).get();
115 if (nwOptional.isPresent()) {
116 nw = nwOptional.get();
117 LOG.debug("readMdSal: network nodes: nwOptional.isPresent = true {}", nw.toString());
119 } catch (ExecutionException | InterruptedException e) {
120 LOG.error("readMdSal: Error reading topology {}", nwInstanceIdentifier);
121 readOnlyTransaction.close();
122 this.returnStructure.setRC(ResponseCodes.RESPONSE_FAILED);
123 throw new RuntimeException(
124 "readMdSal: Error reading from operational store, topology : " + nwInstanceIdentifier + " :" + e);
126 readOnlyTransaction.close();
128 LOG.error("readMdSal: network is null: {}", nwInstanceIdentifier);
131 this.allNodes = nw.getNode();
132 Network1 nw1 = nw.augmentation(Network1.class);
133 this.allLinks = nw1.getLink();
134 if ((this.allNodes == null) || this.allNodes.isEmpty()) {
135 LOG.error("readMdSal: no nodes ");
138 LOG.info("readMdSal: network nodes: {} nodes added", this.allNodes.size());
139 if ((this.allLinks == null) || this.allLinks.isEmpty()) {
140 LOG.error("readMdSal: no links ");
143 LOG.info("readMdSal: network links: {} links added", this.allLinks.size());
147 private boolean analyzeNw() {
148 LOG.debug("analyzeNw: allNodes size {}, allLinks size {}", this.allNodes.size(), this.allLinks.size());
149 for (Node node : this.allNodes) {
152 LOG.info("analyzeNw: allPceNodes size {} : {}", this.allPceNodes.size(), this.allPceNodes.toString());
153 if ((this.aendPceNode == null) || (this.zendPceNode == null)) {
154 LOG.error("analyzeNw: Error in reading nodes: A or Z do not present in the network");
157 for (Link link : this.allLinks) {
160 LOG.debug("analyzeNw: AddLinks size {}, DropLinks size {}", this.addLinks.size(), this.dropLinks.size());
162 LOG.info("analyzeNw: AZSrgs size = {}", this.azSrgs.size());
163 for (NodeId srg : this.azSrgs) {
164 LOG.info("analyzeNw: A/Z Srgs SRG = {}", srg.getValue());
167 for (PceLink link : this.addLinks) {
168 filterAddLinks(link);
170 for (PceLink link : this.dropLinks) {
171 filterDropLinks(link);
173 LOG.info("analyzeNw: allPceNodes size {}, allPceLinks size {}", this.allPceNodes.size(), this.allPceLinks
178 private boolean filterAddLinks(PceLink pcelink) {
179 NodeId nodeId = pcelink.getSourceId();
180 if (this.azSrgs.contains(nodeId)) {
181 this.allPceLinks.put(pcelink.getLinkId(), pcelink);
182 this.allPceNodes.get(nodeId).addOutgoingLink(pcelink);
183 LOG.info("analyzeNw: Add_LINK added to source and to allPceLinks {}", pcelink.getLinkId().toString());
186 // remove the SRG from PceNodes, as it is not directly connected to A/Z
187 this.allPceNodes.remove(nodeId);
188 LOG.info("analyzeNw: SRG removed {}", nodeId.getValue());
192 private boolean filterDropLinks(PceLink pcelink) {
193 NodeId nodeId = pcelink.getDestId();
194 if (this.azSrgs.contains(nodeId)) {
195 this.allPceLinks.put(pcelink.getLinkId(), pcelink);
196 this.allPceNodes.get(nodeId).addOutgoingLink(pcelink);
197 LOG.info("analyzeNw: Drop_LINK added to dest and to allPceLinks {}", pcelink.getLinkId().toString());
200 // remove the SRG from PceNodes, as it is not directly connected to A/Z
201 this.allPceNodes.remove(pcelink.getDestId());
202 LOG.info("analyzeNw: SRG removed {}", nodeId.getValue());
206 private boolean validateLink(Link link) {
207 LOG.info("validateLink: link {} ", link.toString());
208 NodeId sourceId = link.getSource().getSourceNode();
209 NodeId destId = link.getDestination().getDestNode();
210 PceNode source = this.allPceNodes.get(sourceId);
211 PceNode dest = this.allPceNodes.get(destId);
212 if (source == null) {
213 LOG.warn("validateLink: source node is rejected by node validation - {}", link.getSource().getSourceNode()
218 LOG.warn("validateLink: dest node is rejected by node validation - {}", link.getDestination().getDestNode()
222 PceLink pcelink = new PceLink(link);
223 if (!pcelink.isValid()) {
224 LOG.error(" validateLink: Link is ignored due errors in network data ");
227 LinkId linkId = pcelink.getLinkId();
228 switch (pcelink.getLinkType()) {
230 this.allPceLinks.put(linkId, pcelink);
231 source.addOutgoingLink(pcelink);
232 LOG.info("validateLink: ROADMTOROADM-LINK added to allPceLinks {}", pcelink.toString());
235 this.allPceLinks.put(linkId, pcelink);
236 source.addOutgoingLink(pcelink);
237 LOG.info("validateLink: EXPRESS-LINK added to allPceLinks {}", pcelink.toString());
240 pcelink.setClient(source.getRdmSrgClient(pcelink.getSourceTP().toString()));
241 this.addLinks.add(pcelink);
242 LOG.debug("validateLink: ADD-LINK saved {}", pcelink.toString());
245 pcelink.setClient(dest.getRdmSrgClient(pcelink.getDestTP().toString()));
246 this.dropLinks.add(pcelink);
247 LOG.info("validateLink: DROP-LINK saved {}", pcelink.toString());
250 this.azSrgs.add(sourceId);
251 // store separately all SRG links directly connected to A/Z
252 if (!dest.checkTP(pcelink.getDestTP().toString())) {
253 LOG.debug("validateLink: XPONDER-INPUT is rejected as NW port is busy - {} ", pcelink.toString());
256 pcelink.setClient(dest.getXpdrClient(pcelink.getDestTP().toString()));
257 this.allPceLinks.put(linkId, pcelink);
258 source.addOutgoingLink(pcelink);
259 LOG.info("validateLink: XPONDER-INPUT link added to allPceLinks {}", pcelink.toString());
262 // does it mean XPONDER==>>SRG ?
263 this.azSrgs.add(destId);
264 // store separately all SRG links directly connected to A/Z
265 if (!source.checkTP(pcelink.getSourceTP().toString())) {
266 LOG.debug("validateLink: XPONDER-OUTPUT is rejected as NW port is busy - {} ", pcelink.toString());
269 pcelink.setClient(source.getXpdrClient(pcelink.getSourceTP().toString()));
270 this.allPceLinks.put(linkId, pcelink);
271 source.addOutgoingLink(pcelink);
272 LOG.info("validateLink: XPONDER-OUTPUT link added to allPceLinks {}", pcelink.toString());
275 LOG.warn("validateLink: link type is not supported {}", pcelink.toString());
280 private boolean validateNode(Node node) {
281 String supNodeId = "";
282 OpenroadmNodeType nodeType = null;
283 NodeId nodeId = null;
285 LOG.error("validateNode: node is null, ignored ");
289 // TODO: supporting IDs exist as a List. this code takes just the first element
290 nodeId = node.getNodeId();
291 supNodeId = node.getSupportingNode().get(0).getNodeRef().getValue();
292 if (supNodeId.equals("")) {
293 LOG.error("validateNode: Supporting node for node: [{}]. Node is ignored", nodeId.getValue());
297 Node1 node1 = node.augmentation(Node1.class);
299 LOG.error("validateNode: no Node1 (type) Augmentation for node: [{}]. Node is ignored", nodeId
303 nodeType = node1.getNodeType();
304 /** Catch exception 'RuntimeException' is not allowed. [IllegalCatch]. */
305 } catch (NullPointerException e) {
306 LOG.error("validateNode: Error reading supporting node or node type for node '{}'", nodeId, e);
309 if (nodeType == OpenroadmNodeType.XPONDER) {
311 if (supNodeId.equals(this.anodeId) || (supNodeId.equals(this.znodeId))) {
312 LOG.info("validateNode: A or Z node detected == {}", node.getNodeId().getValue());
314 LOG.warn("validateNode: XPONDER is ignored == {}", node.getNodeId().getValue());
318 switch (validateNodeConstraints(nodeId.getValue(), supNodeId)) {
320 LOG.info("validateNode: constraints : node is ignored == {}", nodeId.getValue());
325 PceNode pceNode = new PceNode(node, nodeType, nodeId);
326 if (!pceNode.isValid()) {
327 LOG.error(" validateNode: Node is ignored due errors in network data ");
330 if (supNodeId.equals(this.anodeId)) {
331 if (endPceNode(nodeType, nodeId, pceNode)) {
332 if (!pceNode.isValid()) {
333 LOG.error("validateNode: There are no available wavelengths in node {}", nodeId.getValue());
336 this.aendPceNode = pceNode;
339 if (supNodeId.equals(this.znodeId)) {
340 if (endPceNode(nodeType, nodeId, pceNode)) {
341 if (!pceNode.isValid()) {
342 LOG.error("validateNode: There are no available wavelengths in node {}", nodeId.getValue());
345 this.zendPceNode = pceNode;
348 pceNode.initWLlist();
349 if (!pceNode.isValid()) {
350 LOG.error("validateNode: There are no available wavelengths in node {}", nodeId.getValue());
353 this.allPceNodes.put(nodeId, pceNode);
354 LOG.info("validateNode: node is saved {}", nodeId.getValue());
358 private Boolean endPceNode(OpenroadmNodeType openroadmNodeType, NodeId nodeId, PceNode pceNode) {
360 switch (openroadmNodeType) {
362 pceNode.initRdmSrgTps();
363 this.azSrgs.add(nodeId);
366 pceNode.initXndrTps();
370 LOG.warn("endPceNode: Node {} is not SRG or XPONDER !", nodeId);
376 private NodeConstraint validateNodeConstraints(String nodeId, String supNodeId) {
377 if (this.pceHardConstraints.getExcludeNodes().contains(nodeId)) {
378 return NodeConstraint.HARD_EXCLUDE;
380 if (this.pceHardConstraints.getExcludeNodes().contains(supNodeId)) {
381 return NodeConstraint.HARD_EXCLUDE;
383 if (this.pceHardConstraints.getIncludeNodes().contains(nodeId)) {
384 return NodeConstraint.HARD_INCLUDE;
386 if (this.pceHardConstraints.getIncludeNodes().contains(supNodeId)) {
387 return NodeConstraint.HARD_INCLUDE;
389 return NodeConstraint.NONE;
392 public PceNode getaPceNode() {
393 return this.aendPceNode;
396 public PceNode getzPceNode() {
397 return this.zendPceNode;
400 public Map<NodeId, PceNode> getAllPceNodes() {
401 return this.allPceNodes;
404 public Map<LinkId, PceLink> getAllPceLinks() {
405 return this.allPceLinks;