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.opendaylight.yangtools.yang.common.Uint8;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
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)
51 public final class DowngradeConstraints {
53 private static final Logger LOG = LoggerFactory.getLogger(DowngradeConstraints.class);
55 private DowngradeConstraints() {
59 * Add Hard Constraints to Soft Constraints.
60 * @param hardConstraints to be added
61 * @param softConstraints to be modified
62 * @return SoftConstraints modified
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());
72 List<String> updatedCustomerCode = new ArrayList<>(softConstraintsBuilder.getCustomerCode());
73 updatedCustomerCode.addAll(hardConstraints.getCustomerCode());
74 softConstraintsBuilder.setCustomerCode(updatedCustomerCode);
77 if (hardConstraints.getOperationalMode() != null && !hardConstraints.getOperationalMode().isEmpty()) {
78 if (softConstraintsBuilder.getOperationalMode() == null
79 || softConstraintsBuilder.getOperationalMode().isEmpty()) {
80 softConstraintsBuilder.setOperationalMode(hardConstraints.getOperationalMode());
82 List<String> updatedOperationalMode = new ArrayList<>(softConstraintsBuilder.getOperationalMode());
83 updatedOperationalMode.addAll(hardConstraints.getOperationalMode());
84 softConstraintsBuilder.setOperationalMode(updatedOperationalMode);
87 if (hardConstraints.getDiversity() != null) {
88 softConstraintsBuilder
89 .setDiversity(updateDiveristy(hardConstraints.getDiversity(), softConstraints.getDiversity()));
91 if (hardConstraints.getExclude() != null) {
92 softConstraintsBuilder
93 .setExclude(updateExclude(hardConstraints.getExclude(), softConstraints.getExclude()));
95 if (hardConstraints.getInclude() != null) {
96 softConstraintsBuilder
97 .setInclude(updateInclude(hardConstraints.getInclude(), softConstraints.getInclude()));
99 if (hardConstraints.getLatency() != null) {
100 softConstraintsBuilder
101 .setLatency(updateLatency(hardConstraints.getLatency(), softConstraints.getLatency()));
103 if (hardConstraints.getDistance() != null) {
104 softConstraintsBuilder
105 .setDistance(updateDistance(hardConstraints.getDistance(), softConstraints.getDistance()));
107 if (hardConstraints.getHopCount() != null) {
108 softConstraintsBuilder
109 .setHopCount(updateHopCount(hardConstraints.getHopCount(), softConstraints.getHopCount()));
111 if (hardConstraints.getTEMetric() != null) {
112 softConstraintsBuilder
113 .setTEMetric(updateTEMetric(hardConstraints.getTEMetric(), softConstraints.getTEMetric()));
116 if (hardConstraints.getCoRouting() != null) {
117 softConstraintsBuilder
118 .setCoRouting(updateCoRouting(hardConstraints.getCoRouting(), softConstraints.getCoRouting()));
120 return softConstraintsBuilder.build();
123 private static Include updateInclude(Include hard, Include soft) {
124 IncludeBuilder includeBldr = soft == null ? new IncludeBuilder() : new IncludeBuilder(soft);
126 if (hard.getFiberBundle() != null && !hard.getFiberBundle().isEmpty()) {
127 if (includeBldr.getFiberBundle() == null) {
128 includeBldr.setFiberBundle(hard.getFiberBundle());
130 Set<String> fiberList = new HashSet<>(includeBldr.getFiberBundle());
131 fiberList.addAll(hard.getFiberBundle());
132 includeBldr.setFiberBundle(new ArrayList<>(fiberList));
135 if (hard.getNodeId() != null && !hard.getNodeId().isEmpty()) {
136 if (includeBldr.getNodeId() == null) {
137 includeBldr.setNodeId(hard.getNodeId());
139 Set<NodeIdType> nodeIdList = new HashSet<>(includeBldr.getNodeId());
140 nodeIdList.addAll(hard.getNodeId());
141 includeBldr.setNodeId(new ArrayList<>(nodeIdList));
144 if (hard.getSite() != null && !hard.getSite().isEmpty()) {
145 if (includeBldr.getSite() == null) {
146 includeBldr.setSite(hard.getSite());
148 Set<String> siteList = new HashSet<>(includeBldr.getSite());
149 siteList.addAll(hard.getSite());
150 includeBldr.setSite(new ArrayList<>(siteList));
153 if (hard.getSrlgId() != null && !hard.getSrlgId().isEmpty()) {
154 if (includeBldr.getSrlgId() == null) {
155 includeBldr.setSrlgId(hard.getSrlgId());
157 Set<Uint32> srlgList = new HashSet<>(includeBldr.getSrlgId());
158 srlgList.addAll(hard.getSrlgId());
159 includeBldr.setSrlgId(new ArrayList<>(srlgList));
162 if (hard.getSupportingServiceName() != null && !hard.getSupportingServiceName().isEmpty()) {
163 if (includeBldr.getSupportingServiceName() == null) {
164 includeBldr.setSupportingServiceName(hard.getSupportingServiceName());
166 Set<String> serviceList = new HashSet<>(includeBldr.getSupportingServiceName());
167 serviceList.addAll(hard.getSupportingServiceName());
168 includeBldr.setSupportingServiceName(new ArrayList<>(serviceList));
171 if (hard.getLinkIdentifier() != null && !hard.getLinkIdentifier().isEmpty()) {
172 if (includeBldr.getLinkIdentifier() == null) {
173 includeBldr.setLinkIdentifier(hard.getLinkIdentifier());
175 Map<LinkIdentifierKey, LinkIdentifier> linkList = new HashMap<>(includeBldr.getLinkIdentifier());
176 linkList.putAll(hard.getLinkIdentifier());
177 includeBldr.setLinkIdentifier(linkList);
180 return includeBldr.build();
183 private static Exclude updateExclude(Exclude hard, Exclude soft) {
184 ExcludeBuilder excludeBldr = soft == null ? new ExcludeBuilder() : new ExcludeBuilder(soft);
186 if (hard.getFiberBundle() != null && !hard.getFiberBundle().isEmpty()) {
187 if (excludeBldr.getFiberBundle() == null) {
188 excludeBldr.setFiberBundle(hard.getFiberBundle());
190 Set<String> fiberList = new HashSet<>(excludeBldr.getFiberBundle());
191 fiberList.addAll(hard.getFiberBundle());
192 excludeBldr.setFiberBundle(new ArrayList<>(fiberList));
195 if (hard.getNodeId() != null && !hard.getNodeId().isEmpty()) {
196 if (excludeBldr.getNodeId() == null) {
197 excludeBldr.setNodeId(hard.getNodeId());
199 Set<NodeIdType> nodeIdList = new HashSet<>(excludeBldr.getNodeId());
200 nodeIdList.addAll(hard.getNodeId());
201 excludeBldr.setNodeId(new ArrayList<>(nodeIdList));
204 if (hard.getSite() != null && !hard.getSite().isEmpty()) {
205 if (excludeBldr.getSite() == null) {
206 excludeBldr.setSite(hard.getSite());
208 Set<String> siteList = new HashSet<>(excludeBldr.getSite());
209 siteList.addAll(hard.getSite());
210 excludeBldr.setSite(new ArrayList<>(siteList));
213 if (hard.getSrlgId() != null && !hard.getSrlgId().isEmpty()) {
214 if (excludeBldr.getSrlgId() == null) {
215 excludeBldr.setSrlgId(hard.getSrlgId());
217 Set<Uint32> srlgList = new HashSet<>(excludeBldr.getSrlgId());
218 srlgList.addAll(hard.getSrlgId());
219 excludeBldr.setSrlgId(new ArrayList<>(srlgList));
222 if (hard.getSupportingServiceName() != null && !hard.getSupportingServiceName().isEmpty()) {
223 if (excludeBldr.getSupportingServiceName() == null) {
224 excludeBldr.setSupportingServiceName(hard.getSupportingServiceName());
226 Set<String> serviceList = new HashSet<>(excludeBldr.getSupportingServiceName());
227 serviceList.addAll(hard.getSupportingServiceName());
228 excludeBldr.setSupportingServiceName(new ArrayList<>(serviceList));
231 if (hard.getLinkIdentifier() != null && !hard.getLinkIdentifier().isEmpty()) {
232 if (excludeBldr.getLinkIdentifier() == null) {
233 excludeBldr.setLinkIdentifier(hard.getLinkIdentifier());
235 Map<LinkIdentifierKey, LinkIdentifier> linkList = new HashMap<>(excludeBldr.getLinkIdentifier());
236 linkList.putAll(hard.getLinkIdentifier());
237 excludeBldr.setLinkIdentifier(linkList);
240 return excludeBldr.build();
243 private static Diversity updateDiveristy(Diversity hard, Diversity soft) {
244 DiversityBuilder diversityBldr = soft == null ? new DiversityBuilder() : new DiversityBuilder(soft);
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
253 : new HashMap<>(diversityBldr.getServiceIdentifierList());
254 if (!hard.getServiceIdentifierList().isEmpty()) {
255 sil.putAll(hard.getServiceIdentifierList());
256 diversityBldr.setServiceIdentifierList(sil);
258 if (hard.getDiversityType() != null) {
259 diversityBldr.setDiversityType(hard.getDiversityType());
261 return diversityBldr.build();
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();
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();
276 private static HopCount updateHopCount(HopCount hard, HopCount soft) {
278 return new HopCountBuilder(hard).build();
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();
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();
300 HopCountBuilder hcBldr = new HopCountBuilder();
301 if (maxWdmHc != null) {
302 hcBldr.setMaxWdmHopCount(Uint8.valueOf(maxWdmHc));
304 if (maxOtnHc != null) {
305 hcBldr.setMaxOtnHopCount(Uint8.valueOf(maxOtnHc));
307 return hcBldr.build();
310 private static TEMetric updateTEMetric(TEMetric hard, TEMetric soft) {
312 return new TEMetricBuilder(hard).build();
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();
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();
334 TEMetricBuilder temBldr = new TEMetricBuilder();
335 if (maxWdmTem != null) {
336 temBldr.setMaxWdmTEMetric(Uint32.valueOf(maxWdmTem));
338 if (maxOtnTem != null) {
339 temBldr.setMaxOtnTEMetric(Uint32.valueOf(maxOtnTem));
341 return temBldr.build();
344 private static CoRouting updateCoRouting(CoRouting hard, CoRouting soft) {
345 CoRoutingBuilder coRoutingBldr = soft == null ? new CoRoutingBuilder() : new CoRoutingBuilder(soft);
348 ServiceIdentifierListKey,
349 ServiceIdentifierList> serviceIdentifierList = coRoutingBldr.getServiceIdentifierList() == null
351 : new HashMap<>(coRoutingBldr.getServiceIdentifierList());
352 if (!hard.getServiceIdentifierList().isEmpty()) {
353 serviceIdentifierList.putAll(hard.getServiceIdentifierList());
354 coRoutingBldr.setServiceIdentifierList(serviceIdentifierList);
356 return coRoutingBldr.build();
360 * Remove all hard constraints except latency.
361 * @param hardConstraints HardConstarints to be downgraded
362 * @return HardConstraints downgraded
364 public static HardConstraints downgradeHardConstraints(HardConstraints hardConstraints) {
365 HardConstraintsBuilder hardConstraintsBuilder = new HardConstraintsBuilder();
366 if (hardConstraints != null && hardConstraints.getLatency() != null) {
367 hardConstraintsBuilder.setLatency(hardConstraints.getLatency());
369 LOG.warn("latency value not found in HardContraints !");
371 return hardConstraintsBuilder.build();
375 * Convert HardConstraints to SoftConstraints.
376 * @param hardConstraints to be converted.
377 * @return SoftConstraints converted.
379 public static SoftConstraints convertToSoftConstraints(HardConstraints hardConstraints) {
380 return hardConstraints == null
381 ? new SoftConstraintsBuilder().build()
382 : new SoftConstraintsBuilder(hardConstraints).build();