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