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;
17 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
18 import org.opendaylight.transportpce.common.Timeouts;
19 import org.opendaylight.transportpce.common.network.NetworkTransactionService;
20 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.PathComputationRequestInput;
21 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.PathDescription;
22 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.path.description.atoz.direction.AToZ;
23 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.pce.resource.resource.resource.Link;
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.Include;
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.constraints.sp.co.routing.or.general.general.include_.OrderedHops;
34 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.diversity.existing.service.contraints.sp.ExistingServiceApplicability;
35 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.ordered.constraints.sp.hop.type.HopType;
36 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.routing.constraints.sp.HardConstraints;
37 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.routing.constraints.sp.SoftConstraints;
38 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.ServicePathList;
39 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.service.path.list.ServicePaths;
40 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.service.path.list.ServicePathsKey;
41 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.toString());
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.toString());
107 tmpGeneral = (General) coRoutingOrGeneral;
108 readGeneralContrains(tmpGeneral, constraints);
112 if (coRoutingOrGeneral instanceof CoRouting) {
113 LOG.info("In readconstraints CoRouting {}", coRoutingOrGeneral.toString());
114 tmpCoRouting = (CoRouting) coRoutingOrGeneral;
115 readCoRoutingContrains(tmpCoRouting, constraints);
123 private void readGeneralContrains(General tmpGeneral, PceConstraints constraints) {
124 LOG.debug("In readGeneralContrains start");
126 if (tmpGeneral == null) {
127 LOG.info("In readGeneralContrains: no General constraints.");
131 Latency latency = tmpGeneral.getLatency();
132 if (latency != null) {
133 constraints.setMaxLatency(latency.getMaxLatency());
134 LOG.info("In readGeneralContrains: read latency {}", latency.toString());
137 Exclude exclude = tmpGeneral.getExclude();
138 List<String> elementsToExclude = null;
139 if (exclude != null) {
140 elementsToExclude = exclude.getNodeId();
141 if (elementsToExclude != null) {
142 constraints.setExcludeSupNodes(elementsToExclude);
145 elementsToExclude = exclude.getSRLG();
146 if (elementsToExclude != null) {
147 List<Long> srlgToExclude = new ArrayList<Long>();
148 for (String str : elementsToExclude) {
149 srlgToExclude.add(Long.parseLong(str));
151 constraints.setExcludeSRLG(srlgToExclude);
154 elementsToExclude = exclude.getClli();
155 if (elementsToExclude != null) {
156 constraints.setExcludeCLLI(elementsToExclude);
160 Include include = tmpGeneral.getInclude();
161 if (include != null) {
162 List<OrderedHops> listHops = include.getOrderedHops();
163 if (listHops != null) {
164 readIncludeNodes(listHops, constraints);
166 LOG.debug("in readGeneralContrains INCLUDE {} ", include.toString());
169 Diversity diversity = tmpGeneral.getDiversity();
170 PceConstraints.ResourceType rt = PceConstraints.ResourceType.NONE;
171 if (diversity != null) {
172 ExistingServiceApplicability temp = diversity.getExistingServiceApplicability();
177 rt = PceConstraints.ResourceType.NODE;
180 rt = PceConstraints.ResourceType.SRLG;
183 rt = PceConstraints.ResourceType.CLLI;
185 LOG.info("in readGeneralContrains {} list is :{}", rt, diversity.toString());
186 readDiversity(diversity.getExistingService(), constraints, rt);
191 private void readIncludeNodes(List<OrderedHops> listHops, PceConstraints constraints) {
192 for (int i = 0; i < listHops.size(); i++) {
193 HopType hoptype = listHops.get(i).getHopType().getHopType();
195 String hopt = hoptype.implementedInterface().getSimpleName();
196 LOG.info("in readIncludeNodes next hop to include {}", hopt);
199 org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints
200 .rev171017.ordered.constraints.sp.hop.type.hop.type.Node
201 node = (org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing
202 .constraints.rev171017.ordered.constraints.sp.hop.type.hop.type.Node) hoptype;
203 constraints.setListToInclude(constraints.new ResourcePair(PceConstraints.ResourceType.NODE,
207 org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints
208 .rev171017.ordered.constraints.sp.hop.type.hop.type.SRLG
209 srlg = (org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing
210 .constraints.rev171017.ordered.constraints.sp.hop.type.hop.type.SRLG) hoptype;
211 constraints.setListToInclude(constraints.new ResourcePair(PceConstraints.ResourceType.SRLG,
215 org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints
216 .rev171017.ordered.constraints.sp.hop.type.hop.type.Clli
217 clli = (org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing
218 .constraints.rev171017.ordered.constraints.sp.hop.type.hop.type.Clli) hoptype;
219 constraints.setListToInclude(constraints.new ResourcePair(PceConstraints.ResourceType.CLLI,
223 LOG.error("in readIncludeNodes unsupported include type {}", hopt);
228 private void readDiversity(List<String> srvList, PceConstraints constraints, PceConstraints.ResourceType rt) {
230 List<String> elementsToExclude = new ArrayList<String>();
231 LOG.info("in readDiversity {}", srvList.toString());
233 for (String srv : srvList) {
234 Optional<PathDescription> service = getPathDescriptionFromDatastore(srv);
235 if (service.isPresent()) {
236 LOG.info("in readDiversity service list {}", service.toString());
240 .addAll(getAToZNodeList(service.get()));
241 LOG.info("readDiversity NODE : {}", elementsToExclude);
242 if (elementsToExclude != null) {
243 constraints.setExcludeNodes(elementsToExclude);
248 .addAll(getSRLGList(service.get()));
249 LOG.info("readDiversity SRLG : {}", elementsToExclude);
250 if (elementsToExclude != null) {
251 constraints.setExcludeSrlgLinks(elementsToExclude);
255 /// Retrieve nodes into dedicated CLLI list
256 /// during node validation check their CLLI and build CLLI exclude list
258 .addAll(getAToZNodeList(service.get()));
259 LOG.info("readDiversity CLLI : {}", elementsToExclude);
260 if (elementsToExclude != null) {
261 constraints.setExcludeClliNodes(elementsToExclude);
265 LOG.info("in readDiversity unsupported divercity type", rt);
269 LOG.info("in readDiversity srv={} is not present", srv);
275 private List<String> getAToZNodeList(PathDescription pathDescription) {
276 List<AToZ> aendToZList = pathDescription.getAToZDirection().getAToZ();
277 return aendToZList.stream().filter(aToZ -> {
278 if (aToZ.getResource() == null || aToZ.getResource().getResource() == null) {
279 LOG.warn("Diversity constraint: Resource of AToZ node {} is null! Skipping this node!", aToZ.getId());
282 return aToZ.getResource().getResource() instanceof Node;
284 Node node = (Node) aToZ.getResource().getResource();
285 if (node.getNodeId() == null) {
286 LOG.warn("Node in AToZ node {} contains null! Skipping this node!", aToZ.getId());
291 Node node = ((Node) aToZ.getResource().getResource());
292 return node.getNodeId().toString();
293 }).collect(Collectors.toList());
296 private List<String> getSRLGList(PathDescription pathDescription) {
297 List<AToZ> aendToZList = pathDescription.getAToZDirection().getAToZ();
298 return aendToZList.stream().filter(aToZ -> {
299 if (aToZ.getResource() == null
300 || aToZ.getResource().getResource() == null) {
301 LOG.warn("Diversity constraint: Resource of AToZ {} is null! Skipping this resource!", aToZ.getId());
304 return aToZ.getResource().getResource() instanceof Link;
306 Link link = (Link) aToZ.getResource().getResource();
307 if (link.getLinkId() == null) {
308 LOG.warn("Link in AToZ link {} contains null! Skipping this link!", aToZ.getId());
313 return ((Link) aToZ.getResource().getResource()).getLinkId();
314 }).collect(Collectors.toList());
317 private Optional<PathDescription> getPathDescriptionFromDatastore(String serviceName) {
318 Optional<PathDescription> result = Optional.empty();
319 InstanceIdentifier<ServicePaths> pathDescriptionIID = InstanceIdentifier.create(ServicePathList.class)
320 .child(ServicePaths.class, new ServicePathsKey(serviceName));
322 LOG.info("PCE diversity constraints: Getting path description for service {}", serviceName);
323 ServicePaths servicePaths =
324 networkTransactionService.read(LogicalDatastoreType.CONFIGURATION, pathDescriptionIID)
325 .get(Timeouts.DATASTORE_READ, TimeUnit.MILLISECONDS).get();
326 if (servicePaths != null) {
327 PathDescription path = servicePaths.getPathDescription();
329 result = Optional.of(path);
332 } catch (InterruptedException | ExecutionException | TimeoutException e) {
334 "PCE diversity constraints: Exception while getting path description from datastore {} for service {}!",
335 pathDescriptionIID,serviceName, e);
341 private void readCoRoutingContrains(CoRouting tmpcoRouting, PceConstraints constraints) {
342 LOG.info("In readCoRoutingContrains start");
344 if (tmpcoRouting == null) {
345 LOG.info("In readCoRoutingContrains: no General constraints.");
351 public PceConstraints getPceHardConstraints() {
352 return pceHardConstraints;
355 public PceConstraints getPceSoftConstraints() {
356 return pceSoftConstraints;
359 public PceMetric getPceMetrics() {