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