fix Databroker deprecated warnings
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / networkanalyzer / 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.networkanalyzer;
10
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;
16 import java.util.Map;
17 import java.util.Optional;
18 import java.util.Set;
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;
41
42 public class PceCalculation {
43     /* Logging. */
44     private static final Logger LOG = LoggerFactory.getLogger(PceCalculation.class);
45     private NetworkTransactionService networkTransactionService = null;
46
47     ///////////// data parsed from Input/////////////////
48     private PathComputationRequestInput input;
49     private String anodeId = "";
50     private String znodeId = "";
51
52     private PceConstraints pceHardConstraints;
53
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>();
58
59     private PceNode aendPceNode = null;
60     private PceNode zendPceNode = null;
61
62     private List<Link> allLinks = null;
63     private List<Node> allNodes = null;
64
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;
72
73     private enum ConstraintTypes {
74         NONE, HARD_EXCLUDE, HARD_INCLUDE, HARD_DIVERSITY, SOFT_EXCLUDE, SOFT_INCLUDE, SOFT_DIVERSITY;
75     }
76
77     public PceCalculation(PathComputationRequestInput input, NetworkTransactionService networkTransactionService,
78             PceConstraints pceHardConstraints, PceConstraints pceSoftConstraints, PceResult rc) {
79         this.input = input;
80         this.networkTransactionService = networkTransactionService;
81         this.returnStructure = rc;
82
83         this.pceHardConstraints = pceHardConstraints;
84         parseInput();
85     }
86
87     public void calcPath() {
88
89         LOG.info("In PceCalculation calcPath: ");
90
91         if (!readMdSal()) {
92             returnStructure.setRC(ResponseCodes.RESPONSE_FAILED);
93             return;
94         }
95
96         MapUtils.mapDiversityConstraints(allNodes, allLinks, pceHardConstraints);
97
98         if (!analyzeNw()) {
99             returnStructure.setRC(ResponseCodes.RESPONSE_FAILED);
100             return;
101         }
102
103         printNodesInfo(allPceNodes);
104
105         returnStructure.setRC(ResponseCodes.RESPONSE_OK);
106         return;
107     }
108
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());
114         return true;
115     }
116
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();
121         Network nw = null;
122         try {
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());
128             }
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);
135         }
136         networkTransactionService.close();
137
138         if (nw == null) {
139             LOG.error("readMdSal: network is null: {}", nwInstanceIdentifier);
140             return false;
141         }
142         allNodes = nw.getNode();
143         Network1 nw1 = nw.augmentation(Network1.class);
144
145         allLinks = nw1.getLink();
146         if (allNodes == null || allNodes.isEmpty()) {
147             LOG.error("readMdSal: no nodes ");
148             return false;
149         }
150         LOG.info("readMdSal: network nodes: {} nodes added", allNodes.size());
151         LOG.debug("readMdSal: network nodes: {} nodes added", allNodes.toString());
152
153         if (allLinks == null || allLinks.isEmpty()) {
154             LOG.error("readMdSal: no links ");
155             return false;
156         }
157         LOG.info("readMdSal: network links: {} links added", allLinks.size());
158         LOG.debug("readMdSal: network links: {} links added", allLinks.toString());
159
160         return true;
161     }
162
163     private boolean analyzeNw() {
164
165         LOG.debug("analyzeNw: allNodes size {}, allLinks size {}", allNodes.size(), allLinks.size());
166
167         for (Node node : allNodes) {
168             validateNode(node);
169         }
170         LOG.debug("analyzeNw: allPceNodes size {}", allPceNodes.size());
171
172         if (aendPceNode == null || zendPceNode == null) {
173             LOG.error("analyzeNw: Error in reading nodes: A or Z do not present in the network");
174             return false;
175         }
176
177         for (Link link : allLinks) {
178             validateLink(link);
179         }
180
181         LOG.debug("analyzeNw: addLinks size {}, dropLinks size {}", addLinks.size(), dropLinks.size());
182
183         // debug prints
184         LOG.debug("analyzeNw: azSrgs size = {}", azSrgs.size());
185         for (NodeId srg : azSrgs) {
186             LOG.debug("analyzeNw: A/Z Srgs SRG = {}", srg.getValue());
187         }
188         // debug prints
189
190         for (PceLink link : addLinks) {
191             filteraddLinks(link);
192         }
193         for (PceLink link : dropLinks) {
194             filterdropLinks(link);
195         }
196
197         LOG.info("analyzeNw: allPceNodes size {}, allPceLinks size {}", allPceNodes.size(), allPceLinks.size());
198
199         if ((allPceNodes.size() == 0) || (allPceLinks.size() == 0)) {
200             return false;
201         }
202
203         LOG.debug("analyzeNw: allPceNodes {}", allPceNodes.toString());
204         LOG.debug("analyzeNw: allPceLinks {}", allPceLinks.toString());
205
206         return true;
207     }
208
209     private boolean filteraddLinks(PceLink pcelink) {
210
211         NodeId nodeId = pcelink.getSourceId();
212
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());
217             return true;
218         }
219
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());
223
224         return false;
225     }
226
227     private boolean filterdropLinks(PceLink pcelink) {
228
229         NodeId nodeId = pcelink.getDestId();
230
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());
235             return true;
236         }
237
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());
241
242         return false;
243     }
244
245     private boolean validateLink(Link link) {
246
247         LOG.debug("validateLink: link {} ", link.toString());
248
249         if (linksToExclude.contains(link.getLinkId())) {
250             LOG.info("validateLink: Link is ignored due opposite link problem - {}", link.getLinkId().getValue());
251             return false;
252         }
253
254         NodeId sourceId = link.getSource().getSourceNode();
255         NodeId destId = link.getDestination().getDestNode();
256         PceNode source = allPceNodes.get(sourceId);
257         PceNode dest = allPceNodes.get(destId);
258
259         if (source == null) {
260             LOG.debug("validateLink: Link is ignored due source node is rejected by node validation - {}",
261                     link.getSource().getSourceNode().getValue());
262             return false;
263         }
264         if (dest == null) {
265             LOG.debug("validateLink: Link is ignored due dest node is rejected by node validation - {}",
266                     link.getDestination().getDestNode().getValue());
267             return false;
268         }
269
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");
274             return false;
275         }
276
277         LinkId linkId = pcelink.getLinkId();
278
279         switch (validateLinkConstraints(pcelink)) {
280             case HARD_EXCLUDE :
281                 dropOppositeLink(link);
282                 LOG.debug("validateLink: constraints : link is ignored == {}", linkId.getValue());
283                 return false;
284             default:
285                 break;
286         }
287
288         switch (pcelink.getlinkType()) {
289             case ROADMTOROADM :
290                 allPceLinks.put(linkId, pcelink);
291                 source.addOutgoingLink(pcelink);
292                 LOG.debug("validateLink: ROADMTOROADM-LINK added to allPceLinks {}", pcelink.toString());
293                 break;
294             case EXPRESSLINK :
295                 allPceLinks.put(linkId, pcelink);
296                 source.addOutgoingLink(pcelink);
297                 LOG.debug("validateLink: EXPRESS-LINK added to allPceLinks {}", pcelink.toString());
298                 break;
299             case ADDLINK :
300                 pcelink.setClient(source.getRdmSrgClient(pcelink.getSourceTP().toString(), true));
301                 addLinks.add(pcelink);
302                 LOG.debug("validateLink: ADD-LINK saved  {}", pcelink.toString());
303                 break;
304             case DROPLINK :
305                 pcelink.setClient(dest.getRdmSrgClient(pcelink.getDestTP().toString(), false));
306                 dropLinks.add(pcelink);
307                 LOG.debug("validateLink: DROP-LINK saved  {}", pcelink.toString());
308                 break;
309             case XPONDERINPUT :
310                 // store separately all SRG links directly
311                 azSrgs.add(sourceId);
312                 // connected to A/Z
313                 if (!dest.checkTP(pcelink.getDestTP().toString())) {
314                     LOG.debug("validateLink: XPONDER-INPUT is rejected as NW port is busy - {} ", pcelink.toString());
315                     return false;
316                 }
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());
321                 break;
322             // does it mean XPONDER==>>SRG ?
323             case XPONDEROUTPUT :
324                 // store separately all SRG links directly
325                 azSrgs.add(destId);
326                 // connected to A/Z
327                 if (!source.checkTP(pcelink.getSourceTP().toString())) {
328                     LOG.debug("validateLink: XPONDER-OUTPUT is rejected as NW port is busy - {} ", pcelink.toString());
329                     return false;
330                 }
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());
335                 break;
336             default:
337                 LOG.warn("validateLink: link type is not supported {}", pcelink.toString());
338
339         }
340
341         return true;
342     }
343
344     private boolean validateNode(Node node) {
345         LOG.debug("validateNode: node {} ", node.toString());
346
347         // PceNode will be used in Graph algorithm
348         Node1 node1 = node.augmentation(Node1.class);
349         if (node1 == null) {
350             LOG.error("getNodeType: no Node1 (type) Augmentation for node: [{}]. Node is ignored", node.getNodeId());
351         }
352         OpenroadmNodeType nodeType = node1.getNodeType();
353
354         PceNode pceNode = new PceNode(node,nodeType,node.getNodeId());
355         pceNode.validateAZxponder(anodeId, znodeId);
356         pceNode.initWLlist();
357
358         if (!pceNode.isValid()) {
359             LOG.warn(" validateNode: Node is ignored");
360             return false;
361         }
362
363         switch (validateNodeConstraints(pceNode)) {
364             case HARD_EXCLUDE :
365                 return false;
366
367             default :
368                 break;
369         }
370
371         if ((pceNode.getSupNodeIdPceNode().equals(anodeId)) && (endPceNode(nodeType,pceNode.getNodeId(), pceNode))) {
372             this.aendPceNode = pceNode;
373         }
374         if ((pceNode.getSupNodeIdPceNode().equals(znodeId)) && (endPceNode(nodeType,pceNode.getNodeId(), pceNode))) {
375             this.zendPceNode = pceNode;
376         }
377
378         allPceNodes.put(pceNode.getNodeId(), pceNode);
379         LOG.debug("validateNode: node is saved {}", pceNode.getNodeId().getValue());
380         return true;
381     }
382
383     private ConstraintTypes validateNodeConstraints(PceNode pcenode) {
384
385         if (pceHardConstraints.getExcludeSupNodes().isEmpty()  && pceHardConstraints.getExcludeCLLI().isEmpty()) {
386             return ConstraintTypes.NONE;
387         }
388
389         if (pceHardConstraints.getExcludeSupNodes().contains(pcenode.getSupNodeIdPceNode())) {
390             LOG.info("validateNodeConstraints: {}", pcenode.getNodeId().getValue());
391             return ConstraintTypes.HARD_EXCLUDE;
392         }
393         if (pceHardConstraints.getExcludeCLLI().contains(pcenode.getCLLI())) {
394             LOG.info("validateNodeConstraints: {}", pcenode.getNodeId().getValue());
395             return ConstraintTypes.HARD_EXCLUDE;
396         }
397
398         return ConstraintTypes.NONE;
399     }
400
401     private ConstraintTypes validateLinkConstraints(PceLink link) {
402         if (pceHardConstraints.getExcludeSRLG().isEmpty()) {
403             return ConstraintTypes.NONE;
404         }
405
406         // for now SRLG is the only constraint for link
407         if (link.getlinkType() != OpenroadmLinkType.ROADMTOROADM) {
408             return ConstraintTypes.NONE;
409         }
410
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;
416         }
417
418         return ConstraintTypes.NONE;
419     }
420
421     private void dropOppositeLink(Link link) {
422         LinkId opplink = MapUtils.extractOppositeLink(link);
423
424         PceLink oppPceLink = allPceLinks.get(opplink);
425         if (oppPceLink != null) {
426             allPceLinks.remove(oppPceLink);
427         } else {
428             linksToExclude.add(opplink);
429         }
430     }
431
432     private Boolean endPceNode(OpenroadmNodeType openroadmNodeType, NodeId nodeId, PceNode pceNode) {
433         Boolean add = true;
434         switch (openroadmNodeType) {
435             case SRG :
436                 pceNode.initSrgTps();
437                 this.azSrgs.add(nodeId);
438                 break;
439             case XPONDER :
440                 pceNode.initXndrTps();
441                 break;
442             default:
443                 add = false;
444                 LOG.warn("endPceNode: Node {} is not SRG or XPONDER !", nodeId);
445                 break;
446         }
447         return add;
448     }
449
450     public PceNode getaendPceNode() {
451         return aendPceNode;
452     }
453
454     public PceNode getzendPceNode() {
455         return zendPceNode;
456     }
457
458     public Map<NodeId, PceNode> getAllPceNodes() {
459         return this.allPceNodes;
460     }
461
462     public Map<LinkId, PceLink> getAllPceLinks() {
463         return this.allPceLinks;
464     }
465
466     public PceResult getReturnStructure() {
467         return returnStructure;
468     }
469
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());
476         }
477     }
478
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());
483         }
484     }*/
485
486 }