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