Add service-resiliency handling in service-create
[transportpce.git] / servicehandler / src / main / java / org / opendaylight / transportpce / servicehandler / DowngradeConstraints.java
1 /*
2  * Copyright © 2017 Orange, Inc. 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.servicehandler;
9
10 import java.util.HashMap;
11 import java.util.HashSet;
12 import java.util.Map;
13 import java.util.Set;
14 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.node.types.rev210528.NodeIdType;
15 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.common.constraints.LinkIdentifier;
16 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.common.constraints.LinkIdentifierKey;
17 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.CoRouting;
18 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.CoRoutingBuilder;
19 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.Distance;
20 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.DistanceBuilder;
21 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.Diversity;
22 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.DiversityBuilder;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.Exclude;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.ExcludeBuilder;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.HopCount;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.HopCountBuilder;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.Include;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.IncludeBuilder;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.Latency;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.LatencyBuilder;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.TEMetric;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.TEMetricBuilder;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.co.routing.ServiceIdentifierList;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.co.routing.ServiceIdentifierListKey;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.routing.constraints.HardConstraints;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.routing.constraints.HardConstraintsBuilder;
37 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.routing.constraints.SoftConstraints;
38 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.routing.constraints.SoftConstraintsBuilder;
39 import org.opendaylight.yangtools.yang.common.Uint32;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 /**
44  * Class to Map Hard Constraints to Soft Constraints.
45  * @author Martial Coulibaly ( martial.coulibaly@gfi.com ) on behalf of Orange
46  * @author gilles Thouenon (gilles.thouenon@orange.com)
47  */
48 public final class DowngradeConstraints {
49
50     private static final Logger LOG = LoggerFactory.getLogger(DowngradeConstraints.class);
51
52     private DowngradeConstraints() {
53     }
54
55     /**
56      * Add Hard Constraints to Soft Constraints.
57      * @param  hardConstraints to be added
58      * @param  softConstraints to be modified
59      * @return                 SoftConstraints modified
60      */
61     public static SoftConstraints updateSoftConstraints(HardConstraints hardConstraints,
62                                                         SoftConstraints softConstraints) {
63         SoftConstraintsBuilder softConstraintsBuilder = new SoftConstraintsBuilder(softConstraints);
64         if (hardConstraints.getCustomerCode() != null && !hardConstraints.getCustomerCode().isEmpty()) {
65             if (softConstraintsBuilder.getCustomerCode() == null
66                     || softConstraintsBuilder.getCustomerCode().isEmpty()) {
67                 softConstraintsBuilder.setCustomerCode(hardConstraints.getCustomerCode());
68             } else {
69                 Set<String> updatedCustomerCode = new HashSet<>(softConstraintsBuilder.getCustomerCode());
70                 updatedCustomerCode.addAll(hardConstraints.getCustomerCode());
71                 softConstraintsBuilder.setCustomerCode(updatedCustomerCode);
72             }
73         }
74         if (hardConstraints.getOperationalMode() != null && !hardConstraints.getOperationalMode().isEmpty()) {
75             if (softConstraintsBuilder.getOperationalMode() == null
76                     || softConstraintsBuilder.getOperationalMode().isEmpty()) {
77                 softConstraintsBuilder.setOperationalMode(hardConstraints.getOperationalMode());
78             } else {
79                 Set<String> updatedOperationalMode = new HashSet<>(softConstraintsBuilder.getOperationalMode());
80                 updatedOperationalMode.addAll(hardConstraints.getOperationalMode());
81                 softConstraintsBuilder.setOperationalMode(updatedOperationalMode);
82             }
83         }
84         if (hardConstraints.getDiversity() != null) {
85             softConstraintsBuilder
86                 .setDiversity(updateDiveristy(hardConstraints.getDiversity(), softConstraints.getDiversity()));
87         }
88         if (hardConstraints.getExclude() != null) {
89             softConstraintsBuilder
90                 .setExclude(updateExclude(hardConstraints.getExclude(), softConstraints.getExclude()));
91         }
92         if (hardConstraints.getInclude() != null) {
93             softConstraintsBuilder
94                 .setInclude(updateInclude(hardConstraints.getInclude(), softConstraints.getInclude()));
95         }
96         if (hardConstraints.getLatency() != null) {
97             softConstraintsBuilder
98                 .setLatency(updateLatency(hardConstraints.getLatency(), softConstraints.getLatency()));
99         }
100         if (hardConstraints.getDistance() != null) {
101             softConstraintsBuilder
102                 .setDistance(updateDistance(hardConstraints.getDistance(), softConstraints.getDistance()));
103         }
104         if (hardConstraints.getHopCount() != null) {
105             softConstraintsBuilder
106                 .setHopCount(updateHopCount(hardConstraints.getHopCount(), softConstraints.getHopCount()));
107         }
108         if (hardConstraints.getTEMetric() != null) {
109             softConstraintsBuilder
110                 .setTEMetric(updateTEMetric(hardConstraints.getTEMetric(), softConstraints.getTEMetric()));
111         }
112
113         if (hardConstraints.getCoRouting() != null) {
114             softConstraintsBuilder
115                 .setCoRouting(updateCoRouting(hardConstraints.getCoRouting(), softConstraints.getCoRouting()));
116         }
117         return softConstraintsBuilder.build();
118     }
119
120     private static Include updateInclude(Include hard, Include soft) {
121         IncludeBuilder includeBldr = soft == null ? new IncludeBuilder() : new IncludeBuilder(soft);
122
123         if (hard.getFiberBundle() != null && !hard.getFiberBundle().isEmpty()) {
124             if (includeBldr.getFiberBundle() == null) {
125                 includeBldr.setFiberBundle(hard.getFiberBundle());
126             } else {
127                 Set<String> fiberList = new HashSet<>(includeBldr.getFiberBundle());
128                 fiberList.addAll(hard.getFiberBundle());
129                 includeBldr.setFiberBundle(new HashSet<>(fiberList));
130             }
131         }
132         if (hard.getNodeId() != null && !hard.getNodeId().isEmpty()) {
133             if (includeBldr.getNodeId() == null) {
134                 includeBldr.setNodeId(hard.getNodeId());
135             } else {
136                 Set<NodeIdType> nodeIdList = new HashSet<>(includeBldr.getNodeId());
137                 nodeIdList.addAll(hard.getNodeId());
138                 includeBldr.setNodeId(new HashSet<>(nodeIdList));
139             }
140         }
141         if (hard.getSite() != null && !hard.getSite().isEmpty()) {
142             if (includeBldr.getSite() == null) {
143                 includeBldr.setSite(hard.getSite());
144             } else {
145                 Set<String> siteList = new HashSet<>(includeBldr.getSite());
146                 siteList.addAll(hard.getSite());
147                 includeBldr.setSite(new HashSet<>(siteList));
148             }
149         }
150         if (hard.getSrlgId() != null && !hard.getSrlgId().isEmpty()) {
151             if (includeBldr.getSrlgId() == null) {
152                 includeBldr.setSrlgId(hard.getSrlgId());
153             } else {
154                 Set<Uint32> srlgList = new HashSet<>(includeBldr.getSrlgId());
155                 srlgList.addAll(hard.getSrlgId());
156                 includeBldr.setSrlgId(new HashSet<>(srlgList));
157             }
158         }
159         if (hard.getSupportingServiceName() != null && !hard.getSupportingServiceName().isEmpty()) {
160             if (includeBldr.getSupportingServiceName() == null) {
161                 includeBldr.setSupportingServiceName(hard.getSupportingServiceName());
162             } else {
163                 Set<String> serviceList = new HashSet<>(includeBldr.getSupportingServiceName());
164                 serviceList.addAll(hard.getSupportingServiceName());
165                 includeBldr.setSupportingServiceName(new HashSet<>(serviceList));
166             }
167         }
168         if (hard.getLinkIdentifier() != null && !hard.getLinkIdentifier().isEmpty()) {
169             if (includeBldr.getLinkIdentifier() == null) {
170                 includeBldr.setLinkIdentifier(hard.getLinkIdentifier());
171             } else {
172                 Map<LinkIdentifierKey, LinkIdentifier> linkList = new HashMap<>(includeBldr.getLinkIdentifier());
173                 linkList.putAll(hard.getLinkIdentifier());
174                 includeBldr.setLinkIdentifier(linkList);
175             }
176         }
177         return includeBldr.build();
178     }
179
180     private static Exclude updateExclude(Exclude hard, Exclude soft) {
181         ExcludeBuilder excludeBldr = soft == null ? new ExcludeBuilder() : new ExcludeBuilder(soft);
182
183         if (hard.getFiberBundle() != null && !hard.getFiberBundle().isEmpty()) {
184             if (excludeBldr.getFiberBundle() == null) {
185                 excludeBldr.setFiberBundle(hard.getFiberBundle());
186             } else {
187                 Set<String> fiberList = new HashSet<>(excludeBldr.getFiberBundle());
188                 fiberList.addAll(hard.getFiberBundle());
189                 excludeBldr.setFiberBundle(new HashSet<>(fiberList));
190             }
191         }
192         if (hard.getNodeId() != null && !hard.getNodeId().isEmpty()) {
193             if (excludeBldr.getNodeId() == null) {
194                 excludeBldr.setNodeId(hard.getNodeId());
195             } else {
196                 Set<NodeIdType> nodeIdList = new HashSet<>(excludeBldr.getNodeId());
197                 nodeIdList.addAll(hard.getNodeId());
198                 excludeBldr.setNodeId(new HashSet<>(nodeIdList));
199             }
200         }
201         if (hard.getSite() != null && !hard.getSite().isEmpty()) {
202             if (excludeBldr.getSite() == null) {
203                 excludeBldr.setSite(hard.getSite());
204             } else {
205                 Set<String> siteList = new HashSet<>(excludeBldr.getSite());
206                 siteList.addAll(hard.getSite());
207                 excludeBldr.setSite(new HashSet<>(siteList));
208             }
209         }
210         if (hard.getSrlgId() != null && !hard.getSrlgId().isEmpty()) {
211             if (excludeBldr.getSrlgId() == null) {
212                 excludeBldr.setSrlgId(hard.getSrlgId());
213             } else {
214                 Set<Uint32> srlgList = new HashSet<>(excludeBldr.getSrlgId());
215                 srlgList.addAll(hard.getSrlgId());
216                 excludeBldr.setSrlgId(new HashSet<>(srlgList));
217             }
218         }
219         if (hard.getSupportingServiceName() != null && !hard.getSupportingServiceName().isEmpty()) {
220             if (excludeBldr.getSupportingServiceName() == null) {
221                 excludeBldr.setSupportingServiceName(hard.getSupportingServiceName());
222             } else {
223                 Set<String> serviceList = new HashSet<>(excludeBldr.getSupportingServiceName());
224                 serviceList.addAll(hard.getSupportingServiceName());
225                 excludeBldr.setSupportingServiceName(new HashSet<>(serviceList));
226             }
227         }
228         if (hard.getLinkIdentifier() != null && !hard.getLinkIdentifier().isEmpty()) {
229             if (excludeBldr.getLinkIdentifier() == null) {
230                 excludeBldr.setLinkIdentifier(hard.getLinkIdentifier());
231             } else {
232                 Map<LinkIdentifierKey, LinkIdentifier> linkList = new HashMap<>(excludeBldr.getLinkIdentifier());
233                 linkList.putAll(hard.getLinkIdentifier());
234                 excludeBldr.setLinkIdentifier(linkList);
235             }
236         }
237         return excludeBldr.build();
238     }
239
240     private static Diversity updateDiveristy(Diversity hard, Diversity soft) {
241         DiversityBuilder diversityBldr = soft == null ? new DiversityBuilder() : new DiversityBuilder(soft);
242
243         Map<
244             org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.diversity.existing.service
245                     .constraints.ServiceIdentifierListKey,
246             org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.diversity.existing.service
247                     .constraints.ServiceIdentifierList> sil =
248                 diversityBldr.getServiceIdentifierList() == null
249                     ? new HashMap<>()
250                     : new HashMap<>(diversityBldr.getServiceIdentifierList());
251         if (!hard.getServiceIdentifierList().isEmpty()) {
252             sil.putAll(hard.getServiceIdentifierList());
253             diversityBldr.setServiceIdentifierList(sil);
254         }
255         if (hard.getDiversityType() != null) {
256             diversityBldr.setDiversityType(hard.getDiversityType());
257         }
258         return diversityBldr.build();
259     }
260
261     private static Latency updateLatency(Latency hard, Latency soft) {
262         return soft == null || hard.getMaxLatency().longValue() <= soft.getMaxLatency().longValue()
263             ? new LatencyBuilder(hard).build()
264             : new LatencyBuilder(soft).build();
265     }
266
267     private static Distance updateDistance(Distance hard, Distance soft) {
268         return soft == null || hard.getMaxDistance().longValue() <= soft.getMaxDistance().longValue()
269             ? new DistanceBuilder(hard).build()
270             : new DistanceBuilder(soft).build();
271     }
272
273     private static HopCount updateHopCount(HopCount hard, HopCount soft) {
274         if (soft == null) {
275             return new HopCountBuilder(hard).build();
276         }
277         HopCountBuilder hcBldr = new HopCountBuilder();
278         if (soft.getMaxWdmHopCount() == null) {
279             if (hard.getMaxWdmHopCount() != null) {
280                 hcBldr.setMaxWdmHopCount(hard.getMaxWdmHopCount());
281             }
282         } else {
283             hcBldr.setMaxWdmHopCount(
284                 hard.getMaxWdmHopCount() == null
285                         || soft.getMaxWdmHopCount().intValue() <= hard.getMaxWdmHopCount().intValue()
286                     ? soft.getMaxWdmHopCount()
287                     : hard.getMaxWdmHopCount());
288         }
289         if (soft.getMaxOtnHopCount() == null) {
290             if (hard.getMaxOtnHopCount() != null) {
291                 hcBldr.setMaxOtnHopCount(hard.getMaxOtnHopCount());
292             }
293         } else {
294             hcBldr.setMaxOtnHopCount(
295                 hard.getMaxOtnHopCount() == null
296                         || soft.getMaxOtnHopCount().intValue() <= hard.getMaxOtnHopCount().intValue()
297                     ? soft.getMaxOtnHopCount()
298                     : hard.getMaxOtnHopCount());
299         }
300         return hcBldr.build();
301     }
302
303     private static TEMetric updateTEMetric(TEMetric hard, TEMetric soft) {
304         if (soft == null) {
305             return new TEMetricBuilder(hard).build();
306         }
307         TEMetricBuilder temBldr = new TEMetricBuilder();
308         if (soft.getMaxWdmTEMetric() == null) {
309             if (hard.getMaxWdmTEMetric() != null) {
310                 temBldr.setMaxWdmTEMetric(hard.getMaxWdmTEMetric());
311             }
312         } else {
313             temBldr.setMaxWdmTEMetric(
314                 hard.getMaxWdmTEMetric() == null
315                         || soft.getMaxWdmTEMetric().intValue() <= hard.getMaxWdmTEMetric().intValue()
316                     ? soft.getMaxWdmTEMetric()
317                     : hard.getMaxWdmTEMetric());
318         }
319         if (soft.getMaxOtnTEMetric() == null) {
320             if (hard.getMaxOtnTEMetric() != null) {
321                 temBldr.setMaxOtnTEMetric(hard.getMaxWdmTEMetric());
322             }
323         } else {
324             temBldr.setMaxOtnTEMetric(
325                 hard.getMaxOtnTEMetric() == null
326                         || soft.getMaxOtnTEMetric().intValue() <= hard.getMaxOtnTEMetric().intValue()
327                     ? soft.getMaxOtnTEMetric()
328                     : hard.getMaxOtnTEMetric());
329         }
330         return temBldr.build();
331     }
332
333     private static CoRouting updateCoRouting(CoRouting hard, CoRouting soft) {
334         CoRoutingBuilder coRoutingBldr = soft == null ? new CoRoutingBuilder() : new CoRoutingBuilder(soft);
335
336         Map<
337             ServiceIdentifierListKey,
338             ServiceIdentifierList> serviceIdentifierList = coRoutingBldr.getServiceIdentifierList() == null
339                 ? new HashMap<>()
340                 : new HashMap<>(coRoutingBldr.getServiceIdentifierList());
341         if (!hard.getServiceIdentifierList().isEmpty()) {
342             serviceIdentifierList.putAll(hard.getServiceIdentifierList());
343             coRoutingBldr.setServiceIdentifierList(serviceIdentifierList);
344         }
345         return coRoutingBldr.build();
346     }
347
348     /**
349      * Remove all hard constraints except latency.
350      * @param  hardConstraints HardConstarints to be downgraded
351      * @return                 HardConstraints downgraded
352      */
353     public static HardConstraints downgradeHardConstraints(HardConstraints hardConstraints) {
354         HardConstraintsBuilder hardConstraintsBuilder = new HardConstraintsBuilder();
355         if (hardConstraints != null && hardConstraints.getLatency() != null) {
356             hardConstraintsBuilder.setLatency(hardConstraints.getLatency());
357         } else {
358             LOG.warn("latency value not found in HardContraints !");
359         }
360         return hardConstraintsBuilder.build();
361     }
362
363     /**
364      * Convert HardConstraints to SoftConstraints.
365      * @param  hardConstraints to be converted.
366      * @return                 SoftConstraints converted.
367      */
368     public static SoftConstraints convertToSoftConstraints(HardConstraints hardConstraints) {
369         return hardConstraints == null
370             ? new SoftConstraintsBuilder().build()
371             : new SoftConstraintsBuilder(hardConstraints).build();
372     }
373 }