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