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