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