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