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