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