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