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