Code changes for A&AI add/update
[appc.git] / appc-outbound / appc-aai-client / provider / src / test / java / org / onap / appc / aai / client / node / TestAAIResourceNode.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.node;
26
27 import static org.junit.Assert.*;
28
29 import java.io.File;
30 import java.nio.charset.Charset;
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.Comparator;
34 import java.util.HashMap;
35 import java.util.List;
36 import java.util.Map;
37
38 import org.apache.commons.io.FileUtils;
39 import org.apache.commons.io.IOUtils;
40 import org.junit.Assert;
41 import org.junit.Test;
42 import org.onap.appc.aai.client.AppcAaiClientConstant;
43 import org.onap.appc.aai.client.aai.AaiService;
44 import org.onap.appc.aai.client.aai.TestAaiService;
45
46 import com.att.eelf.configuration.EELFLogger;
47 import com.att.eelf.configuration.EELFManager;
48 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
49
50 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
51 import org.onap.ccsdk.sli.adaptors.aai.AAIClient;
52
53 import com.fasterxml.jackson.databind.ObjectMapper;
54
55
56 public class TestAAIResourceNode {
57
58     //Removed for ONAP integration
59
60     private static final EELFLogger log = EELFManager.getInstance().getLogger(TestAAIResourceNode.class);
61
62     @Test
63     public void sortVServer() throws Exception{
64
65         //log.info("Test");
66
67         ArrayList<Map<String, String>> vservers = new ArrayList<Map<String, String>>();
68         HashMap<String, String> vserverMap = new HashMap<String, String>();
69         vserverMap.put("vserver-id", "vserverId9");
70         vserverMap.put("tenant-id", "tenantId9");
71         vserverMap.put("cloud-owner", "cloudOwner9");
72         vserverMap.put("cloud-region-id", "cloudRegionId9");
73         vserverMap.put("vserver-name", "vServerName9");
74         vservers.add(vserverMap);
75         vserverMap = new HashMap<String, String>();
76         vserverMap.put("vserver-id", "vserverId1");
77         vserverMap.put("tenant-id", "tenantId1");
78         vserverMap.put("cloud-owner", "cloudOwner1");
79         vserverMap.put("cloud-region-id", "cloudRegionId1");
80         vserverMap.put("vserver-name", "vServerName1");
81         vservers.add(vserverMap);
82         vserverMap = new HashMap<String, String>();
83         vserverMap.put("vserver-id", "vserverId3");
84         vserverMap.put("tenant-id", "tenantId3");
85         vserverMap.put("cloud-owner", "cloudOwner3");
86         vserverMap.put("cloud-region-id", "cloudRegionId3");
87         vserverMap.put("vserver-name", "vServerName3");
88         vservers.add(vserverMap);
89         Collections.sort(vservers, new Comparator<Map<String, String>>() {
90             @Override
91             public int compare(Map<String, String> o1, Map<String, String> o2) {
92                 return o1.get("vserver-name").compareTo(o2.get("vserver-name"));
93             }
94         });
95
96         SvcLogicContext ctx = new SvcLogicContext();
97         AAIResourceNode aai = new AAIResourceNode();
98         aai.populateContext(vservers, ctx, "vserver.");
99         log.info(ctx.getAttribute("vserver.vm[0].vserver-name"));
100     }
101
102     @Test
103     public void testAllVServer() throws Exception{
104
105         MockAAIResourceNode mrn = new MockAAIResourceNode();
106         SvcLogicContext ctx = new SvcLogicContext();
107         populateAllVServerInfo(ctx, "tmp.vnfInfo");
108         Map<String, String> inParams  =new HashMap<String, String>();
109         inParams.put("responsePrefix", "tmp.vnfInfo");
110         mrn.getAllVServersVnfcsInfo(inParams, ctx);
111         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm-count"), "2");
112         assertEquals(ctx.getAttribute("tmp.vnfInfo.vnf.vm-count"), "2");
113         assertEquals(ctx.getAttribute("tmp.vnfInfo.vnf.vm-with-no-vnfcs-count"), "0");
114         // VM1
115         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vserver-id"), "ibcsm0002id");
116         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].tenant-id"), "tenantid2");
117         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].cloud-owner"), "cloudOwner2");
118         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].cloud-region-id"), "cloudRegionId2");
119         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vserver-name"), "vserverName2");
120         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vf-module-id"), "vfModule2");
121         //assertNull(ctx.getAttribute("tmp.vnfInfo.vm[0].vnfc-name"));
122
123         // VM2
124         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vserver-id"), "ibcxvm0001id");
125         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].tenant-id"), "tenantid1");
126         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].cloud-owner"), "cloudOwner1");
127         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].cloud-region-id"), "cloudRegionId1");
128         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vserver-name"), "vserverName2");
129         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vf-module-id"), "vfModule2");
130         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vnfc-name"), "vnfcName2");
131         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vnfc-type"), "vnfcType2");
132         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vnfc-function-code"), "vnfcFuncCode2");
133         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].group-notation"), "vnfcGrpNot2");
134
135         ctx.setAttribute("tmp.vnfInfo.vm[0].vserver-id","ibcm0001id");
136         ctx.setAttribute("req-vf-module-id","vfModule1");
137         mrn.getAllVServersVnfcsInfo(inParams, ctx);
138         assertEquals(ctx.getAttribute("tmp.vnfInfo.vnf.vm-with-no-vnfcs-count-vf-module"),"1");
139         assertEquals(ctx.getAttribute("tmp.vnfInfo.vnf.vm-count-for-vf-module"),"1");
140     }
141
142     public void populateAllVServerInfo(SvcLogicContext ctx, String prefix) throws Exception {
143          ctx.setAttribute("vnf-id", "ibcx0001v");
144          ctx.setAttribute("vnf-host-ip-address", "000.00.00.00");
145          ctx.setAttribute(prefix + ".vm-count", "2");
146          ctx.setAttribute(prefix+ ".vm[0].vserver-id", "ibcsm0002id");
147          ctx.setAttribute(prefix+ ".vm[0].tenant-id", "tenantid2");
148          ctx.setAttribute(prefix+ ".vm[0].cloud-owner", "cloudOwner2");
149          ctx.setAttribute(prefix+ ".vm[0].cloud-region-id", "cloudRegionId2");
150          ctx.setAttribute(prefix+ ".vm[1].vserver-id", "ibcxvm0001id");
151          ctx.setAttribute(prefix+ ".vm[1].tenant-id", "tenantid1");
152          ctx.setAttribute(prefix+ ".vm[1].cloud-owner", "cloudOwner1");
153          ctx.setAttribute(prefix+ ".vm[1].cloud-region-id", "cloudRegionId1");
154
155     }
156
157     public static class MockAAIResourceNode extends AAIResourceNode {
158         private static final EELFLogger log = EELFManager.getInstance().getLogger(MockAAIResourceNode.class);
159          private AAIClient aaiClient;
160
161         public AaiService getAaiService() {
162             log.info("In MockAAI");
163             return new MockAaiService(aaiClient);
164         }
165     }
166
167     @Test
168     public void testPopulateContext() throws Exception{
169
170         ArrayList<Map<String, String>> vservers = new ArrayList<Map<String, String>>();
171         HashMap<String, String> vserverMap = new HashMap<String, String>();
172         vserverMap = new HashMap<String, String>();
173         vserverMap.put("vserver-id", "vserverId1");
174         vserverMap.put("tenant-id", "tenantId1");
175         vserverMap.put("cloud-owner", "cloudOwner1");
176         vserverMap.put("cloud-region-id", "cloudRegionId1");
177         vserverMap.put("vserver-name", "vServerName1");
178         vserverMap.put("vnfc-name", "vnfcName1");
179         vservers.add(vserverMap);
180         vserverMap = new HashMap<String, String>();
181         vserverMap.put("vserver-id", "vserverId3");
182         vserverMap.put("tenant-id", "tenantId3");
183         vserverMap.put("cloud-owner", "cloudOwner3");
184         vserverMap.put("cloud-region-id", "cloudRegionId3");
185         vserverMap.put("vserver-name", "vServerName3");
186         vservers.add(vserverMap);
187         vserverMap = new HashMap<String, String>();
188         vserverMap.put("vserver-id", "vserverId9");
189         vserverMap.put("tenant-id", "tenantId9");
190         vserverMap.put("cloud-owner", "cloudOwner9");
191         vserverMap.put("cloud-region-id", "cloudRegionId9");
192         vserverMap.put("vserver-name", "vServerName9");
193         vservers.add(vserverMap);
194         SvcLogicContext ctx = new SvcLogicContext();
195         AAIResourceNode aai = new AAIResourceNode();
196         aai.populateContext(vservers, ctx, "tmp.vnfInfo.");
197         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vserver-name"), "vServerName1");
198         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].tenant-id"), "tenantId1");
199         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].cloud-owner"), "cloudOwner1");
200         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].cloud-region-id"), "cloudRegionId1");
201         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vserver-id"), "vserverId1");
202         assertEquals(ctx.getAttribute("vm-name"), "vServerName3");
203     }
204
205     @Test
206     public final void testGetVnfInfo() {
207         SvcLogicContext ctx = new SvcLogicContext();
208         AAIResourceNode aai = new AAIResourceNode();
209 Map<String, String> inParams  =new HashMap<String, String>();
210         inParams.put("responsePrefix", "tmp.vnfInfo");
211         try {
212             aai.getVnfInfo(inParams, ctx);
213         } catch (SvcLogicException e) {
214             e.printStackTrace();
215         }
216
217     }
218     @Test
219     public final void testaddVnfcs()
220     {
221         SvcLogicContext ctx = new SvcLogicContext();
222         AAIResourceNode aai = new AAIResourceNode();
223 Map<String, String> inParams  =new HashMap<String, String>();
224         inParams.put("responsePrefix", "tmp.vnfInfo");
225         try {
226             aai.addVnfcs(inParams, ctx);
227         } catch (SvcLogicException e) {
228             e.printStackTrace();
229         }
230
231     }
232     @Test
233     public final void  testupdateVnfAndVServerStatus(){
234         SvcLogicContext ctx = new SvcLogicContext();
235         AAIResourceNode aai = new AAIResourceNode();
236 Map<String, String> inParams  =new HashMap<String, String>();
237
238         inParams.put("responsePrefix", "tmp.vnfInfo");
239         try {
240             aai.updateVnfAndVServerStatus(inParams, ctx);
241         } catch (SvcLogicException e) {
242             e.printStackTrace();
243         }
244     }
245
246     @Test
247     public void testgetVfModduleModelInfo() throws Exception{
248         SvcLogicContext ctx = new SvcLogicContext();
249         AAIResourceNode aai = new AAIResourceNode();
250         AAIClient aaic=null;
251         MockAaiService aaiService=new MockAaiService(aaic);
252         Map<String, String> inParams  =new HashMap<String, String>();
253         inParams.put("responsePrefix", "tmp.vnfInfo");
254         aai.processForVfModuleModelInfo(aaiService,inParams, ctx);
255         assertEquals(ctx.getAttribute("template-model-id"),"model0001");
256
257     }
258     @Test
259     public final void testSetVmParams() {
260         SvcLogicContext ctx = new SvcLogicContext();
261         String vServerId = "vserver02";
262         ctx.setAttribute("tmp.vnfInfo.vm-count","3");
263         ctx.setAttribute("tmp.vnfInfo.vm[0].vserver-id","vserver01");
264         ctx.setAttribute("tmp.vnfInfo.vm[1].vserver-id","vserver02");
265         ctx.setAttribute("tmp.vnfInfo.vm[1].tenant-id","ten01");
266         ctx.setAttribute("tmp.vnfInfo.vm[1].cloud-region-id","cr01");
267         ctx.setAttribute("tmp.vnfInfo.vm[1].cloud-owner","co01");
268         AAIResourceNode aairn= new AAIResourceNode();
269         Map <String, String> params  = aairn.setVmParams(ctx, vServerId);
270         assertNotNull(params);
271     }
272     @Test
273     public final void testGetVnfcInformationForVserver() throws Exception{
274         MockAAIResourceNode aairn = new MockAAIResourceNode();
275         SvcLogicContext ctx = new SvcLogicContext();
276         SvcLogicContext newVnfcCtx = new SvcLogicContext();
277         Map<String,String> inParams = new HashMap<String, String>();
278         Map<String,String> vnfcParams = new HashMap<String, String>();
279         String responsePrefix="test.";
280         inParams.put(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX, "test");
281         vnfcParams.put("vnfcName", "vnfcName2");
282         aairn.getVnfcInformationForVserver(vnfcParams, newVnfcCtx, inParams, ctx, aairn.getAaiService(), responsePrefix);
283         assertEquals(ctx.getAttribute("test.vm.vnfc.vnfc-name"),"vnfcName2");
284         assertEquals(ctx.getAttribute("test.vm.vnfc.vnfc-type"), "vnfcType2");
285         assertEquals(ctx.getAttribute("test.vm.vnfc.vnfc-function-code"), "vnfcFuncCode2");
286         assertEquals(ctx.getAttribute("test.vm.vnfc.vnfc-group-notation"), "vnfcGrpNot2");
287     }
288 }