Merge "ROADM To ROADM Path Calculation"
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / PceCalculation.java
1 /*
2  * Copyright © 2017 AT&T, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.transportpce.pce;
9
10 import com.google.common.base.Optional;
11
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.HashSet;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.concurrent.ExecutionException;
18
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;
38
39 public class PceCalculation {
40     /* Logging. */
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;
63
64     public PceResult getReturnStructure() {
65         return this.returnStructure;
66     }
67
68     public enum NodeConstraint {
69         NONE, HARD_EXCLUDE, HARD_INCLUDE, HARD_DIVERSITY, SOFT_EXCLUDE, SOFT_INCLUDE, SOFT_DIVERSITY;
70     }
71
72     // private static final String NETWORK_ID = "Transport Overlay";
73     public PceCalculation(PathComputationRequestInput input, DataBroker dataBroker, PceConstraints pceHardConstraints,
74             PceConstraints pceSoftConstraints, PceResult rc) {
75         this.input = input;
76         this.dataBroker = dataBroker;
77         this.returnStructure = rc;
78         this.pceHardConstraints = pceHardConstraints;
79         this.pceSoftConstraints = pceSoftConstraints;
80         parseInput();
81     }
82
83     // apply constraints to get applicable result
84     public void calcPath() {
85         LOG.info("In PceCalculation calcPath: ");
86         if (!readMdSal()) {
87             this.returnStructure.setRC(ResponseCodes.RESPONSE_FAILED);
88             return;
89         }
90         if (!analyzeNw()) {
91             this.returnStructure.setRC(ResponseCodes.RESPONSE_FAILED);
92             return;
93         }
94         this.returnStructure.setRC(ResponseCodes.RESPONSE_OK);
95         return;
96     }
97
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());
103         return true;
104     }
105
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();
111         Network nw = null;
112         try {
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());
118             }
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);
125         }
126         readOnlyTransaction.close();
127         if (nw == null) {
128             LOG.error("readMdSal: network is null: {}", nwInstanceIdentifier);
129             return false;
130         }
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 ");
136             return false;
137         }
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 ");
141             return false;
142         }
143         LOG.info("readMdSal: network links: {} links added", this.allLinks.size());
144         return true;
145     }
146
147     private boolean analyzeNw() {
148         LOG.debug("analyzeNw: allNodes size {}, allLinks size {}", this.allNodes.size(), this.allLinks.size());
149         for (Node node : this.allNodes) {
150             validateNode(node);
151         }
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");
155             return false;
156         }
157         for (Link link : this.allLinks) {
158             validateLink(link);
159         }
160         LOG.debug("analyzeNw: AddLinks size {}, DropLinks size {}", this.addLinks.size(), this.dropLinks.size());
161         // debug prints
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());
165         }
166         // debug prints
167         for (PceLink link : this.addLinks) {
168             filterAddLinks(link);
169         }
170         for (PceLink link : this.dropLinks) {
171             filterDropLinks(link);
172         }
173         LOG.info("analyzeNw: allPceNodes size {}, allPceLinks size {}", this.allPceNodes.size(), this.allPceLinks
174                 .size());
175         return true;
176     }
177
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());
184             return true;
185         }
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());
189         return false;
190     }
191
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());
198             return true;
199         }
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());
203         return false;
204     }
205
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()
214                     .getValue());
215             return false;
216         }
217         if (dest == null) {
218             LOG.warn("validateLink: dest node is rejected by node validation - {}", link.getDestination().getDestNode()
219                     .getValue());
220             return false;
221         }
222         PceLink pcelink = new PceLink(link);
223         if (!pcelink.isValid()) {
224             LOG.error(" validateLink: Link is ignored due errors in network data ");
225             return false;
226         }
227         LinkId linkId = pcelink.getLinkId();
228         switch (pcelink.getLinkType()) {
229             case ROADMTOROADM :
230                 this.allPceLinks.put(linkId, pcelink);
231                 source.addOutgoingLink(pcelink);
232                 LOG.info("validateLink: ROADMTOROADM-LINK added to allPceLinks {}", pcelink.toString());
233                 break;
234             case EXPRESSLINK :
235                 this.allPceLinks.put(linkId, pcelink);
236                 source.addOutgoingLink(pcelink);
237                 LOG.info("validateLink: EXPRESS-LINK added to allPceLinks {}", pcelink.toString());
238                 break;
239             case ADDLINK :
240                 pcelink.setClient(source.getRdmSrgClient(pcelink.getSourceTP().toString()));
241                 this.addLinks.add(pcelink);
242                 LOG.debug("validateLink: ADD-LINK saved  {}", pcelink.toString());
243                 break;
244             case DROPLINK :
245                 pcelink.setClient(dest.getRdmSrgClient(pcelink.getDestTP().toString()));
246                 this.dropLinks.add(pcelink);
247                 LOG.info("validateLink: DROP-LINK saved  {}", pcelink.toString());
248                 break;
249             case XPONDERINPUT :
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());
254                     return false;
255                 }
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());
260                 break;
261             case XPONDEROUTPUT :
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());
267                     return false;
268                 }
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());
273                 break;
274             default:
275                 LOG.warn("validateLink: link type is not supported {}", pcelink.toString());
276         }
277         return true;
278     }
279
280     private boolean validateNode(Node node) {
281         String supNodeId = "";
282         OpenroadmNodeType nodeType = null;
283         NodeId nodeId = null;
284         if (node == null) {
285             LOG.error("validateNode: node is null, ignored ");
286             return false;
287         }
288         try {
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());
294                 return false;
295             }
296             // extract node type
297             Node1 node1 = node.augmentation(Node1.class);
298             if (node1 == null) {
299                 LOG.error("validateNode: no Node1 (type) Augmentation for node: [{}]. Node is ignored", nodeId
300                         .getValue());
301                 return false;
302             }
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);
307             return false;
308         }
309         if (nodeType == OpenroadmNodeType.XPONDER) {
310             // Detect A and Z
311             if (supNodeId.equals(this.anodeId) || (supNodeId.equals(this.znodeId))) {
312                 LOG.info("validateNode: A or Z node detected == {}", node.getNodeId().getValue());
313             } else {
314                 LOG.warn("validateNode: XPONDER is ignored == {}", node.getNodeId().getValue());
315                 return false;
316             }
317         }
318         switch (validateNodeConstraints(nodeId.getValue(), supNodeId)) {
319             case HARD_EXCLUDE :
320                 LOG.info("validateNode: constraints : node is ignored == {}", nodeId.getValue());
321                 return false;
322             default:
323                 break;
324         }
325         PceNode pceNode = new PceNode(node, nodeType, nodeId);
326         if (!pceNode.isValid()) {
327             LOG.error(" validateNode: Node is ignored due errors in network data ");
328             return false;
329         }
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());
334                     return false;
335                 }
336                 this.aendPceNode = pceNode;
337             }
338         }
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());
343                     return false;
344                 }
345                 this.zendPceNode = pceNode;
346             }
347         }
348         pceNode.initWLlist();
349         if (!pceNode.isValid()) {
350             LOG.error("validateNode: There are no available wavelengths in node {}", nodeId.getValue());
351             return false;
352         }
353         this.allPceNodes.put(nodeId, pceNode);
354         LOG.info("validateNode: node is saved {}", nodeId.getValue());
355         return true;
356     }
357
358     private Boolean endPceNode(OpenroadmNodeType openroadmNodeType, NodeId nodeId, PceNode pceNode) {
359         Boolean add = true;
360         switch (openroadmNodeType) {
361             case SRG :
362                 pceNode.initRdmSrgTps();
363                 this.azSrgs.add(nodeId);
364                 break;
365             case XPONDER :
366                 pceNode.initXndrTps();
367                 break;
368             default:
369                 add = false;
370                 LOG.warn("endPceNode: Node {} is not SRG or XPONDER !", nodeId);
371                 break;
372         }
373         return add;
374     }
375
376     private NodeConstraint validateNodeConstraints(String nodeId, String supNodeId) {
377         if (this.pceHardConstraints.getExcludeNodes().contains(nodeId)) {
378             return NodeConstraint.HARD_EXCLUDE;
379         }
380         if (this.pceHardConstraints.getExcludeNodes().contains(supNodeId)) {
381             return NodeConstraint.HARD_EXCLUDE;
382         }
383         if (this.pceHardConstraints.getIncludeNodes().contains(nodeId)) {
384             return NodeConstraint.HARD_INCLUDE;
385         }
386         if (this.pceHardConstraints.getIncludeNodes().contains(supNodeId)) {
387             return NodeConstraint.HARD_INCLUDE;
388         }
389         return NodeConstraint.NONE;
390     }
391
392     public PceNode getaPceNode() {
393         return this.aendPceNode;
394     }
395
396     public PceNode getzPceNode() {
397         return this.zendPceNode;
398     }
399
400     public Map<NodeId, PceNode> getAllPceNodes() {
401         return this.allPceNodes;
402     }
403
404     public Map<LinkId, PceLink> getAllPceLinks() {
405         return this.allPceLinks;
406     }
407 }