Fix license headers
[ccsdk/sli/adaptors.git] / resource-assignment / provider / src / test / java / jtest / org / openecomp / sdnc / ra / TestIsAvailable.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                      reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package jtest.org.openecomp.sdnc.ra;
23
24 import org.junit.Assert;
25 import org.junit.FixMethodOrder;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.junit.runners.MethodSorters;
29 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
30 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
32 import org.openecomp.sdnc.ra.ResourceAllocator;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.test.context.ContextConfiguration;
37 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
38
39 @RunWith(SpringJUnit4ClassRunner.class)
40 @ContextConfiguration(locations = { "classpath:test-context.xml" })
41 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
42 public class TestIsAvailable {
43
44     private static final Logger log = LoggerFactory.getLogger(TestIsAvailable.class);
45
46     @Autowired(required = true)
47     private ResourceAllocator resourceAllocator;
48
49     @Autowired(required = true)
50     private DataSetup dataSetup;
51
52     @Test
53     public void test001() throws Exception {
54         String t = "001";
55         log.info("============== isAvailable " + t + " ================================");
56         log.info("=== Test successful response - all resources available");
57
58         String service1 = "isAvailable" + t + "/service1";
59
60         dataSetup.cleanup();
61         dataSetup.setupVpePort("MTSNJA4LCP1", "mtanjrsv126", "ae0", "PROV", "juniper-vpe-image");
62         dataSetup.setupVplspePort("MTSNJA4LCP1", "mtsnj303vr1", "xe-0/0/2", "PROV", null);
63
64         SvcLogicContext ctx = new SvcLogicContext();
65         ctx.setAttribute("tmp.resource-allocator.service-instance-id", service1);
66         ctx.setAttribute("tmp.resource-allocator.speed", "300");
67         ctx.setAttribute("tmp.resource-allocator.speed-unit", "Mbps");
68         ctx.setAttribute("tmp.resource-allocator.aic-site-id", "MTSNJA4LCP1");
69
70         QueryStatus st = resourceAllocator.isAvailable("NetworkCapacity", null, null, ctx);
71
72         log.info("Result: " + st);
73
74         Assert.assertTrue(st == QueryStatus.SUCCESS);
75         Assert.assertTrue(dataSetup.serviceNotInDb(service1, null, null));
76     }
77
78     @Test
79     public void test002() throws Exception {
80         String t = "002";
81         log.info("============== isAvailable " + t + " ================================");
82         log.info("=== Test capacity not found - request very big number that is above the limits");
83
84         String service1 = "isAvailable" + t + "/service1";
85
86         dataSetup.cleanup();
87         dataSetup.setupVpePort("MTSNJA4LCP1", "mtanjrsv126", "ae0", "PROV", "juniper-vpe-image");
88         dataSetup.setupVplspePort("MTSNJA4LCP1", "mtsnj303vr1", "xe-0/0/2", "PROV", null);
89
90         SvcLogicContext ctx = new SvcLogicContext();
91         ctx.setAttribute("tmp.resource-allocator.service-instance-id", service1);
92         ctx.setAttribute("tmp.resource-allocator.speed", "300");
93         ctx.setAttribute("tmp.resource-allocator.speed-unit", "Gbps");
94         ctx.setAttribute("tmp.resource-allocator.aic-site-id", "MTSNJA4LCP1");
95
96         QueryStatus st = resourceAllocator.isAvailable("NetworkCapacity", null, null, ctx);
97
98         String maxAvailableSpeed = ctx.getAttribute("tmp.resource-allocator-output.max-available-speed");
99         String speedUnit = ctx.getAttribute("tmp.resource-allocator-output.speed-unit");
100
101         log.info("Result: " + st);
102         log.info("  tmp.resource-allocator-output.max-available-speed: " + maxAvailableSpeed);
103         log.info("  tmp.resource-allocator-output.speed-unit: " + speedUnit);
104
105         Assert.assertTrue(st == QueryStatus.NOT_FOUND);
106         Assert.assertTrue(maxAvailableSpeed.equals("960000"));
107         Assert.assertTrue(speedUnit.equals("kbps"));
108         Assert.assertTrue(dataSetup.serviceNotInDb(service1, null, null));
109     }
110
111     @Test
112     public void test003() throws Exception {
113         String t = "003";
114         log.info("============== isAvailable " + t + " ================================");
115         log.info("=== Test capacity not found - PROV check for VPE");
116
117         String service1 = "isAvailable" + t + "/service1";
118
119         dataSetup.cleanup();
120         dataSetup.setupVpePort("MTSNJA4LCP1", "mtanjrsv126", "ae0", "---", "juniper-vpe-image");
121         dataSetup.setupVplspePort("MTSNJA4LCP1", "mtsnj303vr1", "xe-0/0/2", "PROV", null);
122
123         SvcLogicContext ctx = new SvcLogicContext();
124         ctx.setAttribute("tmp.resource-allocator.service-instance-id", service1);
125         ctx.setAttribute("tmp.resource-allocator.speed", "300");
126         ctx.setAttribute("tmp.resource-allocator.speed-unit", "Mbps");
127         ctx.setAttribute("tmp.resource-allocator.aic-site-id", "MTSNJA4LCP1");
128
129         QueryStatus st = resourceAllocator.isAvailable("NetworkCapacity", null, null, ctx);
130
131         String maxAvailableSpeed = ctx.getAttribute("tmp.resource-allocator-output.max-available-speed");
132         String speedUnit = ctx.getAttribute("tmp.resource-allocator-output.speed-unit");
133
134         log.info("Result: " + st);
135         log.info("  tmp.resource-allocator-output.max-available-speed: " + maxAvailableSpeed);
136         log.info("  tmp.resource-allocator-output.speed-unit: " + speedUnit);
137
138         Assert.assertTrue(st == QueryStatus.NOT_FOUND);
139         Assert.assertTrue(maxAvailableSpeed.equals("0"));
140         Assert.assertTrue(speedUnit.equals("kbps"));
141         Assert.assertTrue(dataSetup.serviceNotInDb(service1, null, null));
142     }
143
144     @Test
145     public void test004() throws Exception {
146         String t = "004";
147         log.info("============== isAvailable " + t + " ================================");
148         log.info("=== Test capacity not found - PROV check for VPLSPE");
149
150         String service1 = "isAvailable" + t + "/service1";
151
152         dataSetup.cleanup();
153         dataSetup.setupVpePort("MTSNJA4LCP1", "mtanjrsv126", "ae0", "PROV", "juniper-vpe-image");
154         dataSetup.setupVplspePort("MTSNJA4LCP1", "mtsnj303vr1", "xe-0/0/2", "---", null);
155
156         SvcLogicContext ctx = new SvcLogicContext();
157         ctx.setAttribute("tmp.resource-allocator.service-instance-id", service1);
158         ctx.setAttribute("tmp.resource-allocator.speed", "300");
159         ctx.setAttribute("tmp.resource-allocator.speed-unit", "Mbps");
160         ctx.setAttribute("tmp.resource-allocator.aic-site-id", "MTSNJA4LCP1");
161
162         QueryStatus st = resourceAllocator.isAvailable("NetworkCapacity", null, null, ctx);
163
164         String maxAvailableSpeed = ctx.getAttribute("tmp.resource-allocator-output.max-available-speed");
165         String speedUnit = ctx.getAttribute("tmp.resource-allocator-output.speed-unit");
166
167         log.info("Result: " + st);
168         log.info("  tmp.resource-allocator-output.max-available-speed: " + maxAvailableSpeed);
169         log.info("  tmp.resource-allocator-output.speed-unit: " + speedUnit);
170
171         Assert.assertTrue(st == QueryStatus.NOT_FOUND);
172         Assert.assertTrue(maxAvailableSpeed.equals("0"));
173         Assert.assertTrue(speedUnit.equals("kbps"));
174         Assert.assertTrue(dataSetup.serviceNotInDb(service1, null, null));
175     }
176
177     @Test
178     public void test005() throws Exception {
179         String t = "005";
180         log.info("============== isAvailable " + t + " ================================");
181         log.info("=== Test capacity not found - VPE not found in DB");
182
183         String service1 = "isAvailable" + t + "/service1";
184
185         dataSetup.cleanup();
186         dataSetup.setupVplspePort("MTSNJA4LCP1", "mtsnj303vr1", "xe-0/0/2", "PROV", null);
187
188         SvcLogicContext ctx = new SvcLogicContext();
189         ctx.setAttribute("tmp.resource-allocator.service-instance-id", service1);
190         ctx.setAttribute("tmp.resource-allocator.speed", "300");
191         ctx.setAttribute("tmp.resource-allocator.speed-unit", "Mbps");
192         ctx.setAttribute("tmp.resource-allocator.aic-site-id", "MTSNJA4LCP1");
193
194         QueryStatus st = resourceAllocator.isAvailable("NetworkCapacity", null, null, ctx);
195
196         String maxAvailableSpeed = ctx.getAttribute("tmp.resource-allocator-output.max-available-speed");
197         String speedUnit = ctx.getAttribute("tmp.resource-allocator-output.speed-unit");
198
199         log.info("Result: " + st);
200         log.info("  tmp.resource-allocator-output.max-available-speed: " + maxAvailableSpeed);
201         log.info("  tmp.resource-allocator-output.speed-unit: " + speedUnit);
202
203         Assert.assertTrue(st == QueryStatus.NOT_FOUND);
204         Assert.assertTrue(maxAvailableSpeed.equals("0"));
205         Assert.assertTrue(speedUnit.equals("kbps"));
206         Assert.assertTrue(dataSetup.serviceNotInDb(service1, null, null));
207     }
208
209     @Test
210     public void test006() throws Exception {
211         String t = "006";
212         log.info("============== isAvailable " + t + " ================================");
213         log.info("=== Test capacity not found - VPLSPE not found in DB");
214
215         String service1 = "isAvailable" + t + "/service1";
216
217         dataSetup.cleanup();
218         dataSetup.setupVpePort("MTSNJA4LCP1", "mtanjrsv126", "ae0", "PROV", "juniper-vpe-image");
219
220         SvcLogicContext ctx = new SvcLogicContext();
221         ctx.setAttribute("tmp.resource-allocator.service-instance-id", service1);
222         ctx.setAttribute("tmp.resource-allocator.speed", "300");
223         ctx.setAttribute("tmp.resource-allocator.speed-unit", "Mbps");
224         ctx.setAttribute("tmp.resource-allocator.aic-site-id", "MTSNJA4LCP1");
225
226         QueryStatus st = resourceAllocator.isAvailable("NetworkCapacity", null, null, ctx);
227
228         String maxAvailableSpeed = ctx.getAttribute("tmp.resource-allocator-output.max-available-speed");
229         String speedUnit = ctx.getAttribute("tmp.resource-allocator-output.speed-unit");
230
231         log.info("Result: " + st);
232         log.info("  tmp.resource-allocator-output.max-available-speed: " + maxAvailableSpeed);
233         log.info("  tmp.resource-allocator-output.speed-unit: " + speedUnit);
234
235         Assert.assertTrue(st == QueryStatus.NOT_FOUND);
236         Assert.assertTrue(maxAvailableSpeed.equals("0"));
237         Assert.assertTrue(speedUnit.equals("kbps"));
238         Assert.assertTrue(dataSetup.serviceNotInDb(service1, null, null));
239     }
240
241     @Test
242     public void test007() throws Exception {
243         String t = "007";
244         log.info("============== isAvailable " + t + " ================================");
245         log.info("=== Test capacity not found - test max available speed calculation");
246
247         String service1 = "isAvailable" + t + "/service1";
248         String existingService1 = "isAvailable" + t + "/existing-service1";
249         String existingService2 = "isAvailable" + t + "/existing-service2";
250
251         dataSetup.cleanup();
252         dataSetup.setupVpePort("MTSNJA4LCP1", "mtanjrsv126", "ae0", "PROV", "juniper-vpe-image");
253         dataSetup.setupVpePort("MTSNJA4LCP1", "mtanjrsv127", "ae0", "PROV", "juniper-vpe-image");
254         dataSetup.setupVplspePort("MTSNJA4LCP1", "mtsnj303vr1", "xe-0/0/2", "PROV", null);
255         dataSetup.setupService(existingService1, "Active", 2, 200000, "mtanjrsv126", "mtsnj303vr1",
256                 "MTSNJA4LCP1/Server1");
257         dataSetup.setupService(existingService2, "Active", 3, 100000, "mtanjrsv127", "mtsnj303vr1",
258                 "MTSNJA4LCP1/Server1");
259         dataSetup.setupService(existingService2, "Pending", 4, 500000, "mtanjrsv127", "mtsnj303vr1",
260                 "MTSNJA4LCP1/Server1");
261
262         SvcLogicContext ctx = new SvcLogicContext();
263         ctx.setAttribute("tmp.resource-allocator.service-instance-id", service1);
264         ctx.setAttribute("tmp.resource-allocator.speed", "300");
265         ctx.setAttribute("tmp.resource-allocator.speed-unit", "Gbps");
266         ctx.setAttribute("tmp.resource-allocator.aic-site-id", "MTSNJA4LCP1");
267
268         QueryStatus st = resourceAllocator.isAvailable("NetworkCapacity", null, null, ctx);
269
270         String maxAvailableSpeed = ctx.getAttribute("tmp.resource-allocator-output.max-available-speed");
271         String speedUnit = ctx.getAttribute("tmp.resource-allocator-output.speed-unit");
272
273         log.info("Result: " + st);
274         log.info("  tmp.resource-allocator-output.max-available-speed: " + maxAvailableSpeed);
275         log.info("  tmp.resource-allocator-output.speed-unit: " + speedUnit);
276
277         Assert.assertTrue(st == QueryStatus.NOT_FOUND);
278         Assert.assertTrue(maxAvailableSpeed.equals("260000"));
279         Assert.assertTrue(speedUnit.equals("kbps"));
280         Assert.assertTrue(dataSetup.serviceNotInDb(service1, null, null));
281     }
282
283     @Test
284     public void test008() throws Exception {
285         String t = "008";
286         log.info("============== isAvailable " + t + " ================================");
287         log.info("=== Test capacity not found - test server limit depending on number of connections");
288
289         String service1 = "isAvailable" + t + "/service1";
290
291         dataSetup.cleanup();
292         dataSetup.setupVpePort("MTSNJA4LCP1", "mtanjrsv126", "ae0", "PROV", "juniper-vpe-image");
293         dataSetup.setupVpePort("MTSNJA4LCP1", "mtanjrsv127", "ae0", "PROV", "juniper-vpe-image");
294         dataSetup.setupVplspePort("MTSNJA4LCP1", "mtsnj303vr1", "xe-0/0/2", "PROV", null);
295         for (int i = 1; i <= 13; i++)
296             dataSetup.setupService("isAvailable" + t + "/existing-service" + i, "Active", 2, 20000, "mtanjrsv126",
297                     "mtsnj303vr1", "MTSNJA4LCP1/Server1");
298
299         SvcLogicContext ctx = new SvcLogicContext();
300         ctx.setAttribute("tmp.resource-allocator.service-instance-id", service1);
301         ctx.setAttribute("tmp.resource-allocator.speed", "300");
302         ctx.setAttribute("tmp.resource-allocator.speed-unit", "Gbps");
303         ctx.setAttribute("tmp.resource-allocator.aic-site-id", "MTSNJA4LCP1");
304
305         QueryStatus st = resourceAllocator.isAvailable("NetworkCapacity", null, null, ctx);
306
307         String maxAvailableSpeed = ctx.getAttribute("tmp.resource-allocator-output.max-available-speed");
308         String speedUnit = ctx.getAttribute("tmp.resource-allocator-output.speed-unit");
309
310         log.info("Result: " + st);
311         log.info("  tmp.resource-allocator-output.max-available-speed: " + maxAvailableSpeed);
312         log.info("  tmp.resource-allocator-output.speed-unit: " + speedUnit);
313
314         Assert.assertTrue(st == QueryStatus.NOT_FOUND);
315         Assert.assertTrue(maxAvailableSpeed.equals("340000"));
316         Assert.assertTrue(speedUnit.equals("kbps"));
317         Assert.assertTrue(dataSetup.serviceNotInDb(service1, null, null));
318     }
319
320     @Test
321     public void test009() throws Exception {
322         String t = "009";
323         log.info("============== isAvailable " + t + " ================================");
324         log.info("=== Test successful response - no service instance id in input - all resources available");
325
326         dataSetup.cleanup();
327         dataSetup.setupVpePort("MTSNJA4LCP1", "mtanjrsv126", "ae0", "PROV", "juniper-vpe-image");
328         dataSetup.setupVplspePort("MTSNJA4LCP1", "mtsnj303vr1", "xe-0/0/2", "PROV", null);
329
330         SvcLogicContext ctx = new SvcLogicContext();
331         ctx.setAttribute("tmp.resource-allocator.speed", "300");
332         ctx.setAttribute("tmp.resource-allocator.speed-unit", "Mbps");
333         ctx.setAttribute("tmp.resource-allocator.aic-site-id", "MTSNJA4LCP1");
334
335         QueryStatus st = resourceAllocator.isAvailable("NetworkCapacity", null, null, ctx);
336
337         log.info("Result: " + st);
338         log.info("  tmp.resource-allocator-output.max-available-speed: " +
339                 ctx.getAttribute("tmp.resource-allocator-output.max-available-speed"));
340         log.info("  tmp.resource-allocator-output.speed-unit: " +
341                 ctx.getAttribute("tmp.resource-allocator-output.speed-unit"));
342
343         Assert.assertTrue(st == QueryStatus.SUCCESS);
344     }
345
346     @Test
347     public void test010() throws Exception {
348         String t = "010";
349         log.info("============== isAvailable " + t + " ================================");
350         log.info("=== Test input validations - no aic-site-id in input");
351
352         SvcLogicContext ctx = new SvcLogicContext();
353         ctx.setAttribute("tmp.resource-allocator.speed", "300");
354         ctx.setAttribute("tmp.resource-allocator.speed-unit", "Gbps");
355
356         try {
357             resourceAllocator.isAvailable("NetworkCapacity", null, null, ctx);
358         } catch (SvcLogicException e) {
359             Assert.assertTrue(e.getMessage().equals(
360                     "tmp.resource-allocator.aic-site-id is required in ResourceAllocator"));
361             return;
362         }
363         Assert.fail("SvcLogicException expected");
364     }
365
366     @Test
367     public void test011() throws Exception {
368         String t = "011";
369         log.info("============== isAvailable " + t + " ================================");
370         log.info("=== Test input validations - no speed in input");
371
372         SvcLogicContext ctx = new SvcLogicContext();
373         ctx.setAttribute("tmp.resource-allocator.speed-unit", "Gbps");
374         ctx.setAttribute("tmp.resource-allocator.aic-site-id", "MTSNJA4LCP1");
375
376         try {
377             resourceAllocator.isAvailable("NetworkCapacity", null, null, ctx);
378         } catch (SvcLogicException e) {
379             Assert.assertTrue(e.getMessage().equals("tmp.resource-allocator.speed is required in ResourceAllocator"));
380             return;
381         }
382         Assert.fail("SvcLogicException expected");
383     }
384
385     @Test
386     public void test012() throws Exception {
387         String t = "012";
388         log.info("============== isAvailable " + t + " ================================");
389         log.info("=== Test input validations - speed not a number in input");
390
391         SvcLogicContext ctx = new SvcLogicContext();
392         ctx.setAttribute("tmp.resource-allocator.speed", "nnnnn");
393         ctx.setAttribute("tmp.resource-allocator.speed-unit", "Gbps");
394         ctx.setAttribute("tmp.resource-allocator.aic-site-id", "MTSNJA4LCP1");
395
396         try {
397             resourceAllocator.isAvailable("NetworkCapacity", null, null, ctx);
398         } catch (SvcLogicException e) {
399             Assert.assertTrue(e.getMessage().equals("Invalid tmp.resource-allocator.speed. Must be a number."));
400             return;
401         }
402         Assert.fail("SvcLogicException expected");
403     }
404
405     @Test
406     public void test013() throws Exception {
407         String t = "013";
408         log.info("============== isAvailable " + t + " ================================");
409         log.info("=== Test input validations - speed-unit missing in input");
410
411         SvcLogicContext ctx = new SvcLogicContext();
412         ctx.setAttribute("tmp.resource-allocator.speed", "300");
413         ctx.setAttribute("tmp.resource-allocator.aic-site-id", "MTSNJA4LCP1");
414
415         try {
416             resourceAllocator.isAvailable("NetworkCapacity", null, null, ctx);
417         } catch (SvcLogicException e) {
418             Assert.assertTrue(e.getMessage().equals(
419                     "tmp.resource-allocator.speed-unit is required in ResourceAllocator"));
420             return;
421         }
422         Assert.fail("SvcLogicException expected");
423     }
424 }