Refactor SH ConstraintsUtils test class
[transportpce.git] / servicehandler / src / test / java / org / opendaylight / transportpce / servicehandler / utils / ConstraintsUtils.java
1 /*
2  * Copyright © 2018 2022 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.utils;
9
10 import java.math.BigDecimal;
11 import java.util.Arrays;
12 import java.util.HashMap;
13 import java.util.List;
14 import java.util.Map;
15 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.node.types.rev210528.NodeIdType;
16 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.DiversityConstraints.DiversityType;
17 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.common.constraints.LinkIdentifier;
18 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.common.constraints.LinkIdentifierBuilder;
19 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.CoRouting;
20 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.CoRoutingBuilder;
21 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.DistanceBuilder;
22 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.DiversityBuilder;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.Exclude;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.ExcludeBuilder;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.HopCountBuilder;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.Include;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.IncludeBuilder;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.LatencyBuilder;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.TEMetricBuilder;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.co.routing.ServiceIdentifierList;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.co.routing.ServiceIdentifierListBuilder;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.diversity.existing.service.constraints.ServiceIdentifierListKey;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.equipment.EquipmentBuilder;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.routing.constraints.HardConstraints;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.routing.constraints.HardConstraintsBuilder;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.routing.constraints.SoftConstraints;
37 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.routing.constraints.SoftConstraintsBuilder;
38 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.service.applicability.g.ServiceApplicabilityBuilder;
39 import org.opendaylight.yangtools.yang.common.Uint32;
40 import org.opendaylight.yangtools.yang.common.Uint8;
41
42 /**
43  * Utility Class to Build Hard Constraints and Soft Constraints.
44  * @author Ahmed Helmy ( ahmad.helmy@orange.com )
45  * @author Gilles Thouenon (gilles.thouenon@orange.com)
46  */
47 public final class ConstraintsUtils {
48
49     private ConstraintsUtils() {
50     }
51
52     /**
53      * Build a rather configurable soft-constraint.
54      * @param  customerCode         set the list of customer-code provided
55      * @param  operationalMode      set list of operational-mode
56      * @param  diversityServiceList set diversity constraints from serviceListId
57      *                              provided
58      * @param  exclude              set exclude constraints
59      * @param  include              set include constraints
60      * @param  maxLatency           set a maxLatency value, null otherwise
61      * @param  hopCount             set a max-wdm-hop-count value
62      * @param  teMetric             set a max-wdm-TE-metric
63      * @param  maxDistance          set a max-distance value, null otherwise
64      * @param  coRoutingServiceId   set co-routing constraints
65      * @return                      the hard-constraints
66      */
67     public static SoftConstraints buildSoftConstraint(List<String> customerCode, boolean operationalMode,
68                                                       List<String> diversityServiceList, String exclude, String include,
69                                                       Double maxLatency, boolean hopCount, boolean teMetric,
70                                                       String maxDistance, String coRoutingServiceId) {
71
72         HardConstraints baseConstraint = buildHardConstraint(customerCode, operationalMode, diversityServiceList,
73             exclude, include, maxLatency, hopCount, teMetric, maxDistance, coRoutingServiceId);
74         return new SoftConstraintsBuilder()
75             .setCustomerCode(baseConstraint.getCustomerCode())
76             .setOperationalMode(baseConstraint.getOperationalMode())
77             .setDiversity(baseConstraint.getDiversity())
78             .setExclude(baseConstraint.getExclude())
79             .setInclude(baseConstraint.getInclude())
80             .setLatency(baseConstraint.getLatency())
81             .setHopCount(baseConstraint.getHopCount())
82             .setTEMetric(baseConstraint.getTEMetric())
83             .setDistance(baseConstraint.getDistance())
84             .setCoRouting(baseConstraint.getCoRouting())
85             .build();
86     }
87
88     /**
89      * Build a rather configurable hard-constraint.
90      * @param  customerCode         set the list of customer-code provided
91      * @param  operationalMode      set list of operational-mode
92      * @param  diversityServiceList set diversity constraints from serviceListId
93      *                              provided
94      * @param  exclude              set exclude constraints
95      * @param  include              set include constraints
96      * @param  maxLatency           set a maxLatency value, null otherwise
97      * @param  hopCount             set a max-wdm-hop-count value
98      * @param  teMetric             set a max-wdm-TE-metric
99      * @param  maxDistance          set a max-distance value, null otherwise
100      * @param  coRoutingServiceId   set co-routing constraints
101      * @return                      the hard-constraints
102      */
103     public static HardConstraints buildHardConstraint(List<String> customerCode, boolean operationalMode,
104                                                       List<String> diversityServiceList, String exclude, String include,
105                                                       Double maxLatency, boolean hopCount, boolean teMetric,
106                                                       String maxDistance, String coRoutingServiceId) {
107
108         Map<ServiceIdentifierListKey,
109             org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.diversity.existing.service
110                     .constraints.ServiceIdentifierList>
111                 serviceIdList = new HashMap<>();
112         if (diversityServiceList != null) {
113             for (String serviceId : diversityServiceList) {
114                 org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.diversity.existing.service
115                         .constraints.ServiceIdentifierList sil = createServiceIdentifierListForDiversity(serviceId);
116                 serviceIdList.put(sil.key(), sil);
117             }
118         }
119
120         Map<String, Exclude> excludeMap = initialiseExcludeMap();
121         Map<String, Include> includeMap = initialiseIncludeMap();
122         Map<String, CoRouting> coRoutingMap = initialiseCoRoutingMap();
123
124         return new HardConstraintsBuilder()
125             .setCustomerCode(customerCode)
126             .setOperationalMode(
127                 operationalMode
128                     ? Arrays.asList("operational-mode 1", "operational-mode 2")
129                     : null)
130             .setDiversity(
131                 serviceIdList.isEmpty()
132                     ? null
133                     : new DiversityBuilder()
134                         .setDiversityType(DiversityType.Serial)
135                         .setServiceIdentifierList(serviceIdList)
136                         .build())
137             .setExclude(
138                 exclude == null || !excludeMap.containsKey(exclude)
139                     ? null
140                     : excludeMap.get(exclude))
141             .setInclude(
142                 include == null || !includeMap.containsKey(include)
143                     ? null
144                     : includeMap.get(include))
145             .setLatency(
146                 maxLatency == null
147                     ? null
148                     : new LatencyBuilder().setMaxLatency(new BigDecimal(maxLatency)).build())
149             .setHopCount(
150                 hopCount
151                     ? new HopCountBuilder()
152                         .setMaxWdmHopCount(Uint8.valueOf(3))
153                         .setMaxOtnHopCount(Uint8.valueOf(5))
154                         .build()
155                     : null)
156             .setTEMetric(
157                 teMetric
158                     ? new TEMetricBuilder()
159                         .setMaxWdmTEMetric(Uint32.valueOf(8))
160                         .setMaxOtnTEMetric(Uint32.valueOf(11))
161                         .build()
162                     : null)
163             .setDistance(
164                 maxDistance == null
165                     ? null
166                     : new DistanceBuilder().setMaxDistance(new BigDecimal(maxDistance)).build())
167             .setCoRouting(
168                 coRoutingServiceId == null || !coRoutingMap.containsKey(coRoutingServiceId)
169                     ? null
170                     : coRoutingMap.get(coRoutingServiceId))
171             .build();
172     }
173
174     private static org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.diversity.existing
175             .service.constraints.ServiceIdentifierList createServiceIdentifierListForDiversity(String serviceId) {
176         return new org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.diversity.existing
177                     .service.constraints.ServiceIdentifierListBuilder()
178             .setServiceIndentifier(serviceId)
179             .setServiceApplicability(new ServiceApplicabilityBuilder()
180                 .setLink(true)
181                 .setNode(true)
182                 .setSite(false)
183                 .setSrlg(false)
184                 .build())
185             .build();
186     }
187
188     private static Map<String, Exclude> initialiseExcludeMap() {
189         HashMap<String, Exclude> excludeHashMap =  new HashMap<>();
190         LinkIdentifier linkId1 = new LinkIdentifierBuilder()
191             .setLinkId("link-id 1")
192             .setLinkNetworkId("openroadm-topology")
193             .build();
194         excludeHashMap.put(
195             "link1",
196             new ExcludeBuilder()
197                 .setLinkIdentifier(Map.of(linkId1.key(), linkId1))
198                 .build());
199         excludeHashMap.put(
200             "node",
201             new ExcludeBuilder()
202                 .setNodeId(List.of(new NodeIdType("node-id-2")))
203                 .build());
204         excludeHashMap.put(
205             "service",
206             new ExcludeBuilder()
207                 .setSupportingServiceName(List.of("supported-service-1", "supported-service-5"))
208                 .build());
209         excludeHashMap.put(
210             "fiber1",
211             new ExcludeBuilder()
212                 .setFiberBundle(List.of("fiber-1", "fiber-2"))
213                 .build());
214         excludeHashMap.put(
215             "fiber2",
216             new ExcludeBuilder()
217                 .setFiberBundle(List.of("fiber-2", "fiber-3"))
218                 .build());
219         LinkIdentifier linkId2 = new LinkIdentifierBuilder()
220             .setLinkId("link-id 2")
221             .setLinkNetworkId("openroadm-topology")
222             .build();
223         LinkIdentifier linkId3 = new LinkIdentifierBuilder()
224             .setLinkId("link-id 3")
225             .setLinkNetworkId("openroadm-topology")
226             .build();
227         excludeHashMap.put(
228             "link2",
229             new ExcludeBuilder()
230                 .setLinkIdentifier(Map.of(linkId2.key(), linkId2, linkId3.key(), linkId3))
231                 .build());
232         return excludeHashMap;
233     }
234
235     private static Map<String, Include> initialiseIncludeMap() {
236         HashMap<String, Include> includeHashMap =  new HashMap<>();
237         LinkIdentifier linkId1 = new LinkIdentifierBuilder()
238             .setLinkId("link-id 1")
239             .setLinkNetworkId("openroadm-topology")
240             .build();
241         includeHashMap.put(
242             "link1",
243             new IncludeBuilder()
244                 .setLinkIdentifier(Map.of(linkId1.key(), linkId1))
245                 .build());
246         includeHashMap.put(
247             "node",
248             new IncludeBuilder()
249                 .setNodeId(List.of(new NodeIdType("node-id-1"), new NodeIdType("node-id-3")))
250                 .build());
251         includeHashMap.put(
252             "service",
253             new IncludeBuilder()
254                 .setSupportingServiceName(List.of("supported-service-1", "supported-service-5"))
255                 .build());
256         includeHashMap.put(
257             "fiber1",
258             new IncludeBuilder()
259                 .setFiberBundle(List.of("fiber-1", "fiber-2"))
260                 .build());
261         includeHashMap.put(
262             "fiber2",
263             new IncludeBuilder()
264                 .setFiberBundle(List.of("fiber-2", "fiber-3"))
265                 .build());
266         LinkIdentifier linkId2 = new LinkIdentifierBuilder()
267             .setLinkId("link-id 2")
268             .setLinkNetworkId("openroadm-topology")
269             .build();
270         LinkIdentifier linkId3 = new LinkIdentifierBuilder()
271             .setLinkId("link-id 3")
272             .setLinkNetworkId("openroadm-topology")
273             .build();
274         includeHashMap.put(
275             "link2",
276             new IncludeBuilder()
277                 .setLinkIdentifier(Map.of(linkId2.key(), linkId2, linkId3.key(), linkId3))
278                 .build());
279         return includeHashMap;
280     }
281
282     private static Map<String, CoRouting> initialiseCoRoutingMap() {
283         ServiceIdentifierList sil1 = new ServiceIdentifierListBuilder()
284             .setServiceIdentifier("service 1")
285             .setServiceApplicability(new ServiceApplicabilityBuilder()
286                 .setEquipment(new EquipmentBuilder()
287                     .setRoadmSrg(true)
288                     .build())
289                 .build())
290             .build();
291         ServiceIdentifierList sil2 = new ServiceIdentifierListBuilder()
292             .setServiceIdentifier("service 2")
293             .setServiceApplicability(new ServiceApplicabilityBuilder()
294                 .setLink(true)
295                 .build())
296             .build();
297         ServiceIdentifierList sil3 = new ServiceIdentifierListBuilder()
298             .setServiceIdentifier("service 3")
299             .setServiceApplicability(new ServiceApplicabilityBuilder()
300                 .setSite(true)
301                 .build())
302             .build();
303         return Map.of(
304             "coRouting1",
305             new CoRoutingBuilder().setServiceIdentifierList(Map.of(sil1.key(), sil1, sil2.key(), sil2)).build(),
306             "coRouting2",
307             new CoRoutingBuilder().setServiceIdentifierList(Map.of(sil3.key(), sil3)).build());
308     }
309 }