2 * Copyright © 2016 AT&T and others. All rights reserved.
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
8 package org.opendaylight.transportpce.pce.constraints;
10 import java.util.ArrayList;
11 import java.util.List;
12 import java.util.Optional;
13 import java.util.concurrent.ExecutionException;
14 import java.util.concurrent.TimeUnit;
15 import java.util.concurrent.TimeoutException;
16 import java.util.stream.Collectors;
18 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
19 import org.opendaylight.transportpce.common.Timeouts;
20 import org.opendaylight.transportpce.common.network.NetworkTransactionService;
21 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.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.Link;
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.Include;
33 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.constraints.sp.co.routing.or.general.general.Latency;
34 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.constraints.sp.co.routing.or.general.general.include_.OrderedHops;
35 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.diversity.existing.service.contraints.sp.ExistingServiceApplicability;
36 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.ordered.constraints.sp.hop.type.HopType;
37 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.routing.constraints.sp.HardConstraints;
38 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.routing.constraints.sp.SoftConstraints;
39 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.ServicePathList;
40 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.service.path.list.ServicePaths;
41 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.service.path.list.ServicePathsKey;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
46 public class PceConstraintsCalc {
48 private static final Logger LOG = LoggerFactory.getLogger(PceConstraintsCalc.class);
50 private PceConstraints pceHardConstraints = new PceConstraints();
51 private PceConstraints pceSoftConstraints = new PceConstraints();
52 private PceMetric pceMetrics = PceMetric.HopCount;
53 private NetworkTransactionService networkTransactionService;
55 public PceConstraintsCalc(PathComputationRequestInput input, NetworkTransactionService networkTransactionService) {
56 LOG.debug("In PceconstraintsCalc start");
58 pceMetrics = input.getPceMetric();
60 this.networkTransactionService = networkTransactionService;
62 // TODO. for now metrics are set into hard structure
63 LOG.info("In PceConstraintsCalc: read PceMetric {}", pceMetrics);
64 pceHardConstraints.setPceMetrics(pceMetrics);
66 calcHardconstraints(input);
67 calcSoftconstraints(input);
70 private void calcHardconstraints(PathComputationRequestInput input) {
71 HardConstraints servicePathHardConstraints = input.getHardConstraints();
72 if (servicePathHardConstraints == null) {
73 LOG.info("In calcHardconstraints: no hard constraints.");
77 CoRoutingOrGeneral coRoutingOrGeneral = servicePathHardConstraints.getCoRoutingOrGeneral();
78 readconstraints(coRoutingOrGeneral, pceHardConstraints);
82 private void calcSoftconstraints(PathComputationRequestInput input) {
83 SoftConstraints servicePathSoftConstraints = input.getSoftConstraints();
84 if (servicePathSoftConstraints == null) {
85 LOG.info("In calcSoftconstraints: no soft constraints.");
89 CoRoutingOrGeneral coRoutingOrGeneral = servicePathSoftConstraints.getCoRoutingOrGeneral();
90 readconstraints(coRoutingOrGeneral, pceSoftConstraints);
94 private void readconstraints(CoRoutingOrGeneral coRoutingOrGeneral, PceConstraints constraints) {
95 LOG.debug("In readconstraints start");
97 if (coRoutingOrGeneral == null) {
98 LOG.info("In readHardconstraints: no CoRoutingOrGeneral constraints.");
102 General tmpGeneral = null;
103 CoRouting tmpCoRouting = null;
105 if (coRoutingOrGeneral instanceof General) {
106 LOG.info("In readconstraints General {}", coRoutingOrGeneral);
107 tmpGeneral = (General) coRoutingOrGeneral;
108 readGeneralContrains(tmpGeneral, constraints);
112 if (coRoutingOrGeneral instanceof CoRouting) {
113 LOG.info("In readconstraints CoRouting {}", coRoutingOrGeneral);
114 tmpCoRouting = (CoRouting) coRoutingOrGeneral;
115 readCoRoutingContrains(tmpCoRouting, constraints);
119 private void readGeneralContrains(General tmpGeneral, PceConstraints constraints) {
120 LOG.debug("In readGeneralContrains start");
122 if (tmpGeneral == null) {
123 LOG.info("In readGeneralContrains: no General constraints.");
127 Latency latency = tmpGeneral.getLatency();
128 if (latency != null) {
129 constraints.setMaxLatency(latency.getMaxLatency().toJava());
130 LOG.info("In readGeneralContrains: read latency {}", latency);
133 Exclude exclude = tmpGeneral.getExclude();
134 List<String> elementsToExclude = null;
135 if (exclude != null) {
136 elementsToExclude = exclude.getNodeId();
137 if (elementsToExclude != null) {
138 constraints.setExcludeSupNodes(elementsToExclude);
141 elementsToExclude = exclude.getSRLG();
142 if (elementsToExclude != null) {
143 List<Long> srlgToExclude = new ArrayList<>();
144 for (String str : elementsToExclude) {
145 srlgToExclude.add(Long.parseLong(str));
147 constraints.setExcludeSRLG(srlgToExclude);
150 elementsToExclude = exclude.getClli();
151 if (elementsToExclude != null) {
152 constraints.setExcludeCLLI(elementsToExclude);
156 Include include = tmpGeneral.getInclude();
157 if (include != null) {
158 List<OrderedHops> listHops = include.getOrderedHops();
159 if (listHops != null) {
160 readIncludeNodes(listHops, constraints);
162 LOG.debug("in readGeneralContrains INCLUDE {} ", include);
165 Diversity diversity = tmpGeneral.getDiversity();
166 PceConstraints.ResourceType rt = PceConstraints.ResourceType.NONE;
167 if (diversity != null) {
168 ExistingServiceApplicability temp = diversity.getExistingServiceApplicability();
172 if (Boolean.TRUE.equals(temp.isNode())) {
173 rt = PceConstraints.ResourceType.NODE;
175 if (Boolean.TRUE.equals(temp.isSrlg())) {
176 rt = PceConstraints.ResourceType.SRLG;
178 if (Boolean.TRUE.equals(temp.isClli())) {
179 rt = PceConstraints.ResourceType.CLLI;
181 LOG.info("in readGeneralContrains {} list is :{}", rt, diversity);
182 readDiversity(diversity.getExistingService(), constraints, rt);
187 private void readIncludeNodes(List<OrderedHops> listHops, PceConstraints constraints) {
188 for (int i = 0; i < listHops.size(); i++) {
189 HopType hoptype = listHops.get(i).getHopType().getHopType();
191 String hopt = hoptype.implementedInterface().getSimpleName();
192 LOG.info("in readIncludeNodes next hop to include {}", hopt);
195 org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints
196 .rev171017.ordered.constraints.sp.hop.type.hop.type.Node
197 node = (org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing
198 .constraints.rev171017.ordered.constraints.sp.hop.type.hop.type.Node) hoptype;
199 constraints.setListToInclude(new PceConstraints.ResourcePair(PceConstraints.ResourceType.NODE,
203 org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints
204 .rev171017.ordered.constraints.sp.hop.type.hop.type.SRLG
205 srlg = (org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing
206 .constraints.rev171017.ordered.constraints.sp.hop.type.hop.type.SRLG) hoptype;
207 constraints.setListToInclude(new PceConstraints.ResourcePair(PceConstraints.ResourceType.SRLG,
211 org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints
212 .rev171017.ordered.constraints.sp.hop.type.hop.type.Clli
213 clli = (org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing
214 .constraints.rev171017.ordered.constraints.sp.hop.type.hop.type.Clli) hoptype;
215 constraints.setListToInclude(new PceConstraints.ResourcePair(PceConstraints.ResourceType.CLLI,
219 LOG.error("in readIncludeNodes unsupported include type {}", hopt);
224 private void readDiversity(List<String> srvList, PceConstraints constraints, PceConstraints.ResourceType rt) {
226 List<String> elementsToExclude = new ArrayList<>();
227 LOG.info("in readDiversity {}", srvList);
229 for (String srv : srvList) {
230 Optional<PathDescription> service = getPathDescriptionFromDatastore(srv);
231 if (service.isPresent()) {
232 LOG.info("in readDiversity service list {}", service);
235 elementsToExclude.addAll(getAToZNodeList(service.get()));
236 LOG.info("readDiversity NODE : {}", elementsToExclude);
237 if (elementsToExclude != null) {
238 constraints.setExcludeNodes(elementsToExclude);
242 elementsToExclude.addAll(getSRLGList(service.get()));
243 LOG.info("readDiversity SRLG : {}", elementsToExclude);
244 if (elementsToExclude != null) {
245 constraints.setExcludeSrlgLinks(elementsToExclude);
249 /// Retrieve nodes into dedicated CLLI list
250 /// during node validation check their CLLI and build CLLI exclude list
251 elementsToExclude.addAll(getAToZNodeList(service.get()));
252 LOG.info("readDiversity CLLI : {}", elementsToExclude);
253 if (elementsToExclude != null) {
254 constraints.setExcludeClliNodes(elementsToExclude);
258 LOG.info("in readDiversity unsupported divercity type {}", rt);
262 LOG.info("in readDiversity srv={} is not present", srv);
268 private List<String> getAToZNodeList(PathDescription pathDescription) {
269 List<AToZ> aendToZList = pathDescription.getAToZDirection().getAToZ();
270 return aendToZList.stream().filter(aToZ -> {
271 if (aToZ.getResource() == null || aToZ.getResource().getResource() == null) {
272 LOG.warn("Diversity constraint: Resource of AToZ node {} is null! Skipping this node!", aToZ.getId());
275 return aToZ.getResource().getResource() instanceof Node;
277 Node node = (Node) aToZ.getResource().getResource();
278 if (node.getNodeId() == null) {
279 LOG.warn("Node in AToZ node {} contains null! Skipping this node!", aToZ.getId());
284 Node node = ((Node) aToZ.getResource().getResource());
285 return node.getNodeId();
286 }).collect(Collectors.toList());
289 private List<String> getSRLGList(PathDescription pathDescription) {
290 List<AToZ> aendToZList = pathDescription.getAToZDirection().getAToZ();
291 return aendToZList.stream().filter(aToZ -> {
292 if (aToZ.getResource() == null
293 || aToZ.getResource().getResource() == null) {
294 LOG.warn("Diversity constraint: Resource of AToZ {} is null! Skipping this resource!", aToZ.getId());
297 return aToZ.getResource().getResource() instanceof Link;
299 Link link = (Link) aToZ.getResource().getResource();
300 if (link.getLinkId() == null) {
301 LOG.warn("Link in AToZ link {} contains null! Skipping this link!", aToZ.getId());
306 return ((Link) aToZ.getResource().getResource()).getLinkId();
307 }).collect(Collectors.toList());
310 private Optional<PathDescription> getPathDescriptionFromDatastore(String serviceName) {
311 Optional<PathDescription> result = Optional.empty();
312 InstanceIdentifier<ServicePaths> pathDescriptionIID = InstanceIdentifier.create(ServicePathList.class)
313 .child(ServicePaths.class, new ServicePathsKey(serviceName));
315 LOG.info("PCE diversity constraints: Getting path description for service {}", serviceName);
316 ServicePaths servicePaths =
317 networkTransactionService.read(LogicalDatastoreType.CONFIGURATION, pathDescriptionIID)
318 .get(Timeouts.DATASTORE_READ, TimeUnit.MILLISECONDS).get();
319 if (servicePaths != null) {
320 PathDescription path = servicePaths.getPathDescription();
322 result = Optional.of(path);
325 } catch (InterruptedException | ExecutionException | TimeoutException e) {
327 "PCE diversity constraints: Exception while getting path description from datastore {} for service {}!",
328 pathDescriptionIID,serviceName, e);
334 private void readCoRoutingContrains(CoRouting tmpcoRouting, PceConstraints constraints) {
335 LOG.info("In readCoRoutingContrains start");
337 if (tmpcoRouting == null) {
338 LOG.info("In readCoRoutingContrains: no General constraints.");
343 public PceConstraints getPceHardConstraints() {
344 return pceHardConstraints;
347 public PceConstraints getPceSoftConstraints() {
348 return pceSoftConstraints;
351 public PceMetric getPceMetrics() {