Migrate servicehandler module to JUnit5
[transportpce.git] / servicehandler / src / test / java / org / opendaylight / transportpce / servicehandler / DowngradeConstraintsTest.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;
9
10 import static org.hamcrest.CoreMatchers.is;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
13 import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
14 import static org.junit.jupiter.api.Assertions.assertEquals;
15 import static org.junit.jupiter.api.Assertions.assertNotNull;
16 import static org.junit.jupiter.api.Assertions.assertNull;
17 import static org.junit.jupiter.api.Assertions.assertTrue;
18 import static org.opendaylight.transportpce.servicehandler.utils.ConstraintsUtils.buildHardConstraint;
19 import static org.opendaylight.transportpce.servicehandler.utils.ConstraintsUtils.buildSoftConstraint;
20
21 import java.util.Set;
22 import org.hamcrest.collection.IsMapContaining;
23 import org.junit.jupiter.api.Test;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.node.types.rev210528.NodeIdType;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.DiversityConstraints.DiversityType;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.common.constraints.LinkIdentifierKey;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.diversity.existing.service.constraints.ServiceIdentifierListKey;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.routing.constraints.HardConstraints;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.routing.constraints.HardConstraintsBuilder;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.routing.constraints.SoftConstraints;
31
32 /**
33  * Class to test downgrading and updating Constraints .
34  * @author Ahmed Helmy ( ahmad.helmy@orange.com )
35  * @author Gilles Thouenon (gilles.thouenon@orange.com)
36  */
37 public class DowngradeConstraintsTest {
38
39     @Test
40     void testUpdateSoftConstraintsForCustomerCode() {
41         // test no addition when hard customer-code is null or empty
42         HardConstraints initialHardConstraints =
43             buildHardConstraint(null, false, null, null, null, null, false, false, null, null);
44         SoftConstraints initialSoftConstraints =
45             buildSoftConstraint(null, false, null, null, null, null, false, false, null, null);
46         SoftConstraints generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints(
47             initialHardConstraints, initialSoftConstraints);
48         assertNull(generatedSoftConstraints.getCustomerCode(),
49             "updated soft constraints should contain no customer code");
50         Set<String> softCustomerCode = Set.of("soft-customer-code 3", "soft-customer-code 4");
51         initialSoftConstraints =
52             buildSoftConstraint(softCustomerCode, false, null, null, null, null, false, false, null, null);
53         generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints(
54             initialHardConstraints, initialSoftConstraints);
55         assertEquals(initialSoftConstraints.getCustomerCode(), generatedSoftConstraints.getCustomerCode());
56
57         // test addition of hard customer-code when no soft customer-code
58         Set<String> hardCustomerCode = Set.of("hard-customer-code 1", "hard-customer-code 2");
59         initialHardConstraints =
60             buildHardConstraint(hardCustomerCode, false, null, null, null, null, false, false, null, null);
61         initialSoftConstraints = buildSoftConstraint(null, false, null, null, null, null, false, false, null, null);
62         generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints(
63             initialHardConstraints, initialSoftConstraints);
64         assertEquals(generatedSoftConstraints.getCustomerCode(),initialHardConstraints.getCustomerCode(),
65             "updated soft constraints should contain the customer code of hard constraint");
66         // test addition of hard customer-code when existing soft customer-code
67         initialSoftConstraints =
68             buildSoftConstraint(softCustomerCode, false, null, null, null, null, false, false, null, null);
69         generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints(
70                 initialHardConstraints, initialSoftConstraints);
71         assertThat("updated soft constraints should contain 4 customer code",
72             generatedSoftConstraints.getCustomerCode(), hasSize(4));
73         assertThat(generatedSoftConstraints.getCustomerCode(),
74             containsInAnyOrder("hard-customer-code 1", "hard-customer-code 2", "soft-customer-code 3",
75                 "soft-customer-code 4"));
76     }
77
78     @Test
79     void testUpdateSoftConstraintsForDiversity() {
80         HardConstraints initialHardConstraints =
81             buildHardConstraint(null, false, null, null, null, null, false, false, null, null);
82         SoftConstraints initialSoftConstraints =
83             buildSoftConstraint(null, false, null, null, null, null, false, false, null, null);
84         SoftConstraints generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints(
85             initialHardConstraints, initialSoftConstraints);
86         assertNull(generatedSoftConstraints.getDiversity(),
87             "updated soft constraints should contain no diversity constraint");
88         Set<String> softDiversityServiceid = Set.of("soft-service 3");
89         initialSoftConstraints =
90             buildSoftConstraint(null, false, softDiversityServiceid, null, null, null, false, false, null, null);
91         generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints(
92             initialHardConstraints, initialSoftConstraints);
93         assertEquals(initialSoftConstraints.getDiversity(), generatedSoftConstraints.getDiversity());
94
95         // test addition of hard diversity when no soft diversity
96         Set<String> hardDiversityServiceid = Set.of("hard-service 1", "hard-service 2");
97         initialHardConstraints =
98             buildHardConstraint(null, false, hardDiversityServiceid, null, null, null, false, false, null, null);
99         initialSoftConstraints = buildSoftConstraint(null, false, null, null, null, null, false, false, null, null);
100         generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints(
101             initialHardConstraints, initialSoftConstraints);
102         assertEquals(generatedSoftConstraints.getDiversity(), initialHardConstraints.getDiversity(),
103             "updated soft constraints should contain the diversity of hard constraint");
104
105         // test addition of hard diversity when existing soft diversity
106         initialSoftConstraints =
107             buildSoftConstraint(null, false, softDiversityServiceid, null, null, null, false, false, null, null);
108         generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints(
109             initialHardConstraints, initialSoftConstraints);
110         assertThat("updated soft constraints should contain diversity with 3 services",
111             generatedSoftConstraints.getDiversity().getServiceIdentifierList().size(), is(3));
112         assertEquals(DiversityType.Serial, generatedSoftConstraints.getDiversity().getDiversityType(),
113             "updated soft constraints should have diversity type of serial");
114         assertThat(generatedSoftConstraints.getDiversity().getServiceIdentifierList(),
115             IsMapContaining.hasKey(new ServiceIdentifierListKey("hard-service 1")));
116         assertThat(generatedSoftConstraints.getDiversity().getServiceIdentifierList(),
117             IsMapContaining.hasKey(new ServiceIdentifierListKey("hard-service 2")));
118         assertThat(generatedSoftConstraints.getDiversity().getServiceIdentifierList(),
119             IsMapContaining.hasKey(new ServiceIdentifierListKey("soft-service 3")));
120     }
121
122     @Test
123     void testUpdateSoftConstraintsForExclude() {
124         HardConstraints initialHardConstraints =
125             buildHardConstraint(null, false, null, null, null, null, false, false, null, null);
126         SoftConstraints initialSoftConstraints =
127             buildSoftConstraint(null, false, null, null, null, null, false, false, null, null);
128         SoftConstraints generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints(
129             initialHardConstraints, initialSoftConstraints);
130         assertNull(generatedSoftConstraints.getExclude(),
131             "updated soft constraints should contain no exclude constraint");
132
133         initialSoftConstraints = buildSoftConstraint(null, false, null, "link", null, null, false, false, null, null);
134         generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints(
135             initialHardConstraints, initialSoftConstraints);
136         assertEquals(initialSoftConstraints.getExclude(), generatedSoftConstraints.getExclude(),
137             "updated soft constraints should not be changed");
138
139         // test addition of hard exclude with fiber list when no soft exclude
140         initialHardConstraints =
141             buildHardConstraint(null, false, null, "fiber1", null, null, false, false, null, null);
142         initialSoftConstraints = buildSoftConstraint(null, false, null, null, null, null, false, false, null, null);
143         generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints(
144             initialHardConstraints, initialSoftConstraints);
145         assertEquals(generatedSoftConstraints.getExclude(), initialHardConstraints.getExclude(),
146             "updated soft constraints should contain the exclude constraint of hard constraint");
147
148         // test addition of hard exclude with fiber list when existing soft
149         // exclude with fiber list
150         initialSoftConstraints =
151             buildSoftConstraint(null, false, null, "fiber2", null, null, false, false, null, null);
152         generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints(
153             initialHardConstraints, initialSoftConstraints);
154         assertThat("updated soft constraints should contain exclude with 3 fiber bundles",
155             generatedSoftConstraints.getExclude().getFiberBundle().size(), is(3));
156         assertThat(generatedSoftConstraints.getExclude().getFiberBundle(),
157             containsInAnyOrder("fiber-1", "fiber-2", "fiber-3"));
158
159         // test addition of hard exclude with link list when no soft exclude
160         initialHardConstraints = buildHardConstraint(null, false, null, "link1", null, null, false, false, null, null);
161         initialSoftConstraints = buildSoftConstraint(null, false, null, null, null, null, false, false, null, null);
162         generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints(
163             initialHardConstraints, initialSoftConstraints);
164         assertEquals(generatedSoftConstraints.getExclude(), initialHardConstraints.getExclude(),
165             "updated soft constraints should contain the exclude constraint of hard constraint");
166
167         // test addition of hard exclude with link list when existing soft
168         // exclude with link list
169         initialSoftConstraints = buildSoftConstraint(null, false, null, "link2", null, null, false, false, null, null);
170         generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints(
171             initialHardConstraints, initialSoftConstraints);
172         assertThat("updated soft constraints should contain exclude with 3 links",
173             generatedSoftConstraints.getExclude().getLinkIdentifier().size(), is(3));
174         assertThat(generatedSoftConstraints.getExclude().getLinkIdentifier(),
175             IsMapContaining.hasKey(new LinkIdentifierKey("link-id 1", "openroadm-topology")));
176         assertThat(generatedSoftConstraints.getExclude().getLinkIdentifier(),
177             IsMapContaining.hasKey(new LinkIdentifierKey("link-id 2", "openroadm-topology")));
178         assertThat(generatedSoftConstraints.getExclude().getLinkIdentifier(),
179             IsMapContaining.hasKey(new LinkIdentifierKey("link-id 3", "openroadm-topology")));
180     }
181
182     @Test
183     void testUpdateSoftConstraintsForInclude() {
184         HardConstraints initialHardConstraints =
185             buildHardConstraint(null, false, null, null, null, null, false, false, null, null);
186         SoftConstraints initialSoftConstraints =
187             buildSoftConstraint(null, false, null, null, null, null, false, false, null, null);
188         SoftConstraints generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints(
189             initialHardConstraints, initialSoftConstraints);
190         assertNull(generatedSoftConstraints.getInclude(),
191             "updated soft constraints should contain no include constraint");
192
193         initialSoftConstraints = buildSoftConstraint(null, false, null, null, "link", null, false, false, null, null);
194         generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints(
195             initialHardConstraints, initialSoftConstraints);
196         assertEquals(initialSoftConstraints.getInclude(), generatedSoftConstraints.getInclude(),
197             "updated soft constraints should not be changed");
198
199         // test addition of hard include with fiber list when no soft include
200         initialHardConstraints =
201             buildHardConstraint(null, false, null, null, "fiber1", null, false, false, null, null);
202         initialSoftConstraints = buildSoftConstraint(null, false, null, null, null, null, false, false, null, null);
203         generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints(
204             initialHardConstraints, initialSoftConstraints);
205         assertEquals(generatedSoftConstraints.getInclude(), initialHardConstraints.getInclude(),
206             "updated soft constraints should contain the include constraint of hard constraint");
207
208         // test addition of hard include with fiber list when existing soft
209         // include with fiber list
210         initialSoftConstraints =
211             buildSoftConstraint(null, false, null, null, "fiber2", null, false, false, null, null);
212         generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints(
213             initialHardConstraints, initialSoftConstraints);
214         assertThat("updated soft constraints should contain exclude with 3 fiber bundles",
215             generatedSoftConstraints.getInclude().getFiberBundle().size(), is(3));
216         assertThat(generatedSoftConstraints.getInclude().getFiberBundle(),
217             containsInAnyOrder("fiber-1", "fiber-2", "fiber-3"));
218
219         // test addition of hard include with link list when no soft include
220         initialHardConstraints = buildHardConstraint(null, false, null, null, "link1", null, false, false, null, null);
221         initialSoftConstraints = buildSoftConstraint(null, false, null, null, null, null, false, false, null, null);
222         generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints(
223             initialHardConstraints, initialSoftConstraints);
224         assertEquals(generatedSoftConstraints.getInclude(), initialHardConstraints.getInclude(),
225             "updated soft constraints should contain the include constraint of hard constraint");
226
227         // test addition of hard include with link list when existing soft
228         // include with link list
229         initialSoftConstraints = buildSoftConstraint(null, false, null, null, "link2", null, false, false, null, null);
230         generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints(
231             initialHardConstraints, initialSoftConstraints);
232         assertThat("updated soft constraints should contain include with 3 links",
233             generatedSoftConstraints.getInclude().getLinkIdentifier().size(), is(3));
234         assertThat(generatedSoftConstraints.getInclude().getLinkIdentifier(),
235             IsMapContaining.hasKey(new LinkIdentifierKey("link-id 1", "openroadm-topology")));
236         assertThat(generatedSoftConstraints.getInclude().getLinkIdentifier(),
237             IsMapContaining.hasKey(new LinkIdentifierKey("link-id 2", "openroadm-topology")));
238         assertThat(generatedSoftConstraints.getInclude().getLinkIdentifier(),
239             IsMapContaining.hasKey(new LinkIdentifierKey("link-id 3", "openroadm-topology")));
240     }
241
242     @Test
243     void testUpdateSoftConstraintsForLatency() {
244         HardConstraints initialHardConstraints =
245             buildHardConstraint(null, false, null, null, null, null, false, false, null, null);
246         SoftConstraints initialSoftConstraints =
247             buildSoftConstraint(null, false, null, null, null, null, false, false, null, null);
248         SoftConstraints generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints(
249             initialHardConstraints, initialSoftConstraints);
250         assertNull(generatedSoftConstraints.getLatency(),
251             "updated soft constraints should contain no latency constraint");
252
253         initialSoftConstraints =
254             buildSoftConstraint(null, false, null, null, null, Double.valueOf(12.2), false, false, null, null);
255         generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints(
256             initialHardConstraints, initialSoftConstraints);
257         assertEquals(initialSoftConstraints.getLatency(), generatedSoftConstraints.getLatency(),
258             "updated soft constraints should not be changed");
259         assertEquals((float) 12.2, generatedSoftConstraints.getLatency().getMaxLatency().floatValue(), 0.0f,
260             "updated soft constraints value should be '12.2'");
261
262         // test addition of hard latency when no soft latency
263         initialHardConstraints =
264             buildHardConstraint(null, false, null, null, null, Double.valueOf(16.59), false, false, null, null);
265         initialSoftConstraints = buildSoftConstraint(null, false, null, null, null, null, false, false, null, null);
266         generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints(
267             initialHardConstraints, initialSoftConstraints);
268         assertEquals((float) 16.59, generatedSoftConstraints.getLatency().getMaxLatency().floatValue(), 0.0f,
269             "updated soft constraints value should be '16.59'");
270
271         // test addition of hard latency when existing different soft latency
272         initialSoftConstraints =
273             buildSoftConstraint(null, false, null, null, null, Double.valueOf(12.2), false, false, null, null);
274         generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints(
275             initialHardConstraints, initialSoftConstraints);
276         assertEquals((float) 12.2, generatedSoftConstraints.getLatency().getMaxLatency().floatValue(), 0.0f,
277             "updated soft constraints value should be '12.2'");
278     }
279
280     @Test
281     void testUpdateSoftConstraintsForDistance() {
282         HardConstraints initialHardConstraints =
283             buildHardConstraint(null, false, null, null, null, null, false, false, null, null);
284         SoftConstraints initialSoftConstraints =
285             buildSoftConstraint(null, false, null, null, null, null, false, false, null, null);
286         SoftConstraints generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints(
287             initialHardConstraints, initialSoftConstraints);
288         assertNull(generatedSoftConstraints.getDistance(),
289             "updated soft constraints should contain no distance constraint");
290
291         initialSoftConstraints = buildSoftConstraint(null, false, null, null, null, null, false, false, "750.2", null);
292         generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints(
293             initialHardConstraints, initialSoftConstraints);
294         assertEquals(initialSoftConstraints.getDistance(), generatedSoftConstraints.getDistance(),
295             "updated soft constraints should not be changed");
296         assertEquals((float) 750.2, generatedSoftConstraints.getDistance().getMaxDistance().floatValue(), 0.0f,
297             "updated soft constraints value should be '750.2'");
298
299         // test addition of hard distance when no soft distance
300         initialHardConstraints = buildHardConstraint(null, false, null, null, null, null, false, false, "555.5", null);
301         initialSoftConstraints = buildSoftConstraint(null, false, null, null, null, null, false, false, null, null);
302         generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints(
303             initialHardConstraints, initialSoftConstraints);
304         assertEquals((float) 555.5, generatedSoftConstraints.getDistance().getMaxDistance().floatValue(), 0.0f,
305             "updated soft constraints value should be '555.5'");
306
307         // test addition of hard distance when existing different soft distance
308         initialSoftConstraints = buildSoftConstraint(null, false, null, null, null, null, false, false, "750.2", null);
309         generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints(
310             initialHardConstraints, initialSoftConstraints);
311         assertEquals((float) 555.5, generatedSoftConstraints.getDistance().getMaxDistance().floatValue(), 0.0f,
312             "updated soft constraints value should be '555.5'");
313     }
314
315     @Test
316     void testUpdateSoftConstraintsForHopCountAndTEmetric() {
317         HardConstraints initialHardConstraints =
318             buildHardConstraint(null, false, null, null, null, null, false, false, null, null);
319         SoftConstraints initialSoftConstraints =
320             buildSoftConstraint(null, false, null, null, null, null, false, false, null, null);
321         SoftConstraints generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints(
322             initialHardConstraints, initialSoftConstraints);
323         assertNull(generatedSoftConstraints.getHopCount(),
324             "updated soft constraints should contain no hop-count constraint");
325
326         initialSoftConstraints = buildSoftConstraint(null, false, null, null, null, null, true, true, null, null);
327         generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints(
328             initialHardConstraints, initialSoftConstraints);
329         assertEquals(initialSoftConstraints.getHopCount(), generatedSoftConstraints.getHopCount(),
330             "updated soft constraints should not be changed");
331         assertEquals(3, generatedSoftConstraints.getHopCount().getMaxWdmHopCount().intValue(),
332             "updated soft constraints max-wdm-hop-count should be '3'");
333         assertEquals(5, generatedSoftConstraints.getHopCount().getMaxOtnHopCount().intValue(),
334             "updated soft constraints max-otn-hop-count should be '5'");
335         assertEquals(8, generatedSoftConstraints.getTEMetric().getMaxWdmTEMetric().intValue(),
336             "updated soft constraints max-wdm-TE-metric should be '8'");
337         assertEquals(11, generatedSoftConstraints.getTEMetric().getMaxOtnTEMetric().intValue(),
338             "updated soft constraints max-otn-TE-metric should be '11'");
339
340         // test addition of hard hop-count when no soft hop-count
341         initialHardConstraints = buildHardConstraint(null, false, null, null, null, null, true, true, null, null);
342         initialSoftConstraints = buildSoftConstraint(null, false, null, null, null, null, false, false, null, null);
343         generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints(
344             initialHardConstraints, initialSoftConstraints);
345         assertEquals(initialHardConstraints.getHopCount(), generatedSoftConstraints.getHopCount(),
346             "updated soft constraints should contain hard constraint");
347         assertEquals(3, generatedSoftConstraints.getHopCount().getMaxWdmHopCount().intValue(),
348             "updated soft constraints max-wdm-hop-count should be '3'");
349         assertEquals(5, generatedSoftConstraints.getHopCount().getMaxOtnHopCount().intValue(),
350             "updated soft constraints max-otn-hop-count should be '5'");
351         assertEquals(8, generatedSoftConstraints.getTEMetric().getMaxWdmTEMetric().intValue(),
352             "updated soft constraints max-wdm-TE-metric should be '8'");
353         assertEquals(11, generatedSoftConstraints.getTEMetric().getMaxOtnTEMetric().intValue(),
354             "updated soft constraints max-otn-TE-metric should be '11'");
355
356         // test addition of hard hop-count when existing soft hop-count
357         initialSoftConstraints = buildSoftConstraint(null, false, null, null, null, null, true, true, null, null);
358         generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints(
359             initialHardConstraints, initialSoftConstraints);
360         assertEquals(3, generatedSoftConstraints.getHopCount().getMaxWdmHopCount().intValue(),
361             "updated soft constraints max-wdm-hop-count should be '3'");
362         assertEquals(5, generatedSoftConstraints.getHopCount().getMaxOtnHopCount().intValue(),
363             "updated soft constraints max-otn-hop-count should be '5'");
364         assertEquals(8, generatedSoftConstraints.getTEMetric().getMaxWdmTEMetric().intValue(),
365             "updated soft constraints max-wdm-TE-metric should be '8'");
366         assertEquals(11, generatedSoftConstraints.getTEMetric().getMaxOtnTEMetric().intValue(),
367             "updated soft constraints max-otn-TE-metric should be '11'");
368     }
369
370     @Test
371     void testUpdateSoftConstraintsForCoRouting() {
372         HardConstraints initialHardConstraints =
373             buildHardConstraint(null, false, null, null, null, null, false, false, null, null);
374         SoftConstraints initialSoftConstraints =
375             buildSoftConstraint(null, false, null, null, null, null, false, false, null, null);
376         SoftConstraints generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints(
377             initialHardConstraints, initialSoftConstraints);
378         assertNull(generatedSoftConstraints.getCoRouting(),
379             "updated soft constraints should contain no co-routing constraint");
380
381         initialSoftConstraints =
382             buildSoftConstraint(null, false, null, null, null, null, true, false, null, "coRouting1");
383         generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints(
384             initialHardConstraints, initialSoftConstraints);
385         assertEquals(initialSoftConstraints.getCoRouting(), generatedSoftConstraints.getCoRouting(),
386             "updated soft constraints should not be changed");
387         assertEquals(2, generatedSoftConstraints.getCoRouting().getServiceIdentifierList().size(),
388             "updated soft constraints should contain 2 co-routed services");
389         assertTrue(generatedSoftConstraints.getCoRouting().getServiceIdentifierList()
390             .get(new org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.co
391                     .routing.ServiceIdentifierListKey("service 1"))
392             .getServiceApplicability().getEquipment().getRoadmSrg());
393         assertNull(generatedSoftConstraints.getCoRouting().getServiceIdentifierList()
394             .get(new org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.co
395                         .routing.ServiceIdentifierListKey("service 1"))
396             .getServiceApplicability().getLink());
397         assertNull(generatedSoftConstraints.getCoRouting().getServiceIdentifierList()
398             .get(new org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.co
399                         .routing.ServiceIdentifierListKey("service 2"))
400             .getServiceApplicability().getEquipment());
401         assertTrue(generatedSoftConstraints.getCoRouting().getServiceIdentifierList()
402             .get(new org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.co
403                     .routing.ServiceIdentifierListKey("service 2"))
404             .getServiceApplicability().getLink());
405
406         // test addition of hard co-routing when no soft co-routing
407         initialHardConstraints =
408             buildHardConstraint(null, false, null, null, null, null, true, false, null, "coRouting2");
409         initialSoftConstraints = buildSoftConstraint(null, false, null, null, null, null, false, false, null, null);
410         generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints(
411             initialHardConstraints, initialSoftConstraints);
412         assertEquals(initialHardConstraints.getCoRouting(), generatedSoftConstraints.getCoRouting(),
413             "updated soft constraints should contain hard constraint");
414         assertEquals(1, generatedSoftConstraints.getCoRouting().getServiceIdentifierList().size(),
415             "updated soft constraints should contain 1 co-routed service");
416         assertTrue(generatedSoftConstraints.getCoRouting().getServiceIdentifierList()
417             .get(new org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.co
418                         .routing.ServiceIdentifierListKey("service 3"))
419             .getServiceApplicability().getSite());
420         assertNull(generatedSoftConstraints.getCoRouting().getServiceIdentifierList()
421             .get(new org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.co
422                         .routing.ServiceIdentifierListKey("service 3"))
423             .getServiceApplicability().getLink());
424
425         // test addition of hard hop-count when existing soft hop-count
426         initialSoftConstraints =
427             buildSoftConstraint(null, false, null, null, null, null, true, false, null, "coRouting1");
428         generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints(
429             initialHardConstraints, initialSoftConstraints);
430         assertEquals(3, generatedSoftConstraints.getCoRouting().getServiceIdentifierList().size(),
431             "updated soft constraints should contain 3 co-routed service");
432         assertThat(generatedSoftConstraints.getCoRouting().getServiceIdentifierList(),
433             IsMapContaining.hasKey(
434                 new org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.co
435                         .routing.ServiceIdentifierListKey("service 1")));
436         assertThat(generatedSoftConstraints.getCoRouting().getServiceIdentifierList(),
437             IsMapContaining.hasKey(
438                 new org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.co
439                         .routing.ServiceIdentifierListKey("service 2")));
440         assertThat(generatedSoftConstraints.getCoRouting().getServiceIdentifierList(),
441             IsMapContaining.hasKey(
442                 new org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.co
443                         .routing.ServiceIdentifierListKey("service 3")));
444     }
445
446     @Test
447     void testDowngradeHardConstraints() {
448         HardConstraints initialHardConstraints = null;
449         HardConstraints genHardConstraints = DowngradeConstraints.downgradeHardConstraints(initialHardConstraints);
450         assertNotNull(genHardConstraints, "generated hard-constraints should be empty, not null");
451         initialHardConstraints = new HardConstraintsBuilder().build();
452         genHardConstraints = DowngradeConstraints.downgradeHardConstraints(initialHardConstraints);
453         assertNotNull(genHardConstraints, "generated hard-constraints should be empty, not null");
454         initialHardConstraints = buildHardConstraint(null, false, null, "link1", null, Double.valueOf(12.8), false,
455             false, null, null);
456         genHardConstraints = DowngradeConstraints.downgradeHardConstraints(initialHardConstraints);
457         assertEquals((long) 12.8, genHardConstraints.getLatency().getMaxLatency().longValue(),
458             "Latency value should be 12.8");
459         assertNull(genHardConstraints.getCoRouting(),
460             "generated hard constraints should only contain max-latency value");
461         assertNull(genHardConstraints.getExclude(), "generated hard constraints should only contain max-latency value");
462         assertNull(genHardConstraints.getInclude(), "generated hard constraints should only contain max-latency value");
463     }
464
465     @Test
466     void testConvertToSoftConstraints() {
467         HardConstraints initialHardConstraints = null;
468         SoftConstraints genSoftConstraints = DowngradeConstraints.convertToSoftConstraints(initialHardConstraints);
469         assertNotNull(genSoftConstraints, "generated soft constraints should never be null");
470         assertNull(genSoftConstraints.getExclude(), "generated soft constraints should be empty");
471         assertNull(genSoftConstraints.getCoRouting(), "generated soft constraints should be empty");
472         assertNull(genSoftConstraints.getLatency(), "generated soft constraints should be empty");
473
474         Set<String> hardCustomerCode = Set.of("customer-code 1", "customer-code 2");
475         initialHardConstraints =
476             buildHardConstraint(hardCustomerCode, false, null, "link1", "node", null, false, false, null, null);
477         genSoftConstraints = DowngradeConstraints.convertToSoftConstraints(initialHardConstraints);
478         assertEquals(2, genSoftConstraints.getCustomerCode().size(),
479             "generated soft constraints should contain customer-code items");
480         assertTrue(genSoftConstraints.getCustomerCode().contains("customer-code 1"));
481         assertTrue(genSoftConstraints.getCustomerCode().contains("customer-code 2"));
482         assertNotNull(genSoftConstraints.getExclude(), "generated soft constraints should contain exclude constraint");
483         assertEquals(1, genSoftConstraints.getExclude().getLinkIdentifier().values().size(),
484             "generated soft constraints should contain exclude constraint with one link-id");
485         assertEquals("link-id 1",
486             genSoftConstraints.getExclude().getLinkIdentifier().values().stream().findAny().get().getLinkId());
487         assertEquals("openroadm-topology",
488             genSoftConstraints.getExclude().getLinkIdentifier().values().stream().findAny().get().getLinkNetworkId());
489         assertNotNull(genSoftConstraints.getInclude(), "generated soft constraints should contain include constraint");
490         assertEquals(2, genSoftConstraints.getInclude().getNodeId().size(),
491             "generated soft constraints should contain include constraint with two node-id");
492         assertTrue(genSoftConstraints.getInclude().getNodeId().contains(new NodeIdType("node-id-1")));
493         assertTrue(genSoftConstraints.getInclude().getNodeId().contains(new NodeIdType("node-id-3")));
494         assertNull(genSoftConstraints.getLatency(),
495             "generated soft constraints should not contain any latency constraint");
496         assertNull(genSoftConstraints.getDistance(),
497             "generated soft constraints should not contain any max-distance constraint");
498         assertNull(genSoftConstraints.getCoRouting(),
499             "generated soft constraints should not contain any co-routing constraint");
500     }
501 }