6dc9c089ea796206080050c38978555a9321b0ab
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / constraints / PceConstraintsCalc.java
1 /*
2  * Copyright © 2016 AT&T 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.constraints;
9
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;
44
45 public class PceConstraintsCalc {
46     /* Logging. */
47     private static final Logger LOG = LoggerFactory.getLogger(PceConstraintsCalc.class);
48
49     private PceConstraints pceHardConstraints = new PceConstraints();
50     private PceConstraints pceSoftConstraints = new PceConstraints();
51     private PceMetric pceMetrics = PceMetric.HopCount;
52     private NetworkTransactionService networkTransactionService;
53
54     public PceConstraintsCalc(PathComputationRequestInput input, NetworkTransactionService networkTransactionService) {
55         LOG.debug("In PceconstraintsCalc start");
56
57         pceMetrics = input.getPceMetric();
58
59         this.networkTransactionService = networkTransactionService;
60
61         // TODO. for now metrics are set into hard structure
62         LOG.info("In PceConstraintsCalc: read PceMetric {}", pceMetrics);
63         pceHardConstraints.setPceMetrics(pceMetrics);
64
65         calcHardconstraints(input);
66         calcSoftconstraints(input);
67     }
68
69     private void calcHardconstraints(PathComputationRequestInput input) {
70         HardConstraints servicePathHardConstraints = input.getHardConstraints();
71         if (servicePathHardConstraints == null) {
72             LOG.info("In calcHardconstraints: no hard constraints.");
73             return;
74         }
75
76         CoRoutingOrGeneral coRoutingOrGeneral = servicePathHardConstraints.getCoRoutingOrGeneral();
77         readconstraints(coRoutingOrGeneral, pceHardConstraints);
78
79     }
80
81     private void calcSoftconstraints(PathComputationRequestInput input) {
82         SoftConstraints servicePathSoftConstraints = input.getSoftConstraints();
83         if (servicePathSoftConstraints == null) {
84             LOG.info("In calcSoftconstraints: no soft constraints.");
85             return;
86         }
87
88         CoRoutingOrGeneral coRoutingOrGeneral = servicePathSoftConstraints.getCoRoutingOrGeneral();
89         readconstraints(coRoutingOrGeneral, pceSoftConstraints);
90
91     }
92
93     private void readconstraints(CoRoutingOrGeneral coRoutingOrGeneral, PceConstraints constraints) {
94         LOG.debug("In readconstraints start");
95
96         if (coRoutingOrGeneral == null) {
97             LOG.info("In readHardconstraints: no CoRoutingOrGeneral constraints.");
98             return;
99         }
100
101         General tmpGeneral = null;
102         CoRouting tmpCoRouting = null;
103
104         if (coRoutingOrGeneral instanceof General) {
105             LOG.info("In readconstraints General {}", coRoutingOrGeneral);
106             tmpGeneral = (General) coRoutingOrGeneral;
107             readGeneralContrains(tmpGeneral, constraints);
108             return;
109         }
110
111         if (coRoutingOrGeneral instanceof CoRouting) {
112             LOG.info("In readconstraints CoRouting {}", coRoutingOrGeneral);
113             tmpCoRouting = (CoRouting) coRoutingOrGeneral;
114             readCoRoutingContrains(tmpCoRouting, constraints);
115         }
116     }
117
118     private void readGeneralContrains(General tmpGeneral, PceConstraints constraints) {
119         LOG.debug("In readGeneralContrains start");
120
121         if (tmpGeneral == null) {
122             LOG.info("In readGeneralContrains: no General constraints.");
123             return;
124         }
125
126         Latency latency = tmpGeneral.getLatency();
127         if (latency != null) {
128             constraints.setMaxLatency(latency.getMaxLatency().toJava());
129             LOG.info("In readGeneralContrains: read latency {}", latency);
130         }
131
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);
138             }
139
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));
145                 }
146                 constraints.setExcludeSRLG(srlgToExclude);
147             }
148
149             elementsToExclude = exclude.getClli();
150             if (elementsToExclude != null) {
151                 constraints.setExcludeCLLI(elementsToExclude);
152             }
153         }
154
155         Include include = tmpGeneral.getInclude();
156         if (include != null) {
157             List<OrderedHops> listHops = include.getOrderedHops();
158             if (listHops != null) {
159                 readIncludeNodes(listHops, constraints);
160             }
161             LOG.debug("in readGeneralContrains INCLUDE {} ", include);
162         }
163
164         Diversity diversity = tmpGeneral.getDiversity();
165         PceConstraints.ResourceType rt = PceConstraints.ResourceType.NONE;
166         if (diversity != null) {
167             ExistingServiceApplicability temp = diversity.getExistingServiceApplicability();
168             if (temp == null) {
169                 return;
170             }
171             if (Boolean.TRUE.equals(temp.isNode())) {
172                 rt = PceConstraints.ResourceType.NODE;
173             }
174             if (Boolean.TRUE.equals(temp.isSrlg())) {
175                 rt = PceConstraints.ResourceType.SRLG;
176             }
177             if (Boolean.TRUE.equals(temp.isClli())) {
178                 rt = PceConstraints.ResourceType.CLLI;
179             }
180             LOG.info("in readGeneralContrains {} list is :{}", rt, diversity);
181             readDiversity(diversity.getExistingService(), constraints, rt);
182         }
183
184     }
185
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();
189
190             String hopt = hoptype.implementedInterface().getSimpleName();
191             LOG.info("in readIncludeNodes next hop to include {}", hopt);
192             switch (hopt) {
193                 case "Node":
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,
199                             node.getNodeId()));
200                     break;
201                 case "SRLG":
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,
207                             srlg.getSRLG()));
208                     break;
209                 case "Clli":
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,
215                             clli.getClli()));
216                     break;
217                 default:
218                     LOG.error("in readIncludeNodes unsupported include type {}", hopt);
219             }
220         }
221     }
222
223     private void readDiversity(List<String> srvList, PceConstraints constraints, PceConstraints.ResourceType rt) {
224
225         List<String> elementsToExclude = new ArrayList<>();
226         LOG.info("in readDiversity {}", srvList);
227
228         for (String srv : srvList) {
229             Optional<PathDescription> service = getPathDescriptionFromDatastore(srv);
230             if (service.isPresent()) {
231                 LOG.info("in readDiversity service list {}", service);
232                 switch (rt) {
233                     case NODE:
234                         elementsToExclude.addAll(getAToZNodeList(service.get()));
235                         LOG.info("readDiversity NODE : {}", elementsToExclude);
236                         if (elementsToExclude != null) {
237                             constraints.setExcludeNodes(elementsToExclude);
238                         }
239                         break;
240                     case SRLG:
241                         elementsToExclude.addAll(getSRLGList(service.get()));
242                         LOG.info("readDiversity SRLG : {}", elementsToExclude);
243                         if (elementsToExclude != null) {
244                             constraints.setExcludeSrlgLinks(elementsToExclude);
245                         }
246                         break;
247                     case CLLI:
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);
254                         }
255                         break;
256                     default:
257                         LOG.info("in readDiversity unsupported divercity type {}", rt);
258                 }
259
260             } else {
261                 LOG.info("in readDiversity srv={} is not present", srv);
262             }
263         }
264
265     }
266
267     private List<String> getAToZNodeList(PathDescription pathDescription) {
268         List<AToZ> aendToZList = pathDescription.getAToZDirection().getAToZ();
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());
272                 return false;
273             }
274             return aToZ.getResource().getResource() instanceof Node;
275         }).filter(aToZ -> {
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());
279                 return false;
280             }
281             return true;
282         }).map(aToZ -> {
283             Node node = ((Node) aToZ.getResource().getResource());
284             return node.getNodeId();
285         }).collect(Collectors.toList());
286     }
287
288     private List<String> getSRLGList(PathDescription pathDescription) {
289         List<AToZ> aendToZList = pathDescription.getAToZDirection().getAToZ();
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());
294                 return false;
295             }
296             return aToZ.getResource().getResource() instanceof Link;
297         }).filter(aToZ -> {
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());
301                 return false;
302             }
303             return true;
304         }).map(aToZ -> {
305             return ((Link) aToZ.getResource().getResource()).getLinkId();
306         }).collect(Collectors.toList());
307     }
308
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));
313         try {
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();
320                 if (path != null) {
321                     result = Optional.of(path);
322                 }
323             }
324         } catch (InterruptedException | ExecutionException | TimeoutException e) {
325             LOG.warn(
326                 "PCE diversity constraints: Exception while getting path description from datastore {} for service {}!",
327                 pathDescriptionIID,serviceName, e);
328             return result;
329         }
330         return result;
331     }
332
333     private void readCoRoutingContrains(CoRouting tmpcoRouting, PceConstraints constraints) {
334         LOG.info("In readCoRoutingContrains start");
335
336         if (tmpcoRouting == null) {
337             LOG.info("In readCoRoutingContrains: no General constraints.");
338         }
339
340     }
341
342     public PceConstraints getPceHardConstraints() {
343         return pceHardConstraints;
344     }
345
346     public PceConstraints getPceSoftConstraints() {
347         return pceSoftConstraints;
348     }
349
350     public PceMetric getPceMetrics() {
351         return pceMetrics;
352     }
353
354 }