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