remove dead code pointed out by sonar
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / PceConstraintsCalc.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 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.concurrent.ExecutionException;
14 import java.util.concurrent.TimeUnit;
15 import java.util.concurrent.TimeoutException;
16 import java.util.stream.Collectors;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.transportpce.common.Timeouts;
21 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev171017.PathComputationRequestInput;
22 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.PathDescription;
23 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.path.description.atoz.direction.AToZ;
24 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.pce.resource.resource.resource.Node;
25 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.RoutingConstraintsSp.PceMetric;
26 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.constraints.sp.CoRoutingOrGeneral;
27 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.constraints.sp.co.routing.or.general.CoRouting;
28 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.constraints.sp.co.routing.or.general.General;
29 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.constraints.sp.co.routing.or.general.general.Diversity;
30 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.constraints.sp.co.routing.or.general.general.Exclude;
31 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.constraints.sp.co.routing.or.general.general.Latency;
32 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.routing.constraints.sp.HardConstraints;
33 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.routing.constraints.sp.SoftConstraints;
34 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.ServicePathList;
35 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.service.path.list.ServicePaths;
36 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.service.path.list.ServicePathsKey;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 public class PceConstraintsCalc {
42     /* Logging. */
43     private static final Logger LOG = LoggerFactory.getLogger(PceCalculation.class);
44
45     private PceConstraints pceHardConstraints = new PceConstraints();
46     private PceConstraints pceSoftConstraints = new PceConstraints();
47     private PceMetric pceMetrics = PceMetric.HopCount;
48     private DataBroker dataBroker;
49
50
51     public PceConstraintsCalc(PathComputationRequestInput input, DataBroker dataBroker) {
52         LOG.debug("In PceconstraintsCalc start");
53
54         this.pceMetrics = input.getPceMetric();
55
56         this.dataBroker = dataBroker;
57
58         // TODO. for now metrics are set into hard structure
59         LOG.info("In PceConstraintsCalc: read PceMetric {}", this.pceMetrics.toString());
60         this.pceHardConstraints.setPceMetrics(this.pceMetrics);
61
62         calcHardconstraints(input);
63         calcSoftconstraints(input);
64     }
65
66     private void calcHardconstraints(PathComputationRequestInput input) {
67         HardConstraints servicePathHardConstraints = input.getHardConstraints();
68         if (servicePathHardConstraints == null) {
69             LOG.info("In calcHardconstraints: no hard constraints.");
70             return;
71         }
72
73         CoRoutingOrGeneral coRoutingOrGeneral = servicePathHardConstraints.getCoRoutingOrGeneral();
74         readconstraints(coRoutingOrGeneral, this.pceHardConstraints);
75
76     }
77
78     private void calcSoftconstraints(PathComputationRequestInput input) {
79         SoftConstraints servicePathSoftConstraints = input.getSoftConstraints();
80         if (servicePathSoftConstraints == null) {
81             LOG.info("In calcSoftconstraints: no soft constraints.");
82             return;
83         }
84
85         CoRoutingOrGeneral coRoutingOrGeneral = servicePathSoftConstraints.getCoRoutingOrGeneral();
86         readconstraints(coRoutingOrGeneral, this.pceSoftConstraints);
87
88     }
89
90     private void readconstraints(CoRoutingOrGeneral coRoutingOrGeneral, PceConstraints constraints) {
91         LOG.debug("In readconstraints start");
92
93         if (coRoutingOrGeneral == null) {
94             LOG.info("In readHardconstraints: no CoRoutingOrGeneral constraints.");
95             return;
96         }
97
98         General tmpGeneral = null;
99         CoRouting tmpCoRouting = null;
100
101         if (coRoutingOrGeneral instanceof General) {
102             LOG.info("In readconstraints General {}", coRoutingOrGeneral.toString());
103             tmpGeneral = (General) coRoutingOrGeneral;
104             readGeneralContrains(tmpGeneral, constraints);
105             return;
106         }
107
108         if (coRoutingOrGeneral instanceof CoRouting) {
109             LOG.info("In readconstraints CoRouting {}", coRoutingOrGeneral.toString());
110             tmpCoRouting = (CoRouting) coRoutingOrGeneral;
111             readCoRoutingContrains(tmpCoRouting, constraints);
112             return;
113         }
114
115         return;
116
117     }
118
119     private void readGeneralContrains(General tmpGeneral, PceConstraints constraints) {
120         LOG.debug("In readGeneralContrains start");
121
122         if (tmpGeneral  ==  null) {
123             LOG.info("In readGeneralContrains: no General constraints.");
124             return;
125         }
126
127         Latency latency = tmpGeneral.getLatency();
128         if (latency != null) {
129             constraints.setMaxLatency(latency.getMaxLatency());
130             LOG.info("In readGeneralContrains: read latency {}", latency.toString());
131         }
132
133         Exclude exclude = tmpGeneral.getExclude();
134         List<String> elementsToExclude = null;
135         if (exclude != null) {
136             elementsToExclude = exclude.getNodeId();
137             if (elementsToExclude != null) {
138                 constraints.setExcludeNodes(elementsToExclude);
139             }
140             elementsToExclude = exclude.getSRLG();
141             if (elementsToExclude != null) {
142                 constraints.setExcludeSRLG(elementsToExclude);
143             }
144         }
145
146         Diversity diversity = tmpGeneral.getDiversity();
147         if (diversity != null) {
148             if (diversity.getExistingServiceApplicability().isNode()) {
149                 LOG.info("in readGeneralContrains {}", diversity.toString());
150                 readDiversityNodes(diversity.getExistingService(), constraints);
151             }
152         }
153
154     }
155
156     private void readDiversityNodes(List<String> srvList, PceConstraints constraints) {
157
158         List<String> elementsToExclude = new ArrayList<String>();
159         LOG.info("in readDiversityNodes {}", srvList.toString());
160
161         for (String srv : srvList) {
162             Optional<PathDescription> service = getPathDescriptionFromDatastore(srv);
163             if (service.isPresent()) {
164                 elementsToExclude.addAll(getAToZNodeList(service.get()));
165                 LOG.info("readDiversityNodes : {}", elementsToExclude);
166
167             } else {
168                 LOG.info("in readDiversityNodes srv={} is not present", srv);
169             }
170         }
171
172         if (elementsToExclude != null) {
173             constraints.setExcludeNodes(elementsToExclude);
174         }
175     }
176
177     private List<String> getAToZNodeList(PathDescription pathDescription) {
178         List<AToZ> atozList = pathDescription.getAToZDirection().getAToZ();
179         return atozList.stream().filter(aToZ -> {
180             if ((aToZ.getResource() == null) || (aToZ.getResource().getResource() == null)) {
181                 LOG.warn("Diversity constraint: Resource of AToZ node {} is null! Skipping this node!", aToZ.getId());
182                 return false;
183             }
184             return aToZ.getResource().getResource() instanceof Node;
185         }).map(aToZ -> {
186             Node node = (Node) aToZ.getResource().getResource();
187             if (node.getNodeId() == null) {
188                 LOG.warn("Node in AToZ node {} contains null! Skipping this node!", aToZ.getId());
189                 return null;
190             }
191             return node.getNodeId().toString();
192         }).collect(Collectors.toList());
193     }
194
195     private Optional<PathDescription> getPathDescriptionFromDatastore(String serviceName) {
196         Optional<PathDescription> result = Optional.absent();
197         InstanceIdentifier<ServicePaths> pathDescriptionIID = InstanceIdentifier.create(ServicePathList.class)
198                 .child(ServicePaths.class, new ServicePathsKey(serviceName));
199         ReadOnlyTransaction pathDescReadTx = this.dataBroker.newReadOnlyTransaction();
200         try {
201             LOG.info("PCE diversity constraints: Getting path description for service {}", serviceName);
202             ServicePaths servicePaths = pathDescReadTx.read(LogicalDatastoreType.CONFIGURATION, pathDescriptionIID)
203                     .get(Timeouts.DATASTORE_READ, TimeUnit.MILLISECONDS).get();
204             if (servicePaths != null) {
205                 PathDescription path = servicePaths.getPathDescription();
206                 if (path != null) {
207                     result = Optional.of(path);
208                 }
209             }
210         } catch (InterruptedException | ExecutionException | TimeoutException e) {
211             LOG.warn(
212                 "PCE diversity constraints: Exception while getting path description from datastore {} for service {}!",
213                 pathDescriptionIID,serviceName, e);
214             return result;
215         }
216         return result;
217     }
218
219     private void readCoRoutingContrains(CoRouting tmpcoRouting, PceConstraints constraints) {
220         LOG.info("In readCoRoutingContrains start");
221
222         if (tmpcoRouting  ==  null) {
223             LOG.info("In readCoRoutingContrains: no General constraints.");
224             return;
225         }
226
227     }
228
229     public PceConstraints getPceHardConstraints() {
230         return this.pceHardConstraints;
231     }
232
233     public PceConstraints getPceSoftConstraints() {
234         return this.pceSoftConstraints;
235     }
236
237     public PceMetric getPceMetrics() {
238         return this.pceMetrics;
239     }
240
241
242 }