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.rev200629.PathDescription;
22 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev200629.path.description.atoz.direction.AToZ;
23 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev200629.pce.resource.resource.resource.Link;
24 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev200629.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;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 public class PceConstraintsCalc {
47 private static final Logger LOG = LoggerFactory.getLogger(PceConstraintsCalc.class);
49 private PceConstraints pceHardConstraints = new PceConstraints();
50 private PceConstraints pceSoftConstraints = new PceConstraints();
51 private PceMetric pceMetrics = PceMetric.HopCount;
52 private NetworkTransactionService networkTransactionService;
54 public PceConstraintsCalc(PathComputationRequestInput input, NetworkTransactionService networkTransactionService) {
55 LOG.debug("In PceconstraintsCalc start");
57 pceMetrics = input.getPceMetric();
59 this.networkTransactionService = networkTransactionService;
61 // TODO. for now metrics are set into hard structure
62 LOG.info("In PceConstraintsCalc: read PceMetric {}", pceMetrics);
63 pceHardConstraints.setPceMetrics(pceMetrics);
65 calcHardconstraints(input);
66 calcSoftconstraints(input);
69 private void calcHardconstraints(PathComputationRequestInput input) {
70 HardConstraints servicePathHardConstraints = input.getHardConstraints();
71 if (servicePathHardConstraints == null) {
72 LOG.info("In calcHardconstraints: no hard constraints.");
76 CoRoutingOrGeneral coRoutingOrGeneral = servicePathHardConstraints.getCoRoutingOrGeneral();
77 readconstraints(coRoutingOrGeneral, pceHardConstraints);
81 private void calcSoftconstraints(PathComputationRequestInput input) {
82 SoftConstraints servicePathSoftConstraints = input.getSoftConstraints();
83 if (servicePathSoftConstraints == null) {
84 LOG.info("In calcSoftconstraints: no soft constraints.");
88 CoRoutingOrGeneral coRoutingOrGeneral = servicePathSoftConstraints.getCoRoutingOrGeneral();
89 readconstraints(coRoutingOrGeneral, pceSoftConstraints);
93 private void readconstraints(CoRoutingOrGeneral coRoutingOrGeneral, PceConstraints constraints) {
94 LOG.debug("In readconstraints start");
96 if (coRoutingOrGeneral == null) {
97 LOG.info("In readHardconstraints: no CoRoutingOrGeneral constraints.");
101 General tmpGeneral = null;
102 CoRouting tmpCoRouting = null;
104 if (coRoutingOrGeneral instanceof General) {
105 LOG.info("In readconstraints General {}", coRoutingOrGeneral);
106 tmpGeneral = (General) coRoutingOrGeneral;
107 readGeneralContrains(tmpGeneral, constraints);
111 if (coRoutingOrGeneral instanceof CoRouting) {
112 LOG.info("In readconstraints CoRouting {}", coRoutingOrGeneral);
113 tmpCoRouting = (CoRouting) coRoutingOrGeneral;
114 readCoRoutingContrains(tmpCoRouting, constraints);
118 private void readGeneralContrains(General tmpGeneral, PceConstraints constraints) {
119 LOG.debug("In readGeneralContrains start");
121 if (tmpGeneral == null) {
122 LOG.info("In readGeneralContrains: no General constraints.");
126 Latency latency = tmpGeneral.getLatency();
127 if (latency != null) {
128 constraints.setMaxLatency(latency.getMaxLatency().toJava());
129 LOG.info("In readGeneralContrains: read latency {}", latency);
132 Exclude exclude = tmpGeneral.getExclude();
133 List<String> elementsToExclude = null;
134 if (exclude != null) {
135 elementsToExclude = exclude.getNodeId();
136 if (elementsToExclude != null) {
137 constraints.setExcludeSupNodes(elementsToExclude);
140 elementsToExclude = exclude.getSRLG();
141 if (elementsToExclude != null) {
142 List<Long> srlgToExclude = new ArrayList<>();
143 for (String str : elementsToExclude) {
144 srlgToExclude.add(Long.parseLong(str));
146 constraints.setExcludeSRLG(srlgToExclude);
149 elementsToExclude = exclude.getClli();
150 if (elementsToExclude != null) {
151 constraints.setExcludeCLLI(elementsToExclude);
155 Include include = tmpGeneral.getInclude();
156 if (include != null) {
157 List<OrderedHops> listHops = new ArrayList<>(include.nonnullOrderedHops().values());
158 if (listHops != null) {
159 readIncludeNodes(listHops, constraints);
161 LOG.debug("in readGeneralContrains INCLUDE {} ", include);
164 Diversity diversity = tmpGeneral.getDiversity();
165 PceConstraints.ResourceType rt = PceConstraints.ResourceType.NONE;
166 if (diversity != null) {
167 ExistingServiceApplicability temp = diversity.getExistingServiceApplicability();
171 if (Boolean.TRUE.equals(temp.isNode())) {
172 rt = PceConstraints.ResourceType.NODE;
174 if (Boolean.TRUE.equals(temp.isSrlg())) {
175 rt = PceConstraints.ResourceType.SRLG;
177 if (Boolean.TRUE.equals(temp.isClli())) {
178 rt = PceConstraints.ResourceType.CLLI;
180 LOG.info("in readGeneralContrains {} list is :{}", rt, diversity);
181 readDiversity(diversity.getExistingService(), constraints, rt);
186 private void readIncludeNodes(List<OrderedHops> listHops, PceConstraints constraints) {
187 for (int i = 0; i < listHops.size(); i++) {
188 HopType hoptype = listHops.get(i).getHopType().getHopType();
190 String hopt = hoptype.implementedInterface().getSimpleName();
191 LOG.info("in readIncludeNodes next hop to include {}", hopt);
194 org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints
195 .rev171017.ordered.constraints.sp.hop.type.hop.type.Node
196 node = (org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing
197 .constraints.rev171017.ordered.constraints.sp.hop.type.hop.type.Node) hoptype;
198 constraints.setListToInclude(new PceConstraints.ResourcePair(PceConstraints.ResourceType.NODE,
202 org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints
203 .rev171017.ordered.constraints.sp.hop.type.hop.type.SRLG
204 srlg = (org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing
205 .constraints.rev171017.ordered.constraints.sp.hop.type.hop.type.SRLG) hoptype;
206 constraints.setListToInclude(new PceConstraints.ResourcePair(PceConstraints.ResourceType.SRLG,
210 org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints
211 .rev171017.ordered.constraints.sp.hop.type.hop.type.Clli
212 clli = (org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing
213 .constraints.rev171017.ordered.constraints.sp.hop.type.hop.type.Clli) hoptype;
214 constraints.setListToInclude(new PceConstraints.ResourcePair(PceConstraints.ResourceType.CLLI,
218 LOG.error("in readIncludeNodes unsupported include type {}", hopt);
223 private void readDiversity(List<String> srvList, PceConstraints constraints, PceConstraints.ResourceType rt) {
225 List<String> elementsToExclude = new ArrayList<>();
226 LOG.info("in readDiversity {}", srvList);
228 for (String srv : srvList) {
229 Optional<PathDescription> service = getPathDescriptionFromDatastore(srv);
230 if (service.isPresent()) {
231 LOG.info("in readDiversity service list {}", service);
234 elementsToExclude.addAll(getAToZNodeList(service.get()));
235 LOG.info("readDiversity NODE : {}", elementsToExclude);
236 if (elementsToExclude != null) {
237 constraints.setExcludeNodes(elementsToExclude);
241 elementsToExclude.addAll(getSRLGList(service.get()));
242 LOG.info("readDiversity SRLG : {}", elementsToExclude);
243 if (elementsToExclude != null) {
244 constraints.setExcludeSrlgLinks(elementsToExclude);
248 /// Retrieve nodes into dedicated CLLI list
249 /// during node validation check their CLLI and build CLLI exclude list
250 elementsToExclude.addAll(getAToZNodeList(service.get()));
251 LOG.info("readDiversity CLLI : {}", elementsToExclude);
252 if (elementsToExclude != null) {
253 constraints.setExcludeClliNodes(elementsToExclude);
257 LOG.info("in readDiversity unsupported divercity type {}", rt);
261 LOG.info("in readDiversity srv={} is not present", srv);
267 private List<String> getAToZNodeList(PathDescription pathDescription) {
268 List<AToZ> aendToZList = new ArrayList<>(pathDescription.getAToZDirection().nonnullAToZ().values());
269 return aendToZList.stream().filter(aToZ -> {
270 if (aToZ.getResource() == null || aToZ.getResource().getResource() == null) {
271 LOG.warn("Diversity constraint: Resource of AToZ node {} is null! Skipping this node!", aToZ.getId());
274 return aToZ.getResource().getResource() instanceof Node;
276 Node node = (Node) aToZ.getResource().getResource();
277 if (node.getNodeId() == null) {
278 LOG.warn("Node in AToZ node {} contains null! Skipping this node!", aToZ.getId());
283 Node node = ((Node) aToZ.getResource().getResource());
284 return node.getNodeId();
285 }).collect(Collectors.toList());
288 private List<String> getSRLGList(PathDescription pathDescription) {
289 List<AToZ> aendToZList = new ArrayList<>(pathDescription.getAToZDirection().nonnullAToZ().values());
290 return aendToZList.stream().filter(aToZ -> {
291 if (aToZ.getResource() == null
292 || aToZ.getResource().getResource() == null) {
293 LOG.warn("Diversity constraint: Resource of AToZ {} is null! Skipping this resource!", aToZ.getId());
296 return aToZ.getResource().getResource() instanceof Link;
298 Link link = (Link) aToZ.getResource().getResource();
299 if (link.getLinkId() == null) {
300 LOG.warn("Link in AToZ link {} contains null! Skipping this link!", aToZ.getId());
305 return ((Link) aToZ.getResource().getResource()).getLinkId();
306 }).collect(Collectors.toList());
309 private Optional<PathDescription> getPathDescriptionFromDatastore(String serviceName) {
310 Optional<PathDescription> result = Optional.empty();
311 InstanceIdentifier<ServicePaths> pathDescriptionIID = InstanceIdentifier.create(ServicePathList.class)
312 .child(ServicePaths.class, new ServicePathsKey(serviceName));
314 LOG.info("PCE diversity constraints: Getting path description for service {}", serviceName);
315 ServicePaths servicePaths =
316 networkTransactionService.read(LogicalDatastoreType.CONFIGURATION, pathDescriptionIID)
317 .get(Timeouts.DATASTORE_READ, TimeUnit.MILLISECONDS).get();
318 if (servicePaths != null) {
319 PathDescription path = servicePaths.getPathDescription();
321 result = Optional.of(path);
324 } catch (InterruptedException | ExecutionException | TimeoutException e) {
326 "PCE diversity constraints: Exception while getting path description from datastore {} for service {}!",
327 pathDescriptionIID,serviceName, e);
333 private void readCoRoutingContrains(CoRouting tmpcoRouting, PceConstraints constraints) {
334 LOG.info("In readCoRoutingContrains start");
336 if (tmpcoRouting == null) {
337 LOG.info("In readCoRoutingContrains: no General constraints.");
342 public PceConstraints getPceHardConstraints() {
343 return pceHardConstraints;
346 public PceConstraints getPceSoftConstraints() {
347 return pceSoftConstraints;
350 public PceMetric getPceMetrics() {