clean some compilation warnings
[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.rev170426.PathComputationRequestInput;
14 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.path.description.AToZDirection;
15 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.path.description.ZToADirection;
16 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev170426.RoutingConstraintsSp.PceMetric;
17 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev170426.response.parameters.sp.response.parameters.PathDescriptionBuilder;
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         PceGraph graph = new PceGraph(
93                 nwAnalizer.getaPceNode(),
94                 nwAnalizer.getzPceNode(),
95                 nwAnalizer.getAllPceNodes(),
96                 pceHardConstraints, pceSoftConstraints, rc);
97         graph.calcPath();
98         rc = graph.getReturnStructure();
99         if (!rc.getStatus()) {
100
101             LOG.warn("In pathComputation : Graph return without Path ");
102
103             // TODO fix. This is quick workaround for algorithm problem
104             if ((rc.getLocalCause() == LocalCause.TOO_HIGH_LATENCY)
105                 && (pceHardConstraints.getPceMetrics() == PceMetric.HopCount)
106                 && (pceHardConstraints.getMaxLatency() != -1)) {
107
108                 pceHardConstraints.setPceMetrics(PceMetric.PropagationDelay);
109                 graph = patchRerunGraph(graph, pceHardConstraints, pceSoftConstraints);
110             }
111
112             if (!rc.getStatus()) {
113                 LOG.error("In pathComputation graph.calcPath: result = {}", rc.toString());
114                 return;
115             }
116         }
117
118         LOG.info("PcePathDescription ...");
119         PcePathDescription description = new PcePathDescription(
120                 graph.getPathAtoZ(),
121                 nwAnalizer.getAllPceLinks(), rc);
122         description.buildDescriptions();
123         rc = description.getReturnStructure();
124         if (!rc.getStatus()) {
125             LOG.error("In pathComputation description: result = {}", rc.toString());
126             return;
127         }
128
129         LOG.info("setPathDescription ...");
130         AToZDirection atoz = rc.getAtoZDirection();
131         ZToADirection ztoa = rc.getZtoADirection();
132         if ((atoz == null) || (atoz.getAToZ() == null)) {
133             rc.setRC("400");
134             LOG.error("In pathComputation empty atoz path after description: result = {}", rc.toString());
135             return;
136         }
137         if ((ztoa == null) || (ztoa.getZToA() == null)) {
138             rc.setRC("400");
139             LOG.error("In pathComputation empty ztoa path after description: result = {}", rc.toString());
140             return;
141         }
142         setPathDescription(new PathDescriptionBuilder()
143                 .setAToZDirection(atoz)
144                 .setZToADirection(ztoa));
145         LOG.info("In pathComputation Graph is Found");
146     }
147
148     private PceGraph patchRerunGraph(PceGraph graph, PceConstraints pceHardCons, PceConstraints pceSoftCons) {
149
150         LOG.info("In pathComputation patchRerunGraph : rerun Graph with metric = PROPAGATION-DELAY ");
151         graph.setConstrains(pceHardCons, pceSoftCons);
152         graph.calcPath();
153         return graph;
154
155     }
156
157     public PathDescriptionBuilder getPathDescription() {
158         return pathDescription;
159     }
160
161     private void setPathDescription(PathDescriptionBuilder pathDescription) {
162         this.pathDescription = pathDescription;
163     }
164
165     public Boolean getSuccess() {
166         return rc.getStatus();
167     }
168
169     public String getMessage() {
170         return rc.getMessage();
171     }
172
173     public String getResponseCode() {
174         return rc.getResponseCode();
175     }
176
177 }