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