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