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