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.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;
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)
50 public final class DowngradeConstraints {
52 private static final Logger LOG = LoggerFactory.getLogger(DowngradeConstraints.class);
54 private DowngradeConstraints() {
58 * Add Hard Constraints to Soft Constraints.
59 * @param hardConstraints to be added
60 * @param softConstraints to be modified
61 * @return SoftConstraints modified
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());
71 List<String> updatedCustomerCode = new ArrayList<>(softConstraintsBuilder.getCustomerCode());
72 updatedCustomerCode.addAll(hardConstraints.getCustomerCode());
73 softConstraintsBuilder.setCustomerCode(updatedCustomerCode);
76 if (hardConstraints.getOperationalMode() != null && !hardConstraints.getOperationalMode().isEmpty()) {
77 if (softConstraintsBuilder.getOperationalMode() == null
78 || softConstraintsBuilder.getOperationalMode().isEmpty()) {
79 softConstraintsBuilder.setOperationalMode(hardConstraints.getOperationalMode());
81 List<String> updatedOperationalMode = new ArrayList<>(softConstraintsBuilder.getOperationalMode());
82 updatedOperationalMode.addAll(hardConstraints.getOperationalMode());
83 softConstraintsBuilder.setOperationalMode(updatedOperationalMode);
86 if (hardConstraints.getDiversity() != null) {
87 softConstraintsBuilder
88 .setDiversity(updateDiveristy(hardConstraints.getDiversity(), softConstraints.getDiversity()));
90 if (hardConstraints.getExclude() != null) {
91 softConstraintsBuilder
92 .setExclude(updateExclude(hardConstraints.getExclude(), softConstraints.getExclude()));
94 if (hardConstraints.getInclude() != null) {
95 softConstraintsBuilder
96 .setInclude(updateInclude(hardConstraints.getInclude(), softConstraints.getInclude()));
98 if (hardConstraints.getLatency() != null) {
99 softConstraintsBuilder
100 .setLatency(updateLatency(hardConstraints.getLatency(), softConstraints.getLatency()));
102 if (hardConstraints.getDistance() != null) {
103 softConstraintsBuilder
104 .setDistance(updateDistance(hardConstraints.getDistance(), softConstraints.getDistance()));
106 if (hardConstraints.getHopCount() != null) {
107 softConstraintsBuilder
108 .setHopCount(updateHopCount(hardConstraints.getHopCount(), softConstraints.getHopCount()));
110 if (hardConstraints.getTEMetric() != null) {
111 softConstraintsBuilder
112 .setTEMetric(updateTEMetric(hardConstraints.getTEMetric(), softConstraints.getTEMetric()));
115 if (hardConstraints.getCoRouting() != null) {
116 softConstraintsBuilder
117 .setCoRouting(updateCoRouting(hardConstraints.getCoRouting(), softConstraints.getCoRouting()));
119 return softConstraintsBuilder.build();
122 private static Include updateInclude(Include hard, Include soft) {
123 IncludeBuilder includeBldr = soft == null ? new IncludeBuilder() : new IncludeBuilder(soft);
125 if (hard.getFiberBundle() != null && !hard.getFiberBundle().isEmpty()) {
126 if (includeBldr.getFiberBundle() == null) {
127 includeBldr.setFiberBundle(hard.getFiberBundle());
129 Set<String> fiberList = new HashSet<>(includeBldr.getFiberBundle());
130 fiberList.addAll(hard.getFiberBundle());
131 includeBldr.setFiberBundle(new ArrayList<>(fiberList));
134 if (hard.getNodeId() != null && !hard.getNodeId().isEmpty()) {
135 if (includeBldr.getNodeId() == null) {
136 includeBldr.setNodeId(hard.getNodeId());
138 Set<NodeIdType> nodeIdList = new HashSet<>(includeBldr.getNodeId());
139 nodeIdList.addAll(hard.getNodeId());
140 includeBldr.setNodeId(new ArrayList<>(nodeIdList));
143 if (hard.getSite() != null && !hard.getSite().isEmpty()) {
144 if (includeBldr.getSite() == null) {
145 includeBldr.setSite(hard.getSite());
147 Set<String> siteList = new HashSet<>(includeBldr.getSite());
148 siteList.addAll(hard.getSite());
149 includeBldr.setSite(new ArrayList<>(siteList));
152 if (hard.getSrlgId() != null && !hard.getSrlgId().isEmpty()) {
153 if (includeBldr.getSrlgId() == null) {
154 includeBldr.setSrlgId(hard.getSrlgId());
156 Set<Uint32> srlgList = new HashSet<>(includeBldr.getSrlgId());
157 srlgList.addAll(hard.getSrlgId());
158 includeBldr.setSrlgId(new ArrayList<>(srlgList));
161 if (hard.getSupportingServiceName() != null && !hard.getSupportingServiceName().isEmpty()) {
162 if (includeBldr.getSupportingServiceName() == null) {
163 includeBldr.setSupportingServiceName(hard.getSupportingServiceName());
165 Set<String> serviceList = new HashSet<>(includeBldr.getSupportingServiceName());
166 serviceList.addAll(hard.getSupportingServiceName());
167 includeBldr.setSupportingServiceName(new ArrayList<>(serviceList));
170 if (hard.getLinkIdentifier() != null && !hard.getLinkIdentifier().isEmpty()) {
171 if (includeBldr.getLinkIdentifier() == null) {
172 includeBldr.setLinkIdentifier(hard.getLinkIdentifier());
174 Map<LinkIdentifierKey, LinkIdentifier> linkList = new HashMap<>(includeBldr.getLinkIdentifier());
175 linkList.putAll(hard.getLinkIdentifier());
176 includeBldr.setLinkIdentifier(linkList);
179 return includeBldr.build();
182 private static Exclude updateExclude(Exclude hard, Exclude soft) {
183 ExcludeBuilder excludeBldr = soft == null ? new ExcludeBuilder() : new ExcludeBuilder(soft);
185 if (hard.getFiberBundle() != null && !hard.getFiberBundle().isEmpty()) {
186 if (excludeBldr.getFiberBundle() == null) {
187 excludeBldr.setFiberBundle(hard.getFiberBundle());
189 Set<String> fiberList = new HashSet<>(excludeBldr.getFiberBundle());
190 fiberList.addAll(hard.getFiberBundle());
191 excludeBldr.setFiberBundle(new ArrayList<>(fiberList));
194 if (hard.getNodeId() != null && !hard.getNodeId().isEmpty()) {
195 if (excludeBldr.getNodeId() == null) {
196 excludeBldr.setNodeId(hard.getNodeId());
198 Set<NodeIdType> nodeIdList = new HashSet<>(excludeBldr.getNodeId());
199 nodeIdList.addAll(hard.getNodeId());
200 excludeBldr.setNodeId(new ArrayList<>(nodeIdList));
203 if (hard.getSite() != null && !hard.getSite().isEmpty()) {
204 if (excludeBldr.getSite() == null) {
205 excludeBldr.setSite(hard.getSite());
207 Set<String> siteList = new HashSet<>(excludeBldr.getSite());
208 siteList.addAll(hard.getSite());
209 excludeBldr.setSite(new ArrayList<>(siteList));
212 if (hard.getSrlgId() != null && !hard.getSrlgId().isEmpty()) {
213 if (excludeBldr.getSrlgId() == null) {
214 excludeBldr.setSrlgId(hard.getSrlgId());
216 Set<Uint32> srlgList = new HashSet<>(excludeBldr.getSrlgId());
217 srlgList.addAll(hard.getSrlgId());
218 excludeBldr.setSrlgId(new ArrayList<>(srlgList));
221 if (hard.getSupportingServiceName() != null && !hard.getSupportingServiceName().isEmpty()) {
222 if (excludeBldr.getSupportingServiceName() == null) {
223 excludeBldr.setSupportingServiceName(hard.getSupportingServiceName());
225 Set<String> serviceList = new HashSet<>(excludeBldr.getSupportingServiceName());
226 serviceList.addAll(hard.getSupportingServiceName());
227 excludeBldr.setSupportingServiceName(new ArrayList<>(serviceList));
230 if (hard.getLinkIdentifier() != null && !hard.getLinkIdentifier().isEmpty()) {
231 if (excludeBldr.getLinkIdentifier() == null) {
232 excludeBldr.setLinkIdentifier(hard.getLinkIdentifier());
234 Map<LinkIdentifierKey, LinkIdentifier> linkList = new HashMap<>(excludeBldr.getLinkIdentifier());
235 linkList.putAll(hard.getLinkIdentifier());
236 excludeBldr.setLinkIdentifier(linkList);
239 return excludeBldr.build();
242 private static Diversity updateDiveristy(Diversity hard, Diversity soft) {
243 DiversityBuilder diversityBldr = soft == null ? new DiversityBuilder() : new DiversityBuilder(soft);
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
252 : new HashMap<>(diversityBldr.getServiceIdentifierList());
253 if (!hard.getServiceIdentifierList().isEmpty()) {
254 sil.putAll(hard.getServiceIdentifierList());
255 diversityBldr.setServiceIdentifierList(sil);
257 if (hard.getDiversityType() != null) {
258 diversityBldr.setDiversityType(hard.getDiversityType());
260 return diversityBldr.build();
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();
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();
275 private static HopCount updateHopCount(HopCount hard, HopCount soft) {
277 return new HopCountBuilder(hard).build();
279 HopCountBuilder hcBldr = new HopCountBuilder();
280 if (soft.getMaxWdmHopCount() == null) {
281 if (hard.getMaxWdmHopCount() != null) {
282 hcBldr.setMaxWdmHopCount(hard.getMaxWdmHopCount());
285 hcBldr.setMaxWdmHopCount(
286 hard.getMaxWdmHopCount() == null
287 || soft.getMaxWdmHopCount().intValue() <= hard.getMaxWdmHopCount().intValue()
288 ? soft.getMaxWdmHopCount()
289 : hard.getMaxWdmHopCount());
291 if (soft.getMaxOtnHopCount() == null) {
292 if (hard.getMaxOtnHopCount() != null) {
293 hcBldr.setMaxOtnHopCount(hard.getMaxOtnHopCount());
296 hcBldr.setMaxOtnHopCount(
297 hard.getMaxOtnHopCount() == null
298 || soft.getMaxOtnHopCount().intValue() <= hard.getMaxOtnHopCount().intValue()
299 ? soft.getMaxOtnHopCount()
300 : hard.getMaxOtnHopCount());
302 return hcBldr.build();
305 private static TEMetric updateTEMetric(TEMetric hard, TEMetric soft) {
307 return new TEMetricBuilder(hard).build();
309 TEMetricBuilder temBldr = new TEMetricBuilder();
310 if (soft.getMaxWdmTEMetric() == null) {
311 if (hard.getMaxWdmTEMetric() != null) {
312 temBldr.setMaxWdmTEMetric(hard.getMaxWdmTEMetric());
315 temBldr.setMaxWdmTEMetric(
316 hard.getMaxWdmTEMetric() == null
317 || soft.getMaxWdmTEMetric().intValue() <= hard.getMaxWdmTEMetric().intValue()
318 ? soft.getMaxWdmTEMetric()
319 : hard.getMaxWdmTEMetric());
321 if (soft.getMaxOtnTEMetric() == null) {
322 if (hard.getMaxOtnTEMetric() != null) {
323 temBldr.setMaxOtnTEMetric(hard.getMaxWdmTEMetric());
326 temBldr.setMaxOtnTEMetric(
327 hard.getMaxOtnTEMetric() == null
328 || soft.getMaxOtnTEMetric().intValue() <= hard.getMaxOtnTEMetric().intValue()
329 ? soft.getMaxOtnTEMetric()
330 : hard.getMaxOtnTEMetric());
332 return temBldr.build();
335 private static CoRouting updateCoRouting(CoRouting hard, CoRouting soft) {
336 CoRoutingBuilder coRoutingBldr = soft == null ? new CoRoutingBuilder() : new CoRoutingBuilder(soft);
339 ServiceIdentifierListKey,
340 ServiceIdentifierList> serviceIdentifierList = coRoutingBldr.getServiceIdentifierList() == null
342 : new HashMap<>(coRoutingBldr.getServiceIdentifierList());
343 if (!hard.getServiceIdentifierList().isEmpty()) {
344 serviceIdentifierList.putAll(hard.getServiceIdentifierList());
345 coRoutingBldr.setServiceIdentifierList(serviceIdentifierList);
347 return coRoutingBldr.build();
351 * Remove all hard constraints except latency.
352 * @param hardConstraints HardConstarints to be downgraded
353 * @return HardConstraints downgraded
355 public static HardConstraints downgradeHardConstraints(HardConstraints hardConstraints) {
356 HardConstraintsBuilder hardConstraintsBuilder = new HardConstraintsBuilder();
357 if (hardConstraints != null && hardConstraints.getLatency() != null) {
358 hardConstraintsBuilder.setLatency(hardConstraints.getLatency());
360 LOG.warn("latency value not found in HardContraints !");
362 return hardConstraintsBuilder.build();
366 * Convert HardConstraints to SoftConstraints.
367 * @param hardConstraints to be converted.
368 * @return SoftConstraints converted.
370 public static SoftConstraints convertToSoftConstraints(HardConstraints hardConstraints) {
371 return hardConstraints == null
372 ? new SoftConstraintsBuilder().build()
373 : new SoftConstraintsBuilder(hardConstraints).build();