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