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.Collection;
12 import java.util.List;
14 import java.util.Optional;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.TimeUnit;
17 import java.util.concurrent.TimeoutException;
18 import java.util.stream.Collectors;
19 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
20 import org.opendaylight.transportpce.common.Timeouts;
21 import org.opendaylight.transportpce.common.network.NetworkTransactionService;
22 import org.opendaylight.transportpce.pce.constraints.PceConstraints.ResourcePair;
23 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220615.PathComputationRequestInput;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.node.types.rev210528.NodeIdType;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.Constraints;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.CoRouting;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.Diversity;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.Exclude;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.Include;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.diversity.existing.service.constraints.ServiceIdentifierList;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.diversity.existing.service.constraints.ServiceIdentifierListKey;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.routing.constraints.HardConstraints;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.routing.constraints.SoftConstraints;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.service.applicability.g.ServiceApplicability;
35 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.PathDescription;
36 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.path.description.atoz.direction.AToZ;
37 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.pce.resource.resource.resource.Link;
38 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.pce.resource.resource.resource.Node;
39 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev220118.PceMetric;
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;
44 import org.opendaylight.yangtools.yang.common.Uint32;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
49 public class PceConstraintsCalc {
51 private static final Logger LOG = LoggerFactory.getLogger(PceConstraintsCalc.class);
53 private PceConstraints pceHardConstraints = new PceConstraints();
54 private PceConstraints pceSoftConstraints = new PceConstraints();
55 private PceMetric pceMetrics = PceMetric.HopCount;
56 private NetworkTransactionService networkTransactionService;
58 public PceConstraintsCalc(PathComputationRequestInput input, NetworkTransactionService networkTransactionService) {
59 LOG.debug("In PceconstraintsCalc start");
61 pceMetrics = input.getPceRoutingMetric();
63 this.networkTransactionService = networkTransactionService;
65 // TODO. for now metrics are set into hard structure
66 LOG.info("In PceConstraintsCalc: read PceMetric {}", pceMetrics);
67 pceHardConstraints.setPceMetrics(pceMetrics);
69 calcHardconstraints(input);
70 calcSoftconstraints(input);
73 private void calcHardconstraints(PathComputationRequestInput input) {
74 HardConstraints servicePathHardConstraints = input.getHardConstraints();
75 if (servicePathHardConstraints == null) {
76 LOG.info("In calcHardconstraints: no hard constraints.");
79 readConstraints(servicePathHardConstraints, pceHardConstraints);
82 private void calcSoftconstraints(PathComputationRequestInput input) {
83 SoftConstraints servicePathSoftConstraints = input.getSoftConstraints();
84 if (servicePathSoftConstraints == null) {
85 LOG.info("In calcSoftconstraints: no soft constraints.");
88 readConstraints(servicePathSoftConstraints, pceSoftConstraints);
91 private void readConstraints(Constraints hardConstraints, PceConstraints constraints) {
92 LOG.debug("In readHardconstraints start");
94 if (hardConstraints.getInclude() != null) {
95 readInclude(hardConstraints.getInclude(), constraints);
97 if (hardConstraints.getExclude() != null) {
98 readExclude(hardConstraints.getExclude(), constraints);
100 if (hardConstraints.getCoRouting() != null) {
101 readCoRouting(hardConstraints.getCoRouting(), constraints);
103 if (hardConstraints.getDiversity() != null) {
104 readDiversity(hardConstraints.getDiversity(), constraints);
106 if (hardConstraints.getLatency() != null) {
107 constraints.setMaxLatency(hardConstraints.getLatency().getMaxLatency().longValue());
111 private void readExclude(Exclude exclude, PceConstraints constraints) {
112 //TODO: Implement other exclude constraints: fiber-bundle, link-identifier
113 // and supporting-service-name
114 if (exclude.getNodeId() != null) {
115 List<String> elementsToExclude = new ArrayList<>();
116 for (NodeIdType node : exclude.getNodeId()) {
117 elementsToExclude.add(node.getValue());
119 constraints.setExcludeSupNodes(elementsToExclude);
121 if (exclude.getSrlgId() != null) {
122 List<Long> elementsToExclude = new ArrayList<>();
123 for (Uint32 srlg : exclude.getSrlgId()) {
124 elementsToExclude.add(srlg.longValue());
126 constraints.setExcludeSRLG(elementsToExclude);
128 if (exclude.getSite() != null) {
129 constraints.setExcludeCLLI(new ArrayList<>(exclude.getSite()));
131 if (exclude.getFiberBundle() != null || exclude.getLinkIdentifier() != null
132 || exclude.getSupportingServiceName() != null) {
133 LOG.warn("exclude constraints of type fiber-bundle, link-identifier"
134 + "or supporting-service-name are not implemented yet");
138 private void readInclude(Include include, PceConstraints constraints) {
139 if (include.getNodeId() != null) {
140 for (NodeIdType node : include.getNodeId()) {
141 constraints.setListToInclude(new ResourcePair(PceConstraints.ResourceType.NODE, node.getValue()));
144 if (include.getSrlgId() != null) {
145 for (Uint32 srlg : include.getSrlgId()) {
146 constraints.setListToInclude(new ResourcePair(PceConstraints.ResourceType.SRLG, srlg.toString()));
149 if (include.getSite() != null) {
150 for (String site : include.getSite()) {
151 constraints.setListToInclude(new ResourcePair(PceConstraints.ResourceType.CLLI, site));
156 private void readCoRouting(CoRouting tmpcoRouting, PceConstraints constraints) {
157 if (tmpcoRouting == null) {
158 LOG.info("In readCoRoutingContrains: no CoRouting constraints.");
160 LOG.warn("CoRouting constraints handling not implemented yet");
164 private void readDiversity(Diversity diversity, PceConstraints constraints) {
165 //TODO: How to implement the DiversityType: serial or synchronous?
166 Map<ServiceIdentifierListKey, ServiceIdentifierList> serviceIdList = diversity.getServiceIdentifierList();
167 Collection<ServiceIdentifierList> services = serviceIdList.values();
168 for (ServiceIdentifierList serviceIdentifier : services) {
169 String serviceId = serviceIdentifier.getServiceIndentifier();
170 ServiceApplicability serviceApplicability = serviceIdentifier.getServiceApplicability();
171 Optional<PathDescription> serviceOpt = getPathDescriptionFromDatastore(serviceId);
172 if (serviceOpt.isPresent()) {
173 List<String> serviceNodes = getAToZNodeList(serviceOpt.get());
174 if (serviceApplicability.getNode() && !serviceNodes.isEmpty()) {
175 constraints.setExcludeNodes(serviceNodes);
177 List<String> serviceLinks = getSRLGList(serviceOpt.get());
178 if (serviceApplicability.getLink() && !serviceLinks.isEmpty()) {
179 constraints.setExcludeSrlgLinks(serviceLinks);
181 if (serviceApplicability.getSite() && !serviceNodes.isEmpty()) {
182 constraints.setExcludeClliNodes(serviceNodes);
188 private List<String> getAToZNodeList(PathDescription pathDescription) {
189 List<AToZ> aendToZList = new ArrayList<>(pathDescription.getAToZDirection().nonnullAToZ().values());
190 return aendToZList.stream().filter(aToZ -> {
191 if (aToZ.getResource() == null || aToZ.getResource().getResource() == null) {
192 LOG.warn("Diversity constraint: Resource of AToZ node {} is null! Skipping this node!", aToZ.getId());
195 return aToZ.getResource().getResource() instanceof Node;
197 Node node = (Node) aToZ.getResource().getResource();
198 if (node.getNodeId() == null) {
199 LOG.warn("Node in AToZ node {} contains null! Skipping this node!", aToZ.getId());
204 Node node = ((Node) aToZ.getResource().getResource());
205 return node.getNodeId();
206 }).collect(Collectors.toList());
209 private List<String> getSRLGList(PathDescription pathDescription) {
210 List<AToZ> aendToZList = new ArrayList<>(pathDescription.getAToZDirection().nonnullAToZ().values());
211 return aendToZList.stream().filter(aToZ -> {
212 if (aToZ.getResource() == null
213 || aToZ.getResource().getResource() == null) {
214 LOG.warn("Diversity constraint: Resource of AToZ {} is null! Skipping this resource!", aToZ.getId());
217 return aToZ.getResource().getResource() instanceof Link;
219 Link link = (Link) aToZ.getResource().getResource();
220 if (link.getLinkId() == null) {
221 LOG.warn("Link in AToZ link {} contains null! Skipping this link!", aToZ.getId());
226 return ((Link) aToZ.getResource().getResource()).getLinkId();
227 }).collect(Collectors.toList());
230 private Optional<PathDescription> getPathDescriptionFromDatastore(String serviceName) {
231 Optional<PathDescription> result = Optional.empty();
232 InstanceIdentifier<ServicePaths> pathDescriptionIID = InstanceIdentifier.create(ServicePathList.class)
233 .child(ServicePaths.class, new ServicePathsKey(serviceName));
235 LOG.info("PCE diversity constraints: Getting path description for service {}", serviceName);
236 ServicePaths servicePaths =
237 networkTransactionService.read(LogicalDatastoreType.CONFIGURATION, pathDescriptionIID)
238 .get(Timeouts.DATASTORE_READ, TimeUnit.MILLISECONDS).get();
239 if (servicePaths != null) {
240 PathDescription path = servicePaths.getPathDescription();
242 result = Optional.of(path);
245 } catch (InterruptedException | ExecutionException | TimeoutException e) {
247 "PCE diversity constraints: Exception while getting path description from datastore {} for service {}!",
248 pathDescriptionIID,serviceName, e);
254 public PceConstraints getPceHardConstraints() {
255 return pceHardConstraints;
258 public PceConstraints getPceSoftConstraints() {
259 return pceSoftConstraints;
262 public PceMetric getPceMetrics() {