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;
12 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.CoRouting;
13 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.CoRoutingBuilder;
14 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.Diversity;
15 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.DiversityBuilder;
16 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.Exclude;
17 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.ExcludeBuilder;
18 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.Include;
19 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.IncludeBuilder;
20 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.Latency;
21 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.LatencyBuilder;
22 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.co.routing.ServiceIdentifierList;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.co.routing.ServiceIdentifierListKey;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.routing.constraints.HardConstraints;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.routing.constraints.HardConstraintsBuilder;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.routing.constraints.SoftConstraints;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.routing.constraints.SoftConstraintsBuilder;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * Class to Map Hard Constraints to Soft Constraints.
34 * @author Martial Coulibaly ( martial.coulibaly@gfi.com ) on behalf of Orange
37 public final class DowngradeConstraints {
39 private static final Logger LOG = LoggerFactory.getLogger(DowngradeConstraints.class);
41 private DowngradeConstraints() {
45 * Add Hard Constraints to Soft Constraints.
48 * @param hardConstraints to be added
49 * @param softConstraints to be modified
50 * @return SoftConstraints modified
52 public static SoftConstraints updateSoftConstraints(HardConstraints hardConstraints,
53 SoftConstraints softConstraints) {
54 SoftConstraintsBuilder softConstraintsBuilder = new SoftConstraintsBuilder(softConstraints);
55 if (hardConstraints.getCustomerCode() != null) {
56 if (!hardConstraints.getCustomerCode().isEmpty()) {
57 softConstraintsBuilder.getCustomerCode().addAll(hardConstraints.getCustomerCode());
60 if (hardConstraints.getOperationalMode() != null) {
61 if (!hardConstraints.getOperationalMode().isEmpty()) {
62 softConstraintsBuilder.getOperationalMode().addAll(hardConstraints.getOperationalMode());
65 if (hardConstraints.getDiversity() != null) {
66 if (softConstraints.getDiversity() != null) {
67 softConstraintsBuilder
68 .setDiversity(updateDiveristy(hardConstraints.getDiversity(), softConstraints.getDiversity()));
71 if (hardConstraints.getExclude() != null) {
72 if (softConstraints.getExclude() != null) {
73 softConstraintsBuilder
74 .setExclude(updateExclude(hardConstraints.getExclude(), softConstraints.getExclude()));
77 if (hardConstraints.getInclude() != null) {
78 if (softConstraints.getInclude() != null) {
79 softConstraintsBuilder
80 .setInclude(updateInclude(hardConstraints.getInclude(), softConstraints.getInclude()));
83 if (hardConstraints.getLatency() != null) {
84 if (softConstraints.getLatency() != null) {
85 softConstraintsBuilder
86 .setLatency(updateLatency(hardConstraints.getLatency(), softConstraints.getLatency()));
89 if (hardConstraints.getCoRouting() != null) {
90 if (softConstraints.getCoRouting() != null) {
91 softConstraintsBuilder
92 .setCoRouting(updateCoRouting(hardConstraints.getCoRouting(), softConstraints.getCoRouting()));
95 return softConstraintsBuilder.build();
98 private static Include updateInclude(Include hard, Include soft) {
99 IncludeBuilder includeBldr = new IncludeBuilder(soft);
100 includeBldr.getFiberBundle().addAll(hard.getFiberBundle());
101 includeBldr.getNodeId().addAll(hard.getNodeId());
102 includeBldr.getSite().addAll(hard.getSite());
103 includeBldr.getSupportingServiceName().addAll(hard.getSupportingServiceName());
104 return includeBldr.build();
107 private static Exclude updateExclude(Exclude hard, Exclude soft) {
108 ExcludeBuilder excludeBldr = new ExcludeBuilder(soft);
109 excludeBldr.getFiberBundle().addAll(hard.getFiberBundle());
110 excludeBldr.getNodeId().addAll(hard.getNodeId());
111 excludeBldr.getSite().addAll(hard.getSite());
112 excludeBldr.getSupportingServiceName().addAll(hard.getSupportingServiceName());
113 return excludeBldr.build();
116 private static Diversity updateDiveristy(Diversity hard, Diversity soft) {
117 DiversityBuilder diversityBldr = new DiversityBuilder(soft);
118 if (!hard.getServiceIdentifierList().isEmpty()) {
119 Map<org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.diversity.existing.service
120 .constraints.ServiceIdentifierListKey,
121 org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.diversity.existing.service
122 .constraints.ServiceIdentifierList> sil = new HashMap<>(diversityBldr.getServiceIdentifierList());
123 sil.putAll(hard.getServiceIdentifierList());
124 diversityBldr.setServiceIdentifierList(sil);
126 return diversityBldr.build();
129 private static Latency updateLatency(Latency hard, Latency soft) {
130 LatencyBuilder latencyBldr = new LatencyBuilder(soft);
131 if (hard.getMaxLatency() != null) {
132 latencyBldr.setMaxLatency(hard.getMaxLatency());
134 return latencyBldr.build();
137 private static CoRouting updateCoRouting(CoRouting hard, CoRouting soft) {
138 CoRoutingBuilder coRoutingBldr = new CoRoutingBuilder(soft);
139 Map<ServiceIdentifierListKey, ServiceIdentifierList> serviceIdentifierList
140 = new HashMap<ServiceIdentifierListKey, ServiceIdentifierList>(coRoutingBldr.getServiceIdentifierList());
141 serviceIdentifierList.putAll(hard.getServiceIdentifierList());
143 .setServiceIdentifierList(serviceIdentifierList)
148 * Remove all hard constraints except latency.
150 * @param hardConstraints HardConstarints to be downgraded
151 * @return HardConstraints downgraded
153 public static HardConstraints downgradeHardConstraints(HardConstraints hardConstraints) {
154 HardConstraintsBuilder hardConstraintsBuilder = new HardConstraintsBuilder();
155 if (hardConstraints != null && hardConstraints.getLatency() != null) {
156 hardConstraintsBuilder.setLatency(hardConstraints.getLatency());
158 LOG.warn("latency value not found in HardContraints !");
160 return hardConstraintsBuilder.build();
164 * Convert HardConstraints to SoftConstraints.
166 * @param hardConstraints to be converted.
167 * @return SoftConstraints converted.
169 public static SoftConstraints convertToSoftConstraints(HardConstraints hardConstraints) {
170 SoftConstraintsBuilder softConstraintsBuilder = new SoftConstraintsBuilder(hardConstraints);
171 return softConstraintsBuilder.build();