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.List;
12 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.constraints.CoRoutingOrGeneral;
13 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.constraints.co.routing.or.general.CoRouting;
14 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.constraints.co.routing.or.general.CoRoutingBuilder;
15 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.constraints.co.routing.or.general.General;
16 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.constraints.co.routing.or.general.GeneralBuilder;
17 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.constraints.co.routing.or.general.general.Diversity;
18 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.constraints.co.routing.or.general.general.DiversityBuilder;
19 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.constraints.co.routing.or.general.general.Exclude;
20 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.constraints.co.routing.or.general.general.ExcludeBuilder;
21 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.constraints.co.routing.or.general.general.Include;
22 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.constraints.co.routing.or.general.general.IncludeBuilder;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.constraints.co.routing.or.general.general.Latency;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.routing.constraints.HardConstraints;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.routing.constraints.HardConstraintsBuilder;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.routing.constraints.SoftConstraints;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.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 List<String> hardCustomerCode = hardConstraints.getCustomerCode();
56 if (!hardCustomerCode.isEmpty()) {
57 softConstraintsBuilder.getCustomerCode().addAll(hardCustomerCode);
59 LOG.warn("hard constraints customer code list is empty !");
61 CoRoutingOrGeneral coRoutingOrGeneral = hardConstraints.getCoRoutingOrGeneral();
62 if (coRoutingOrGeneral != null) {
63 if (coRoutingOrGeneral instanceof General) {
64 General hardGeneral = (General) coRoutingOrGeneral;
65 if (softConstraintsBuilder.getCoRoutingOrGeneral() instanceof General) {
66 General softGeneral = (General) softConstraintsBuilder.getCoRoutingOrGeneral();
67 updateGeneral(hardGeneral, softGeneral);
69 softConstraintsBuilder.setCoRoutingOrGeneral(hardGeneral);
71 } else if (coRoutingOrGeneral instanceof CoRouting) {
72 CoRouting hardCoRouting = (CoRouting) coRoutingOrGeneral;
73 if (softConstraintsBuilder.getCoRoutingOrGeneral() instanceof CoRouting) {
74 CoRouting softCoRouting = (CoRouting) softConstraintsBuilder.getCoRoutingOrGeneral();
75 updateCoRouting(hardCoRouting, softCoRouting);
77 softConstraintsBuilder.setCoRoutingOrGeneral(hardCoRouting);
81 LOG.warn("hard constraints CoRoutingOrGeneral is null !");
83 return softConstraintsBuilder.build();
86 private static General updateGeneral(General hard, General soft) {
87 GeneralBuilder result = new GeneralBuilder(soft);
89 result.setDiversity(updateDiveristy(hard.getDiversity(), soft.getDiversity()));
90 result.setExclude(updateExclude(hard.getExclude(), soft.getExclude()));
91 result.setInclude(updateInclude(hard.getInclude(), soft.getInclude()));
92 } catch (NullPointerException e) {
93 LOG.warn("failed to update some 'General' parameters !", e);
95 return result.build();
98 private static Include updateInclude(Include hard, Include soft) {
99 IncludeBuilder result = new IncludeBuilder(soft);
101 result.getFiberBundle().addAll(hard.getFiberBundle());
102 result.getNodeId().addAll(hard.getNodeId());
103 result.getSite().addAll(hard.getSite());
104 result.getSupportingServiceName().addAll(hard.getSupportingServiceName());
106 return result.build();
109 private static Exclude updateExclude(Exclude hard, Exclude soft) {
110 ExcludeBuilder result = new ExcludeBuilder(soft);
112 result.getFiberBundle().addAll(hard.getFiberBundle());
113 result.getNodeId().addAll(hard.getNodeId());
114 result.getSite().addAll(hard.getSite());
115 result.getSupportingServiceName().addAll(hard.getSupportingServiceName());
117 return result.build();
120 private static Diversity updateDiveristy(Diversity hard, Diversity soft) {
121 DiversityBuilder result = new DiversityBuilder(soft);
123 result.getExistingService().addAll(hard.getExistingService());
124 result.setExistingServiceApplicability(hard.getExistingServiceApplicability());
126 return result.build();
129 private static CoRouting updateCoRouting(CoRouting hard, CoRouting soft) {
130 CoRoutingBuilder result = new CoRoutingBuilder(soft);
133 updateCoCoRouting(hard.getCoRouting(), soft.getCoRouting()));
134 } catch (NullPointerException e) {
135 LOG.warn("failed to update some 'CoRouting' parameters !", e);
137 return result.build();
140 private static org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.constraints.co.routing
141 .or.general.co.routing.CoRouting updateCoCoRouting(org.opendaylight.yang.gen.v1.http.org.openroadm.routing
142 .constrains.rev190329.constraints.co.routing.or.general.co.routing.CoRouting hard, org.opendaylight
143 .yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.constraints.co.routing.or.general.co
144 .routing.CoRouting soft) {
145 org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.constraints.co.routing.or.general
146 .co.routing.CoRoutingBuilder result = new org.opendaylight.yang.gen.v1.http.org.openroadm.routing
147 .constrains.rev190329.constraints.co.routing.or.general.co.routing.CoRoutingBuilder(soft);
149 result.getExistingService().addAll(hard.getExistingService());
151 return result.build();
155 * Remove all hard constraints except latency.
157 * @param hardConstraints HardConstarints to be downgraded
158 * @return HardConstraints downgraded
160 public static HardConstraints downgradeHardConstraints(HardConstraints hardConstraints) {
161 HardConstraintsBuilder hardConstraintsBuilder = new HardConstraintsBuilder();
162 CoRoutingOrGeneral coRoutingOrGeneral = hardConstraints.getCoRoutingOrGeneral();
163 Latency latency = null;
164 if (coRoutingOrGeneral instanceof General) {
165 General general = (General) coRoutingOrGeneral;
166 if (general != null) {
167 latency = general.getLatency();
168 if (latency != null) {
169 hardConstraintsBuilder.setCoRoutingOrGeneral(new GeneralBuilder().setLatency(latency).build());
171 LOG.warn("latency value not found in HardContraints !");
175 return hardConstraintsBuilder.build();
179 * Convert HardConstraints to SoftConstraints.
181 * @param hardConstraints to be converted.
182 * @return SoftConstraints converted.
184 public static SoftConstraints convertToSoftConstraints(HardConstraints hardConstraints) {
185 SoftConstraintsBuilder softConstraintsBuilder = new SoftConstraintsBuilder(hardConstraints);
186 return softConstraintsBuilder.build();