Move otn link update from renderer to SH
[transportpce.git] / servicehandler / src / main / java / org / opendaylight / transportpce / servicehandler / DowngradeConstraints.java
1 /*
2  * Copyright © 2017 Orange, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.transportpce.servicehandler;
9
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;
29
30 /**
31  * Class to Map Hard Constraints to Soft Constraints.
32  *
33  * @author Martial Coulibaly ( martial.coulibaly@gfi.com ) on behalf of Orange
34  *
35  */
36 public final class DowngradeConstraints {
37
38     private static final Logger LOG = LoggerFactory.getLogger(DowngradeConstraints.class);
39
40     private DowngradeConstraints() {
41     }
42
43     /**
44      * Add Hard Constraints to Soft Constraints.
45      *
46      *
47      * @param hardConstraints to be added
48      * @param softConstraints to be modified
49      * @return SoftConstraints modified
50      */
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);
57         } else {
58             LOG.warn("hard constraints customer code list is empty !");
59         }
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);
67                 } else {
68                     softConstraintsBuilder.setCoRoutingOrGeneral(hardGeneral);
69                 }
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);
75                 } else {
76                     softConstraintsBuilder.setCoRoutingOrGeneral(hardCoRouting);
77                 }
78             }
79         } else {
80             LOG.warn("hard constraints CoRoutingOrGeneral is null !");
81         }
82         return softConstraintsBuilder.build();
83     }
84
85     private static General updateGeneral(General hard, General soft) {
86         GeneralBuilder result = new GeneralBuilder(soft);
87         try {
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);
93         }
94         return result.build();
95     }
96
97     private static Include updateInclude(Include hard, Include soft) {
98         IncludeBuilder result = new IncludeBuilder(soft);
99         if (hard != null) {
100             result.getFiberBundle().addAll(hard.getFiberBundle());
101             result.getNodeId().addAll(hard.getNodeId());
102             result.getSite().addAll(hard.getSite());
103             result.getSupportingServiceName().addAll(hard.getSupportingServiceName());
104         }
105         return result.build();
106     }
107
108     private static Exclude updateExclude(Exclude hard, Exclude soft) {
109         ExcludeBuilder result = new ExcludeBuilder(soft);
110         if (hard != null) {
111             result.getFiberBundle().addAll(hard.getFiberBundle());
112             result.getNodeId().addAll(hard.getNodeId());
113             result.getSite().addAll(hard.getSite());
114             result.getSupportingServiceName().addAll(hard.getSupportingServiceName());
115         }
116         return result.build();
117     }
118
119     private static Diversity updateDiveristy(Diversity hard, Diversity soft) {
120         DiversityBuilder result = new DiversityBuilder(soft);
121         if (hard != null) {
122             result.getExistingService().addAll(hard.getExistingService());
123             result.setExistingServiceApplicability(hard.getExistingServiceApplicability());
124         }
125         return result.build();
126     }
127
128     private static CoRouting updateCoRouting(CoRouting hard, CoRouting soft) {
129         CoRoutingBuilder result = new CoRoutingBuilder(soft);
130         try {
131             result.setCoRouting(
132                     updateCoCoRouting(hard.getCoRouting(), soft.getCoRouting()));
133         } catch (NullPointerException e) {
134             LOG.warn("failed to update some 'CoRouting' parameters !", e);
135         }
136         return result.build();
137     }
138
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);
147         if (hard != null) {
148             result.getExistingService().addAll(hard.getExistingService());
149         }
150         return result.build();
151     }
152
153     /**
154      * Remove all hard constraints except latency.
155      *
156      * @param hardConstraints HardConstarints to be downgraded
157      * @return HardConstraints downgraded
158      */
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());
169                 } else {
170                     LOG.warn("latency value not found in HardContraints !");
171                 }
172             }
173         }
174         return hardConstraintsBuilder.build();
175     }
176
177     /**
178      * Convert HardConstraints to SoftConstraints.
179      *
180      * @param hardConstraints to be converted.
181      * @return SoftConstraints converted.
182      */
183     public static SoftConstraints convertToSoftConstraints(HardConstraints hardConstraints) {
184         SoftConstraintsBuilder softConstraintsBuilder = new SoftConstraintsBuilder(hardConstraints);
185         return softConstraintsBuilder.build();
186     }
187 }