ae6b8d5611bd59edf9ebe0bd46741260dd915750
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / PceConstraintsCalc.java
1 /*
2  * Copyright © 2017 AT&T, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.transportpce.pce;
9
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;
40
41 public class PceConstraintsCalc {
42     /* Logging. */
43     private static final Logger LOG = LoggerFactory.getLogger(PceCalculation.class);
44
45     private PceConstraints pceHardConstraints = new PceConstraints();
46     private PceConstraints pceSoftConstraints = new PceConstraints();
47     private PceMetric pceMetrics = PceMetric.HopCount;
48     private DataBroker dataBroker;
49
50
51     public PceConstraintsCalc(PathComputationRequestInput input, DataBroker dataBroker) {
52         LOG.debug("In PceconstraintsCalc start");
53
54         this.pceMetrics = input.getPceMetric();
55
56         this.dataBroker = dataBroker;
57
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);
61
62         calcHardconstraints(input);
63         calcSoftconstraints(input);
64     }
65
66     private void calcHardconstraints(PathComputationRequestInput input) {
67         HardConstraints servicePathHardConstraints = input.getHardConstraints();
68         if (servicePathHardConstraints == null) {
69             LOG.info("In calcHardconstraints: no hard constraints.");
70             return;
71         }
72
73         CoRoutingOrGeneral coRoutingOrGeneral = servicePathHardConstraints.getCoRoutingOrGeneral();
74         readconstraints(coRoutingOrGeneral, this.pceHardConstraints);
75
76     }
77
78     private void calcSoftconstraints(PathComputationRequestInput input) {
79         SoftConstraints servicePathSoftConstraints = input.getSoftConstraints();
80         if (servicePathSoftConstraints == null) {
81             LOG.info("In calcSoftconstraints: no soft constraints.");
82             return;
83         }
84
85         CoRoutingOrGeneral coRoutingOrGeneral = servicePathSoftConstraints.getCoRoutingOrGeneral();
86         readconstraints(coRoutingOrGeneral, this.pceSoftConstraints);
87
88     }
89
90     private void readconstraints(CoRoutingOrGeneral coRoutingOrGeneral, PceConstraints constraints) {
91         LOG.debug("In readconstraints start");
92
93         if (coRoutingOrGeneral == null) {
94             LOG.info("In readHardconstraints: no CoRoutingOrGeneral constraints.");
95             return;
96         }
97
98         General tmpGeneral = null;
99         CoRouting tmpCoRouting = null;
100
101         if (coRoutingOrGeneral instanceof General) {
102             LOG.info("In readconstraints General {}", coRoutingOrGeneral.toString());
103             tmpGeneral = (General) coRoutingOrGeneral;
104             readGeneralContrains(tmpGeneral, constraints);
105             return;
106         }
107
108         if (coRoutingOrGeneral instanceof CoRouting) {
109             LOG.info("In readconstraints CoRouting {}", coRoutingOrGeneral.toString());
110             tmpCoRouting = (CoRouting) coRoutingOrGeneral;
111             readCoRoutingContrains(tmpCoRouting, constraints);
112             return;
113         }
114
115         return;
116
117     }
118
119     private void readGeneralContrains(General tmpGeneral, PceConstraints constraints) {
120         LOG.debug("In readGeneralContrains start");
121
122         if (tmpGeneral  ==  null) {
123             LOG.info("In readGeneralContrains: no General constraints.");
124             return;
125         }
126
127         Latency latency = tmpGeneral.getLatency();
128         if (latency != null) {
129             constraints.setMaxLatency(latency.getMaxLatency());
130             LOG.info("In readGeneralContrains: read latency {}", latency.toString());
131         }
132
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);
139             }
140             elementsToExclude = exclude.getSRLG();
141             if (elementsToExclude != null) {
142                 constraints.setExcludeSRLG(elementsToExclude);
143             }
144         }
145
146         Diversity diversity = tmpGeneral.getDiversity();
147         if ((diversity != null) && (diversity.getExistingServiceApplicability().isNode())) {
148             LOG.info("in readGeneralContrains {}", diversity.toString());
149             readDiversityNodes(diversity.getExistingService(), constraints);
150         }
151
152     }
153
154     private void readDiversityNodes(List<String> srvList, PceConstraints constraints) {
155
156         List<String> elementsToExclude = new ArrayList<String>();
157         LOG.info("in readDiversityNodes {}", srvList.toString());
158
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);
164
165             } else {
166                 LOG.info("in readDiversityNodes srv={} is not present", srv);
167             }
168         }
169
170         if (elementsToExclude != null) {
171             constraints.setExcludeNodes(elementsToExclude);
172         }
173     }
174
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());
180                 return false;
181             }
182             return aToZ.getResource().getResource() instanceof Node;
183         }).map(aToZ -> {
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());
187                 return null;
188             }
189             return node.getNodeId().toString();
190         }).collect(Collectors.toList());
191     }
192
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();
198         try {
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();
204                 if (path != null) {
205                     result = Optional.of(path);
206                 }
207             }
208         } catch (InterruptedException | ExecutionException | TimeoutException e) {
209             LOG.warn(
210                 "PCE diversity constraints: Exception while getting path description from datastore {} for service {}!",
211                 pathDescriptionIID,serviceName, e);
212             return result;
213         }
214         return result;
215     }
216
217     private void readCoRoutingContrains(CoRouting tmpcoRouting, PceConstraints constraints) {
218         LOG.info("In readCoRoutingContrains start");
219
220         if (tmpcoRouting  ==  null) {
221             LOG.info("In readCoRoutingContrains: no General constraints.");
222             return;
223         }
224
225     }
226
227     public PceConstraints getPceHardConstraints() {
228         return this.pceHardConstraints;
229     }
230
231     public PceConstraints getPceSoftConstraints() {
232         return this.pceSoftConstraints;
233     }
234
235     public PceMetric getPceMetrics() {
236         return this.pceMetrics;
237     }
238
239
240 }