8a7a8c6ec430bbda7eeff96066f7aabeb3f7da5c
[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.Collections;
12 import java.util.Comparator;
13 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.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220118.PathComputationRequestInput;
23 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.PathDescription;
24 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.path.description.atoz.direction.AToZ;
25 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.pce.resource.resource.resource.Link;
26 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.pce.resource.resource.resource.Node;
27 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118.constraints.sp.CoRoutingOrGeneral;
28 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118.constraints.sp.co.routing.or.general.CoRouting;
29 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118.constraints.sp.co.routing.or.general.General;
30 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118.constraints.sp.co.routing.or.general.general.Diversity;
31 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118.constraints.sp.co.routing.or.general.general.Exclude;
32 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118.constraints.sp.co.routing.or.general.general.Include;
33 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118.constraints.sp.co.routing.or.general.general.Latency;
34 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118.constraints.sp.co.routing.or.general.general.include_.OrderedHops;
35 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118.diversity.existing.service.contraints.sp.ExistingServiceApplicability;
36 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118.ordered.constraints.sp.hop.type.HopType;
37 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118.ordered.constraints.sp.hop.type.hop.type.Clli;
38 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118.ordered.constraints.sp.hop.type.hop.type.SRLG;
39 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118.routing.constraints.sp.HardConstraints;
40 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118.routing.constraints.sp.SoftConstraints;
41 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev220118.PceMetric;
42 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.ServicePathList;
43 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.service.path.list.ServicePaths;
44 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.service.path.list.ServicePathsKey;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 public class PceConstraintsCalc {
50     /* Logging. */
51     private static final Logger LOG = LoggerFactory.getLogger(PceConstraintsCalc.class);
52     private static final Comparator<OrderedHops> ORDERED_HOP_COMPARATOR =
53             Comparator.comparing(OrderedHops::getHopNumber);
54
55     private PceConstraints pceHardConstraints = new PceConstraints();
56     private PceConstraints pceSoftConstraints = new PceConstraints();
57     private PceMetric pceMetrics = PceMetric.HopCount;
58     private NetworkTransactionService networkTransactionService;
59
60     public PceConstraintsCalc(PathComputationRequestInput input, NetworkTransactionService networkTransactionService) {
61         LOG.debug("In PceconstraintsCalc start");
62
63         pceMetrics = input.getPceRoutingMetric();
64
65         this.networkTransactionService = networkTransactionService;
66
67         // TODO. for now metrics are set into hard structure
68         LOG.info("In PceConstraintsCalc: read PceMetric {}", pceMetrics);
69         pceHardConstraints.setPceMetrics(pceMetrics);
70
71         calcHardconstraints(input);
72         calcSoftconstraints(input);
73     }
74
75     private void calcHardconstraints(PathComputationRequestInput input) {
76         HardConstraints servicePathHardConstraints = input.getHardConstraints();
77         if (servicePathHardConstraints == null) {
78             LOG.info("In calcHardconstraints: no hard constraints.");
79             return;
80         }
81
82         CoRoutingOrGeneral coRoutingOrGeneral = servicePathHardConstraints.getCoRoutingOrGeneral();
83         readconstraints(coRoutingOrGeneral, pceHardConstraints);
84
85     }
86
87     private void calcSoftconstraints(PathComputationRequestInput input) {
88         SoftConstraints servicePathSoftConstraints = input.getSoftConstraints();
89         if (servicePathSoftConstraints == null) {
90             LOG.info("In calcSoftconstraints: no soft constraints.");
91             return;
92         }
93
94         CoRoutingOrGeneral coRoutingOrGeneral = servicePathSoftConstraints.getCoRoutingOrGeneral();
95         readconstraints(coRoutingOrGeneral, pceSoftConstraints);
96
97     }
98
99     private void readconstraints(CoRoutingOrGeneral coRoutingOrGeneral, PceConstraints constraints) {
100         LOG.debug("In readconstraints start");
101
102         if (coRoutingOrGeneral == null) {
103             LOG.info("In readHardconstraints: no CoRoutingOrGeneral constraints.");
104             return;
105         }
106
107         General tmpGeneral = null;
108         CoRouting tmpCoRouting = null;
109
110         if (coRoutingOrGeneral instanceof General) {
111             LOG.info("In readconstraints General {}", coRoutingOrGeneral);
112             tmpGeneral = (General) coRoutingOrGeneral;
113             readGeneralContrains(tmpGeneral, constraints);
114             return;
115         }
116
117         if (coRoutingOrGeneral instanceof CoRouting) {
118             LOG.info("In readconstraints CoRouting {}", coRoutingOrGeneral);
119             tmpCoRouting = (CoRouting) coRoutingOrGeneral;
120             readCoRoutingContrains(tmpCoRouting, constraints);
121         }
122     }
123
124     private void readGeneralContrains(General tmpGeneral, PceConstraints constraints) {
125         LOG.debug("In readGeneralContrains start");
126
127         if (tmpGeneral == null) {
128             LOG.info("In readGeneralContrains: no General constraints.");
129             return;
130         }
131
132         Latency latency = tmpGeneral.getLatency();
133         if (latency != null) {
134             constraints.setMaxLatency(latency.getMaxLatency().toJava());
135             LOG.info("In readGeneralContrains: read latency {}", latency);
136         }
137
138         Exclude exclude = tmpGeneral.getExclude();
139         List<String> elementsToExclude = null;
140         if (exclude != null) {
141             elementsToExclude = exclude.getNodeId();
142             if (elementsToExclude != null) {
143                 constraints.setExcludeSupNodes(elementsToExclude);
144             }
145
146             elementsToExclude = exclude.getSRLG();
147             if (elementsToExclude != null) {
148                 List<Long> srlgToExclude = new ArrayList<>();
149                 for (String str : elementsToExclude) {
150                     srlgToExclude.add(Long.parseLong(str));
151                 }
152                 constraints.setExcludeSRLG(srlgToExclude);
153             }
154
155             elementsToExclude = exclude.getClli();
156             if (elementsToExclude != null) {
157                 constraints.setExcludeCLLI(elementsToExclude);
158             }
159         }
160
161         Include include = tmpGeneral.getInclude();
162         if (include != null) {
163             List<OrderedHops> listHops = new ArrayList<>(include.nonnullOrderedHops().values());
164             if (listHops != null) {
165                 readIncludeNodes(listHops, constraints);
166             }
167             LOG.debug("in readGeneralContrains INCLUDE {} ", include);
168         }
169
170         Diversity diversity = tmpGeneral.getDiversity();
171         PceConstraints.ResourceType rt = PceConstraints.ResourceType.NONE;
172         if (diversity != null) {
173             ExistingServiceApplicability temp = diversity.getExistingServiceApplicability();
174             if (temp == null) {
175                 return;
176             }
177             if (Boolean.TRUE.equals(temp.getNode())) {
178                 rt = PceConstraints.ResourceType.NODE;
179             }
180             if (Boolean.TRUE.equals(temp.getSrlg())) {
181                 rt = PceConstraints.ResourceType.SRLG;
182             }
183             if (Boolean.TRUE.equals(temp.getClli())) {
184                 rt = PceConstraints.ResourceType.CLLI;
185             }
186             LOG.info("in readGeneralContrains {} list is :{}", rt, diversity);
187             readDiversity(diversity.getExistingService(), constraints, rt);
188         }
189
190     }
191
192     private void readIncludeNodes(List<OrderedHops> listHops, PceConstraints constraints) {
193         Collections.sort(listHops, ORDERED_HOP_COMPARATOR);
194         for (int i = 0; i < listHops.size(); i++) {
195             HopType hoptype = listHops.get(i).getHopType().getHopType();
196
197             String hopt = hoptype.implementedInterface().getSimpleName();
198             LOG.info("in readIncludeNodes next hop to include {}", hopt);
199             switch (hopt) {
200                 case "Node":
201                     org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118
202                         .ordered.constraints.sp.hop.type.hop.type.Node node =
203                         (org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118
204                             .ordered.constraints.sp.hop.type.hop.type.Node) hoptype;
205                     constraints.setListToInclude(
206                         new PceConstraints.ResourcePair(PceConstraints.ResourceType.NODE, node.getNodeId()));
207                     break;
208                 case "SRLG":
209                     SRLG srlg = (SRLG) hoptype;
210                     constraints.setListToInclude(
211                         new PceConstraints.ResourcePair(PceConstraints.ResourceType.SRLG, srlg.getSRLG()));
212                     break;
213                 case "Clli":
214                     Clli clli = (Clli) hoptype;
215                     constraints.setListToInclude(
216                         new PceConstraints.ResourcePair(PceConstraints.ResourceType.CLLI, clli.getClli()));
217                     break;
218                 default:
219                     LOG.error("in readIncludeNodes unsupported include type {}", hopt);
220             }
221         }
222     }
223
224     private void readDiversity(List<String> srvList, PceConstraints constraints, PceConstraints.ResourceType rt) {
225
226         List<String> elementsToExclude = new ArrayList<>();
227         LOG.info("in readDiversity {}", srvList);
228
229         for (String srv : srvList) {
230             Optional<PathDescription> service = getPathDescriptionFromDatastore(srv);
231             if (service.isPresent()) {
232                 LOG.info("in readDiversity service list {}", service);
233                 switch (rt) {
234                     case NODE:
235                         elementsToExclude.addAll(getAToZNodeList(service.get()));
236                         LOG.info("readDiversity NODE : {}", elementsToExclude);
237                         if (elementsToExclude != null) {
238                             constraints.setExcludeNodes(elementsToExclude);
239                         }
240                         break;
241                     case SRLG:
242                         elementsToExclude.addAll(getSRLGList(service.get()));
243                         LOG.info("readDiversity SRLG : {}", elementsToExclude);
244                         if (elementsToExclude != null) {
245                             constraints.setExcludeSrlgLinks(elementsToExclude);
246                         }
247                         break;
248                     case CLLI:
249                         /// Retrieve nodes into dedicated CLLI list
250                         /// during node validation check their CLLI and build CLLI exclude list
251                         elementsToExclude.addAll(getAToZNodeList(service.get()));
252                         LOG.info("readDiversity CLLI : {}", elementsToExclude);
253                         if (elementsToExclude != null) {
254                             constraints.setExcludeClliNodes(elementsToExclude);
255                         }
256                         break;
257                     default:
258                         LOG.info("in readDiversity unsupported divercity type {}", rt);
259                 }
260
261             } else {
262                 LOG.info("in readDiversity srv={} is not present", srv);
263             }
264         }
265
266     }
267
268     private List<String> getAToZNodeList(PathDescription pathDescription) {
269         List<AToZ> aendToZList = new ArrayList<>(pathDescription.getAToZDirection().nonnullAToZ().values());
270         return aendToZList.stream().filter(aToZ -> {
271             if (aToZ.getResource() == null || aToZ.getResource().getResource() == null) {
272                 LOG.warn("Diversity constraint: Resource of AToZ node {} is null! Skipping this node!", aToZ.getId());
273                 return false;
274             }
275             return aToZ.getResource().getResource() instanceof Node;
276         }).filter(aToZ -> {
277             Node node = (Node) aToZ.getResource().getResource();
278             if (node.getNodeId() == null) {
279                 LOG.warn("Node in AToZ node {} contains null! Skipping this node!", aToZ.getId());
280                 return false;
281             }
282             return true;
283         }).map(aToZ -> {
284             Node node = ((Node) aToZ.getResource().getResource());
285             return node.getNodeId();
286         }).collect(Collectors.toList());
287     }
288
289     private List<String> getSRLGList(PathDescription pathDescription) {
290         List<AToZ> aendToZList = new ArrayList<>(pathDescription.getAToZDirection().nonnullAToZ().values());
291         return aendToZList.stream().filter(aToZ -> {
292             if (aToZ.getResource() == null
293                     || aToZ.getResource().getResource() == null) {
294                 LOG.warn("Diversity constraint: Resource of AToZ {} is null! Skipping this resource!", aToZ.getId());
295                 return false;
296             }
297             return aToZ.getResource().getResource() instanceof Link;
298         }).filter(aToZ -> {
299             Link link = (Link) aToZ.getResource().getResource();
300             if (link.getLinkId() == null) {
301                 LOG.warn("Link in AToZ link {} contains null! Skipping this link!", aToZ.getId());
302                 return false;
303             }
304             return true;
305         }).map(aToZ -> {
306             return ((Link) aToZ.getResource().getResource()).getLinkId();
307         }).collect(Collectors.toList());
308     }
309
310     private Optional<PathDescription> getPathDescriptionFromDatastore(String serviceName) {
311         Optional<PathDescription> result = Optional.empty();
312         InstanceIdentifier<ServicePaths> pathDescriptionIID = InstanceIdentifier.create(ServicePathList.class)
313                 .child(ServicePaths.class, new ServicePathsKey(serviceName));
314         try {
315             LOG.info("PCE diversity constraints: Getting path description for service {}", serviceName);
316             ServicePaths servicePaths =
317                 networkTransactionService.read(LogicalDatastoreType.CONFIGURATION, pathDescriptionIID)
318                     .get(Timeouts.DATASTORE_READ, TimeUnit.MILLISECONDS).get();
319             if (servicePaths != null) {
320                 PathDescription path = servicePaths.getPathDescription();
321                 if (path != null) {
322                     result = Optional.of(path);
323                 }
324             }
325         } catch (InterruptedException | ExecutionException | TimeoutException e) {
326             LOG.warn(
327                 "PCE diversity constraints: Exception while getting path description from datastore {} for service {}!",
328                 pathDescriptionIID,serviceName, e);
329             return result;
330         }
331         return result;
332     }
333
334     private void readCoRoutingContrains(CoRouting tmpcoRouting, PceConstraints constraints) {
335         LOG.info("In readCoRoutingContrains start");
336
337         if (tmpcoRouting == null) {
338             LOG.info("In readCoRoutingContrains: no General constraints.");
339         }
340
341     }
342
343     public PceConstraints getPceHardConstraints() {
344         return pceHardConstraints;
345     }
346
347     public PceConstraints getPceSoftConstraints() {
348         return pceSoftConstraints;
349     }
350
351     public PceMetric getPceMetrics() {
352         return pceMetrics;
353     }
354
355 }