4b561e3c2f5b138b49e2edea6ac1c55f3fd2dfe3
[transportpce.git] / servicehandler / src / main / java / org / opendaylight / transportpce / servicehandler / MappingConstraints.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
9
10 package org.opendaylight.transportpce.servicehandler;
11
12 import java.util.ArrayList;
13 import java.util.List;
14 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.node.types.rev181130.NodeIdType;
15 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.Constraints;
16 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.constraints.CoRoutingOrGeneral;
17 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.constraints.co.routing.or.general.CoRouting;
18 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.constraints.co.routing.or.general.General;
19 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.constraints.co.routing.or.general.general.Diversity;
20 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.constraints.co.routing.or.general.general.Exclude;
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.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.SoftConstraints;
25 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118.ConstraintsSp;
26 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118.constraints.sp.co.routing.or.general.co.routing.CoRoutingBuilder;
27 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118.constraints.sp.co.routing.or.general.general.DiversityBuilder;
28 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118.constraints.sp.co.routing.or.general.general.ExcludeBuilder;
29 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118.constraints.sp.co.routing.or.general.general.IncludeBuilder;
30 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118.constraints.sp.co.routing.or.general.general.LatencyBuilder;
31 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118.diversity.existing.service.contraints.sp.ExistingServiceApplicabilityBuilder;
32 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118.routing.constraints.sp.HardConstraintsBuilder;
33 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118.routing.constraints.sp.SoftConstraintsBuilder;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * Class for mapping
39  * Hard/soft constraint from Service 1.2
40  * to servicePath 1.4.
41  * @author Martial Coulibaly ( martial.coulibaly@gfi.com ) on behalf of Orange
42  *
43  */
44 public class MappingConstraints {
45     /** Logging. */
46     private static final Logger LOG = LoggerFactory.getLogger(MappingConstraints.class);
47     private HardConstraints serviceHardConstraints;
48     private SoftConstraints serviceSoftConstraints;
49     private org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118.routing
50         .constraints.sp.HardConstraints servicePathHardConstraints;
51     private org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118.routing
52         .constraints.sp.SoftConstraints servicePathSoftConstraints;
53
54     /**
55      * MappingConstraints class constructor
56      * for hard/soft from service 1.2.
57      *
58      * @param hard HardConstraints
59      * @param soft SoftConstraints
60      */
61     public MappingConstraints(HardConstraints hard, SoftConstraints soft) {
62         setServiceHardConstraints(hard);
63         setServiceSoftConstraints(soft);
64     }
65
66     /**
67      * MappingConstraints class constructor
68      * for hard/soft from servicePath 1.4.
69      *
70      * @param hard HardConstraints
71      * @param soft SoftConstraints
72      */
73     public MappingConstraints(
74             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118.routing
75                 .constraints.sp.HardConstraints hard,
76             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118.routing
77                 .constraints.sp.SoftConstraints soft) {
78         setServicePathHardConstraints(hard);
79         setServicePathSoftConstraints(soft);
80     }
81
82     /**
83      *get all constraints informations
84      *from service constraints to map to
85      *servicePath constraints.
86      *
87      * @param input HardConstraints or SoftConstraints
88      * @return ConstraintsSp HardConstraintsSp or HardConstraintsSp
89      */
90     private <T> ConstraintsSp getConstraints(T input) {
91         HardConstraintsBuilder tempHard = new HardConstraintsBuilder();
92         SoftConstraintsBuilder tempSoft = new SoftConstraintsBuilder();
93         if ((input  !=  null) && (input instanceof Constraints)) {
94             updateConstraintBuilders(input, tempHard, tempSoft);
95         }
96         if (input instanceof HardConstraints) {
97             return tempHard.build();
98         } else if (input instanceof SoftConstraints) {
99             return tempSoft.build();
100         } else {
101             return null;
102         }
103     }
104
105     /**
106      * Update hardConstraintsBuilder and softConstraintsBuilder regarding input.
107      * @param <T> T
108      * @param input T
109      * @param hardConstraintsBuilder HardConstraintsBuilder
110      * @param softConstraintsBuilder SoftConstraintsBuilder
111      */
112     private <T> void updateConstraintBuilders(T input,
113             HardConstraintsBuilder hardConstraintsBuilder,
114             SoftConstraintsBuilder softConstraintsBuilder) {
115         Constraints constraints = (Constraints)input;
116         CoRoutingOrGeneral coRoutingOrGeneral = constraints.getCoRoutingOrGeneral();
117         if (coRoutingOrGeneral  !=  null) {
118             if (coRoutingOrGeneral instanceof General) {
119                 updateConstraintBuilders4General(hardConstraintsBuilder, softConstraintsBuilder, constraints);
120             } else if (coRoutingOrGeneral instanceof CoRouting) {
121                 updateConstraintBuilders4Corouting(hardConstraintsBuilder, softConstraintsBuilder, constraints);
122             }
123         }
124     }
125
126     /**
127      * Update hardConstraintsBuilder and softConstraintsBuilder for corouting constraints.
128      * @param hardConstraintsBuilder HardConstraintsBuilder
129      * @param softConstraintsBuilder SoftConstraintsBuilder
130      * @param constraints Constraints
131      */
132     private void updateConstraintBuilders4Corouting(HardConstraintsBuilder hardConstraintsBuilder,
133             SoftConstraintsBuilder softConstraintsBuilder, Constraints constraints) {
134         org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118.constraints.sp
135             .co.routing.or.general.CoRoutingBuilder finalCoRouting =
136             new org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118
137                 .constraints.sp.co.routing.or.general.CoRoutingBuilder();
138         CoRouting tmpCoRouting = (CoRouting)constraints.getCoRoutingOrGeneral();
139         if (tmpCoRouting  !=  null) {
140             org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev190329.constraints.co.routing.or
141                 .general.co.routing.CoRouting tmpCoRoutingCoRouting = tmpCoRouting.getCoRouting();
142             if (tmpCoRoutingCoRouting  !=  null) {
143                 finalCoRouting.setCoRouting(new CoRoutingBuilder()
144                     .setExistingService(tmpCoRoutingCoRouting.getExistingService())
145                     .build());
146             }
147         }
148         hardConstraintsBuilder
149             .setCoRoutingOrGeneral(finalCoRouting.build())
150             .setCustomerCode(constraints.getCustomerCode());
151         softConstraintsBuilder
152             .setCoRoutingOrGeneral(finalCoRouting.build())
153             .setCustomerCode(constraints.getCustomerCode());
154     }
155
156     /**
157      * Update hardConstraintsBuilder and softConstraintsBuilder for general constraints.
158      * @param hardConstraintsBuilder HardConstraintsBuilder
159      * @param softConstraintsBuilder SoftConstraintsBuilder
160      * @param constraints Constraints
161      */
162     private void updateConstraintBuilders4General(HardConstraintsBuilder hardConstraintsBuilder,
163             SoftConstraintsBuilder softConstraintsBuilder, Constraints constraints) {
164         org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118.constraints.sp
165             .co.routing.or.general.GeneralBuilder finalGeneral =
166             new org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118
167                 .constraints.sp.co.routing.or.general.GeneralBuilder();
168         General tmpGeneral = (General) constraints.getCoRoutingOrGeneral();
169         if (tmpGeneral  !=  null) {
170             Diversity tmpDiversity =  tmpGeneral.getDiversity();
171             if (tmpDiversity  !=  null) {
172                 finalGeneral.setDiversity(
173                         new DiversityBuilder()
174                         .setExistingService(tmpDiversity.getExistingService())
175                         .setExistingServiceApplicability(
176                                 new ExistingServiceApplicabilityBuilder()
177                                 .setClli(tmpDiversity.getExistingServiceApplicability().getSite())
178                                 .setNode(tmpDiversity.getExistingServiceApplicability().getNode())
179                                 .setSrlg(tmpDiversity.getExistingServiceApplicability().getSrlg())
180                                 .build())
181                         .build());
182             }
183             Exclude tmpExclude = tmpGeneral.getExclude();
184             if (tmpExclude  !=  null) {
185                 List<String> nodeIdList = new ArrayList<>();
186                 for (NodeIdType nodeId : tmpExclude.getNodeId()) {
187                     nodeIdList.add(nodeId.getValue());
188                 }
189                 finalGeneral.setExclude(
190                         new ExcludeBuilder()
191                         .setSupportingServiceName(tmpExclude.getSupportingServiceName())
192                         .setClli(tmpExclude.getSite())
193                         .setNodeId(nodeIdList)
194                         .build());
195             }
196             Include tmpInclude = tmpGeneral.getInclude();
197             if (tmpInclude  !=  null) {
198                 finalGeneral.setInclude(
199                         new IncludeBuilder()
200                         .build());
201             }
202             Latency tmpLatency = tmpGeneral.getLatency();
203             if (tmpLatency != null) {
204                 finalGeneral.setLatency(
205                         new LatencyBuilder()
206                         .setMaxLatency(tmpLatency.getMaxLatency())
207                         .build());
208             }
209         }
210         hardConstraintsBuilder.setCoRoutingOrGeneral(finalGeneral.build())
211             .setCustomerCode(constraints.getCustomerCode());
212         softConstraintsBuilder.setCoRoutingOrGeneral(finalGeneral.build())
213             .setCustomerCode(constraints.getCustomerCode());
214     }
215
216     /**
217      * map hard/soft constraints from Service 1.2
218      * to ServicePath 1.4.
219      */
220     public void serviceToServicePathConstarints() {
221         LOG.info("Mapping Service Constraints to ServicePath Constraints");
222         if (serviceHardConstraints  !=  null) {
223             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118.routing
224                 .constraints.sp.HardConstraints tempHard =
225                 (org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118.routing
226                     .constraints.sp.HardConstraints) getConstraints(serviceHardConstraints);
227             if (tempHard != null) {
228                 servicePathHardConstraints = tempHard;
229             }
230         } else if (serviceSoftConstraints  !=  null) {
231             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118.routing
232                 .constraints.sp.SoftConstraints tempSoft =
233                 (org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev220118.routing
234                     .constraints.sp.SoftConstraints) getConstraints(serviceSoftConstraints);
235             if (tempSoft != null) {
236                 servicePathSoftConstraints = tempSoft;
237             }
238         }
239     }
240
241
242     public HardConstraints getServiceHardConstraints() {
243         return serviceHardConstraints;
244     }
245
246
247     public void setServiceHardConstraints(HardConstraints serviceHardConstraints) {
248         this.serviceHardConstraints = serviceHardConstraints;
249     }
250
251
252     public SoftConstraints getServiceSoftConstraints() {
253         return serviceSoftConstraints;
254     }
255
256
257     public void setServiceSoftConstraints(SoftConstraints serviceSoftConstraints) {
258         this.serviceSoftConstraints = serviceSoftConstraints;
259     }
260
261
262     public org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface
263             .routing.constraints.rev220118.routing.constraints.sp.HardConstraints getServicePathHardConstraints() {
264         return servicePathHardConstraints;
265     }
266
267
268     public void setServicePathHardConstraints(org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface
269             .routing.constraints.rev220118.routing.constraints.sp.HardConstraints servicePathHardConstraints) {
270         this.servicePathHardConstraints = servicePathHardConstraints;
271     }
272
273
274     public org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface
275             .routing.constraints.rev220118.routing.constraints.sp.SoftConstraints getServicePathSoftConstraints() {
276         return servicePathSoftConstraints;
277     }
278
279
280     public void setServicePathSoftConstraints(org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface
281             .routing.constraints.rev220118.routing.constraints.sp.SoftConstraints servicePathSoftConstraints) {
282         this.servicePathSoftConstraints = servicePathSoftConstraints;
283     }
284
285 }