Merge "ROADM To ROADM Path Calculation"
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / PceSendingPceRPCs.java
1 /*
2  * Copyright © 2017 AT&T 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 org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.transportpce.pce.PceResult.LocalCause;
13 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev171017.PathComputationRequestInput;
14 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev171017.service.path.rpc.result.PathDescriptionBuilder;
15 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.path.description.AToZDirection;
16 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.path.description.ZToADirection;
17 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.RoutingConstraintsSp.PceMetric;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /*
22  * Class for Sending
23  * PCE requests :
24  * - path-computation-request
25  * - cancel-resource-reserve.
26  * @author Martial Coulibaly ( martial.coulibaly@gfi.com ) on behalf of Orange
27  *
28  */
29 public class PceSendingPceRPCs {
30
31     /* Logging. */
32     private static final Logger LOG = LoggerFactory.getLogger(PceSendingPceRPCs.class);
33     /* define procedure success (or not ). */
34     private PceResult rc = new PceResult();
35
36     /*
37      * define type of request<br> <code>true</code> pathcomputation <br>
38      * <code>false</code> cancelresourcereserve .
39      */
40     private PathDescriptionBuilder pathDescription;
41
42     private PathComputationRequestInput input;
43     private DataBroker dataBroker;
44
45     private PceConstraints pceHardConstraints = new PceConstraints();
46     private PceConstraints pceSoftConstraints = new PceConstraints();
47
48     public PceSendingPceRPCs() {
49         setPathDescription(null);
50
51         this.input = null;
52         this.dataBroker = null;
53     }
54
55     public PceSendingPceRPCs(PathComputationRequestInput input, DataBroker dataBroker) {
56         setPathDescription(null);
57
58         // TODO compliance check to check that input is not empty
59         this.input = input;
60         this.dataBroker = dataBroker;
61     }
62
63     public void cancelResourceReserve() {
64         LOG.info("Wait for 10s til beginning the PCE cancelResourceReserve request");
65         try {
66             Thread.sleep(10000); // sleep for 10s
67         } catch (InterruptedException e) {
68             LOG.error(e.toString());
69         }
70         LOG.info("cancelResourceReserve ...");
71     }
72
73     public void pathComputation() {
74         LOG.info("PathComputation ...");
75
76         PceConstraintsCalc constraints = new PceConstraintsCalc(input, dataBroker);
77         pceHardConstraints = constraints.getPceHardConstraints();
78         pceSoftConstraints = constraints.getPceSoftConstraints();
79
80
81         LOG.info("nwAnalizer ...");
82         PceCalculation nwAnalizer = new PceCalculation(input, dataBroker,
83                 pceHardConstraints, pceSoftConstraints, rc);
84         nwAnalizer.calcPath();
85         rc = nwAnalizer.getReturnStructure();
86         if (!rc.getStatus()) {
87             LOG.error("In pathComputation nwAnalizer: result = {}", rc.toString());
88             return;
89         }
90
91         LOG.info("PceGraph ...");
92         LOG.warn("PathComputation: aPceNode '{}' - zPceNode '{}'", nwAnalizer.getaPceNode(), nwAnalizer.getzPceNode());
93         PceGraph graph = new PceGraph(
94                 nwAnalizer.getaPceNode(),
95                 nwAnalizer.getzPceNode(),
96                 nwAnalizer.getAllPceNodes(),
97                 pceHardConstraints, pceSoftConstraints, rc);
98         graph.calcPath();
99         rc = graph.getReturnStructure();
100         if (!rc.getStatus()) {
101
102             LOG.warn("In pathComputation : Graph return without Path ");
103
104             // TODO fix. This is quick workaround for algorithm problem
105             if ((rc.getLocalCause() == LocalCause.TOO_HIGH_LATENCY)
106                 && (pceHardConstraints.getPceMetrics() == PceMetric.HopCount)
107                 && (pceHardConstraints.getMaxLatency() != -1)) {
108
109                 pceHardConstraints.setPceMetrics(PceMetric.PropagationDelay);
110                 graph = patchRerunGraph(graph, pceHardConstraints, pceSoftConstraints);
111             }
112
113             if (!rc.getStatus()) {
114                 LOG.error("In pathComputation graph.calcPath: result = {}", rc.toString());
115                 return;
116             }
117         }
118
119         LOG.info("PcePathDescription ...");
120         PcePathDescription description = new PcePathDescription(
121                 graph.getPathAtoZ(),
122                 nwAnalizer.getAllPceLinks(), rc);
123         description.buildDescriptions();
124         rc = description.getReturnStructure();
125         if (!rc.getStatus()) {
126             LOG.error("In pathComputation description: result = {}", rc.toString());
127             return;
128         }
129
130         LOG.info("setPathDescription ...");
131         AToZDirection atoz = rc.getAtoZDirection();
132         ZToADirection ztoa = rc.getZtoADirection();
133         if ((atoz == null) || (atoz.getAToZ() == null)) {
134             rc.setRC("400");
135             LOG.error("In pathComputation empty atoz path after description: result = {}", rc.toString());
136             return;
137         }
138         if ((ztoa == null) || (ztoa.getZToA() == null)) {
139             rc.setRC("400");
140             LOG.error("In pathComputation empty ztoa path after description: result = {}", rc.toString());
141             return;
142         }
143         setPathDescription(new PathDescriptionBuilder()
144                 .setAToZDirection(atoz)
145                 .setZToADirection(ztoa));
146         LOG.info("In pathComputation Graph is Found");
147     }
148
149     private PceGraph patchRerunGraph(PceGraph graph, PceConstraints pceHardCons, PceConstraints pceSoftCons) {
150
151         LOG.info("In pathComputation patchRerunGraph : rerun Graph with metric = PROPAGATION-DELAY ");
152         graph.setConstrains(pceHardCons, pceSoftCons);
153         graph.calcPath();
154         return graph;
155
156     }
157
158     public PathDescriptionBuilder getPathDescription() {
159         return pathDescription;
160     }
161
162     private void setPathDescription(PathDescriptionBuilder pathDescription) {
163         this.pathDescription = pathDescription;
164     }
165
166     public Boolean getSuccess() {
167         return rc.getStatus();
168     }
169
170     public String getMessage() {
171         return rc.getMessage();
172     }
173
174     public String getResponseCode() {
175         return rc.getResponseCode();
176     }
177
178 }