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;
11 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.constraints.CoRoutingOrGeneral;
12 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.constraints.co.routing.or.general.CoRouting;
13 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.constraints.co.routing.or.general.CoRoutingBuilder;
14 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.constraints.co.routing.or.general.General;
15 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.constraints.co.routing.or.general.GeneralBuilder;
16 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.constraints.co.routing.or.general.general.Diversity;
17 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.constraints.co.routing.or.general.general.DiversityBuilder;
18 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.constraints.co.routing.or.general.general.Exclude;
19 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.constraints.co.routing.or.general.general.ExcludeBuilder;
20 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.constraints.co.routing.or.general.general.Include;
21 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.constraints.co.routing.or.general.general.IncludeBuilder;
22 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.constraints.co.routing.or.general.general.Latency;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.routing.constraints.HardConstraints;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.routing.constraints.HardConstraintsBuilder;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.routing.constraints.SoftConstraints;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.routing.constraints.SoftConstraintsBuilder;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * Class to Map Hard Constraints to Soft Constraints.
33 * @author Martial Coulibaly ( martial.coulibaly@gfi.com ) on behalf of Orange
36 public final class DowngradeConstraints {
38 private static final Logger LOG = LoggerFactory.getLogger(DowngradeConstraints.class);
40 private DowngradeConstraints() {
44 * Add Hard Constraints to Soft Constraints.
47 * @param hardConstraints to be added
48 * @param softConstraints to be modified
49 * @return SoftConstraints modified
51 public static SoftConstraints updateSoftConstraints(HardConstraints hardConstraints,
52 SoftConstraints softConstraints) {
53 SoftConstraintsBuilder softConstraintsBuilder = new SoftConstraintsBuilder(softConstraints);
54 List<String> hardCustomerCode = hardConstraints.getCustomerCode();
55 if (!hardCustomerCode.isEmpty()) {
56 softConstraintsBuilder.getCustomerCode().addAll(hardCustomerCode);
58 LOG.warn("hard constraints customer code list is empty !");
60 CoRoutingOrGeneral coRoutingOrGeneral = hardConstraints.getCoRoutingOrGeneral();
61 if (coRoutingOrGeneral != null) {
62 if (coRoutingOrGeneral instanceof General) {
63 General hardGeneral = (General) coRoutingOrGeneral;
64 if (softConstraintsBuilder.getCoRoutingOrGeneral() instanceof General) {
65 General softGeneral = (General) softConstraintsBuilder.getCoRoutingOrGeneral();
66 updateGeneral(hardGeneral, softGeneral);
68 softConstraintsBuilder.setCoRoutingOrGeneral(hardGeneral);
70 } else if (coRoutingOrGeneral instanceof CoRouting) {
71 CoRouting hardCoRouting = (CoRouting) coRoutingOrGeneral;
72 if (softConstraintsBuilder.getCoRoutingOrGeneral() instanceof CoRouting) {
73 CoRouting softCoRouting = (CoRouting) softConstraintsBuilder.getCoRoutingOrGeneral();
74 updateCoRouting(hardCoRouting, softCoRouting);
76 softConstraintsBuilder.setCoRoutingOrGeneral(hardCoRouting);
80 LOG.warn("hard constraints CoRoutingOrGeneral is null !");
82 return softConstraintsBuilder.build();
85 private static General updateGeneral(General hard, General soft) {
86 GeneralBuilder result = new GeneralBuilder(soft);
88 result.setDiversity(updateDiveristy(hard.getDiversity(), soft.getDiversity()));
89 result.setExclude(updateExclude(hard.getExclude(), soft.getExclude()));
90 result.setInclude(updateInclude(hard.getInclude(), soft.getInclude()));
91 } catch (NullPointerException e) {
92 LOG.warn("failed to update some 'General' parameters !", e);
94 return result.build();
97 private static Include updateInclude(Include hard, Include soft) {
98 IncludeBuilder result = new IncludeBuilder(soft);
100 result.getFiberBundle().addAll(hard.getFiberBundle());
101 result.getNodeId().addAll(hard.getNodeId());
102 result.getSite().addAll(hard.getSite());
103 result.getSupportingServiceName().addAll(hard.getSupportingServiceName());
105 return result.build();
108 private static Exclude updateExclude(Exclude hard, Exclude soft) {
109 ExcludeBuilder result = new ExcludeBuilder(soft);
111 result.getFiberBundle().addAll(hard.getFiberBundle());
112 result.getNodeId().addAll(hard.getNodeId());
113 result.getSite().addAll(hard.getSite());
114 result.getSupportingServiceName().addAll(hard.getSupportingServiceName());
116 return result.build();
119 private static Diversity updateDiveristy(Diversity hard, Diversity soft) {
120 DiversityBuilder result = new DiversityBuilder(soft);
122 result.getExistingService().addAll(hard.getExistingService());
123 result.setExistingServiceApplicability(hard.getExistingServiceApplicability());
125 return result.build();
128 private static CoRouting updateCoRouting(CoRouting hard, CoRouting soft) {
129 CoRoutingBuilder result = new CoRoutingBuilder(soft);
132 updateCoCoRouting(hard.getCoRouting(), soft.getCoRouting()));
133 } catch (NullPointerException e) {
134 LOG.warn("failed to update some 'CoRouting' parameters !", e);
136 return result.build();
139 private static org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.constraints.co.routing
140 .or.general.co.routing.CoRouting updateCoCoRouting(org.opendaylight.yang.gen.v1.http.org.openroadm.routing
141 .constrains.rev190329.constraints.co.routing.or.general.co.routing.CoRouting hard, org.opendaylight
142 .yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.constraints.co.routing.or.general.co
143 .routing.CoRouting soft) {
144 org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.constraints.co.routing.or.general
145 .co.routing.CoRoutingBuilder result = new org.opendaylight.yang.gen.v1.http.org.openroadm.routing
146 .constrains.rev190329.constraints.co.routing.or.general.co.routing.CoRoutingBuilder(soft);
148 result.getExistingService().addAll(hard.getExistingService());
150 return result.build();
154 * Remove all hard constraints except latency.
156 * @param hardConstraints HardConstarints to be downgraded
157 * @return HardConstraints downgraded
159 public static HardConstraints downgradeHardConstraints(HardConstraints hardConstraints) {
160 HardConstraintsBuilder hardConstraintsBuilder = new HardConstraintsBuilder();
161 CoRoutingOrGeneral coRoutingOrGeneral = hardConstraints.getCoRoutingOrGeneral();
162 Latency latency = null;
163 if (coRoutingOrGeneral instanceof General) {
164 General general = (General) coRoutingOrGeneral;
165 if (general != null) {
166 latency = general.getLatency();
167 if (latency != null) {
168 hardConstraintsBuilder.setCoRoutingOrGeneral(new GeneralBuilder().setLatency(latency).build());
170 LOG.warn("latency value not found in HardContraints !");
174 return hardConstraintsBuilder.build();
178 * Convert HardConstraints to SoftConstraints.
180 * @param hardConstraints to be converted.
181 * @return SoftConstraints converted.
183 public static SoftConstraints convertToSoftConstraints(HardConstraints hardConstraints) {
184 SoftConstraintsBuilder softConstraintsBuilder = new SoftConstraintsBuilder(hardConstraints);
185 return softConstraintsBuilder.build();