717348584a23279079cdce9d762a1a65106e523d
[appc.git] / appc-outbound / appc-aai-client / provider / src / test / java / org / onap / appc / aai / client / aai / TestAaiService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.aai.client.aai;
26
27 import static org.junit.Assert.assertEquals;
28
29 import java.util.HashMap;
30 import java.util.Map;
31
32 import org.apache.commons.lang3.StringUtils;
33 import org.junit.Test;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
35 import org.onap.ccsdk.sli.adaptors.aai.AAIClient;
36
37 import com.att.eelf.configuration.EELFLogger;
38 import com.att.eelf.configuration.EELFManager;
39
40 public class TestAaiService {
41     // ONAP merging
42
43     private static final EELFLogger log = EELFManager.getInstance().getLogger(TestAaiService.class);
44     private AAIClient aaiClient;
45
46     @Test
47     public void testGetGenericVnfInfo() throws Exception {
48
49         MockAaiService mockAai = new MockAaiService(aaiClient);
50
51         Map<String, String> inParams = new HashMap<String, String>();
52         inParams.put("vnfId", "ibcxvm0000");
53         inParams.put("responsePrefix", "tmp.vnfInfo");
54
55         SvcLogicContext ctx = new SvcLogicContext();
56
57         mockAai.getGenericVnfInfo(inParams, ctx);
58
59         assertEquals(ctx.getAttribute("tmp.vnfInfo.vnf.vnf-type"), "vUSP-Metaswitch");
60         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm-count"), "2");
61         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vserver-id"), "ibcx001vm001-id");
62         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vserver-id"), "ibcx000000");
63     }
64
65     @Test
66     public void testGetVmInfo() throws Exception {
67
68         MockAaiService mockAai = new MockAaiService(aaiClient);
69
70         Map<String, String> inParams = new HashMap<String, String>();
71         inParams.put("vserverId", "vserverId1");
72         inParams.put("tenantId", "tenantId1");
73         inParams.put("cloudOwner", "cloudOwner1");
74         inParams.put("cloudRegionId", "cloudRegionId1");
75         inParams.put("responsePrefix", "tmp.vnfInfo");
76
77         SvcLogicContext ctx = new SvcLogicContext();
78
79         mockAai.getVMInfo(inParams, ctx);
80
81         printContext(ctx);
82
83         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm.vserver-name"), "ibcx0000000");
84         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm.vnfc-count"), "1");
85         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm.vf-module-id"), "vfModule1");
86         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm.vnfc[0].vnfc-name"), "ibcx0001vm001vnfc1");
87
88     }
89
90     @Test
91     public void testGetVnfcInfo() throws Exception {
92
93         MockAaiService mockAai = new MockAaiService(aaiClient);
94
95         Map<String, String> inParams = new HashMap<String, String>();
96         inParams.put("vnfcName", "vnfcName1");
97
98         inParams.put("responsePrefix", "tmp.vnfInfo");
99
100         SvcLogicContext ctx = new SvcLogicContext();
101
102         mockAai.getVnfcInfo(inParams, ctx);
103
104         printContext(ctx);
105
106         assertEquals(ctx.getAttribute("tmp.vnfInfo.vnfc.vnfc-type"), null);
107         assertEquals(ctx.getAttribute("tmp.vnfInfo.vnfc.vnfc-function-code"), null);
108         assertEquals(ctx.getAttribute("tmp.vnfInfo.vnfc.group-notation"), "grpnot1");
109
110     }
111
112     @Test
113     public void testGetFirstVnfcNameForVnfcType() throws Exception {
114
115         MockAaiService mockAai = new MockAaiService(aaiClient);
116
117         String prefix = "tmp.vnfInfo.";
118
119         SvcLogicContext ctx = new SvcLogicContext(); // VNFC with specified vnfc
120                                                         // type found
121         mockAai.populateFirstVnfcData(ctx, prefix);
122         String firstVnfcName = mockAai.getFirstVnfcNameForVnfcType(ctx, prefix, "ssc");
123         assertEquals(firstVnfcName, "vnfcname3");
124
125         ctx = new SvcLogicContext(); // no VMS found
126         firstVnfcName = mockAai.getFirstVnfcNameForVnfcType(ctx, prefix, "ssc");
127         assertEquals(firstVnfcName, null);
128
129         ctx = new SvcLogicContext(); // no VMS found with specified type
130         mockAai.populateFirstVnfcData(ctx, prefix);
131         firstVnfcName = mockAai.getFirstVnfcNameForVnfcType(ctx, prefix, "test");
132         assertEquals(firstVnfcName, null);
133
134     }
135
136     @Test
137     public void testGroupNotation() throws Exception {
138
139         MockAaiService mockAai = new MockAaiService(aaiClient);
140
141         String prefix = "tmp.vnfInfo.";
142
143         SvcLogicContext ctx = new SvcLogicContext();
144         mockAai.populateGroupNotation(ctx, prefix);
145
146         // printContext(ctx);
147         String grpNotation = mockAai.getGroupNotationForVServer(ctx, prefix, "ibcxvm0002");
148         assertEquals(grpNotation, "grpNot2");
149
150         ctx = new SvcLogicContext(); // no VMS found
151         grpNotation = mockAai.getGroupNotationForVServer(ctx, prefix, "ibcxvm0002");
152         assertEquals(grpNotation, null);
153
154         ctx = new SvcLogicContext(); // no VMS found with specified name
155         mockAai.populateGroupNotation(ctx, prefix);
156         grpNotation = mockAai.getGroupNotationForVServer(ctx, prefix, "test");
157         assertEquals(grpNotation, null);
158
159     }
160
161     @Test
162     public void testGetGroupNotation() throws Exception {
163
164         MockAaiService mockAai = new MockAaiService(aaiClient);
165
166         String prefix = "tmp.vnfInfo.";
167
168         SvcLogicContext ctx = new SvcLogicContext();
169
170         String grpNotation = mockAai.getGroupNotation("fixed-value", "2", null, null, null, null, null,null, 0);
171         assertEquals(grpNotation, "2");
172
173         mockAai.populateFirstVnfcData(ctx, prefix); // Existing VNFC Found
174         grpNotation = mockAai.getGroupNotation("first-vnfc-name", "2", "currentVnfcName", "currentVServerName", prefix,
175                 ctx, "ssc", null, 0);
176         assertEquals(grpNotation, "vnfcname32");
177
178         ctx = new SvcLogicContext(); // no vnfcs exist in A&AI- Use current
179                                         // vnfcName
180         grpNotation = mockAai.getGroupNotation("first-vnfc-name", "2", "currentVnfcName", "currentVServerName", prefix,
181                 ctx, "ssc", null, 0);
182         assertEquals(grpNotation, "currentVnfcName2");
183
184         ctx = new SvcLogicContext();
185         mockAai.populateGroupNotation(ctx, prefix);
186         grpNotation = mockAai.getGroupNotation("relative-value", "same", "currentVnfcName", "ibcxvm0003", prefix, ctx,
187                 "ssc", null, 0);
188         assertEquals(grpNotation, "grpNot2");
189
190         ctx = new SvcLogicContext();
191         mockAai.populateGroupNotation(ctx, prefix);
192         grpNotation = mockAai.getGroupNotation("relative-value", "next", "currentVnfcName", "ibcxvm0006", prefix, ctx,
193                 "ssc",null,0);
194         assertEquals(grpNotation, "5");
195
196         ctx = new SvcLogicContext();
197         mockAai.populateGroupNotation(ctx, prefix);
198         grpNotation = mockAai.getGroupNotation("relative-value", "next", "currentVnfcName", "ibcxvm0003", prefix, ctx,
199                 "ssc",null,0);
200         assertEquals(grpNotation, null); // Null if grpNotation is not numeric
201
202     }
203
204 @Test
205     public void testInsertVnfcs() throws Exception {
206
207         MockAaiService mockAai = new MockAaiService(aaiClient);
208
209         Map<String, String> inParams = new HashMap<String, String>();
210
211         inParams.put("responsePrefix", "tmp.vnfInfo");
212
213         SvcLogicContext ctx = new SvcLogicContext();
214
215         mockAai.populateVnfcRef(ctx);
216
217         mockAai.populateAllVnfInfo(ctx, "tmp.vnfInfo");
218
219         // mockAai.insertVnfcs(inParams,ctx,2, 2) ;
220     }
221
222     @Test
223     public void testUpdateVServerStatus() throws Exception {
224
225         MockAaiService mockAai = new MockAaiService(aaiClient);
226
227         Map<String, String> inParams = new HashMap<String, String>();
228
229         inParams.put("responsePrefix", "tmp.vnfInfo");
230
231         SvcLogicContext ctx = new SvcLogicContext();
232
233         mockAai.populateAllVnfInfo(ctx, "tmp.vnfInfo");
234
235         mockAai.updateVServerStatus(inParams, ctx, 2);
236     }
237
238     @Test
239     public void testInsertVnfcsForFirstVnfc() throws Exception {
240
241         MockAaiService mockAai = new MockAaiService(aaiClient);
242
243         Map<String, String> inParams = new HashMap<String, String>();
244
245         inParams.put("responsePrefix", "tmp.vnfInfo");
246
247         SvcLogicContext ctx = new SvcLogicContext();
248
249         mockAai.populateVnfcRefFirstVnfcName(ctx);
250
251         mockAai.populateAllVnfInfo1(ctx, "tmp.vnfInfo");
252
253         mockAai.insertVnfcs(inParams, ctx, 2, 2);
254
255
256     }
257     @Test
258     public void testInsertVnfcsForRelativeValueSame() throws Exception {
259
260         MockAaiService mockAai = new MockAaiService(aaiClient);
261
262         Map<String, String> inParams = new HashMap<String, String>();
263
264         inParams.put("responsePrefix", "tmp.vnfInfo");
265
266         SvcLogicContext ctx = new SvcLogicContext();
267
268         mockAai.populateVnfcRefRelValueSame(ctx);
269
270         mockAai.populateAllVnfInfo1(ctx, "tmp.vnfInfo");
271
272         mockAai.insertVnfcs(inParams, ctx, 2, 2);
273
274         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vnfc-name"), "dbjx0001vm001dbj001");
275         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vnfc-function-code"), "dbj");
276         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vnfc-type"), "v-I? - DBJX");
277         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].group-notation"), "1");
278         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vnfc-name"), "dbjx0001vm002dbj001");
279         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vnfc-function-code"), "dbj");
280         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vnfc-type"), "v-I? - DBJX");
281         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].group-notation"), "1");
282     }
283
284     @Test
285     public void testInsertVnfcsForRelativeValueNext() throws Exception {
286
287         MockAaiService mockAai = new MockAaiService(aaiClient);
288
289         Map<String, String> inParams = new HashMap<String, String>();
290
291         inParams.put("responsePrefix", "tmp.vnfInfo");
292
293         SvcLogicContext ctx = new SvcLogicContext();
294
295         mockAai.populateVnfcRefRelValueNext(ctx);
296
297         mockAai.populateAllVnfInfo1(ctx, "tmp.vnfInfo");
298
299         mockAai.insertVnfcs(inParams, ctx, 2, 2);
300
301
302
303         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vnfc-name"), "dbjx0001vm001dbj001");
304         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vnfc-function-code"), "dbj");
305         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vnfc-type"), "v-I? - DBJX");
306         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].group-notation"), "1");
307         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vnfc-name"), "dbjx0001vm002dbj001");
308         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vnfc-function-code"), "dbj");
309         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vnfc-type"), "v-I? - DBJX");
310         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].group-notation"), "2");
311     }
312
313     @Test
314     public void testUpdateVnfStatus() throws Exception {
315
316         MockAaiService mockAai = new MockAaiService(aaiClient);
317
318         Map<String, String> inParams = new HashMap<String, String>();
319
320         inParams.put("responsePrefix", "tmp.vnfInfo");
321
322         SvcLogicContext ctx = new SvcLogicContext();
323
324         mockAai.populateAllVnfInfo(ctx, "tmp.vnfInfo");
325
326         mockAai.updateVnfStatus(inParams, ctx);
327     }
328
329     @Test
330     public void testReadResource() throws Exception {
331
332         MockAaiService mockAai = new MockAaiService(aaiClient);
333         // AaiService mockAai = new AaiService(new AAIClientMock());
334
335         String vnfId = "ibcx0001v";
336         String resourceKey = "generic-vnf.vnf-id = '" + vnfId + "'";
337         String resourceType = "generic-vnf";
338         String queryPrefix = "vnfInfo";
339         SvcLogicContext ctx = mockAai.readResource(resourceKey, queryPrefix, resourceType);
340
341         // System.out.println("VNF TYPE " + queryPrefix + ".vnf.vnf-type");
342
343         assertEquals(ctx.getAttribute("vnfInfo.vnf-type"), "vUSP-Metaswitch");
344
345     }
346     private void printContext(SvcLogicContext ctx) throws Exception {
347         for (String key : ctx.getAttributeKeySet()) {
348             log.info(" KEY " + key);
349             log.info(" VALUE " + ctx.getAttribute(key));
350         }
351     }
352
353     @Test
354     public void testGetGroupNotationForExistingValue() throws Exception {
355
356         SvcLogicContext ctx = new SvcLogicContext();
357         ctx.setAttribute("req-vf-module-id",  "vfmodule01");
358         ctx.setAttribute("tmp.vnfInfo.vm[0].vf-module-id", "vfmodule01");
359         ctx.setAttribute("tmp.vnfInfo.vm[0].vnfc-function-code", "fc1");
360         ctx.setAttribute("tmp.vnfInfo.vm[0].group-notation", "gn1");
361         ctx.setAttribute("tmp.vnfInfo.vm[1].vf-module-id", "vfmodule01");
362         ctx.setAttribute("tmp.vnfInfo.vm[1].vnfc-function-code", "fc1");
363         ctx.setAttribute("tmp.vnfInfo.vm[0].vnfc-function-code", "fc1");
364         ctx.setAttribute("tmp.vnfInfo.vm[0].group-notation", "gn2");
365         MockAaiService aai=new MockAaiService(aaiClient);
366         String groupNotationValue1 = aai.getGroupNotationForExistigValue(ctx, "tmp.vnfInfo", "fc1", 2);
367         assertEquals (groupNotationValue1,null);
368
369         ctx.setAttribute("tmp.vnfInfo.vm[0].group-notation", "gn1");
370         ctx.setAttribute("tmp.vnfInfo.vm[1].group-notation", "gn1");
371         String groupNotationValue2 = aai.getGroupNotationForExistigValue(ctx, "tmp.vnfInfo", "fc1", 2);
372         assertEquals (groupNotationValue2,"gn1");
373
374         ctx.setAttribute("tmp.vnfInfo.vm[2].vf-module-id", "vfmodule01");
375         ctx.setAttribute("tmp.vnfInfo.vm[2].vnfc-function-code", "fc1");
376         ctx.setAttribute("tmp.vnfInfo.vm[2].group-notation", "gn2");
377         String groupNotationValue3 = aai.getGroupNotationForExistigValue(ctx, "tmp.vnfInfo", "fc1", 3);
378         assertEquals (groupNotationValue3,null);
379
380         ctx.setAttribute("tmp.vnfInfo.vm[2].group-notation", "gn1");
381         String groupNotationValue4 = aai.getGroupNotationForExistigValue(ctx, "tmp.vnfInfo", "fc1", 3);
382         assertEquals (groupNotationValue4,"gn1");
383
384     }
385 }