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 com.google.common.base.Optional;
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.concurrent.ExecutionException;
15 import java.util.concurrent.TimeUnit;
16 import java.util.concurrent.TimeoutException;
17 import java.util.stream.Collectors;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.transportpce.common.Timeouts;
21 import org.opendaylight.transportpce.common.network.NetworkTransactionService;
22 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev190624.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.Link;
26 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.pce.resource.resource.resource.Node;
27 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.RoutingConstraintsSp.PceMetric;
28 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.constraints.sp.CoRoutingOrGeneral;
29 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.constraints.sp.co.routing.or.general.CoRouting;
30 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.constraints.sp.co.routing.or.general.General;
31 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.constraints.sp.co.routing.or.general.general.Diversity;
32 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.constraints.sp.co.routing.or.general.general.Exclude;
33 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.constraints.sp.co.routing.or.general.general.Include;
34 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.constraints.sp.co.routing.or.general.general.Latency;
35 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.constraints.sp.co.routing.or.general.general.include_.OrderedHops;
36 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.diversity.existing.service.contraints.sp.ExistingServiceApplicability;
37 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.ordered.constraints.sp.hop.type.HopType;
38 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.routing.constraints.sp.HardConstraints;
39 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.routing.constraints.sp.SoftConstraints;
40 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.ServicePathList;
41 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.service.path.list.ServicePaths;
42 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.service.path.list.ServicePathsKey;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
48 public class PceConstraintsCalc {
50 private static final Logger LOG = LoggerFactory.getLogger(PceConstraintsCalc.class);
52 private PceConstraints pceHardConstraints = new PceConstraints();
53 private PceConstraints pceSoftConstraints = new PceConstraints();
54 private PceMetric pceMetrics = PceMetric.HopCount;
55 private NetworkTransactionService networkTransactionService;
57 public PceConstraintsCalc(PathComputationRequestInput input, NetworkTransactionService networkTransactionService) {
58 LOG.debug("In PceconstraintsCalc start");
60 pceMetrics = input.getPceMetric();
62 this.networkTransactionService = networkTransactionService;
64 // TODO. for now metrics are set into hard structure
65 LOG.info("In PceConstraintsCalc: read PceMetric {}", pceMetrics.toString());
66 pceHardConstraints.setPceMetrics(pceMetrics);
68 calcHardconstraints(input);
69 calcSoftconstraints(input);
72 private void calcHardconstraints(PathComputationRequestInput input) {
73 HardConstraints servicePathHardConstraints = input.getHardConstraints();
74 if (servicePathHardConstraints == null) {
75 LOG.info("In calcHardconstraints: no hard constraints.");
79 CoRoutingOrGeneral coRoutingOrGeneral = servicePathHardConstraints.getCoRoutingOrGeneral();
80 readconstraints(coRoutingOrGeneral, pceHardConstraints);
84 private void calcSoftconstraints(PathComputationRequestInput input) {
85 SoftConstraints servicePathSoftConstraints = input.getSoftConstraints();
86 if (servicePathSoftConstraints == null) {
87 LOG.info("In calcSoftconstraints: no soft constraints.");
91 CoRoutingOrGeneral coRoutingOrGeneral = servicePathSoftConstraints.getCoRoutingOrGeneral();
92 readconstraints(coRoutingOrGeneral, pceSoftConstraints);
96 private void readconstraints(CoRoutingOrGeneral coRoutingOrGeneral, PceConstraints constraints) {
97 LOG.debug("In readconstraints start");
99 if (coRoutingOrGeneral == null) {
100 LOG.info("In readHardconstraints: no CoRoutingOrGeneral constraints.");
104 General tmpGeneral = null;
105 CoRouting tmpCoRouting = null;
107 if (coRoutingOrGeneral instanceof General) {
108 LOG.info("In readconstraints General {}", coRoutingOrGeneral.toString());
109 tmpGeneral = (General) coRoutingOrGeneral;
110 readGeneralContrains(tmpGeneral, constraints);
114 if (coRoutingOrGeneral instanceof CoRouting) {
115 LOG.info("In readconstraints CoRouting {}", coRoutingOrGeneral.toString());
116 tmpCoRouting = (CoRouting) coRoutingOrGeneral;
117 readCoRoutingContrains(tmpCoRouting, constraints);
125 private void readGeneralContrains(General tmpGeneral, PceConstraints constraints) {
126 LOG.debug("In readGeneralContrains start");
128 if (tmpGeneral == null) {
129 LOG.info("In readGeneralContrains: no General constraints.");
133 Latency latency = tmpGeneral.getLatency();
134 if (latency != null) {
135 constraints.setMaxLatency(latency.getMaxLatency());
136 LOG.info("In readGeneralContrains: read latency {}", latency.toString());
139 Exclude exclude = tmpGeneral.getExclude();
140 List<String> elementsToExclude = null;
141 if (exclude != null) {
142 elementsToExclude = exclude.getNodeId();
143 if (elementsToExclude != null) {
144 constraints.setExcludeSupNodes(elementsToExclude);
147 elementsToExclude = exclude.getSRLG();
148 if (elementsToExclude != null) {
149 List<Long> srlgToExclude = new ArrayList<Long>();
150 for (String str : elementsToExclude) {
151 srlgToExclude.add(Long.parseLong(str));
153 constraints.setExcludeSRLG(srlgToExclude);
156 elementsToExclude = exclude.getClli();
157 if (elementsToExclude != null) {
158 constraints.setExcludeCLLI(elementsToExclude);
162 Include include = tmpGeneral.getInclude();
163 if (include != null) {
164 List<OrderedHops> listHops = include.getOrderedHops();
165 if (listHops != null) {
166 readIncludeNodes(listHops, constraints);
168 LOG.debug("in readGeneralContrains INCLUDE {} ", include.toString());
171 Diversity diversity = tmpGeneral.getDiversity();
172 PceConstraints.ResourceType rt = PceConstraints.ResourceType.NONE;
173 if (diversity != null) {
174 ExistingServiceApplicability temp = diversity.getExistingServiceApplicability();
179 rt = PceConstraints.ResourceType.NODE;
182 rt = PceConstraints.ResourceType.SRLG;
185 rt = PceConstraints.ResourceType.CLLI;
187 LOG.info("in readGeneralContrains {} list is :{}", rt, diversity.toString());
188 readDiversity(diversity.getExistingService(), constraints, rt);
193 private void readIncludeNodes(List<OrderedHops> listHops, PceConstraints constraints) {
194 for (int i = 0; i < listHops.size(); i++) {
195 HopType hoptype = listHops.get(i).getHopType().getHopType();
197 String hopt = hoptype.getImplementedInterface().getSimpleName();
198 LOG.info("in readIncludeNodes next hop to include {}", hopt);
201 org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints
202 .rev171017.ordered.constraints.sp.hop.type.hop.type.Node
203 node = (org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing
204 .constraints.rev171017.ordered.constraints.sp.hop.type.hop.type.Node) hoptype;
205 constraints.setListToInclude(constraints.new ResourcePair(PceConstraints.ResourceType.NODE,
209 org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints
210 .rev171017.ordered.constraints.sp.hop.type.hop.type.SRLG
211 srlg = (org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing
212 .constraints.rev171017.ordered.constraints.sp.hop.type.hop.type.SRLG) hoptype;
213 constraints.setListToInclude(constraints.new ResourcePair(PceConstraints.ResourceType.SRLG,
217 org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints
218 .rev171017.ordered.constraints.sp.hop.type.hop.type.Clli
219 clli = (org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing
220 .constraints.rev171017.ordered.constraints.sp.hop.type.hop.type.Clli) hoptype;
221 constraints.setListToInclude(constraints.new ResourcePair(PceConstraints.ResourceType.CLLI,
225 LOG.error("in readIncludeNodes unsupported include type {}", hopt);
230 private void readDiversity(List<String> srvList, PceConstraints constraints, PceConstraints.ResourceType rt) {
232 List<String> elementsToExclude = new ArrayList<String>();
233 LOG.info("in readDiversity {}", srvList.toString());
235 for (String srv : srvList) {
236 Optional<PathDescription> service = getPathDescriptionFromDatastore(srv);
237 if (service.isPresent()) {
238 LOG.info("in readDiversity service list {}", service.toString());
242 .addAll(getAToZNodeList(service.get()));
243 LOG.info("readDiversity NODE : {}", elementsToExclude);
244 if (elementsToExclude != null) {
245 constraints.setExcludeNodes(elementsToExclude);
250 .addAll(getSRLGList(service.get()));
251 LOG.info("readDiversity SRLG : {}", elementsToExclude);
252 if (elementsToExclude != null) {
253 constraints.setExcludeSrlgLinks(elementsToExclude);
257 /// Retrieve nodes into dedicated CLLI list
258 /// during node validation check their CLLI and build CLLI exclude list
260 .addAll(getAToZNodeList(service.get()));
261 LOG.info("readDiversity CLLI : {}", elementsToExclude);
262 if (elementsToExclude != null) {
263 constraints.setExcludeClliNodes(elementsToExclude);
267 LOG.info("in readDiversity unsupported divercity type", rt);
271 LOG.info("in readDiversity srv={} is not present", srv);
277 private List<String> getAToZNodeList(PathDescription pathDescription) {
278 List<AToZ> aendToZList = pathDescription.getAToZDirection().getAToZ();
279 return aendToZList.stream().filter(aToZ -> {
280 if (aToZ.getResource() == null || aToZ.getResource().getResource() == null) {
281 LOG.warn("Diversity constraint: Resource of AToZ node {} is null! Skipping this node!", aToZ.getId());
284 return aToZ.getResource().getResource() instanceof Node;
286 Node node = (Node) aToZ.getResource().getResource();
287 if (node.getNodeId() == null) {
288 LOG.warn("Node in AToZ node {} contains null! Skipping this node!", aToZ.getId());
293 Node node = ((Node) aToZ.getResource().getResource());
294 return node.getNodeId().toString();
295 }).collect(Collectors.toList());
298 private List<String> getSRLGList(PathDescription pathDescription) {
299 List<AToZ> aendToZList = pathDescription.getAToZDirection().getAToZ();
300 return aendToZList.stream().filter(aToZ -> {
301 if (aToZ.getResource() == null
302 || aToZ.getResource().getResource() == null) {
303 LOG.warn("Diversity constraint: Resource of AToZ {} is null! Skipping this resource!", aToZ.getId());
306 return aToZ.getResource().getResource() instanceof Link;
308 Link link = (Link) aToZ.getResource().getResource();
309 if (link.getLinkId() == null) {
310 LOG.warn("Link in AToZ link {} contains null! Skipping this link!", aToZ.getId());
315 return ((Link) aToZ.getResource().getResource()).getLinkId();
316 }).collect(Collectors.toList());
319 private Optional<PathDescription> getPathDescriptionFromDatastore(String serviceName) {
320 Optional<PathDescription> result = Optional.absent();
321 InstanceIdentifier<ServicePaths> pathDescriptionIID = InstanceIdentifier.create(ServicePathList.class)
322 .child(ServicePaths.class, new ServicePathsKey(serviceName));
324 LOG.info("PCE diversity constraints: Getting path description for service {}", serviceName);
325 ServicePaths servicePaths =
326 networkTransactionService.read(LogicalDatastoreType.CONFIGURATION, pathDescriptionIID)
327 .get(Timeouts.DATASTORE_READ, TimeUnit.MILLISECONDS).get();
328 if (servicePaths != null) {
329 PathDescription path = servicePaths.getPathDescription();
331 result = Optional.of(path);
334 } catch (InterruptedException | ExecutionException | TimeoutException e) {
336 "PCE diversity constraints: Exception while getting path description from datastore {} for service {}!",
337 pathDescriptionIID,serviceName, e);
343 private void readCoRoutingContrains(CoRouting tmpcoRouting, PceConstraints constraints) {
344 LOG.info("In readCoRoutingContrains start");
346 if (tmpcoRouting == null) {
347 LOG.info("In readCoRoutingContrains: no General constraints.");
353 public PceConstraints getPceHardConstraints() {
354 return pceHardConstraints;
357 public PceConstraints getPceSoftConstraints() {
358 return pceSoftConstraints;
361 public PceMetric getPceMetrics() {