Test coverage in aai-client-provider
[appc.git] / appc-outbound / appc-aai-client / provider / src / main / java / org / onap / appc / aai / client / node / AAIResourceNode.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  * Modifications Copyright (C) 2018 IBM.
10  * ================================================================================
11  * Modifications Copyright (C) 2019 Ericsson
12  * =============================================================================
13  * Licensed under the Apache License, Version 2.0 (the "License");
14  * you may not use this file except in compliance with the License.
15  * You may obtain a copy of the License at
16  *
17  *      http://www.apache.org/licenses/LICENSE-2.0
18  *
19  * Unless required by applicable law or agreed to in writing, software
20  * distributed under the License is distributed on an "AS IS" BASIS,
21  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22  * See the License for the specific language governing permissions and
23  * limitations under the License.
24  *
25  * ============LICENSE_END=========================================================
26  */
27
28 package org.onap.appc.aai.client.node;
29
30 import com.att.eelf.configuration.EELFLogger;
31 import com.att.eelf.configuration.EELFManager;
32 import java.util.ArrayList;
33 import java.util.Collections;
34 import java.util.Comparator;
35 import java.util.HashMap;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.Map.Entry;
39 import org.apache.commons.lang3.StringUtils;
40 import org.onap.appc.aai.client.AppcAaiClientConstant;
41 import org.onap.appc.aai.client.aai.AaiService;
42 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
43 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
44 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
45
46 public class AAIResourceNode implements SvcLogicJavaPlugin {
47
48     private static final EELFLogger log = EELFManager.getInstance().getLogger(AAIResourceNode.class);
49
50     private static final String STR_VSERVER_ID = "].vserver-id";
51     private static final String STR_TENANT_ID = "].tenant-id";
52     private static final String STR_CLOUD_OWNER = "].cloud-owner";
53     private static final String STR_CLOUD_REGION_ID = "].cloud-region-id";
54     private static final String STR_VSERVER_SELFLINK ="].vserver-selflink";
55     private static final String STR_TMP_VNF_INFO = "tmp.vnfInfo.vm[";
56
57     private static final String PARAM_VSERVER_ID = "vserverId";
58     private static final String PARAM_TENANT_ID = "tenantId";
59     private static final String PARAM_CLOUD_OWNER = "cloudOwner";
60     private static final String PARAM_CLOUD_REGION_ID = "cloudRegionId";
61     private static final String PARAM_VSERVER_NAME = "vserver-name";
62     private static final String PARAM_VSERVER_SELFLINK = "vserver-selflink";
63     private static final String PARAM_VNFC_NAME = "vnfcName";
64
65     private static final String ATTR_VNF_VM_COUNT = "vnf.vm-count";
66
67     public AaiService getAaiService() {
68         return new AaiService();
69     }
70
71     /* Gets VNF Info and All VServers associated with Vnf */
72     public void getVnfInfo(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
73
74         log.info("Received getVnfInfo call with params : " + inParams);
75
76         String responsePrefix = inParams.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
77
78         try {
79
80             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
81             AaiService aai = getAaiService();
82             aai.getGenericVnfInfo(inParams, ctx);
83
84             String cloudOwnerValue = ctx.getAttribute(responsePrefix + "vm[0].cloud-owner");
85             String cloudRegionValue = ctx.getAttribute(responsePrefix + "vm[0].cloud-region-id");
86
87             log.debug("Cloud Owner" + cloudOwnerValue);
88             log.debug("CloudRegionId" + cloudOwnerValue);
89             Map<String, String> paramsCloud = new HashMap<String, String>();
90             paramsCloud.put(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX,
91                     inParams.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX));
92
93             if(StringUtils.isNotBlank(cloudOwnerValue) && StringUtils.isNotBlank(cloudRegionValue)) {
94
95                 paramsCloud.put(PARAM_CLOUD_OWNER, cloudOwnerValue);
96                 paramsCloud.put(PARAM_CLOUD_REGION_ID, cloudRegionValue);
97
98                 aai.getIdentityUrl(paramsCloud, ctx);
99             }
100
101             ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_STATUS,
102                 AppcAaiClientConstant.OUTPUT_STATUS_SUCCESS);
103             log.info("getVnfInfo Successful ");
104         } catch (Exception e) {
105             ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_STATUS,
106                 AppcAaiClientConstant.OUTPUT_STATUS_FAILURE);
107             ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
108             log.error("Failed in getVnfInfo", e);
109
110             throw new SvcLogicException(e.getMessage());
111         }
112     }
113
114
115     public void getAllVServersVnfcsInfo(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
116
117         log.info("Received getAllVServersVnfcsInfo call with params : " + inParams);
118
119         String responsePrefix = inParams.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
120
121         try {
122             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
123             AaiService aai = getAaiService();
124
125             ArrayList<Map<String, String>> vservers = new ArrayList<>();
126
127             int vmWithNoVnfcsCount = 0;
128             int vmsWithNoVnfcsForVfModule = 0;
129             int vmCountForVfModule = 0;
130             String vmCountStr = ctx.getAttribute(responsePrefix + "vm-count");
131             String vfModuleFromRequest =  ctx.getAttribute("req-vf-module-id");
132             log.info("getAllVServersVnfcsInfo()::: vfMOdule=" + vfModuleFromRequest);
133
134             if (vmCountStr == null) {
135                 throw new ResourceNodeInternalException("Unable to get VServers for the VNF");
136             }
137
138             int vmCount = Integer.parseInt(vmCountStr);
139             for (int i = 0; i < vmCount; i++) {
140
141                 SvcLogicContext vmServerCtx = new SvcLogicContext();
142
143                 Map<String, String> paramsVm = new HashMap<>();
144                 paramsVm.put(PARAM_VSERVER_ID, ctx.getAttribute(responsePrefix + "vm[" + i + STR_VSERVER_ID));
145                 paramsVm.put(PARAM_TENANT_ID, ctx.getAttribute(responsePrefix + "vm[" + i + STR_TENANT_ID));
146                 paramsVm.put(PARAM_CLOUD_OWNER, ctx.getAttribute(responsePrefix + "vm[" + i + STR_CLOUD_OWNER));
147                 paramsVm.put(PARAM_CLOUD_REGION_ID, ctx.getAttribute(responsePrefix + "vm[" + i + STR_CLOUD_REGION_ID));
148                 paramsVm.put(PARAM_VSERVER_SELFLINK, ctx.getAttribute(responsePrefix + "vm[" + i + STR_VSERVER_SELFLINK));
149                 paramsVm.put(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX,
150                     inParams.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX));
151
152                 aai.getVMInfo(paramsVm, vmServerCtx);
153
154                 HashMap<String, String> vserverMap = new HashMap<>();
155                 vserverMap.put("vserver-id", ctx.getAttribute(responsePrefix + "vm[" + i + STR_VSERVER_ID));
156                 vserverMap.put("tenant-id", ctx.getAttribute(responsePrefix + "vm[" + i + STR_TENANT_ID));
157                 vserverMap.put("cloud-owner", ctx.getAttribute(responsePrefix + "vm[" + i + STR_CLOUD_OWNER));
158                 vserverMap.put("cloud-region-id", ctx.getAttribute(responsePrefix + "vm[" + i + STR_CLOUD_REGION_ID));
159
160                 // Parameters returned by getVMInfo
161                 vserverMap.put(PARAM_VSERVER_NAME, vmServerCtx.getAttribute(responsePrefix + "vm.vserver-name"));
162                 vserverMap.put("vf-module-id", vmServerCtx.getAttribute(responsePrefix + "vm.vf-module-id"));
163                 vserverMap.put(PARAM_VSERVER_SELFLINK, vmServerCtx.getAttribute(responsePrefix + "vm.vserver-selflink"));
164
165                 log.info("VSERVER-LINK VALUE:" + vmServerCtx.getAttribute(responsePrefix + "vm.vserver-selflink"));
166
167                 // as Per 17.07 requirements we are supporting only one VNFC per VM.
168
169                 String vnfcName = vmServerCtx.getAttribute(responsePrefix + "vm.vnfc[0].vnfc-name");
170                 vserverMap.put("vnfc-name", vnfcName);
171
172                 String vnfcCount = vmServerCtx.getAttribute(responsePrefix + "vm.vnfc-count");
173                 if (vnfcCount == null) {
174                     vnfcCount = "0";
175                 }
176
177                 vserverMap.put("vnfc-count", vnfcCount);
178                 String vfModuleForVserver = vmServerCtx.getAttribute(responsePrefix + "vm.vf-module-id");
179
180                 if (vnfcName != null) {
181                     Map<String, String> paramsVnfc = new HashMap<String, String>();
182                     paramsVnfc.put(PARAM_VNFC_NAME, vnfcName);
183
184                     paramsVnfc.put(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX,
185                         inParams.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX));
186
187                     SvcLogicContext vnfcCtx = new SvcLogicContext();
188
189                     aai.getVnfcInfo(paramsVnfc, vnfcCtx);
190
191                     vserverMap.put("vnfc-type", vnfcCtx.getAttribute(responsePrefix + "vnfc.vnfc-type"));
192                     vserverMap
193                         .put("vnfc-function-code", vnfcCtx.getAttribute(responsePrefix + "vnfc.vnfc-function-code"));
194                     vserverMap.put("group-notation", vnfcCtx.getAttribute(responsePrefix + "vnfc.group-notation"));
195                     vserverMap.put("vnfc-ipaddress-v4-oam-vip",
196                         vnfcCtx.getAttribute(responsePrefix + "vnfc.ipaddress-v4-oam-vip"));
197
198                 } else {
199                     vmWithNoVnfcsCount++;
200                     //ConfigScaleOut
201                     log.info("getAllVServersVnfcsInfo()::Vf Modules: " + vfModuleForVserver+", " + vfModuleFromRequest);
202                     if (StringUtils.isNotBlank(vfModuleFromRequest) && StringUtils.isNotBlank(vfModuleForVserver) && StringUtils.equalsIgnoreCase(vfModuleForVserver,vfModuleFromRequest)) {
203                         vmsWithNoVnfcsForVfModule++;
204                     }
205                 }
206
207                 if (StringUtils.isNotBlank(vfModuleFromRequest) && StringUtils.isNotBlank(vfModuleForVserver) && StringUtils.equalsIgnoreCase(vfModuleForVserver,vfModuleFromRequest)){
208                     vmCountForVfModule++;
209                 }
210                 vservers.add(vserverMap);
211
212             } // vmCount
213
214             Collections.sort(vservers, Comparator.comparing(o -> o.get(PARAM_VSERVER_NAME)));
215
216             log.info("SORTED VSERVERS " + vservers.toString());
217
218             populateContext(vservers, ctx, responsePrefix);
219
220             log.info("VMCOUNT IN GETALLVSERVERS " + vmCount);
221             log.info("VMSWITHNOVNFCSCOUNT IN GETALLVSERVERS " + vmWithNoVnfcsCount);
222             log.info("VMSWITHNOVNFCSCOUNTFOR VFMODULE IN GETALLVSERVERS " + vmsWithNoVnfcsForVfModule);
223             log.info("VMCOUNT FOR VFMODULE IN GETALLVSERVERS " + vmCountForVfModule);
224             ctx.setAttribute(responsePrefix + ATTR_VNF_VM_COUNT, String.valueOf(vmCount));
225             ctx.setAttribute(responsePrefix + "vnf.vm-with-no-vnfcs-count", String.valueOf(vmWithNoVnfcsCount));
226             ctx.setAttribute(responsePrefix + "vnf.vm-with-no-vnfcs-count-vf-module", String.valueOf(vmsWithNoVnfcsForVfModule));
227             ctx.setAttribute(responsePrefix + "vnf.vm-count-for-vf-module", String.valueOf(vmCountForVfModule));
228
229
230         } catch (Exception e) {
231             ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_STATUS,
232                 AppcAaiClientConstant.OUTPUT_STATUS_FAILURE);
233             ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
234             log.error("Failed in getAllVServersVnfcsInfo", e);
235
236             throw new SvcLogicException(e.getMessage());
237         }
238     }
239
240     public void populateContext(List<Map<String, String>> vservers, SvcLogicContext ctx, String prefix) {
241
242         log.info("Populating Final Context");
243         int counter = 0;
244
245         for (Map<String, String> input : vservers) {
246             for (Entry<String, String> entry : input.entrySet()) {
247
248                 ctx.setAttribute(prefix + "vm[" + counter + "]." + entry.getKey(), entry.getValue());
249                 log.info("Populating Context Key = " + prefix + "vm[" + counter + "]." + entry.getKey() + " Value = " + entry.getValue());
250             }
251             counter++;
252         }
253
254         String firstVServerName = null;
255         for (int i = 0; i < counter; i++) {
256             String vnfcName = ctx.getAttribute(prefix + "vm[" + i + "].vnfc-name");
257             log.info("VNFCNAME " + i + vnfcName);
258             if (vnfcName == null && firstVServerName == null) {
259                 firstVServerName = ctx.getAttribute(prefix + "vm[" + i + "].vserver-name");
260                 ctx.setAttribute("vm-name", firstVServerName);
261                 log.info("Populating Context Key = " + "vm-name" + " Value = " + firstVServerName);
262             }
263         }
264     }
265
266
267     public void addVnfcs(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
268
269         log.info("Received addVnfcs call with params : " + inParams);
270
271         String responsePrefix = inParams.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
272
273         int vnfcRefLen;
274         int vmCount;
275         int vmWithNoVnfcCount = 0;
276
277         try {
278
279             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
280             AaiService aai = getAaiService();
281
282             //no:of vnfcs from the vnfc_reference table
283             String vnfcRefLenStr = ctx.getAttribute("vnfcReference_length");
284
285             vnfcRefLen = trySetVnfcRefLen(vnfcRefLenStr);
286
287             //Vms without vnfc from A&AI
288             String vmWithNoVnfcCountStr = ctx.getAttribute(responsePrefix + "vnf.vm-with-no-vnfcs-count");
289
290             // Modified for 1710
291             if (vmWithNoVnfcCountStr == null) {
292                 log.info("Parameter VM without VNFCs(vmWithNoVnfcCountStr) from A&AI is Null");
293             } else {
294                 vmWithNoVnfcCount = Integer.parseInt(vmWithNoVnfcCountStr);
295             }
296
297             log.info("No of VM without VNFCs(vmWithNoVnfcCount) from A&AI is " + vmWithNoVnfcCount);
298
299             String vmCountStr = ctx.getAttribute(responsePrefix + ATTR_VNF_VM_COUNT);
300
301             if (vmCountStr == null) {
302                 throw new ResourceNodeInternalException("VM data from A&AI is missing");
303             } else {
304                 vmCount = Integer.parseInt(vmCountStr);
305             }
306             String vfModuleIdFromRequest = ctx.getAttribute("req-vf-module-id");
307             if ((vmCount < vnfcRefLen) &&  StringUtils.isBlank(vfModuleIdFromRequest)) {
308                 throw new ResourceNodeInternalException("Vnfc and VM count mismatch");
309             }
310
311             //ConfigScaleOut
312             if (StringUtils.isNotBlank(vfModuleIdFromRequest)) {
313                 processCheckForVfModule(vfModuleIdFromRequest, ctx, responsePrefix,vnfcRefLen);
314             }
315
316             log.info("VMCOUNT " + vmCount);
317             log.info("VNFCREFLEN " + vnfcRefLen);
318
319
320             if (StringUtils.isBlank(ctx.getAttribute("vnfc-type"))) {
321                 aai.updateVnfStatusWithOAMAddress(inParams, ctx);
322             }
323
324             aai.insertVnfcs(inParams, ctx, vnfcRefLen, vmCount,vfModuleIdFromRequest);
325
326             ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_STATUS,
327                 AppcAaiClientConstant.OUTPUT_STATUS_SUCCESS);
328
329             log.info("addVnfcs Successful ");
330         } catch (Exception e) {
331             ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_STATUS,
332                 AppcAaiClientConstant.OUTPUT_STATUS_FAILURE);
333             ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
334             log.error("Failed in addVnfcs", e);
335
336             throw new SvcLogicException(e.getMessage());
337         }
338     }
339
340     public void processCheckForVfModule(String vfModuleIdFromRequest, SvcLogicContext ctx,
341     String responsePrefix, int vnfcRefLen) throws ResourceNodeInternalException {
342
343         log.info("processCheckForVfModule()::vfModuleId From Request"+vfModuleIdFromRequest+"-"+vnfcRefLen);
344         int vmsWithoutVnfcsForVfModule = 0;
345         String vmsWithoutVnfcsForVfModuleStr = ctx.getAttribute(responsePrefix + "vnf.vm-with-no-vnfcs-count-vf-module");
346         if (StringUtils.isBlank(vmsWithoutVnfcsForVfModuleStr) && StringUtils.isNotBlank(vfModuleIdFromRequest)) {
347             log.info("addVnfcs()::No vmsWithoutVnfcsForVfModule (is null) for vfmodule=" + vfModuleIdFromRequest);
348         }
349         else {
350             vmsWithoutVnfcsForVfModule = Integer.parseInt(vmsWithoutVnfcsForVfModuleStr);
351         }
352         log.info("addVnfcs():::Number of VMs without vnfcs for vfmodule: " + vmsWithoutVnfcsForVfModule);
353         String vmsForVfModuleStr = ctx.getAttribute(responsePrefix + "vnf.vm-count-for-vf-module");
354         int vmsForVfModule = 0;
355         if (StringUtils.isNotBlank(vmsForVfModuleStr)) {
356             vmsForVfModule = Integer.parseInt(vmsForVfModuleStr);
357         }
358         if ((vmsForVfModule != vnfcRefLen ) &&  StringUtils.isNotBlank(vfModuleIdFromRequest)) {
359             throw new ResourceNodeInternalException("Vnfc and VM count mismatch for vfModule in request=" + vfModuleIdFromRequest);
360         }
361         log.info("processCheckForVfModule()::vmsForVfModule " + vmsForVfModule);
362
363     }
364
365     private int trySetVnfcRefLen(String vnfcRefLenStr) throws ResourceNodeInternalException {
366
367         if (vnfcRefLenStr == null) {
368             log.info("Vnfc Reference data is missing");
369             throw new ResourceNodeInternalException("Vnfc Reference data is missing");
370
371         } else {
372             return Integer.parseInt(vnfcRefLenStr);
373         }
374     }
375
376
377     public void updateVnfAndVServerStatus(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
378
379         log.info("Received updateVnfAndVServerStatus call with params : " + inParams);
380
381         String responsePrefix = inParams.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
382
383         int vmCount;
384
385         try {
386
387             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
388             AaiService aai = getAaiService();
389
390             String vmCountStr = ctx.getAttribute(responsePrefix + ATTR_VNF_VM_COUNT);
391
392             if (vmCountStr == null) {
393                 throw new ResourceNodeInternalException("VM data from A&AI is missing");
394             } else {
395                 vmCount = Integer.parseInt(vmCountStr);
396             }
397
398             log.info("VMCOUNT " + vmCount);
399
400             aai.updateVnfStatus(inParams, ctx);
401             aai.updateVServerStatus(inParams, ctx, vmCount);
402
403             ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_STATUS,
404                 AppcAaiClientConstant.OUTPUT_STATUS_SUCCESS);
405
406             log.info("updateVnfAndVServerStatus Successful ");
407         } catch (Exception e) {
408             ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_STATUS,
409                 AppcAaiClientConstant.OUTPUT_STATUS_FAILURE);
410             ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
411             log.error("Failed in updateVnfAndVServerStatus", e);
412
413             throw new SvcLogicException(e.getMessage());
414         }
415     }
416
417     public void getVserverInfo(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
418         log.info("getVserverInfo()::Retrieving vm and vnfc information for vserver:" + inParams.toString());
419         String responsePrefix = inParams.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
420         try {
421             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
422             AaiService aaiService = getAaiService();
423             String vServerId = inParams.get(PARAM_VSERVER_ID);
424             Map<String, String> params = setVmParams(ctx, vServerId);
425             Map<String, String> vnfcParams = new HashMap<>();
426             if (null == params) {
427                 log.error("getVserverInfo()::No Vm Info found!!");
428                 throw new SvcLogicException("No Vm Info in Context");
429             }
430             params.put(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX,
431                 inParams.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX));
432             SvcLogicContext newVmCtx = new SvcLogicContext();
433             aaiService.getVMInfo(params, newVmCtx);
434
435             String vnfcName = newVmCtx.getAttribute(responsePrefix + "vm.vnfc[0].vnfc-name");
436             log.info("getVnfcFunctionCodeForVserver()::vnfcName=" + vnfcName);
437             SvcLogicContext newVnfcCtx = new SvcLogicContext();
438             if (StringUtils.isNotBlank(vnfcName)) {
439                 vnfcParams.put(PARAM_VNFC_NAME, vnfcName);
440             } else {
441                 log.info("getVserverInfo()::vnfc Name is blank, not setting vnfc info !!!!");
442                 return;
443             }
444             getVnfcInformationForVserver(vnfcParams, newVnfcCtx, inParams, ctx, aaiService, responsePrefix);
445         } catch (Exception e) {
446             ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_STATUS,
447                 AppcAaiClientConstant.OUTPUT_STATUS_FAILURE);
448             ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
449             log.error("Failed in getVserverInfo", e);
450         }
451     }
452
453     public void getVnfcInformationForVserver(Map<String, String> vnfcParams, SvcLogicContext newVnfcCtx,
454         Map<String, String> inParams, SvcLogicContext ctx, AaiService aaiService, String responsePrefix)
455         throws Exception {
456         log.info("getVnfcInformationForVserver()::vnfcParams:" + vnfcParams.toString());
457         vnfcParams.put(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX,
458             inParams.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX));
459
460         aaiService.getVnfcInfo(vnfcParams, newVnfcCtx);
461
462         String vnfcType = newVnfcCtx.getAttribute(responsePrefix + "vnfc.vnfc-type");
463         String vnfcFunctionCode = newVnfcCtx.getAttribute(responsePrefix + "vnfc.vnfc-function-code");
464         String vnfcGroupNotation = newVnfcCtx.getAttribute(responsePrefix + "vnfc.group-notation");
465         String vnfcV4OamIp = newVnfcCtx.getAttribute(responsePrefix + "vnfc.ipaddress-v4-oam-vip");
466
467         if (StringUtils.isBlank(vnfcType) || StringUtils.isBlank(vnfcFunctionCode)
468             || StringUtils.isBlank(vnfcGroupNotation) || StringUtils.isBlank(vnfcV4OamIp)) {
469             log.info("getVnfcInformationForVserver()::Some vnfc parameters are blank!!!!");
470         }
471         log.info("getVnfcInformationForVserver()::vnfcType=" + vnfcType + ",vnfcFunctionCode=" + vnfcFunctionCode,
472             ", vnfc-ipaddress-v4-oam-vip=" + vnfcV4OamIp);
473         ctx.setAttribute(responsePrefix + "vm.vnfc.vnfc-name", vnfcParams.get(PARAM_VNFC_NAME));
474         ctx.setAttribute(responsePrefix + "vm.vnfc.vnfc-type", vnfcType);
475         ctx.setAttribute(responsePrefix + "vm.vnfc.vnfc-function-code", vnfcFunctionCode);
476         ctx.setAttribute(responsePrefix + "vm.vnfc.vnfc-group-notation", vnfcGroupNotation);
477         ctx.setAttribute(responsePrefix + "vm.vnfc.vnfc-ipaddress-v4-oam-vip", vnfcV4OamIp);
478     }
479
480     public Map<String, String> setVmParams(SvcLogicContext ctx, String vServerId) {
481         log.info("setVmParams()::setVmParamsVM level action:" + vServerId);
482         Map<String, String> params = new HashMap<>();
483         int vmCount = 0;
484         int arrayIndex = -1;
485         String vmCountStr = ctx.getAttribute("tmp.vnfInfo.vm-count");
486         if (StringUtils.isNotBlank(vmCountStr)) {
487             vmCount = Integer.parseInt(vmCountStr);
488         }
489         for (int cnt = 0; cnt < vmCount; cnt++) {
490             String vsId = ctx.getAttribute(STR_TMP_VNF_INFO + cnt + STR_VSERVER_ID);
491             log.info("setVmParams():::vserver details::" + cnt + ":" + vsId);
492             if (StringUtils.equals(vServerId, vsId)) {
493                 arrayIndex = cnt;
494             }
495         }
496         if (arrayIndex < 0) {
497             log.info("setVmParams()::VserverId not found in context!! Returning null for params!!");
498             return null;
499         }
500         String tenantId = ctx.getAttribute(STR_TMP_VNF_INFO + arrayIndex + STR_TENANT_ID);
501         String cloudOwner = ctx.getAttribute(STR_TMP_VNF_INFO + arrayIndex + STR_CLOUD_OWNER);
502         String cloudRegionId = ctx.getAttribute(STR_TMP_VNF_INFO + arrayIndex + STR_CLOUD_REGION_ID);
503         String vserverSelflink = ctx.getAttribute(STR_TMP_VNF_INFO + arrayIndex + STR_VSERVER_SELFLINK);
504         log.info("setVmParams()::tenantId=" + tenantId + " cloudOwner=" + cloudOwner + " cloudRegiodId= "
505             + cloudRegionId);
506         params.put(PARAM_VSERVER_ID, vServerId);
507         params.put(PARAM_TENANT_ID, tenantId);
508         params.put(PARAM_CLOUD_OWNER, cloudOwner);
509         params.put(PARAM_CLOUD_REGION_ID, cloudRegionId);
510         params.put(PARAM_VSERVER_SELFLINK, vserverSelflink);
511         log.info("setVmParams()::setVmParamsVM level action:" + params.toString());
512         return params;
513     }
514
515     public void getVfModuleModelInfo(Map<String, String> inParams, SvcLogicContext ctx) {
516         log.info("vfModuleInfo()::Retrieving vf-module information :" + inParams.toString());
517         String responsePrefix = inParams.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
518         try {
519             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
520             AaiService aaiService = getAaiService();
521             processForVfModuleModelInfo(aaiService, inParams, ctx);
522         } catch (Exception e) {
523             ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_STATUS,
524                 AppcAaiClientConstant.OUTPUT_STATUS_FAILURE);
525             ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
526             log.error("Failed in vfModuleInfo", e);
527         }
528     }
529
530     public void processForVfModuleModelInfo(AaiService aaiService, Map<String, String> inParams, SvcLogicContext ctx) {
531         log.info("processForVfModuleModelInfo()::Retrieving vf-module information :" + inParams);
532         String responsePrefix = inParams.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
533         try {
534             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
535             Map<String, String> params = new HashMap<>();
536             params.put(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX,
537                 inParams.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX));
538             params.put("vnfId", inParams.get("vnf-id"));
539             params.put("vfModuleId", inParams.get("vf-module-id"));
540             SvcLogicContext vfModuleCtx = new SvcLogicContext();
541             aaiService.getVfModuleInfo(params, vfModuleCtx);
542
543             String modelInvariantId = vfModuleCtx.getAttribute(responsePrefix + "vfModule.model-invariant-id");
544             String modelVersionId = vfModuleCtx.getAttribute(responsePrefix + "vfModule.model-version-id");
545             log.info("processForVfModuleModelInfo()::modelInvariantId=" + modelInvariantId+",modelVersionId="+modelVersionId);
546
547             Map<String, String> modelParams = new HashMap<>();
548             modelParams.put(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX,
549                     inParams.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX));
550             SvcLogicContext modelCtx = new SvcLogicContext();
551             if (StringUtils.isNotBlank(modelInvariantId) && StringUtils.isNotBlank(modelVersionId)) {
552                 modelParams.put("model-invariant-id", modelInvariantId);
553                 modelParams.put("model-version-id", modelVersionId);
554
555             } else {
556                 log.info("processForVfModuleModelInfo()::model-invariant-id or model-version-id is blank, not getting model info !!!!");
557                 return;
558             }
559             aaiService.getModelVersionInfo(modelParams, modelCtx);
560             String modelName = modelCtx.getAttribute(responsePrefix + "vfModule.model-name");
561             log.info("processForVfModuleModelInfo()::modelName for vfModule:::" + modelName);
562             log.info("Setting context template-model-id as :::" + modelName);
563             ctx.setAttribute("template-model-id", modelName);
564             log.info("processForVfModuleModelInfo() ::: End");
565             }
566             catch (Exception e) {
567                 ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_STATUS,
568                     AppcAaiClientConstant.OUTPUT_STATUS_FAILURE);
569                 ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
570                 log.error("Failed in vfModuleInfo", e);
571             }
572
573     }
574
575     public void getFormattedValue(Map<String, String> inParams, SvcLogicContext ctx) {
576         log.info("getFormattedValue()::Formatting values :" + inParams.toString());
577         String responsePrefix = inParams.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
578         try {
579             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
580             String inputValue = inParams.get("inputParameter");
581             if (StringUtils.isBlank(inputValue)) {
582                 return;
583             }
584             String outputValue = StringUtils.replace(inputValue, "/", "_");//change / to _
585             outputValue = StringUtils.replace(outputValue, " ", "");//remove space
586             ctx.setAttribute("template-model-id", outputValue);
587         } catch (Exception e) {
588             ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_STATUS,
589                 AppcAaiClientConstant.OUTPUT_STATUS_FAILURE);
590             ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
591             log.error("Failed in getFormattedValue", e);
592         }
593     }
594 }