2 * Copyright © 2017 AT&T, Inc. 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;
10 import com.google.common.base.Optional;
11 import java.util.ArrayList;
12 import java.util.List;
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.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.transportpce.common.Timeouts;
21 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev190624.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.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.Latency;
32 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.routing.constraints.sp.HardConstraints;
33 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.routing.constraints.sp.SoftConstraints;
34 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.ServicePathList;
35 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.service.path.list.ServicePaths;
36 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.service.path.list.ServicePathsKey;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 public class PceConstraintsCalc {
43 private static final Logger LOG = LoggerFactory.getLogger(PceCalculation.class);
45 private PceConstraints pceHardConstraints = new PceConstraints();
46 private PceConstraints pceSoftConstraints = new PceConstraints();
47 private PceMetric pceMetrics = PceMetric.HopCount;
48 private DataBroker dataBroker;
51 public PceConstraintsCalc(PathComputationRequestInput input, DataBroker dataBroker) {
52 LOG.debug("In PceconstraintsCalc start");
54 this.pceMetrics = input.getPceMetric();
56 this.dataBroker = dataBroker;
58 // TODO. for now metrics are set into hard structure
59 LOG.info("In PceConstraintsCalc: read PceMetric {}", this.pceMetrics.toString());
60 this.pceHardConstraints.setPceMetrics(this.pceMetrics);
62 calcHardconstraints(input);
63 calcSoftconstraints(input);
66 private void calcHardconstraints(PathComputationRequestInput input) {
67 HardConstraints servicePathHardConstraints = input.getHardConstraints();
68 if (servicePathHardConstraints == null) {
69 LOG.info("In calcHardconstraints: no hard constraints.");
73 CoRoutingOrGeneral coRoutingOrGeneral = servicePathHardConstraints.getCoRoutingOrGeneral();
74 readconstraints(coRoutingOrGeneral, this.pceHardConstraints);
78 private void calcSoftconstraints(PathComputationRequestInput input) {
79 SoftConstraints servicePathSoftConstraints = input.getSoftConstraints();
80 if (servicePathSoftConstraints == null) {
81 LOG.info("In calcSoftconstraints: no soft constraints.");
85 CoRoutingOrGeneral coRoutingOrGeneral = servicePathSoftConstraints.getCoRoutingOrGeneral();
86 readconstraints(coRoutingOrGeneral, this.pceSoftConstraints);
90 private void readconstraints(CoRoutingOrGeneral coRoutingOrGeneral, PceConstraints constraints) {
91 LOG.debug("In readconstraints start");
93 if (coRoutingOrGeneral == null) {
94 LOG.info("In readHardconstraints: no CoRoutingOrGeneral constraints.");
98 General tmpGeneral = null;
99 CoRouting tmpCoRouting = null;
101 if (coRoutingOrGeneral instanceof General) {
102 LOG.info("In readconstraints General {}", coRoutingOrGeneral.toString());
103 tmpGeneral = (General) coRoutingOrGeneral;
104 readGeneralContrains(tmpGeneral, constraints);
108 if (coRoutingOrGeneral instanceof CoRouting) {
109 LOG.info("In readconstraints CoRouting {}", coRoutingOrGeneral.toString());
110 tmpCoRouting = (CoRouting) coRoutingOrGeneral;
111 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());
130 LOG.info("In readGeneralContrains: read latency {}", latency.toString());
133 Exclude exclude = tmpGeneral.getExclude();
134 List<String> elementsToExclude = null;
135 if (exclude != null) {
136 elementsToExclude = exclude.getNodeId();
137 if (elementsToExclude != null) {
138 constraints.setExcludeNodes(elementsToExclude);
140 elementsToExclude = exclude.getSRLG();
141 if (elementsToExclude != null) {
142 constraints.setExcludeSRLG(elementsToExclude);
146 Diversity diversity = tmpGeneral.getDiversity();
147 if ((diversity != null) && (diversity.getExistingServiceApplicability().isNode())) {
148 LOG.info("in readGeneralContrains {}", diversity.toString());
149 readDiversityNodes(diversity.getExistingService(), constraints);
154 private void readDiversityNodes(List<String> srvList, PceConstraints constraints) {
156 List<String> elementsToExclude = new ArrayList<String>();
157 LOG.info("in readDiversityNodes {}", srvList.toString());
159 for (String srv : srvList) {
160 Optional<PathDescription> service = getPathDescriptionFromDatastore(srv);
161 if (service.isPresent()) {
162 elementsToExclude.addAll(getAToZNodeList(service.get()));
163 LOG.info("readDiversityNodes : {}", elementsToExclude);
166 LOG.info("in readDiversityNodes srv={} is not present", srv);
170 if (elementsToExclude != null) {
171 constraints.setExcludeNodes(elementsToExclude);
175 private List<String> getAToZNodeList(PathDescription pathDescription) {
176 List<AToZ> atozList = pathDescription.getAToZDirection().getAToZ();
177 return atozList.stream().filter(aToZ -> {
178 if ((aToZ.getResource() == null) || (aToZ.getResource().getResource() == null)) {
179 LOG.warn("Diversity constraint: Resource of AToZ node {} is null! Skipping this node!", aToZ.getId());
182 return aToZ.getResource().getResource() instanceof Node;
184 Node node = (Node) aToZ.getResource().getResource();
185 if (node.getNodeId() == null) {
186 LOG.warn("Node in AToZ node {} contains null! Skipping this node!", aToZ.getId());
189 return node.getNodeId().toString();
190 }).collect(Collectors.toList());
193 private Optional<PathDescription> getPathDescriptionFromDatastore(String serviceName) {
194 Optional<PathDescription> result = Optional.absent();
195 InstanceIdentifier<ServicePaths> pathDescriptionIID = InstanceIdentifier.create(ServicePathList.class)
196 .child(ServicePaths.class, new ServicePathsKey(serviceName));
197 ReadOnlyTransaction pathDescReadTx = this.dataBroker.newReadOnlyTransaction();
199 LOG.info("PCE diversity constraints: Getting path description for service {}", serviceName);
200 ServicePaths servicePaths = pathDescReadTx.read(LogicalDatastoreType.CONFIGURATION, pathDescriptionIID)
201 .get(Timeouts.DATASTORE_READ, TimeUnit.MILLISECONDS).get();
202 if (servicePaths != null) {
203 PathDescription path = servicePaths.getPathDescription();
205 result = Optional.of(path);
208 } catch (InterruptedException | ExecutionException | TimeoutException e) {
210 "PCE diversity constraints: Exception while getting path description from datastore {} for service {}!",
211 pathDescriptionIID,serviceName, e);
217 private void readCoRoutingContrains(CoRouting tmpcoRouting, PceConstraints constraints) {
218 LOG.info("In readCoRoutingContrains start");
220 if (tmpcoRouting == null) {
221 LOG.info("In readCoRoutingContrains: no General constraints.");
227 public PceConstraints getPceHardConstraints() {
228 return this.pceHardConstraints;
231 public PceConstraints getPceSoftConstraints() {
232 return this.pceSoftConstraints;
235 public PceMetric getPceMetrics() {
236 return this.pceMetrics;