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.HashMap;
11 import java.util.HashSet;
14 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.node.types.rev210528.NodeIdType;
15 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.common.constraints.LinkIdentifier;
16 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.common.constraints.LinkIdentifierKey;
17 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.CoRouting;
18 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.CoRoutingBuilder;
19 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.Distance;
20 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.DistanceBuilder;
21 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.Diversity;
22 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.DiversityBuilder;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.Exclude;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.ExcludeBuilder;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.HopCount;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.HopCountBuilder;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.Include;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.IncludeBuilder;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.Latency;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.LatencyBuilder;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.TEMetric;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.TEMetricBuilder;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.co.routing.ServiceIdentifierList;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.co.routing.ServiceIdentifierListKey;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.routing.constraints.HardConstraints;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.routing.constraints.HardConstraintsBuilder;
37 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.routing.constraints.SoftConstraints;
38 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.routing.constraints.SoftConstraintsBuilder;
39 import org.opendaylight.yangtools.yang.common.Uint32;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 * Class to Map Hard Constraints to Soft Constraints.
45 * @author Martial Coulibaly ( martial.coulibaly@gfi.com ) on behalf of Orange
46 * @author gilles Thouenon (gilles.thouenon@orange.com)
48 public final class DowngradeConstraints {
50 private static final Logger LOG = LoggerFactory.getLogger(DowngradeConstraints.class);
52 private DowngradeConstraints() {
56 * Add Hard Constraints to Soft Constraints.
57 * @param hardConstraints to be added
58 * @param softConstraints to be modified
59 * @return SoftConstraints modified
61 public static SoftConstraints updateSoftConstraints(HardConstraints hardConstraints,
62 SoftConstraints softConstraints) {
63 SoftConstraintsBuilder softConstraintsBuilder = new SoftConstraintsBuilder(softConstraints);
64 if (hardConstraints.getCustomerCode() != null && !hardConstraints.getCustomerCode().isEmpty()) {
65 if (softConstraintsBuilder.getCustomerCode() == null
66 || softConstraintsBuilder.getCustomerCode().isEmpty()) {
67 softConstraintsBuilder.setCustomerCode(hardConstraints.getCustomerCode());
69 Set<String> updatedCustomerCode = new HashSet<>(softConstraintsBuilder.getCustomerCode());
70 updatedCustomerCode.addAll(hardConstraints.getCustomerCode());
71 softConstraintsBuilder.setCustomerCode(updatedCustomerCode);
74 if (hardConstraints.getOperationalMode() != null && !hardConstraints.getOperationalMode().isEmpty()) {
75 if (softConstraintsBuilder.getOperationalMode() == null
76 || softConstraintsBuilder.getOperationalMode().isEmpty()) {
77 softConstraintsBuilder.setOperationalMode(hardConstraints.getOperationalMode());
79 Set<String> updatedOperationalMode = new HashSet<>(softConstraintsBuilder.getOperationalMode());
80 updatedOperationalMode.addAll(hardConstraints.getOperationalMode());
81 softConstraintsBuilder.setOperationalMode(updatedOperationalMode);
84 if (hardConstraints.getDiversity() != null) {
85 softConstraintsBuilder
86 .setDiversity(updateDiveristy(hardConstraints.getDiversity(), softConstraints.getDiversity()));
88 if (hardConstraints.getExclude() != null) {
89 softConstraintsBuilder
90 .setExclude(updateExclude(hardConstraints.getExclude(), softConstraints.getExclude()));
92 if (hardConstraints.getInclude() != null) {
93 softConstraintsBuilder
94 .setInclude(updateInclude(hardConstraints.getInclude(), softConstraints.getInclude()));
96 if (hardConstraints.getLatency() != null) {
97 softConstraintsBuilder
98 .setLatency(updateLatency(hardConstraints.getLatency(), softConstraints.getLatency()));
100 if (hardConstraints.getDistance() != null) {
101 softConstraintsBuilder
102 .setDistance(updateDistance(hardConstraints.getDistance(), softConstraints.getDistance()));
104 if (hardConstraints.getHopCount() != null) {
105 softConstraintsBuilder
106 .setHopCount(updateHopCount(hardConstraints.getHopCount(), softConstraints.getHopCount()));
108 if (hardConstraints.getTEMetric() != null) {
109 softConstraintsBuilder
110 .setTEMetric(updateTEMetric(hardConstraints.getTEMetric(), softConstraints.getTEMetric()));
113 if (hardConstraints.getCoRouting() != null) {
114 softConstraintsBuilder
115 .setCoRouting(updateCoRouting(hardConstraints.getCoRouting(), softConstraints.getCoRouting()));
117 return softConstraintsBuilder.build();
120 private static Include updateInclude(Include hard, Include soft) {
121 IncludeBuilder includeBldr = soft == null ? new IncludeBuilder() : new IncludeBuilder(soft);
123 if (hard.getFiberBundle() != null && !hard.getFiberBundle().isEmpty()) {
124 if (includeBldr.getFiberBundle() == null) {
125 includeBldr.setFiberBundle(hard.getFiberBundle());
127 Set<String> fiberList = new HashSet<>(includeBldr.getFiberBundle());
128 fiberList.addAll(hard.getFiberBundle());
129 includeBldr.setFiberBundle(new HashSet<>(fiberList));
132 if (hard.getNodeId() != null && !hard.getNodeId().isEmpty()) {
133 if (includeBldr.getNodeId() == null) {
134 includeBldr.setNodeId(hard.getNodeId());
136 Set<NodeIdType> nodeIdList = new HashSet<>(includeBldr.getNodeId());
137 nodeIdList.addAll(hard.getNodeId());
138 includeBldr.setNodeId(new HashSet<>(nodeIdList));
141 if (hard.getSite() != null && !hard.getSite().isEmpty()) {
142 if (includeBldr.getSite() == null) {
143 includeBldr.setSite(hard.getSite());
145 Set<String> siteList = new HashSet<>(includeBldr.getSite());
146 siteList.addAll(hard.getSite());
147 includeBldr.setSite(new HashSet<>(siteList));
150 if (hard.getSrlgId() != null && !hard.getSrlgId().isEmpty()) {
151 if (includeBldr.getSrlgId() == null) {
152 includeBldr.setSrlgId(hard.getSrlgId());
154 Set<Uint32> srlgList = new HashSet<>(includeBldr.getSrlgId());
155 srlgList.addAll(hard.getSrlgId());
156 includeBldr.setSrlgId(new HashSet<>(srlgList));
159 if (hard.getSupportingServiceName() != null && !hard.getSupportingServiceName().isEmpty()) {
160 if (includeBldr.getSupportingServiceName() == null) {
161 includeBldr.setSupportingServiceName(hard.getSupportingServiceName());
163 Set<String> serviceList = new HashSet<>(includeBldr.getSupportingServiceName());
164 serviceList.addAll(hard.getSupportingServiceName());
165 includeBldr.setSupportingServiceName(new HashSet<>(serviceList));
168 if (hard.getLinkIdentifier() != null && !hard.getLinkIdentifier().isEmpty()) {
169 if (includeBldr.getLinkIdentifier() == null) {
170 includeBldr.setLinkIdentifier(hard.getLinkIdentifier());
172 Map<LinkIdentifierKey, LinkIdentifier> linkList = new HashMap<>(includeBldr.getLinkIdentifier());
173 linkList.putAll(hard.getLinkIdentifier());
174 includeBldr.setLinkIdentifier(linkList);
177 return includeBldr.build();
180 private static Exclude updateExclude(Exclude hard, Exclude soft) {
181 ExcludeBuilder excludeBldr = soft == null ? new ExcludeBuilder() : new ExcludeBuilder(soft);
183 if (hard.getFiberBundle() != null && !hard.getFiberBundle().isEmpty()) {
184 if (excludeBldr.getFiberBundle() == null) {
185 excludeBldr.setFiberBundle(hard.getFiberBundle());
187 Set<String> fiberList = new HashSet<>(excludeBldr.getFiberBundle());
188 fiberList.addAll(hard.getFiberBundle());
189 excludeBldr.setFiberBundle(new HashSet<>(fiberList));
192 if (hard.getNodeId() != null && !hard.getNodeId().isEmpty()) {
193 if (excludeBldr.getNodeId() == null) {
194 excludeBldr.setNodeId(hard.getNodeId());
196 Set<NodeIdType> nodeIdList = new HashSet<>(excludeBldr.getNodeId());
197 nodeIdList.addAll(hard.getNodeId());
198 excludeBldr.setNodeId(new HashSet<>(nodeIdList));
201 if (hard.getSite() != null && !hard.getSite().isEmpty()) {
202 if (excludeBldr.getSite() == null) {
203 excludeBldr.setSite(hard.getSite());
205 Set<String> siteList = new HashSet<>(excludeBldr.getSite());
206 siteList.addAll(hard.getSite());
207 excludeBldr.setSite(new HashSet<>(siteList));
210 if (hard.getSrlgId() != null && !hard.getSrlgId().isEmpty()) {
211 if (excludeBldr.getSrlgId() == null) {
212 excludeBldr.setSrlgId(hard.getSrlgId());
214 Set<Uint32> srlgList = new HashSet<>(excludeBldr.getSrlgId());
215 srlgList.addAll(hard.getSrlgId());
216 excludeBldr.setSrlgId(new HashSet<>(srlgList));
219 if (hard.getSupportingServiceName() != null && !hard.getSupportingServiceName().isEmpty()) {
220 if (excludeBldr.getSupportingServiceName() == null) {
221 excludeBldr.setSupportingServiceName(hard.getSupportingServiceName());
223 Set<String> serviceList = new HashSet<>(excludeBldr.getSupportingServiceName());
224 serviceList.addAll(hard.getSupportingServiceName());
225 excludeBldr.setSupportingServiceName(new HashSet<>(serviceList));
228 if (hard.getLinkIdentifier() != null && !hard.getLinkIdentifier().isEmpty()) {
229 if (excludeBldr.getLinkIdentifier() == null) {
230 excludeBldr.setLinkIdentifier(hard.getLinkIdentifier());
232 Map<LinkIdentifierKey, LinkIdentifier> linkList = new HashMap<>(excludeBldr.getLinkIdentifier());
233 linkList.putAll(hard.getLinkIdentifier());
234 excludeBldr.setLinkIdentifier(linkList);
237 return excludeBldr.build();
240 private static Diversity updateDiveristy(Diversity hard, Diversity soft) {
241 DiversityBuilder diversityBldr = soft == null ? new DiversityBuilder() : new DiversityBuilder(soft);
244 org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.diversity.existing.service
245 .constraints.ServiceIdentifierListKey,
246 org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.diversity.existing.service
247 .constraints.ServiceIdentifierList> sil =
248 diversityBldr.getServiceIdentifierList() == null
250 : new HashMap<>(diversityBldr.getServiceIdentifierList());
251 if (!hard.getServiceIdentifierList().isEmpty()) {
252 sil.putAll(hard.getServiceIdentifierList());
253 diversityBldr.setServiceIdentifierList(sil);
255 if (hard.getDiversityType() != null) {
256 diversityBldr.setDiversityType(hard.getDiversityType());
258 return diversityBldr.build();
261 private static Latency updateLatency(Latency hard, Latency soft) {
262 return soft == null || hard.getMaxLatency().longValue() <= soft.getMaxLatency().longValue()
263 ? new LatencyBuilder(hard).build()
264 : new LatencyBuilder(soft).build();
267 private static Distance updateDistance(Distance hard, Distance soft) {
268 return soft == null || hard.getMaxDistance().longValue() <= soft.getMaxDistance().longValue()
269 ? new DistanceBuilder(hard).build()
270 : new DistanceBuilder(soft).build();
273 private static HopCount updateHopCount(HopCount hard, HopCount soft) {
275 return new HopCountBuilder(hard).build();
277 HopCountBuilder hcBldr = new HopCountBuilder();
278 if (soft.getMaxWdmHopCount() == null) {
279 if (hard.getMaxWdmHopCount() != null) {
280 hcBldr.setMaxWdmHopCount(hard.getMaxWdmHopCount());
283 hcBldr.setMaxWdmHopCount(
284 hard.getMaxWdmHopCount() == null
285 || soft.getMaxWdmHopCount().intValue() <= hard.getMaxWdmHopCount().intValue()
286 ? soft.getMaxWdmHopCount()
287 : hard.getMaxWdmHopCount());
289 if (soft.getMaxOtnHopCount() == null) {
290 if (hard.getMaxOtnHopCount() != null) {
291 hcBldr.setMaxOtnHopCount(hard.getMaxOtnHopCount());
294 hcBldr.setMaxOtnHopCount(
295 hard.getMaxOtnHopCount() == null
296 || soft.getMaxOtnHopCount().intValue() <= hard.getMaxOtnHopCount().intValue()
297 ? soft.getMaxOtnHopCount()
298 : hard.getMaxOtnHopCount());
300 return hcBldr.build();
303 private static TEMetric updateTEMetric(TEMetric hard, TEMetric soft) {
305 return new TEMetricBuilder(hard).build();
307 TEMetricBuilder temBldr = new TEMetricBuilder();
308 if (soft.getMaxWdmTEMetric() == null) {
309 if (hard.getMaxWdmTEMetric() != null) {
310 temBldr.setMaxWdmTEMetric(hard.getMaxWdmTEMetric());
313 temBldr.setMaxWdmTEMetric(
314 hard.getMaxWdmTEMetric() == null
315 || soft.getMaxWdmTEMetric().intValue() <= hard.getMaxWdmTEMetric().intValue()
316 ? soft.getMaxWdmTEMetric()
317 : hard.getMaxWdmTEMetric());
319 if (soft.getMaxOtnTEMetric() == null) {
320 if (hard.getMaxOtnTEMetric() != null) {
321 temBldr.setMaxOtnTEMetric(hard.getMaxWdmTEMetric());
324 temBldr.setMaxOtnTEMetric(
325 hard.getMaxOtnTEMetric() == null
326 || soft.getMaxOtnTEMetric().intValue() <= hard.getMaxOtnTEMetric().intValue()
327 ? soft.getMaxOtnTEMetric()
328 : hard.getMaxOtnTEMetric());
330 return temBldr.build();
333 private static CoRouting updateCoRouting(CoRouting hard, CoRouting soft) {
334 CoRoutingBuilder coRoutingBldr = soft == null ? new CoRoutingBuilder() : new CoRoutingBuilder(soft);
337 ServiceIdentifierListKey,
338 ServiceIdentifierList> serviceIdentifierList = coRoutingBldr.getServiceIdentifierList() == null
340 : new HashMap<>(coRoutingBldr.getServiceIdentifierList());
341 if (!hard.getServiceIdentifierList().isEmpty()) {
342 serviceIdentifierList.putAll(hard.getServiceIdentifierList());
343 coRoutingBldr.setServiceIdentifierList(serviceIdentifierList);
345 return coRoutingBldr.build();
349 * Remove all hard constraints except latency.
350 * @param hardConstraints HardConstarints to be downgraded
351 * @return HardConstraints downgraded
353 public static HardConstraints downgradeHardConstraints(HardConstraints hardConstraints) {
354 HardConstraintsBuilder hardConstraintsBuilder = new HardConstraintsBuilder();
355 if (hardConstraints != null && hardConstraints.getLatency() != null) {
356 hardConstraintsBuilder.setLatency(hardConstraints.getLatency());
358 LOG.warn("latency value not found in HardContraints !");
360 return hardConstraintsBuilder.build();
364 * Convert HardConstraints to SoftConstraints.
365 * @param hardConstraints to be converted.
366 * @return SoftConstraints converted.
368 public static SoftConstraints convertToSoftConstraints(HardConstraints hardConstraints) {
369 return hardConstraints == null
370 ? new SoftConstraintsBuilder().build()
371 : new SoftConstraintsBuilder(hardConstraints).build();