2 * Copyright © 2017 Orange, Inc. and others. All rights reserved.
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
8 package org.opendaylight.transportpce.servicehandler;
10 import java.util.ArrayList;
11 import java.util.HashMap;
12 import java.util.HashSet;
13 import java.util.List;
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;
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)
49 public final class DowngradeConstraints {
51 private static final Logger LOG = LoggerFactory.getLogger(DowngradeConstraints.class);
53 private DowngradeConstraints() {
57 * Add Hard Constraints to Soft Constraints.
58 * @param hardConstraints to be added
59 * @param softConstraints to be modified
60 * @return SoftConstraints modified
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());
70 Set<String> updatedCustomerCode = new HashSet<>(softConstraintsBuilder.getCustomerCode());
71 updatedCustomerCode.addAll(hardConstraints.getCustomerCode());
72 softConstraintsBuilder.setCustomerCode(updatedCustomerCode);
75 if (hardConstraints.getOperationalMode() != null && !hardConstraints.getOperationalMode().isEmpty()) {
76 if (softConstraintsBuilder.getOperationalMode() == null
77 || softConstraintsBuilder.getOperationalMode().isEmpty()) {
78 softConstraintsBuilder.setOperationalMode(hardConstraints.getOperationalMode());
80 Set<String> updatedOperationalMode = new HashSet<>(softConstraintsBuilder.getOperationalMode());
81 updatedOperationalMode.addAll(hardConstraints.getOperationalMode());
82 softConstraintsBuilder.setOperationalMode(updatedOperationalMode);
85 if (hardConstraints.getDiversity() != null) {
86 softConstraintsBuilder
87 .setDiversity(updateDiveristy(hardConstraints.getDiversity(), softConstraints.getDiversity()));
89 if (hardConstraints.getExclude() != null) {
90 softConstraintsBuilder
91 .setExclude(updateExclude(hardConstraints.getExclude(), softConstraints.getExclude()));
93 if (hardConstraints.getInclude() != null) {
94 softConstraintsBuilder
95 .setInclude(updateInclude(hardConstraints.getInclude(), softConstraints.getInclude()));
97 if (hardConstraints.getLatency() != null) {
98 softConstraintsBuilder
99 .setLatency(updateLatency(hardConstraints.getLatency(), softConstraints.getLatency()));
101 if (hardConstraints.getDistance() != null) {
102 softConstraintsBuilder
103 .setDistance(updateDistance(hardConstraints.getDistance(), softConstraints.getDistance()));
105 if (hardConstraints.getHopCount() != null) {
106 softConstraintsBuilder
107 .setHopCount(updateHopCount(hardConstraints.getHopCount(), softConstraints.getHopCount()));
109 if (hardConstraints.getTEMetric() != null) {
110 softConstraintsBuilder
111 .setTEMetric(updateTEMetric(hardConstraints.getTEMetric(), softConstraints.getTEMetric()));
114 if (hardConstraints.getCoRouting() != null) {
115 softConstraintsBuilder
116 .setCoRouting(updateCoRouting(hardConstraints.getCoRouting(), softConstraints.getCoRouting()));
118 return softConstraintsBuilder.build();
121 private static Include updateInclude(Include hard, Include soft) {
122 IncludeBuilder includeBldr = soft == null ? new IncludeBuilder() : new IncludeBuilder(soft);
124 if (hard.getFiberBundle() != null && !hard.getFiberBundle().isEmpty()) {
125 if (includeBldr.getFiberBundle() == null) {
126 includeBldr.setFiberBundle(hard.getFiberBundle());
128 Set<String> fiberList = new HashSet<>(includeBldr.getFiberBundle());
129 fiberList.addAll(hard.getFiberBundle());
130 includeBldr.setFiberBundle(new ArrayList<>(fiberList));
133 if (hard.getNodeId() != null && !hard.getNodeId().isEmpty()) {
134 if (includeBldr.getNodeId() == null) {
135 includeBldr.setNodeId(hard.getNodeId());
137 List<NodeIdType> nodeIdList = new ArrayList<>(includeBldr.getNodeId());
138 nodeIdList.addAll(hard.getNodeId());
139 includeBldr.setNodeId(new ArrayList<>(nodeIdList));
142 if (hard.getSite() != null && !hard.getSite().isEmpty()) {
143 if (includeBldr.getSite() == null) {
144 includeBldr.setSite(hard.getSite());
146 List<String> siteList = new ArrayList<>(includeBldr.getSite());
147 siteList.addAll(hard.getSite());
148 includeBldr.setSite(new ArrayList<>(siteList));
151 if (hard.getSrlgId() != null && !hard.getSrlgId().isEmpty()) {
152 if (includeBldr.getSrlgId() == null) {
153 includeBldr.setSrlgId(hard.getSrlgId());
155 List<Uint32> srlgList = new ArrayList<>(includeBldr.getSrlgId());
156 srlgList.addAll(hard.getSrlgId());
157 includeBldr.setSrlgId(new ArrayList<>(srlgList));
160 if (hard.getSupportingServiceName() != null && !hard.getSupportingServiceName().isEmpty()) {
161 if (includeBldr.getSupportingServiceName() == null) {
162 includeBldr.setSupportingServiceName(hard.getSupportingServiceName());
164 List<String> serviceList = new ArrayList<>(includeBldr.getSupportingServiceName());
165 serviceList.addAll(hard.getSupportingServiceName());
166 includeBldr.setSupportingServiceName(new ArrayList<>(serviceList));
169 if (hard.getLinkIdentifier() != null && !hard.getLinkIdentifier().isEmpty()) {
170 if (includeBldr.getLinkIdentifier() == null) {
171 includeBldr.setLinkIdentifier(hard.getLinkIdentifier());
173 List<LinkIdentifier> linkList = new ArrayList<>(includeBldr.getLinkIdentifier());
174 linkList.addAll(hard.getLinkIdentifier());
175 includeBldr.setLinkIdentifier(linkList);
178 return includeBldr.build();
181 private static Exclude updateExclude(Exclude hard, Exclude soft) {
182 ExcludeBuilder excludeBldr = soft == null ? new ExcludeBuilder() : new ExcludeBuilder(soft);
184 if (hard.getFiberBundle() != null && !hard.getFiberBundle().isEmpty()) {
185 if (excludeBldr.getFiberBundle() == null) {
186 excludeBldr.setFiberBundle(hard.getFiberBundle());
188 Set<String> fiberList = new HashSet<>(excludeBldr.getFiberBundle());
189 fiberList.addAll(hard.getFiberBundle());
190 excludeBldr.setFiberBundle(new ArrayList<>(fiberList));
193 if (hard.getNodeId() != null && !hard.getNodeId().isEmpty()) {
194 if (excludeBldr.getNodeId() == null) {
195 excludeBldr.setNodeId(hard.getNodeId());
197 List<NodeIdType> nodeIdList = new ArrayList<>(excludeBldr.getNodeId());
198 nodeIdList.addAll(hard.getNodeId());
199 excludeBldr.setNodeId(new ArrayList<>(nodeIdList));
202 if (hard.getSite() != null && !hard.getSite().isEmpty()) {
203 if (excludeBldr.getSite() == null) {
204 excludeBldr.setSite(hard.getSite());
206 List<String> siteList = new ArrayList<>(excludeBldr.getSite());
207 siteList.addAll(hard.getSite());
208 excludeBldr.setSite(new ArrayList<>(siteList));
211 if (hard.getSrlgId() != null && !hard.getSrlgId().isEmpty()) {
212 if (excludeBldr.getSrlgId() == null) {
213 excludeBldr.setSrlgId(hard.getSrlgId());
215 List<Uint32> srlgList = new ArrayList<>(excludeBldr.getSrlgId());
216 srlgList.addAll(hard.getSrlgId());
217 excludeBldr.setSrlgId(new ArrayList<>(srlgList));
220 if (hard.getSupportingServiceName() != null && !hard.getSupportingServiceName().isEmpty()) {
221 if (excludeBldr.getSupportingServiceName() == null) {
222 excludeBldr.setSupportingServiceName(hard.getSupportingServiceName());
224 List<String> serviceList = new ArrayList<>(excludeBldr.getSupportingServiceName());
225 serviceList.addAll(hard.getSupportingServiceName());
226 excludeBldr.setSupportingServiceName(new ArrayList<>(serviceList));
229 if (hard.getLinkIdentifier() != null && !hard.getLinkIdentifier().isEmpty()) {
230 if (excludeBldr.getLinkIdentifier() == null) {
231 excludeBldr.setLinkIdentifier(hard.getLinkIdentifier());
233 List<LinkIdentifier> linkList = new ArrayList<>(excludeBldr.getLinkIdentifier());
234 linkList.addAll(hard.getLinkIdentifier());
235 excludeBldr.setLinkIdentifier(linkList);
238 return excludeBldr.build();
241 private static Diversity updateDiveristy(Diversity hard, Diversity soft) {
242 DiversityBuilder diversityBldr = soft == null ? new DiversityBuilder() : new DiversityBuilder(soft);
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
251 : new HashMap<>(diversityBldr.getServiceIdentifierList());
252 if (!hard.nonnullServiceIdentifierList().isEmpty()) {
253 sil.putAll(hard.getServiceIdentifierList());
254 diversityBldr.setServiceIdentifierList(sil);
256 if (hard.getDiversityType() != null) {
257 diversityBldr.setDiversityType(hard.getDiversityType());
259 return diversityBldr.build();
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();
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();
274 private static HopCount updateHopCount(HopCount hard, HopCount soft) {
276 return new HopCountBuilder(hard).build();
278 HopCountBuilder hcBldr = new HopCountBuilder();
279 if (soft.getMaxWdmHopCount() == null) {
280 if (hard.getMaxWdmHopCount() != null) {
281 hcBldr.setMaxWdmHopCount(hard.getMaxWdmHopCount());
284 hcBldr.setMaxWdmHopCount(
285 hard.getMaxWdmHopCount() == null
286 || soft.getMaxWdmHopCount().intValue() <= hard.getMaxWdmHopCount().intValue()
287 ? soft.getMaxWdmHopCount()
288 : hard.getMaxWdmHopCount());
290 if (soft.getMaxOtnHopCount() == null) {
291 if (hard.getMaxOtnHopCount() != null) {
292 hcBldr.setMaxOtnHopCount(hard.getMaxOtnHopCount());
295 hcBldr.setMaxOtnHopCount(
296 hard.getMaxOtnHopCount() == null
297 || soft.getMaxOtnHopCount().intValue() <= hard.getMaxOtnHopCount().intValue()
298 ? soft.getMaxOtnHopCount()
299 : hard.getMaxOtnHopCount());
301 return hcBldr.build();
304 private static TEMetric updateTEMetric(TEMetric hard, TEMetric soft) {
306 return new TEMetricBuilder(hard).build();
308 TEMetricBuilder temBldr = new TEMetricBuilder();
309 if (soft.getMaxWdmTEMetric() == null) {
310 if (hard.getMaxWdmTEMetric() != null) {
311 temBldr.setMaxWdmTEMetric(hard.getMaxWdmTEMetric());
314 temBldr.setMaxWdmTEMetric(
315 hard.getMaxWdmTEMetric() == null
316 || soft.getMaxWdmTEMetric().intValue() <= hard.getMaxWdmTEMetric().intValue()
317 ? soft.getMaxWdmTEMetric()
318 : hard.getMaxWdmTEMetric());
320 if (soft.getMaxOtnTEMetric() == null) {
321 if (hard.getMaxOtnTEMetric() != null) {
322 temBldr.setMaxOtnTEMetric(hard.getMaxWdmTEMetric());
325 temBldr.setMaxOtnTEMetric(
326 hard.getMaxOtnTEMetric() == null
327 || soft.getMaxOtnTEMetric().intValue() <= hard.getMaxOtnTEMetric().intValue()
328 ? soft.getMaxOtnTEMetric()
329 : hard.getMaxOtnTEMetric());
331 return temBldr.build();
334 private static CoRouting updateCoRouting(CoRouting hard, CoRouting soft) {
335 CoRoutingBuilder coRoutingBldr = soft == null ? new CoRoutingBuilder() : new CoRoutingBuilder(soft);
338 ServiceIdentifierListKey,
339 ServiceIdentifierList> serviceIdentifierList = coRoutingBldr.getServiceIdentifierList() == null
341 : new HashMap<>(coRoutingBldr.getServiceIdentifierList());
342 if (!hard.getServiceIdentifierList().isEmpty()) {
343 serviceIdentifierList.putAll(hard.getServiceIdentifierList());
344 coRoutingBldr.setServiceIdentifierList(serviceIdentifierList);
346 return coRoutingBldr.build();
350 * Remove all hard constraints except latency.
351 * @param hardConstraints HardConstarints to be downgraded
352 * @return HardConstraints downgraded
354 public static HardConstraints downgradeHardConstraints(HardConstraints hardConstraints) {
355 HardConstraintsBuilder hardConstraintsBuilder = new HardConstraintsBuilder();
356 if (hardConstraints != null && hardConstraints.getLatency() != null) {
357 hardConstraintsBuilder.setLatency(hardConstraints.getLatency());
359 LOG.warn("latency value not found in HardContraints !");
361 return hardConstraintsBuilder.build();
365 * Convert HardConstraints to SoftConstraints.
366 * @param hardConstraints to be converted.
367 * @return SoftConstraints converted.
369 public static SoftConstraints convertToSoftConstraints(HardConstraints hardConstraints) {
370 return hardConstraints == null
371 ? new SoftConstraintsBuilder().build()
372 : new SoftConstraintsBuilder(hardConstraints).build();