Update Functional tests for SP1.6
[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
9 package org.opendaylight.transportpce.pce;
10
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;
16 import java.util.Map;
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;
37
38 public class PceCalculation {
39     /* Logging. */
40     private static final Logger LOG = LoggerFactory.getLogger(PceCalculation.class);
41     private DataBroker dataBroker = null;
42
43     ///////////// data parsed from Input/////////////////
44     private PathComputationRequestInput input;
45     private String anodeId = "";
46     private String znodeId = "";
47
48     private PceConstraints pceHardConstraints;
49     private PceConstraints pceSoftConstraints;
50
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>();
55
56     private PceNode aendPceNode = null;
57     private PceNode zendPceNode = null;
58
59     private List<Link> allLinks = null;
60     private List<Node> allNodes = null;
61
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>();
67
68     private PceResult returnStructure;
69
70     public PceResult getReturnStructure() {
71         return this.returnStructure;
72     }
73
74     public enum NodeConstraint {
75         NONE, HARD_EXCLUDE, HARD_INCLUDE, HARD_DIVERSITY, SOFT_EXCLUDE, SOFT_INCLUDE, SOFT_DIVERSITY;
76     }
77
78     //private static final String NETWORK_ID = "Transport Overlay";
79
80     public PceCalculation(PathComputationRequestInput input, DataBroker dataBroker,
81             PceConstraints pceHardConstraints, PceConstraints pceSoftConstraints, PceResult rc) {
82         this.input = input;
83         this.dataBroker = dataBroker;
84         this.returnStructure = rc;
85
86         this.pceHardConstraints = pceHardConstraints;
87         this.pceSoftConstraints = pceSoftConstraints;
88
89         parseInput();
90     }
91
92     // apply constraints to get applicable result
93     public void calcPath() {
94
95         LOG.info("In PceCalculation calcPath: ");
96
97         if (!readMdSal()) {
98             this.returnStructure.setRC(ResponseCodes.RESPONSE_FAILED);
99             return;
100         }
101
102         if (!analyzeNw()) {
103             this.returnStructure.setRC(ResponseCodes.RESPONSE_FAILED);
104             return;
105         }
106
107         this.returnStructure.setRC(ResponseCodes.RESPONSE_OK);
108         return;
109     }
110
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());
116         return true;
117     }
118
119     private boolean readMdSal() {
120         LOG.info("readMdSal: network {}", NetworkUtils.OVERLAY_NETWORK_ID);
121
122         InstanceIdentifier<Network> nwInstanceIdentifier = InstanceIdentifier
123                 .builder(Network.class, new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID))).build();
124
125         ReadOnlyTransaction readOnlyTransaction = this.dataBroker.newReadOnlyTransaction();
126
127         Network nw = null;
128         try {
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());
134             }
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);
141         }
142
143         readOnlyTransaction.close();
144
145         if (nw == null) {
146             LOG.error("readMdSal: network is null: {}", nwInstanceIdentifier);
147             return false;
148         }
149         this.allNodes = nw.getNode();
150         Network1 nw1 = nw.augmentation(Network1.class);
151
152         this.allLinks = nw1.getLink();
153         if ((this.allNodes == null) || this.allNodes.isEmpty()) {
154             LOG.error("readMdSal: no nodes ");
155             return false;
156         }
157         LOG.info("readMdSal: network nodes: {} nodes added", this.allNodes.size());
158
159         if ((this.allLinks == null) || this.allLinks.isEmpty()) {
160             LOG.error("readMdSal: no links ");
161             return false;
162         }
163         LOG.info("readMdSal: network links: {} links added", this.allLinks.size());
164
165         return true;
166     }
167
168     private boolean analyzeNw() {
169
170         LOG.debug("analyzeNw: allNodes size {}, allLinks size {}", this.allNodes.size(), this.allLinks.size());
171
172         for (Node node : this.allNodes) {
173             validateNode(node);
174         }
175         LOG.debug("analyzeNw: allPceNodes size {}", this.allPceNodes.size());
176
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");
179             return false;
180         }
181
182         for (Link link : this.allLinks) {
183             validateLink(link);
184         }
185
186         LOG.debug("analyzeNw: AddLinks size {}, DropLinks size {}", this.addLinks.size(), this.dropLinks.size());
187
188         // debug prints
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());
192         }
193         // debug prints
194
195         for (PceLink link : this.addLinks) {
196             filterAddLinks(link);
197         }
198         for (PceLink link : this.dropLinks) {
199             filterDropLinks(link);
200         }
201
202         LOG.info("analyzeNw: allPceNodes size {}, allPceLinks size {}", this.allPceNodes.size(),
203             this.allPceLinks.size());
204
205         return true;
206     }
207
208     private boolean filterAddLinks(PceLink pcelink) {
209
210         NodeId nodeId = pcelink.getSourceId();
211
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());
216             return true;
217         }
218
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());
222
223         return false;
224     }
225
226     private boolean filterDropLinks(PceLink pcelink) {
227
228         NodeId nodeId = pcelink.getDestId();
229
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());
234             return true;
235         }
236
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());
240
241         return false;
242     }
243
244     private boolean validateLink(Link link) {
245
246         LOG.debug("validateLink: link {} ", link.toString());
247
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);
252
253         if (source == null) {
254             LOG.debug("validateLink: source node is rejected by node validation - {}",
255                     link.getSource().getSourceNode().getValue());
256             return false;
257         }
258         if (dest == null) {
259             LOG.debug("validateLink: dest node is rejected by node validation - {}",
260                     link.getDestination().getDestNode().getValue());
261             return false;
262         }
263
264         PceLink pcelink = new PceLink(link);
265         if (!pcelink.isValid()) {
266             LOG.error(" validateLink: Link is ignored due errors in network data ");
267             return false;
268         }
269         LinkId linkId = pcelink.getLinkId();
270
271         switch (pcelink.getLinkType()) {
272             case ROADMTOROADM:
273                 this.allPceLinks.put(linkId, pcelink);
274                 source.addOutgoingLink(pcelink);
275                 LOG.info("validateLink: ROADMTOROADM-LINK added to allPceLinks {}", pcelink.toString());
276                 break;
277
278             case EXPRESSLINK:
279                 this.allPceLinks.put(linkId, pcelink);
280                 source.addOutgoingLink(pcelink);
281                 LOG.info("validateLink: EXPRESS-LINK added to allPceLinks {}", pcelink.toString());
282                 break;
283
284             case ADDLINK:
285                 this.addLinks.add(pcelink);
286                 LOG.debug("validateLink: ADD-LINK saved  {}", pcelink.toString());
287                 break;
288
289             case DROPLINK:
290                 this.dropLinks.add(pcelink);
291                 LOG.debug("validateLink: DROP-LINK saved  {}", pcelink.toString());
292                 break;
293
294             case XPONDERINPUT:
295                 this.azSrgs.add(sourceId);
296                 // store separately all SRG links directly connected to A/Z
297
298                 if (!dest.checkTP(pcelink.getDestTP().toString())) {
299                     LOG.debug("validateLink: XPONDER-INPUT is rejected as NW port is busy - {} ", pcelink.toString());
300                     return false;
301                 }
302
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());
307
308                 break;
309
310             case XPONDEROUTPUT:
311             // does it mean XPONDER==>>SRG ?
312                 this.azSrgs.add(destId);
313                 // store separately all SRG links directly connected to A/Z
314
315                 if (!source.checkTP(pcelink.getSourceTP().toString())) {
316                     LOG.debug("validateLink: XPONDER-OUTPUT is rejected as NW port is busy - {} ", pcelink.toString());
317                     return false;
318                 }
319
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());
324
325                 break;
326
327             default:
328                 LOG.warn("validateLink: link type is not supported {}", pcelink.toString());
329
330         }
331
332         return true;
333     }
334
335     private boolean validateNode(Node node) {
336         String supNodeId = "";
337         OpenroadmNodeType nodeType = null;
338         NodeId nodeId = null;
339         if (node == null) {
340             LOG.error("validateNode: node is null, ignored ");
341             return false;
342         }
343
344         try {
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());
350                 return false;
351             }
352
353             // extract node type
354             Node1 node1 = node.augmentation(Node1.class);
355             if (node1 == null) {
356                 LOG.error("validateNode: no Node1 (type) Augmentation for node: [{}]. Node is ignored",
357                         nodeId.getValue());
358                 return false;
359             }
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);
364             return false;
365         }
366
367         if (nodeType == OpenroadmNodeType.XPONDER) {
368
369             // Detect A and Z
370             if (supNodeId.equals(this.anodeId) || (supNodeId.equals(this.znodeId))) {
371                 LOG.info("validateNode: A or Z node detected == {}", node.getNodeId().getValue());
372             } else {
373                 LOG.debug("validateNode: XPONDER is ignored == {}", node.getNodeId().getValue());
374                 return false;
375
376             }
377         }
378
379         switch (validateNodeConstraints(nodeId.getValue(), supNodeId)) {
380             case HARD_EXCLUDE:
381                 LOG.info("validateNode: constraints : node is ignored == {}", nodeId.getValue());
382                 return false;
383
384             default:
385                 break;
386         }
387
388         PceNode pceNode = new PceNode(node, nodeType, nodeId);
389         if (! pceNode.isValid()) {
390             LOG.error(" validateNode: Node is ignored due errors in network data ");
391             return false;
392         }
393
394         if (supNodeId.equals(this.anodeId)) {
395             pceNode.initXndrTps();
396             if (!pceNode.isValid()) {
397                 LOG.error("validateNode: A doesn't have free network ports");
398                 return false;
399             }
400             this.aendPceNode = pceNode;
401         }
402         if (supNodeId.equals(this.znodeId)) {
403             pceNode.initXndrTps();
404             if (!pceNode.isValid()) {
405                 LOG.error("validateNode: Z doesn't have free network ports");
406                 return false;
407             }
408             this.zendPceNode = pceNode;
409         }
410
411         pceNode.initWLlist();
412         if (!pceNode.isValid()) {
413             LOG.error("validateNode: There are no available wavelengths in node {}", nodeId.getValue());
414             return false;
415         }
416
417         this.allPceNodes.put(nodeId, pceNode);
418         LOG.debug("validateNode: node is saved {}", nodeId.getValue());
419         return true;
420     }
421
422     private NodeConstraint validateNodeConstraints(String nodeId, String supNodeId) {
423
424         if (this.pceHardConstraints.getExcludeNodes().contains(nodeId)) {
425             return NodeConstraint.HARD_EXCLUDE;
426         }
427         if (this.pceHardConstraints.getExcludeNodes().contains(supNodeId)) {
428             return NodeConstraint.HARD_EXCLUDE;
429         }
430
431         if (this.pceHardConstraints.getIncludeNodes().contains(nodeId)) {
432             return NodeConstraint.HARD_INCLUDE;
433         }
434         if (this.pceHardConstraints.getIncludeNodes().contains(supNodeId)) {
435             return NodeConstraint.HARD_INCLUDE;
436         }
437
438
439         return NodeConstraint.NONE;
440     }
441
442     public PceNode getaPceNode() {
443         return this.aendPceNode;
444     }
445
446     public PceNode getzPceNode() {
447         return this.zendPceNode;
448     }
449
450     public Map<NodeId, PceNode> getAllPceNodes() {
451         return this.allPceNodes;
452     }
453
454     public Map<LinkId, PceLink> getAllPceLinks() {
455         return this.allPceLinks;
456     }
457
458
459 }