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;
11 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;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.transportpce.common.NetworkUtils;
22 import org.opendaylight.transportpce.common.ResponseCodes;
23 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev171017.PathComputationRequestInput;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev170929.Node1;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev170929.OpenroadmNodeType;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev150608.Network;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev150608.NetworkId;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev150608.NetworkKey;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev150608.NodeId;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev150608.network.Node;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev150608.LinkId;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev150608.Network1;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev150608.network.Link;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 public class PceCalculation {
40 private static final Logger LOG = LoggerFactory.getLogger(PceCalculation.class);
41 private DataBroker dataBroker = null;
43 ///////////// data parsed from Input/////////////////
44 private PathComputationRequestInput input;
45 private String anodeId = "";
46 private String znodeId = "";
48 private PceConstraints pceHardConstraints;
49 private PceConstraints pceSoftConstraints;
51 ///////////// Intermediate data/////////////////
52 private List<PceLink> addLinks = new ArrayList<PceLink>();
53 private List<PceLink> dropLinks = new ArrayList<PceLink>();
54 private HashSet<NodeId> azSrgs = new HashSet<NodeId>();
56 private PceNode aendPceNode = null;
57 private PceNode zendPceNode = null;
59 private List<Link> allLinks = null;
60 private List<Node> allNodes = null;
62 // this List serves graph calculation
63 private Map<NodeId, PceNode> allPceNodes = new HashMap<NodeId, PceNode>();
64 // this List serves calculation of ZtoA path descritopn
65 // TODO maybe better solution is possible
66 private Map<LinkId, PceLink> allPceLinks = new HashMap<LinkId, PceLink>();
68 private PceResult returnStructure;
70 public PceResult getReturnStructure() {
71 return this.returnStructure;
74 public enum NodeConstraint {
75 NONE, HARD_EXCLUDE, HARD_INCLUDE, HARD_DIVERSITY, SOFT_EXCLUDE, SOFT_INCLUDE, SOFT_DIVERSITY;
78 //private static final String NETWORK_ID = "Transport Overlay";
80 public PceCalculation(PathComputationRequestInput input, DataBroker dataBroker,
81 PceConstraints pceHardConstraints, PceConstraints pceSoftConstraints, PceResult rc) {
83 this.dataBroker = dataBroker;
84 this.returnStructure = rc;
86 this.pceHardConstraints = pceHardConstraints;
87 this.pceSoftConstraints = pceSoftConstraints;
92 // apply constraints to get applicable result
93 public void calcPath() {
95 LOG.info("In PceCalculation calcPath: ");
98 this.returnStructure.setRC(ResponseCodes.RESPONSE_FAILED);
103 this.returnStructure.setRC(ResponseCodes.RESPONSE_FAILED);
107 this.returnStructure.setRC(ResponseCodes.RESPONSE_OK);
111 private boolean parseInput() {
112 this.anodeId = this.input.getServiceAEnd().getNodeId();
113 this.znodeId = this.input.getServiceZEnd().getNodeId();
114 LOG.info("parseInput: A and Z :[{}] and [{}]", this.anodeId, this.znodeId);
115 this.returnStructure.setRate(this.input.getServiceAEnd().getServiceRate());
119 private boolean readMdSal() {
120 LOG.info("readMdSal: network {}", NetworkUtils.OVERLAY_NETWORK_ID);
122 InstanceIdentifier<Network> nwInstanceIdentifier = InstanceIdentifier
123 .builder(Network.class, new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID))).build();
125 ReadOnlyTransaction readOnlyTransaction = this.dataBroker.newReadOnlyTransaction();
129 Optional<Network> nwOptional = readOnlyTransaction
130 .read(LogicalDatastoreType.CONFIGURATION, nwInstanceIdentifier).get();
131 if (nwOptional.isPresent()) {
132 nw = nwOptional.get();
133 LOG.debug("readMdSal: network nodes: nwOptional.isPresent = true {}", nw.toString());
135 } catch (ExecutionException | InterruptedException e) {
136 LOG.error("readMdSal: Error reading topology {}", nwInstanceIdentifier);
137 readOnlyTransaction.close();
138 this.returnStructure.setRC(ResponseCodes.RESPONSE_FAILED);
139 throw new RuntimeException(
140 "readMdSal: Error reading from operational store, topology : " + nwInstanceIdentifier + " :" + e);
143 readOnlyTransaction.close();
146 LOG.error("readMdSal: network is null: {}", nwInstanceIdentifier);
149 this.allNodes = nw.getNode();
150 Network1 nw1 = nw.augmentation(Network1.class);
152 this.allLinks = nw1.getLink();
153 if ((this.allNodes == null) || this.allNodes.isEmpty()) {
154 LOG.error("readMdSal: no nodes ");
157 LOG.info("readMdSal: network nodes: {} nodes added", this.allNodes.size());
159 if ((this.allLinks == null) || this.allLinks.isEmpty()) {
160 LOG.error("readMdSal: no links ");
163 LOG.info("readMdSal: network links: {} links added", this.allLinks.size());
168 private boolean analyzeNw() {
170 LOG.debug("analyzeNw: allNodes size {}, allLinks size {}", this.allNodes.size(), this.allLinks.size());
172 for (Node node : this.allNodes) {
175 LOG.debug("analyzeNw: allPceNodes size {}", this.allPceNodes.size());
177 if ((this.aendPceNode == null) || (this.zendPceNode == null)) {
178 LOG.error("analyzeNw: Error in reading nodes: A or Z do not present in the network");
182 for (Link link : this.allLinks) {
186 LOG.debug("analyzeNw: AddLinks size {}, DropLinks size {}", this.addLinks.size(), this.dropLinks.size());
189 LOG.info("analyzeNw: AZSrgs size = {}", this.azSrgs.size());
190 for (NodeId srg : this.azSrgs) {
191 LOG.info("analyzeNw: A/Z Srgs SRG = {}", srg.getValue());
195 for (PceLink link : this.addLinks) {
196 filterAddLinks(link);
198 for (PceLink link : this.dropLinks) {
199 filterDropLinks(link);
202 LOG.info("analyzeNw: allPceNodes size {}, allPceLinks size {}", this.allPceNodes.size(),
203 this.allPceLinks.size());
208 private boolean filterAddLinks(PceLink pcelink) {
210 NodeId nodeId = pcelink.getSourceId();
212 if (this.azSrgs.contains(nodeId)) {
213 this.allPceLinks.put(pcelink.getLinkId(), pcelink);
214 this.allPceNodes.get(nodeId).addOutgoingLink(pcelink);
215 LOG.info("analyzeNw: Add_LINK added to source and to allPceLinks {}", pcelink.getLinkId().toString());
219 // remove the SRG from PceNodes, as it is not directly connected to A/Z
220 this.allPceNodes.remove(nodeId);
221 LOG.debug("analyzeNw: SRG removed {}", nodeId.getValue());
226 private boolean filterDropLinks(PceLink pcelink) {
228 NodeId nodeId = pcelink.getDestId();
230 if (this.azSrgs.contains(nodeId)) {
231 this.allPceLinks.put(pcelink.getLinkId(), pcelink);
232 this.allPceNodes.get(nodeId).addOutgoingLink(pcelink);
233 LOG.info("analyzeNw: Drop_LINK added to dest and to allPceLinks {}", pcelink.getLinkId().toString());
237 // remove the SRG from PceNodes, as it is not directly connected to A/Z
238 this.allPceNodes.remove(pcelink.getDestId());
239 LOG.debug("analyzeNw: SRG removed {}", nodeId.getValue());
244 private boolean validateLink(Link link) {
246 LOG.debug("validateLink: link {} ", link.toString());
248 NodeId sourceId = link.getSource().getSourceNode();
249 NodeId destId = link.getDestination().getDestNode();
250 PceNode source = this.allPceNodes.get(sourceId);
251 PceNode dest = this.allPceNodes.get(destId);
253 if (source == null) {
254 LOG.debug("validateLink: source node is rejected by node validation - {}",
255 link.getSource().getSourceNode().getValue());
259 LOG.debug("validateLink: dest node is rejected by node validation - {}",
260 link.getDestination().getDestNode().getValue());
264 PceLink pcelink = new PceLink(link);
265 if (!pcelink.isValid()) {
266 LOG.error(" validateLink: Link is ignored due errors in network data ");
269 LinkId linkId = pcelink.getLinkId();
271 switch (pcelink.getLinkType()) {
273 this.allPceLinks.put(linkId, pcelink);
274 source.addOutgoingLink(pcelink);
275 LOG.info("validateLink: ROADMTOROADM-LINK added to allPceLinks {}", pcelink.toString());
279 this.allPceLinks.put(linkId, pcelink);
280 source.addOutgoingLink(pcelink);
281 LOG.info("validateLink: EXPRESS-LINK added to allPceLinks {}", pcelink.toString());
285 this.addLinks.add(pcelink);
286 LOG.debug("validateLink: ADD-LINK saved {}", pcelink.toString());
290 this.dropLinks.add(pcelink);
291 LOG.debug("validateLink: DROP-LINK saved {}", pcelink.toString());
295 this.azSrgs.add(sourceId);
296 // store separately all SRG links directly connected to A/Z
298 if (!dest.checkTP(pcelink.getDestTP().toString())) {
299 LOG.debug("validateLink: XPONDER-INPUT is rejected as NW port is busy - {} ", pcelink.toString());
303 pcelink.setClient(dest.getClient(pcelink.getDestTP().toString()));
304 this.allPceLinks.put(linkId, pcelink);
305 source.addOutgoingLink(pcelink);
306 LOG.info("validateLink: XPONDER-INPUT link added to allPceLinks {}", pcelink.toString());
311 // does it mean XPONDER==>>SRG ?
312 this.azSrgs.add(destId);
313 // store separately all SRG links directly connected to A/Z
315 if (!source.checkTP(pcelink.getSourceTP().toString())) {
316 LOG.debug("validateLink: XPONDER-OUTPUT is rejected as NW port is busy - {} ", pcelink.toString());
320 pcelink.setClient(source.getClient(pcelink.getSourceTP().toString()));
321 this.allPceLinks.put(linkId, pcelink);
322 source.addOutgoingLink(pcelink);
323 LOG.info("validateLink: XPONDER-OUTPUT link added to allPceLinks {}", pcelink.toString());
328 LOG.warn("validateLink: link type is not supported {}", pcelink.toString());
335 private boolean validateNode(Node node) {
336 String supNodeId = "";
337 OpenroadmNodeType nodeType = null;
338 NodeId nodeId = null;
340 LOG.error("validateNode: node is null, ignored ");
345 // TODO: supporting IDs exist as a List. this code takes just the first element
346 nodeId = node.getNodeId();
347 supNodeId = node.getSupportingNode().get(0).getNodeRef().getValue();
348 if (supNodeId.equals("")) {
349 LOG.error("validateNode: Supporting node for node: [{}]. Node is ignored", nodeId.getValue());
354 Node1 node1 = node.augmentation(Node1.class);
356 LOG.error("validateNode: no Node1 (type) Augmentation for node: [{}]. Node is ignored",
360 nodeType = node1.getNodeType();
361 /** Catch exception 'RuntimeException' is not allowed. [IllegalCatch]. */
362 } catch (NullPointerException e) {
363 LOG.error("validateNode: Error reading supporting node or node type for node '{}'", nodeId,e);
367 if (nodeType == OpenroadmNodeType.XPONDER) {
370 if (supNodeId.equals(this.anodeId) || (supNodeId.equals(this.znodeId))) {
371 LOG.info("validateNode: A or Z node detected == {}", node.getNodeId().getValue());
373 LOG.debug("validateNode: XPONDER is ignored == {}", node.getNodeId().getValue());
379 switch (validateNodeConstraints(nodeId.getValue(), supNodeId)) {
381 LOG.info("validateNode: constraints : node is ignored == {}", nodeId.getValue());
388 PceNode pceNode = new PceNode(node, nodeType, nodeId);
389 if (! pceNode.isValid()) {
390 LOG.error(" validateNode: Node is ignored due errors in network data ");
394 if (supNodeId.equals(this.anodeId)) {
395 pceNode.initXndrTps();
396 if (!pceNode.isValid()) {
397 LOG.error("validateNode: A doesn't have free network ports");
400 this.aendPceNode = pceNode;
402 if (supNodeId.equals(this.znodeId)) {
403 pceNode.initXndrTps();
404 if (!pceNode.isValid()) {
405 LOG.error("validateNode: Z doesn't have free network ports");
408 this.zendPceNode = pceNode;
411 pceNode.initWLlist();
412 if (!pceNode.isValid()) {
413 LOG.error("validateNode: There are no available wavelengths in node {}", nodeId.getValue());
417 this.allPceNodes.put(nodeId, pceNode);
418 LOG.debug("validateNode: node is saved {}", nodeId.getValue());
422 private NodeConstraint validateNodeConstraints(String nodeId, String supNodeId) {
424 if (this.pceHardConstraints.getExcludeNodes().contains(nodeId)) {
425 return NodeConstraint.HARD_EXCLUDE;
427 if (this.pceHardConstraints.getExcludeNodes().contains(supNodeId)) {
428 return NodeConstraint.HARD_EXCLUDE;
431 if (this.pceHardConstraints.getIncludeNodes().contains(nodeId)) {
432 return NodeConstraint.HARD_INCLUDE;
434 if (this.pceHardConstraints.getIncludeNodes().contains(supNodeId)) {
435 return NodeConstraint.HARD_INCLUDE;
439 return NodeConstraint.NONE;
442 public PceNode getaPceNode() {
443 return this.aendPceNode;
446 public PceNode getzPceNode() {
447 return this.zendPceNode;
450 public Map<NodeId, PceNode> getAllPceNodes() {
451 return this.allPceNodes;
454 public Map<LinkId, PceLink> getAllPceLinks() {
455 return this.allPceLinks;