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