3902f86c0d960b7f8fe41d8fe7addbf61f7efb08
[appc.git] / appc-outbound / appc-aai-client / provider / src / main / java / org / onap / appc / aai / client / aai / AaiService.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 (C) 2019 Ericsson
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  *
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.aai.client.aai;
27
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
30 import org.apache.commons.lang3.StringUtils;
31 import org.apache.commons.lang3.math.NumberUtils;
32 import org.onap.appc.aai.client.AppcAaiClientConstant;
33 import org.onap.ccsdk.sli.adaptors.aai.AAIClient;
34 import org.onap.ccsdk.sli.adaptors.aai.AAIService;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
37 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
38 import org.osgi.framework.BundleContext;
39 import org.osgi.framework.FrameworkUtil;
40 import org.osgi.framework.ServiceReference;
41
42 import java.util.ArrayList;
43 import java.util.HashMap;
44 import java.util.List;
45 import java.util.Map;
46
47 public class AaiService {
48
49     private static final EELFLogger log = EELFManager.getInstance().getLogger(AaiService.class);
50     private static final String STR_VNF_ID = "generic-vnf.vnf-id = '";
51     private static final String STR_VNFC_REF = "vnfcReference[";
52     private static final String STR_VNFC_REF_KEY = "VNFCREFKEY ";
53     private static final String STR_AAI_REF_KEY = "AAIREFKEY ";
54     private static final String STR_RELATIONSHIP_LIST = ".relationship-list.relationship[";
55     private static final String STR_VNFC_NAME = "vnfc.vnfc-name = '";
56     private static final String QUERY_STR_VNFC_NAME = "VNFCNAME IN INSERTVNFCS ";
57
58     private static final String PARAM_GENERIC_VNF = "generic-vnf";
59     private static final String PARAM_VNF_INFO = "vnfInfo";
60     private static final String PARAM_VSERVER = "vserver";
61     private static final String PARAM_VM_INFO = "vmInfo";
62     private static final String PARAM_PROV_STATUS = "prov-status";
63     private static final String PARAM_VAL_NVTPROV = "NVTPROV";
64
65     private static final String ATTR_VSERVER_ID = "vserver-id";
66     private static final String ATTR_TENANT_ID = "tenant-id";
67     private static final String ATTR_CLOUD_OWNER = "cloud-owner";
68     private static final String ATTR_CLOUD_REGION_ID = "cloud-region-id";
69     private static final String ATTR_VNFC_COUNT = "vm.vnfc-count";
70     private static final String ATTR_VNFC_NAME = "vnfc-name";
71     private static final String ATTR_VNFC_FUNC_CODE = "VNFC-FUNCTION-CODE";
72     private static final String ATTR_VSERVER_NAME = "vserver-name";
73     private static final String ATTR_VNF_ID = "vnf-id";
74
75     private AAIClient aaiClient;
76
77     public AaiService(AAIClient aaiClient) {
78         this.aaiClient = aaiClient;
79     }
80
81     public AaiService() {
82         BundleContext bctx = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
83         ServiceReference sref = bctx.getServiceReference(AAIService.class);
84         aaiClient = (AAIClient) bctx.getService(sref);
85     }
86
87     public void getGenericVnfInfo(Map<String, String> params, SvcLogicContext ctx)
88         throws AaiServiceInternalException, SvcLogicException {
89
90         String vnfId = params.get("vnfId");
91         if (StringUtils.isBlank(vnfId)) {
92             throw new AaiServiceInternalException("VnfId is missing");
93         }
94         String prefix = params.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
95         prefix = StringUtils.isNotBlank(prefix) ? (prefix + ".") : "";
96         String resourceKey = STR_VNF_ID + vnfId + "'";
97         SvcLogicContext vnfCtx = readResource(resourceKey, PARAM_VNF_INFO, PARAM_GENERIC_VNF);
98
99         ctx.setAttribute(prefix + "vnf.vnf-name", vnfCtx.getAttribute("vnfInfo.vnf-name"));
100         ctx.setAttribute(prefix + "vnf.vnf-type", vnfCtx.getAttribute("vnfInfo.vnf-type"));
101         ctx.setAttribute(prefix + "vnf.prov-status", vnfCtx.getAttribute("vnfInfo.prov-status"));
102         ctx.setAttribute(prefix + "vnf.orchestration-status", vnfCtx.getAttribute("vnfInfo.orchestration-status"));
103         ctx.setAttribute(prefix + "vnf.ipv4-oam-address", vnfCtx.getAttribute("vnfInfo.ipv4-oam-address"));
104
105         int vmCount = 0;
106         String relLen = vnfCtx.getAttribute("vnfInfo.relationship-list.relationship_length");
107         int relationshipLength = 0;
108         if (relLen != null) {
109             relationshipLength = Integer.parseInt(relLen);
110         }
111         log.info("RELLEN " + relationshipLength);
112         for (int i = 0; i < relationshipLength; i++) {
113             String vserverId = getRelationshipValue(i, vnfCtx, PARAM_VSERVER, "vserver.vserver-id", PARAM_VNF_INFO);
114             String tenantId = getRelationshipValue(i, vnfCtx, PARAM_VSERVER, "tenant.tenant-id", PARAM_VNF_INFO);
115             String cloudOwner = getRelationshipValue(i, vnfCtx, PARAM_VSERVER, "cloud-region.cloud-owner",
116                 PARAM_VNF_INFO);
117             String cloudRegionId =
118                 getRelationshipValue(i, vnfCtx, PARAM_VSERVER, "cloud-region.cloud-region-id", PARAM_VNF_INFO);
119             if (vserverId != null) {
120                 log.info("VSERVER KEYS " + vserverId + " " + tenantId + " " + cloudOwner + " " + cloudRegionId);
121                 String vnfPrefix = prefix + "vm[" + vmCount + "].";
122                 ctx.setAttribute(vnfPrefix + ATTR_VSERVER_ID, vserverId);
123                 ctx.setAttribute(vnfPrefix + ATTR_TENANT_ID, tenantId);
124                 ctx.setAttribute(vnfPrefix + ATTR_CLOUD_OWNER, cloudOwner);
125                 ctx.setAttribute(vnfPrefix + ATTR_CLOUD_REGION_ID, cloudRegionId);
126                 vmCount++;
127             }
128         }
129         ctx.setAttribute(prefix + "vm-count", String.valueOf(vmCount));
130         log.info("VMCOUNT FROM VNF INFO " + ctx.getAttribute(prefix + "vm-count"));
131     }
132
133     public void getVMInfo(Map<String, String> params, SvcLogicContext ctx)
134         throws SvcLogicException {
135
136         try {
137             log.info("Received getVmInfo call with params : " + params);
138             String prefix = params.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
139             prefix = StringUtils.isNotBlank(prefix) ? (prefix + ".") : "";
140             int vnfcCount = 0;
141             ctx.setAttribute(prefix + ATTR_VNFC_COUNT, String.valueOf(vnfcCount)); // In case no vnfcs are found
142
143             VServerInfo vServerInfo = new VServerInfo(params);
144
145             String resourceKey =
146                 "vserver.vserver-id = '" + vServerInfo.getVserverId() + "' AND tenant.tenant-id = '" + vServerInfo
147                     .getTenantId()
148                     + "' AND cloud-region.cloud-owner = '" + vServerInfo.getCloudOwner()
149                     + "' AND cloud-region.cloud-region-id = '"
150                     + vServerInfo.getCloudRegionId() + "'";
151
152             SvcLogicContext vmCtx = readResource(resourceKey, PARAM_VM_INFO, PARAM_VSERVER);
153             ctx.setAttribute(prefix + "vm.prov-status", vmCtx.getAttribute("vmInfo.prov-status"));
154             ctx.setAttribute(prefix + "vm.vserver-name", vmCtx.getAttribute("vmInfo.vserver-name"));
155             ctx.setAttribute(prefix + "vm.vserver-selflink", vmCtx.getAttribute("vmInfo.vserver-selflink"));
156
157             String relLen = vmCtx.getAttribute("vmInfo.relationship-list.relationship_length");
158             int relationshipLength = 0;
159             if (relLen != null) {
160                 relationshipLength = Integer.parseInt(relLen);
161             }
162             log.info("RELLEN" + relationshipLength);
163             for (int i = 0; i < relationshipLength; i++) {
164                 String vfModuleId = getRelationshipValue(i, vmCtx, "vf-module", "vf-module.vf-module-id",
165                     PARAM_VM_INFO);
166                 if (vfModuleId != null) {
167                     ctx.setAttribute(prefix + "vm.vf-module-id", vfModuleId);
168                 }
169
170                 String vnfcName = getRelationshipValue(i, vmCtx, "vnfc", "vnfc.vnfc-name", PARAM_VM_INFO);
171                 if (vnfcName != null) {
172                     ctx.setAttribute(prefix + "vm.vnfc[" + vnfcCount + "].vnfc-name", vnfcName);
173                     vnfcCount++;
174                 }
175
176             } // relationshipLength
177             ctx.setAttribute(prefix + ATTR_VNFC_COUNT, String.valueOf(vnfcCount));
178             log.info("VSERVERNAME " + ctx.getAttribute(prefix + "vm.vserver-name") + " HAS NUM VNFCS = "
179                 + ctx.getAttribute(prefix + ATTR_VNFC_COUNT));
180         } catch (Exception e) {
181             log.error("An error occurred when fetching Vm info", e);
182             throw new SvcLogicException("Failed to fetch VM info", e);
183         }
184     }
185
186     private String getRelationshipValue(int i, SvcLogicContext ctx, String relatedTo, String relationshipKey,
187         String prefix) {
188
189         if (relatedTo.equals(ctx.getAttribute(prefix + STR_RELATIONSHIP_LIST + i + "].related-to"))) {
190             log.info("RELATEDTO " + relatedTo);
191             int relationshipDataLength = 0;
192             String relDataLen =
193                 ctx.getAttribute(prefix + STR_RELATIONSHIP_LIST + i + "].relationship-data_length");
194             if (relDataLen != null) {
195                 relationshipDataLength = Integer.parseInt(relDataLen);
196             }
197
198             for (int j = 0; j < relationshipDataLength; j++) {
199
200                 String key = ctx.getAttribute(prefix + STR_RELATIONSHIP_LIST + i + "].relationship-data["
201                     + j + "].relationship-key");
202
203                 String value = ctx.getAttribute(prefix + STR_RELATIONSHIP_LIST + i + "].relationship-data["
204                     + j + "].relationship-value");
205
206                 log.info("GENERIC KEY " + key);
207                 log.info("GENERIC VALUE " + value);
208
209                 if (relationshipKey.equals(key)) {
210                     return value;
211                 }
212
213             } // relationshipDataLength
214         } // if related-To
215
216         return null;
217     }
218
219     public void getVnfcInfo(Map<String, String> params, SvcLogicContext ctx)
220         throws AaiServiceInternalException, SvcLogicException {
221         log.info("Received getVnfc call with params : " + params);
222
223         String prefix = params.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
224         prefix = StringUtils.isNotBlank(prefix) ? (prefix + ".") : "";
225
226         String vnfcName = params.get("vnfcName");
227         if (StringUtils.isBlank(vnfcName)) {
228             throw new AaiServiceInternalException("Vnfc Name is missing");
229         }
230
231         String resourceKey = STR_VNFC_NAME + vnfcName + "'";
232         SvcLogicContext vnfcCtx = readResource(resourceKey, "vnfcInfo", "vnfc");
233
234         // Changes for US 315820 for 1710 vnfc-type renamed to nfc-function,vnfc-function-code renamed to
235         // nfc-naming-code
236
237         ctx.setAttribute(prefix + "vnfc.vnfc-type", vnfcCtx.getAttribute("vnfcInfo.nfc-function"));
238         ctx.setAttribute(prefix + "vnfc.vnfc-function-code", vnfcCtx.getAttribute("vnfcInfo.nfc-naming-code"));
239         ctx.setAttribute(prefix + "vnfc.group-notation", vnfcCtx.getAttribute("vnfcInfo.group-notation"));
240         ctx.setAttribute(prefix + "vnfc.ipaddress-v4-oam-vip", vnfcCtx.getAttribute("vnfcInfo.ipaddress-v4-oam-vip"));
241
242     }
243
244     public void insertVnfcs(Map<String, String> params, SvcLogicContext ctx, int vmCount,
245                             String vfModuleIdFromRequest)
246         throws AaiServiceInternalException, SvcLogicException {
247         log.info("Received insertVnfcs call with params : " + params);
248
249         String prefix = params.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
250
251         prefix = StringUtils.isNotBlank(prefix) ? (prefix + ".") : "";
252         int vnfcRefIndx = 0;
253         log.debug("vnfcRefIndx" + vnfcRefIndx);
254         for (int i = 0; i < vmCount; i++) {
255             String aaiRefKey = prefix + "vm[" + i + "].";
256
257             //ConfigScaleOut - Do not process vms that are not associated with vfmodule id if vfmodule id is present
258             if (StringUtils.isNotBlank(vfModuleIdFromRequest)) {
259                 String vmVfModuleId = ctx.getAttribute(aaiRefKey + "vf-module-id");
260                 log.info("insertVnfcs():::vfModule for vm is=" + vmVfModuleId);
261                 if (StringUtils.isBlank(vmVfModuleId) || !StringUtils
262                     .equalsIgnoreCase(vmVfModuleId, vfModuleIdFromRequest)) {
263                     continue;
264                 }
265             }
266
267             log.info(QUERY_STR_VNFC_NAME + ctx.getAttribute(aaiRefKey + ATTR_VNFC_NAME));
268             String vnfcNameAAI = ctx.getAttribute(aaiRefKey + ATTR_VNFC_NAME);
269             // Get Vnfc_reference data from the table
270             String vnfcRefKey = STR_VNFC_REF + vnfcRefIndx + "].";
271
272             log.info(STR_VNFC_REF_KEY + vnfcRefKey);
273             log.info(STR_AAI_REF_KEY + aaiRefKey);
274
275             String groupNotationType = ctx.getAttribute(vnfcRefKey + "GROUP-NOTATION-TYPE");
276             String groupNotationValue = ctx.getAttribute(vnfcRefKey + "GROUP-NOTATION-VALUE");
277             String vnfcType = ctx.getAttribute(vnfcRefKey + "VNFC-TYPE");
278             String vnfcFuncCode = ctx.getAttribute(vnfcRefKey + ATTR_VNFC_FUNC_CODE);
279             String populateIpAddressV4OamVip = ctx.getAttribute(vnfcRefKey + "IPADDRESS-V4-OAM-VIP");
280
281             // Get vnfc Data to be added
282             String vserverName = ctx.getAttribute(aaiRefKey + ATTR_VSERVER_NAME);
283             String vnfcName = vserverName + vnfcFuncCode + "001";
284             String groupNotation = getGroupNotation(groupNotationType, groupNotationValue, vnfcName, vserverName,
285                 prefix, ctx, vnfcType, vnfcFuncCode, vmCount);
286
287             String ipAddressV4OamVip = null;
288             if ("Y".equals(populateIpAddressV4OamVip)) {
289                 ipAddressV4OamVip = ctx.getAttribute("vnf-host-ip-address"); // from input
290             }
291             Map<String, String> vnfcParams =
292                 populateVnfcParams(ctx, aaiRefKey, ipAddressV4OamVip, groupNotation, vnfcType, vnfcFuncCode);
293
294             log.info("Vnfc name from AAI: " + vnfcNameAAI);
295             log.info("Vnfc name generated: " + vnfcName);
296
297             if (StringUtils.isNotBlank(vnfcNameAAI)) {
298                 if (vnfcName.equalsIgnoreCase(vnfcNameAAI)) {
299                     updateVnfcStatus(vnfcNameAAI, params, prefix);
300                     vnfcRefIndx++;
301                 }
302                 continue;
303             }
304             vnfcRefIndx++;
305             addVnfc(vnfcName, vnfcParams, prefix);
306
307             // Add VNFC Info to context for current added VNFC
308             ctx.setAttribute(aaiRefKey + ATTR_VNFC_NAME, vnfcName);
309             ctx.setAttribute(aaiRefKey + "vnfc-type", vnfcType);
310             ctx.setAttribute(aaiRefKey + "vnfc-function-code", vnfcFuncCode);
311             ctx.setAttribute(aaiRefKey + "group-notation", groupNotation);
312         }
313     }
314
315     public List<String> getVnfcData(Map<String, String> params, SvcLogicContext ctx, int vmCount) {
316
317         String prefix = params.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
318         prefix = StringUtils.isNotBlank(prefix) ? (prefix + ".") : "";
319         List<String> vnfcNames = new ArrayList<>();
320         int vnfcRefIndx = -1;
321         for (int i = 0; i < vmCount; i++) {
322             String aaiRefKey = prefix + "vm[" + i + "].";
323             log.info(QUERY_STR_VNFC_NAME + ctx.getAttribute(aaiRefKey + ATTR_VNFC_NAME));
324             if (ctx.getAttribute(aaiRefKey + ATTR_VNFC_NAME) != null) {
325                 continue;
326             } else {
327                 vnfcRefIndx++;
328             }
329             String vnfcRefKey = STR_VNFC_REF + vnfcRefIndx + "].";
330             log.info(STR_VNFC_REF_KEY + vnfcRefKey);
331             log.info(STR_AAI_REF_KEY + aaiRefKey);
332             String vnfcFuncCode = ctx.getAttribute(vnfcRefKey + ATTR_VNFC_FUNC_CODE);
333
334             // Get vnfc Data to be added
335             String vserverName = ctx.getAttribute(aaiRefKey + ATTR_VSERVER_NAME);
336             String vnfcName = vserverName + vnfcFuncCode + "001";
337
338             vnfcNames.add(vnfcName);
339         }
340
341         return vnfcNames;
342     }
343
344     private Map<String, String> populateVnfcParams(SvcLogicContext ctx, String aaiRefKey, String ipAddressV4OamVip,
345         String groupNotation, String vnfcType, String vnfcFuncCode) {
346
347         Map<String, String> vnfcParams = new HashMap<>();
348
349         // Changes for vnfc-type renamed to nfc-function,vnfc-function-code renamed to
350         // nfc-naming-code
351         vnfcParams.put("nfc-naming-code", vnfcFuncCode);
352         vnfcParams.put("nfc-function", vnfcType);
353         vnfcParams.put("ipaddress-v4-oam-vip", ipAddressV4OamVip);
354         vnfcParams.put(PARAM_PROV_STATUS, PARAM_VAL_NVTPROV);
355         vnfcParams.put("orchestration-status", "CONFIGURED");
356         vnfcParams.put("in-maint", "false");
357         vnfcParams.put("is-closed-loop", "false");
358         vnfcParams.put("group-notation", groupNotation);
359         vnfcParams.put("relationship-list.relationship[0].related-to", PARAM_VSERVER);
360         vnfcParams.put("relationship-list.relationship[0].relationship-data[0].relationship-key", "vserver.vserver-id");
361         vnfcParams.put("relationship-list.relationship[0].relationship-data[0].relationship-value",
362             ctx.getAttribute(aaiRefKey + ATTR_VSERVER_ID));
363         vnfcParams.put("relationship-list.relationship[0].relationship-data[1].relationship-key", "tenant.tenant-id");
364         vnfcParams.put("relationship-list.relationship[0].relationship-data[1].relationship-value",
365             ctx.getAttribute(aaiRefKey + ATTR_TENANT_ID));
366         vnfcParams.put("relationship-list.relationship[0].relationship-data[2].relationship-key",
367             "cloud-region.cloud-owner");
368         vnfcParams.put("relationship-list.relationship[0].relationship-data[2].relationship-value",
369             ctx.getAttribute(aaiRefKey + ATTR_CLOUD_OWNER));
370         vnfcParams.put("relationship-list.relationship[0].relationship-data[3].relationship-key",
371             "cloud-region.cloud-region-id");
372         vnfcParams.put("relationship-list.relationship[0].relationship-data[3].relationship-value",
373             ctx.getAttribute(aaiRefKey + ATTR_CLOUD_REGION_ID));
374         vnfcParams.put("relationship-list.relationship[1].related-to", PARAM_GENERIC_VNF);
375         vnfcParams.put("relationship-list.relationship[1].relationship-data[0].relationship-key", "generic-vnf.vnf-id");
376         vnfcParams.put("relationship-list.relationship[1].relationship-data[0].relationship-value",
377             ctx.getAttribute(ATTR_VNF_ID));
378         vnfcParams.put("relationship-list.relationship[2].related-to", "vf-module");
379         vnfcParams.put("relationship-list.relationship[2].relationship-data[0].relationship-key", "generic-vnf.vnf-id");
380         vnfcParams.put("relationship-list.relationship[2].relationship-data[0].relationship-value",
381             ctx.getAttribute(ATTR_VNF_ID));
382         vnfcParams.put("relationship-list.relationship[2].relationship-data[1].relationship-key",
383             "vf-module.vf-module-id");
384         vnfcParams.put("relationship-list.relationship[2].relationship-data[1].relationship-value",
385             ctx.getAttribute(aaiRefKey + "vf-module-id"));
386
387         return vnfcParams;
388     }
389
390     public void addVnfc(String vnfcName, Map<String, String> params, String prefix)
391         throws AaiServiceInternalException, SvcLogicException {
392
393         log.info("Received addVnfc call with vnfcName : " + vnfcName);
394         log.info("Received addVnfc call with params : " + params);
395         String resourceKey = STR_VNFC_NAME + vnfcName + "'";
396         log.info("Received addVnfc call with resourceKey : " + resourceKey);
397
398         SvcLogicContext vnfcCtx = new SvcLogicContext();
399         SvcLogicResource.QueryStatus response =
400             aaiClient.save("vnfc", true, false, resourceKey, params, prefix, vnfcCtx);
401
402         if (SvcLogicResource.QueryStatus.SUCCESS.equals(response)) {
403             log.info("Added VNFC SUCCESSFULLY " + vnfcName);
404         } else if (SvcLogicResource.QueryStatus.FAILURE.equals(response)) {
405             throw new AaiServiceInternalException("VNFC Add failed for vnfc_name " + vnfcName);
406         }
407     }
408
409     public String getGroupNotation(String groupNotationType, String groupNotationValue, String vnfcName,
410         String vserverName, String prefix, SvcLogicContext ctx, String vnfcRefVnfcType, String vnfcFuncCode,
411         int vmCount) {
412
413         String groupNotation = null;
414
415         if ("fixed-value".equals(groupNotationType)) {
416             groupNotation = groupNotationValue;
417         } else if ("first-vnfc-name".equals(groupNotationType)) {
418
419             /*
420              * If the group-notation-type value = ?first-vnfc-name?,
421              * then populate the group-notation value with the concatenation of
422              * [vnfc name associated with the first vnfc for the vnfc-type (e.g., *******)]
423              * and [the value in group-notation-value (e.g., pair)].
424              * There may be several vnfc-types associated with the VM?s.
425              */
426             /* Vnfc-type should be from refrence data */
427
428             /* vDBE has 2 VNFCs with same VNFC type . The pair name should be same for both . */
429             /*
430              * When first VNFC is added details should be added to context so FirstVnfcName doesnt return null second
431              * time.
432              */
433             String tmpVnfcName = getFirstVnfcNameForVnfcType(ctx, prefix, vnfcRefVnfcType);
434
435             log.info("RETURNED FIRSTVNFCNAME" + tmpVnfcName);
436             log.info("CURRENTVNFCNAME" + vnfcName);
437             groupNotation = resolveGroupNotation(groupNotationValue, vnfcName, tmpVnfcName);
438         } else if ("relative-value".equals(groupNotationType)) {
439
440             /*
441              * If the group-notation-type = ?relative-value?, then find the group-notation value
442              * from the prior vnfc (where prior means the vnfc with where the last three digits of the
443              * vm-name is one lower than the current one; note that this vnfc may have been previously configured.)
444              * 1. If the group-notation-value = next, then add 1 to the group-notation value from the prior vnfc and use
445              * this value
446              * 2. If the group-notation-value = same, then use the group-notation-value from the prior vnfc record
447              */
448
449             // next and same cant be defined for first VM. if next will not generate grpNotation if Prior is not a
450             // number
451             String tmpVserverName;
452             if (vserverName != null) {
453
454                 String vmNamePrefix = vserverName.substring(0, vserverName.length() - 3);
455                 String lastThreeChars = vserverName.substring(vserverName.length() - 3);
456
457                 if (NumberUtils.isDigits(lastThreeChars)) {
458                     int vmNum = Integer.parseInt(lastThreeChars) - 1;
459                     String formatted = String.format("%03d", vmNum);
460
461                     log.info("FORMATTED " + formatted);
462
463                     tmpVserverName = vmNamePrefix + formatted;
464
465                     String priorGroupNotation = getGroupNotationForVServer(ctx, prefix, tmpVserverName);
466                     groupNotation = resolveGroupNotation(groupNotationValue, priorGroupNotation);
467                 }
468             }
469         } else if ("existing-value".equals(groupNotationType)) {
470          /* This is a new value being added.  Find the existing vnfc records in A&AI inventory with the same vnfc-function code as the value in vnfc_reference table.
471           * Verify that the group-notation value is the same for all such records found in inventory.
472           * if all records do not have the same group-notation value, write the new vnfc record to A&AI inventory without a group-notation value and continue to the next VM in the vnfc_reference table.  A 501 intermediate error message should be sent after all new VNFC records have been added to A&AI.
473           * If all records match, use the same group-notation value for the new vnfc record as found in the existing vnfc records.
474           */
475             groupNotation = getGroupNotationForExistigValue(ctx, vnfcFuncCode, vmCount);
476         }
477
478         log.info("RETURNED GROUPNOTATION " + groupNotation);
479         return groupNotation;
480     }
481
482     private String resolveGroupNotation(String groupNotationValue, String vnfcName, String tmpVnfcName) {
483         if (tmpVnfcName == null) {
484             log.info("CURRENTVNFCNAME" + vnfcName);
485             // No Vnfcs currently exist. Use Current vnfcName
486             return vnfcName + groupNotationValue;
487         } else {
488             return tmpVnfcName + groupNotationValue;
489         }
490     }
491
492     private String resolveGroupNotation(String groupNotationValue, String priorGroupNotation) {
493         if ("same".equals(groupNotationValue)) {
494             return priorGroupNotation;
495         } else if ("next".equals(groupNotationValue) && priorGroupNotation != null
496             && NumberUtils.isDigits(priorGroupNotation)) {
497
498             int nextGrpNotation = Integer.parseInt(priorGroupNotation) + 1;
499             return String.valueOf(nextGrpNotation);
500         }
501         return null;
502     }
503
504     public String getGroupNotationForExistigValue(SvcLogicContext ctx, String vnfcFuncCode,
505                                                   int vmCount) {
506         String vfModuleId = ctx.getAttribute("req-vf-module-id"); //Coming from request-params
507         boolean first = true;
508         String aaiGroupNotationValue = null;
509         for (int i = 0; i < vmCount; i++) {
510             String ind = "tmp.vnfInfo.vm[" + i + "].";
511             String aaiFuncCode = ctx.getAttribute(ind + "vnfc-function-code");
512             String aaiGroupNotation = ctx.getAttribute(ind + "group-notation");
513             String aaiVfModuleId = ctx.getAttribute(ind + "vf-module-id");
514
515             log.info("getGroupNotationForExistigValue()::: vfModuleId=" + vfModuleId + ", aaiFuncCode=" + aaiFuncCode
516                 + ", aaiGroupNotation=" + aaiGroupNotation + ",aaiVfMOduleId=" + aaiVfModuleId);
517
518             if (StringUtils.isNotBlank(aaiFuncCode) && aaiFuncCode.equals(vnfcFuncCode) &&
519                 (StringUtils.isNotBlank(vfModuleId) && StringUtils.isNotBlank(aaiVfModuleId) && aaiVfModuleId
520                     .equals(vfModuleId))) {
521                 if (null == aaiGroupNotationValue && first) {
522                     if (null == aaiGroupNotation) {//Return if null
523                         return null;
524                     }
525                     aaiGroupNotationValue = ctx.getAttribute(ind + "group-notation");
526                     first = false;
527                 } else {
528                     if (!StringUtils.equals(aaiGroupNotationValue, ctx.getAttribute(ind + "group-notation"))) {
529                         log.info("Values are different, returning null");
530                         return null;
531                     }
532                 }
533             }
534         }
535
536         return aaiGroupNotationValue;
537     }
538
539     public String getGroupNotationForVServer(SvcLogicContext ctx, String prefix, String vserverName) {
540
541         String vmCountStr = ctx.getAttribute(prefix + "vnf.vm-count");
542
543         if (vmCountStr == null) {
544             return null;
545         }
546
547         int vmCount = Integer.parseInt(vmCountStr);
548         for (int i = 0; i < vmCount; i++) {
549
550             String tmpVserver = ctx.getAttribute(prefix + "vm[" + i + "].vserver-name");
551
552             if (vserverName.equals(tmpVserver)) {
553                 return ctx.getAttribute(prefix + "vm[" + i + "].group-notation");
554             }
555         } // vmCount
556         return null;
557     }
558
559     public String getFirstVnfcNameForVnfcType(SvcLogicContext ctx, String prefix, String vnfcRefVnfcType) {
560
561         String vmCountStr = ctx.getAttribute(prefix + "vnf.vm-count");
562         if (vmCountStr == null) {
563             return null;
564         }
565         int vmCount = Integer.parseInt(vmCountStr);
566         for (int i = 0; i < vmCount; i++) {
567
568             String tmpvnfcType = ctx.getAttribute(prefix + "vm[" + i + "].vnfc-type");
569
570             if (vnfcRefVnfcType.equals(tmpvnfcType)) {
571                 return ctx.getAttribute(prefix + "vm[" + i + "].vnfc-name");
572             }
573         } // vmCount
574         return null;
575     }
576
577     public void updateVServerStatus(Map<String, String> params, SvcLogicContext ctx, int vmCount)
578         throws AaiServiceInternalException, SvcLogicException {
579         log.info("Received updateVServerStatus call with params : " + params);
580
581         String prefix = params.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
582
583         prefix = StringUtils.isNotBlank(prefix) ? (prefix + ".") : "";
584         Map<String, String> vServerParams = new HashMap<>();
585
586         // TODO - Should this just update prov-status or both? What about generic-vnf status? Will that be updated by
587         // Dispatcher?
588
589         vServerParams.put(PARAM_PROV_STATUS, PARAM_VAL_NVTPROV);
590
591         for (int i = 0; i < vmCount; i++) {
592             String aaiRefKey = prefix + "vm[" + i + "].";
593
594             log.info("VNFCNAME IN UpdateVServer " + ctx.getAttribute(aaiRefKey + ATTR_VNFC_NAME));
595
596             if (ctx.getAttribute(aaiRefKey + ATTR_VNFC_NAME) != null) {
597                 continue;
598             }
599
600             String resourceKey = "vserver.vserver-id = '" + ctx.getAttribute(aaiRefKey + ATTR_VSERVER_ID) + "'"
601                 + " AND tenant.tenant-id = '" + ctx.getAttribute(aaiRefKey + ATTR_TENANT_ID) + "'"
602                 + " AND cloud-region.cloud-owner = '" + ctx.getAttribute(aaiRefKey + ATTR_CLOUD_OWNER) + "'"
603                 + " AND cloud-region.cloud-region-id = '" + ctx.getAttribute(aaiRefKey + ATTR_CLOUD_REGION_ID) + "'";
604
605             updateResource(PARAM_VSERVER, resourceKey, vServerParams);
606         }
607     }
608
609     public void updateVnfStatus(Map<String, String> params, SvcLogicContext ctx)
610         throws AaiServiceInternalException, SvcLogicException {
611         log.info("Received updateVnfStatus call with params : " + params);
612
613         Map<String, String> vnfParams = new HashMap<>();
614
615         // TODO - Should this just update prov-status or both? What about generic-vnf status? Will that be updated by
616         // Dispatcher?
617
618         vnfParams.put(PARAM_PROV_STATUS, PARAM_VAL_NVTPROV);
619
620         String resourceKey = STR_VNF_ID + ctx.getAttribute(ATTR_VNF_ID) + "'";
621
622         updateResource(PARAM_GENERIC_VNF, resourceKey, vnfParams);
623     }
624
625     public void updateResource(String resource, String resourceKey, Map<String, String> params)
626         throws AaiServiceInternalException, SvcLogicException {
627
628         log.info("Received updateResource call with Key : " + resourceKey);
629
630         SvcLogicContext ctx = new SvcLogicContext();
631         SvcLogicResource.QueryStatus response = aaiClient.update(resource, resourceKey, params, "tmp.update", ctx);
632
633         if (SvcLogicResource.QueryStatus.SUCCESS.equals(response)) {
634             log.info("Updated " + resource + " SUCCESSFULLY for " + resourceKey);
635
636         } else if (SvcLogicResource.QueryStatus.FAILURE.equals(response)) {
637             throw new AaiServiceInternalException(resource + " Update failed for " + resourceKey);
638         }
639     }
640
641     public SvcLogicContext readResource(String query, String prefix, String resourceType)
642         throws AaiServiceInternalException, SvcLogicException {
643         SvcLogicContext resourceContext = new SvcLogicContext();
644
645         SvcLogicResource.QueryStatus response =
646             aaiClient.query(resourceType, false, null, query, prefix, null, resourceContext);
647         log.info("AAIResponse: " + response.toString());
648         if (resourceType==null || !resourceType.equals("cloud-region")) {
649             if (!SvcLogicResource.QueryStatus.SUCCESS.equals(response)) {
650                 throw new AaiServiceInternalException("Error Retrieving " + resourceType + " from A&AI");
651             }
652         }
653         return resourceContext;
654     }
655
656     // Added for Backward Compatibility
657     public void checkAndUpdateVnfc(Map<String, String> params, SvcLogicContext ctx, int vnfcRefLen, int vmCount)
658         throws AaiServiceInternalException, SvcLogicException {
659         log.info("Received checkAndUpdateVnfcStatus call with params : " + params);
660
661         String prefix = params.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
662
663         prefix = StringUtils.isNotBlank(prefix) ? (prefix + ".") : "";
664
665         for (int i = 0; i < vmCount; i++) {
666             String aaiRefKey = prefix + "vm[" + i + "].";
667
668             log.info(QUERY_STR_VNFC_NAME + aaiRefKey + "vnfc-name:" + ctx.getAttribute(aaiRefKey + ATTR_VNFC_NAME));
669
670             String vnfcNameAai = ctx.getAttribute(aaiRefKey + ATTR_VNFC_NAME);
671
672             if (StringUtils.isNotBlank(vnfcNameAai)) {
673                 // Get Vnfc_reference data
674                 for (int vnfcRefIndx = 0; vnfcRefIndx < vnfcRefLen; vnfcRefIndx++) {
675
676                     String vnfcRefKey = STR_VNFC_REF + vnfcRefIndx + "].";
677
678                     log.info(STR_VNFC_REF_KEY + vnfcRefKey);
679                     log.info(STR_AAI_REF_KEY + aaiRefKey);
680
681                     String vnfcFuncCode = ctx.getAttribute(vnfcRefKey + ATTR_VNFC_FUNC_CODE);
682                     String vserverName = ctx.getAttribute(aaiRefKey + ATTR_VSERVER_NAME);
683                     String vnfcNameReference = vserverName + vnfcFuncCode + "001";
684                     tryUpdateVnfcStatus(params, prefix, vnfcNameAai, vnfcNameReference);
685                 }
686             }
687         }
688     }
689
690     private void tryUpdateVnfcStatus(Map<String, String> params, String prefix, String vnfcNameAai,
691         String vnfcNameReference) throws AaiServiceInternalException, SvcLogicException {
692         if (vnfcNameAai.equals(vnfcNameReference)) {
693             updateVnfcStatus(vnfcNameAai, params, prefix);
694         }
695     }
696
697     public void updateVnfcStatus(String vnfcName, Map<String, String> params, String prefix)
698         throws AaiServiceInternalException, SvcLogicException {
699
700         log.info("Received updateVnfcStatus call with vnfcName : " + vnfcName);
701         log.info("Received updateVnfcStatus call with params : " + params);
702
703         String resourceKey = STR_VNFC_NAME + vnfcName + "'";
704         log.info("Received updateVnfcStatus call with resourceKey : " + resourceKey);
705
706         Map<String, String> vnfcParams = new HashMap<>();
707         vnfcParams.put(PARAM_PROV_STATUS, PARAM_VAL_NVTPROV);
708         vnfcParams.put("orchestration-status", "CONFIGURED");
709
710         log.info("In updateVnfcStatus call with vnfcParams : " + vnfcParams);
711
712         updateResource("vnfc", resourceKey, vnfcParams);
713
714         log.info("End of updateVnfcStatus");
715     }
716
717     public void updateVnfStatusWithOAMAddress(Map<String, String> params, SvcLogicContext ctx)
718         throws AaiServiceInternalException, SvcLogicException {
719         log.info("Received updateVnfStatusWithOAMAddress call with params : " + params);
720
721         String ipAddress = ctx.getAttribute("vnf-host-ip-address");
722         log.debug("Vnf-host-ip-address" + ipAddress);
723
724         Map<String, String> vnfParams = new HashMap<>();
725         vnfParams.put("ipv4-oam-address", ipAddress);
726         String resourceKey = STR_VNF_ID + ctx.getAttribute(ATTR_VNF_ID) + "'";
727         updateResource(PARAM_GENERIC_VNF, resourceKey, vnfParams);
728     }
729
730     public void getVfModuleInfo(Map<String, String> params, SvcLogicContext vfModuleCtx) throws Exception {
731         log.info("Received getVfModuleInfo call with params : " + params);
732         String prefix = params.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
733         prefix = StringUtils.isNotBlank(prefix) ? (prefix + ".") : "";
734
735         String vnfId = params.get("vnfId");
736         String vfModuleId = params.get("vfModuleId");
737         String resourceKey = STR_VNF_ID + vnfId +
738             "' AND vf-module.vf-module-id = '" + vfModuleId + "'";
739         String queryPrefix = "vfModuleInfo";
740         String resourceType = "vf-module";
741         SvcLogicContext vfmCtx = readResource(resourceKey, queryPrefix, resourceType);
742         String modelInvariantId = vfmCtx.getAttribute("vfModuleInfo.model-invariant-id");
743         log.info("getVfModuleInfo():::modelInvariant=" + modelInvariantId);
744         vfModuleCtx.setAttribute(prefix + "vfModule.model-invariant-id",
745             vfmCtx.getAttribute("vfModuleInfo.model-invariant-id"));
746         vfModuleCtx
747             .setAttribute(prefix + "vfModule.model-version-id", vfmCtx.getAttribute("vfModuleInfo.model-version-id"));
748         log.info("End - getVfModuleInfo");
749     }
750
751     public void getModelVersionInfo(Map<String, String> modelParams, SvcLogicContext modelCtx) throws Exception {
752         log.info("Received getModelVersionInfo call with params : " + modelParams);
753         String prefix = modelParams.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
754         prefix = StringUtils.isNotBlank(prefix) ? (prefix + ".") : "";
755
756         String modelInvariantId = modelParams.get("model-invariant-id");
757         String modelVersionId = modelParams.get("model-version-id");
758         String resourceKey = "model.model-invariant-id = '" + modelInvariantId +
759             "' AND model-ver.model-version-id = '" + modelVersionId + "'";
760         String queryPrefix = "modelInfo";
761         String resourceType = "model-ver";
762         SvcLogicContext vfmCtx = readResource(resourceKey, queryPrefix, resourceType);
763         log.info("getModelVersionInfo():::modelname=" + vfmCtx.getAttribute("modelInfo.model-name"));
764         modelCtx.setAttribute(prefix + "vfModule.model-name", vfmCtx.getAttribute("modelInfo.model-name"));
765         log.info("End - getModelVersionInfo");
766
767     }
768
769     public void getIdentityUrl(Map<String, String> params, SvcLogicContext ctx) throws Exception{
770         log.info("Recieved getIdentityUrl call with params : "+params);
771         String prefix = params.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
772         prefix = StringUtils.isNotBlank(prefix) ? (prefix + ".") : "";
773
774         String cloudOwner = params.get("cloudOwner");
775         String cloudRegionId = params.get("cloudRegionId");
776         // per comment from git review, we need to sanitize the two parameters 
777         // to avoid security issues
778         cloudOwner = cloudOwner.replaceAll("'", "");
779         cloudRegionId = cloudRegionId.replaceAll("'", "");
780         log.debug("cloudOwner" +cloudOwner +"," +"cloudRegionId"+ cloudRegionId);
781         String resourceKey = "depth = '" + 0 + "' AND cloud-region.cloud-owner = '" + cloudOwner +
782                 "' AND cloud-region.cloud-region-id = '" + cloudRegionId + "'";
783         String queryPrefix ="urlInfo";
784         String resourceType = "cloud-region";
785         SvcLogicContext urlCtx = readResource(resourceKey, queryPrefix, resourceType);
786         log.info("IdentityUrl: "+urlCtx.getAttribute("urlInfo.identity-url"));
787         log.info("Prefix for getIdentityUrl: "+prefix+"cloud-region.identity-url");
788         ctx.setAttribute(prefix+"cloud-region.identity-url", urlCtx.getAttribute("urlInfo.identity-url"));
789  
790     }
791 }