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