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