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.Optional;
14 import java.util.concurrent.ExecutionException;
15 import java.util.concurrent.TimeUnit;
16 import java.util.concurrent.TimeoutException;
17 import java.util.stream.Collectors;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.transportpce.common.Timeouts;
22 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev171017.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.Node;
26 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.RoutingConstraintsSp.PceMetric;
27 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.constraints.sp.CoRoutingOrGeneral;
28 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.constraints.sp.co.routing.or.general.CoRouting;
29 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.constraints.sp.co.routing.or.general.General;
30 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.constraints.sp.co.routing.or.general.general.Diversity;
31 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.constraints.sp.co.routing.or.general.general.Exclude;
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.routing.constraints.sp.HardConstraints;
34 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.routing.constraints.sp.SoftConstraints;
35 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.ServicePathList;
36 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.service.path.list.ServicePaths;
37 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.service.path.list.ServicePathsKey;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 public class PceConstraintsCalc {
44 private static final Logger LOG = LoggerFactory.getLogger(PceCalculation.class);
46 private PceConstraints pceHardConstraints = new PceConstraints();
47 private PceConstraints pceSoftConstraints = new PceConstraints();
48 private PceMetric pceMetrics = PceMetric.HopCount;
49 private DataBroker dataBroker;
52 public PceConstraintsCalc(PathComputationRequestInput input, DataBroker dataBroker) {
53 LOG.debug("In PceconstraintsCalc start");
55 this.pceMetrics = input.getPceMetric();
57 this.dataBroker = dataBroker;
59 // TODO. for now metrics are set into hard structure
60 LOG.info("In PceConstraintsCalc: read PceMetric {}", this.pceMetrics.toString());
61 this.pceHardConstraints.setPceMetrics(this.pceMetrics);
63 calcHardconstraints(input);
64 calcSoftconstraints(input);
67 private void calcHardconstraints(PathComputationRequestInput input) {
68 HardConstraints servicePathHardConstraints = input.getHardConstraints();
69 if (servicePathHardConstraints == null) {
70 LOG.info("In calcHardconstraints: no hard constraints.");
74 CoRoutingOrGeneral coRoutingOrGeneral = servicePathHardConstraints.getCoRoutingOrGeneral();
75 readconstraints(coRoutingOrGeneral, this.pceHardConstraints);
79 private void calcSoftconstraints(PathComputationRequestInput input) {
80 SoftConstraints servicePathSoftConstraints = input.getSoftConstraints();
81 if (servicePathSoftConstraints == null) {
82 LOG.info("In calcSoftconstraints: no soft constraints.");
86 CoRoutingOrGeneral coRoutingOrGeneral = servicePathSoftConstraints.getCoRoutingOrGeneral();
87 readconstraints(coRoutingOrGeneral, this.pceSoftConstraints);
91 private void readconstraints(CoRoutingOrGeneral coRoutingOrGeneral, PceConstraints constraints) {
92 LOG.debug("In readconstraints start");
94 if (coRoutingOrGeneral == null) {
95 LOG.info("In readHardconstraints: no CoRoutingOrGeneral constraints.");
99 General tmpGeneral = null;
100 CoRouting tmpCoRouting = null;
102 if (coRoutingOrGeneral instanceof General) {
103 LOG.info("In readconstraints General {}", coRoutingOrGeneral.toString());
104 tmpGeneral = (General) coRoutingOrGeneral;
105 readGeneralContrains(tmpGeneral, constraints);
109 if (coRoutingOrGeneral instanceof CoRouting) {
110 LOG.info("In readconstraints CoRouting {}", coRoutingOrGeneral.toString());
111 tmpCoRouting = (CoRouting) coRoutingOrGeneral;
112 readCoRoutingContrains(tmpCoRouting, constraints);
120 private void readGeneralContrains(General tmpGeneral, PceConstraints constraints) {
121 LOG.debug("In readGeneralContrains start");
123 if (tmpGeneral == null) {
124 LOG.info("In readGeneralContrains: no General constraints.");
128 Latency latency = tmpGeneral.getLatency();
129 if (latency != null) {
130 constraints.setMaxLatency(latency.getMaxLatency());
131 LOG.info("In readGeneralContrains: read latency {}", latency.toString());
134 Exclude exclude = tmpGeneral.getExclude();
135 List<String> elementsToExclude = null;
136 if (exclude != null) {
137 elementsToExclude = exclude.getNodeId();
138 if (elementsToExclude != null) {
139 constraints.setExcludeNodes(elementsToExclude);
141 elementsToExclude = exclude.getSRLG();
142 if (elementsToExclude != null) {
143 constraints.setExcludeSRLG(elementsToExclude);
147 Diversity diversity = tmpGeneral.getDiversity();
148 if (diversity != null) {
149 if (diversity.getExistingServiceApplicability().isNode()) {
150 LOG.info("in readGeneralContrains {}", diversity.toString());
151 readDiversityNodes(diversity.getExistingService(), constraints);
157 private void readDiversityNodes(List<String> srvList, PceConstraints constraints) {
159 List<String> elementsToExclude = new ArrayList<String>();
160 LOG.info("in readDiversityNodes {}", srvList.toString());
162 for (String srv : srvList) {
163 Optional<PathDescription> service = getPathDescriptionFromDatastore(srv);
164 if (service.isPresent()) {
165 elementsToExclude.addAll(getAToZNodeList(service.get()));
166 LOG.info("readDiversityNodes : {}", elementsToExclude);
169 LOG.info("in readDiversityNodes srv={} is not present", srv);
173 if (elementsToExclude != null) {
174 constraints.setExcludeNodes(elementsToExclude);
178 private List<String> getAToZNodeList(PathDescription pathDescription) {
179 List<AToZ> atozList = pathDescription.getAToZDirection().getAToZ();
180 return atozList.stream().filter(aToZ -> {
181 if ((aToZ.getResource() == null) || (aToZ.getResource().getResource() == null)) {
182 LOG.warn("Diversity constraint: Resource of AToZ node {} is null! Skipping this node!", aToZ.getId());
185 return aToZ.getResource().getResource() instanceof Node;
187 Node node = (Node) aToZ.getResource().getResource();
188 if (node.getNodeId() == null) {
189 LOG.warn("Node in AToZ node {} contains null! Skipping this node!", aToZ.getId());
192 return node.getNodeId().toString();
193 }).collect(Collectors.toList());
196 private Optional<PathDescription> getPathDescriptionFromDatastore(String serviceName) {
197 Optional<PathDescription> result = Optional.absent();
198 InstanceIdentifier<ServicePaths> pathDescriptionIID = InstanceIdentifier.create(ServicePathList.class)
199 .child(ServicePaths.class, new ServicePathsKey(serviceName));
200 ReadOnlyTransaction pathDescReadTx = this.dataBroker.newReadOnlyTransaction();
202 LOG.info("PCE diversity constraints: Getting path description for service {}", serviceName);
203 ServicePaths servicePaths = pathDescReadTx.read(LogicalDatastoreType.CONFIGURATION, pathDescriptionIID)
204 .get(Timeouts.DATASTORE_READ, TimeUnit.MILLISECONDS).get();
205 if (servicePaths != null) {
206 PathDescription path = servicePaths.getPathDescription();
208 result = Optional.of(path);
211 // return pathDescReadTx.read(LogicalDatastoreType.CONFIGURATION, pathDescriptionIID)
212 // .get(Timeouts.DATASTORE_READ, TimeUnit.MILLISECONDS);
213 } catch (InterruptedException | ExecutionException | TimeoutException e) {
215 "PCE diversity constraints: Exception while getting path description from datastore {} for service {}!",
216 pathDescriptionIID,serviceName, e);
222 private void readCoRoutingContrains(CoRouting tmpcoRouting, PceConstraints constraints) {
223 LOG.info("In readCoRoutingContrains start");
225 if (tmpcoRouting == null) {
226 LOG.info("In readCoRoutingContrains: no General constraints.");
232 public PceConstraints getPceHardConstraints() {
233 return this.pceHardConstraints;
236 public PceConstraints getPceSoftConstraints() {
237 return this.pceSoftConstraints;
240 public PceMetric getPceMetrics() {
241 return this.pceMetrics;