2 * ============LICENSE_START=======================================================
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
15 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 * ============LICENSE_END=========================================================
26 package org.onap.appc.aai.client.aai;
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.List;
34 import org.apache.commons.lang3.StringUtils;
35 import org.apache.commons.lang3.math.NumberUtils;
36 import org.onap.appc.aai.client.AppcAaiClientConstant;
37 import org.onap.ccsdk.sli.adaptors.aai.AAIClient;
38 import org.onap.ccsdk.sli.adaptors.aai.AAIService;
39 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
40 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
41 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
42 import org.osgi.framework.BundleContext;
43 import org.osgi.framework.FrameworkUtil;
44 import org.osgi.framework.ServiceReference;
46 public class AaiService {
48 private static final EELFLogger log = EELFManager.getInstance().getLogger(AaiService.class);
49 private static final String STR_VNF_ID = "generic-vnf.vnf-id = '";
50 private static final String STR_VNFC_REF = "vnfcReference[";
51 private static final String STR_VNFC_REF_KEY = "VNFCREFKEY ";
52 private static final String STR_AAI_REF_KEY = "AAIREFKEY ";
53 private static final String STR_RELATIONSHIP_LIST = ".relationship-list.relationship[";
54 private static final String STR_VNFC_NAME = "vnfc.vnfc-name = '";
55 private static final String QUERY_STR_VNFC_NAME = "VNFCNAME IN INSERTVNFCS ";
57 private static final String PARAM_GENERIC_VNF = "generic-vnf";
58 private static final String PARAM_VNF_INFO = "vnfInfo";
59 private static final String PARAM_VSERVER = "vserver";
60 private static final String PARAM_VM_INFO = "vmInfo";
61 private static final String PARAM_PROV_STATUS = "prov-status";
62 private static final String PARAM_VAL_NVTPROV = "NVTPROV";
64 private static final String ATTR_VSERVER_ID = "vserver-id";
65 private static final String ATTR_TENANT_ID = "tenant-id";
66 private static final String ATTR_CLOUD_OWNER = "cloud-owner";
67 private static final String ATTR_CLOUD_REGION_ID = "cloud-region-id";
68 private static final String ATTR_VNFC_COUNT = "vm.vnfc-count";
69 private static final String ATTR_VNFC_NAME = "vnfc-name";
70 private static final String ATTR_VNFC_FUNC_CODE = "VNFC-FUNCTION-CODE";
71 private static final String ATTR_VSERVER_NAME = "vserver-name";
72 private static final String ATTR_VNF_ID = "vnf-id";
74 private AAIClient aaiClient;
76 public AaiService(AAIClient aaiClient) {
77 this.aaiClient = aaiClient;
81 BundleContext bctx = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
82 ServiceReference sref = bctx.getServiceReference(AAIService.class);
83 aaiClient = (AAIClient) bctx.getService(sref);
86 public void getGenericVnfInfo(Map<String, String> params, SvcLogicContext ctx)
87 throws AaiServiceInternalException, SvcLogicException {
89 String vnfId = params.get("vnfId");
90 if (StringUtils.isBlank(vnfId)) {
91 throw new AaiServiceInternalException("VnfId is missing");
93 String prefix = params.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
94 prefix = StringUtils.isNotBlank(prefix) ? (prefix + ".") : "";
95 String resourceKey = STR_VNF_ID + vnfId + "'";
96 SvcLogicContext vnfCtx = readResource(resourceKey, PARAM_VNF_INFO, PARAM_GENERIC_VNF);
98 ctx.setAttribute(prefix + "vnf.vnf-name", vnfCtx.getAttribute("vnfInfo.vnf-name"));
99 ctx.setAttribute(prefix + "vnf.vnf-type", vnfCtx.getAttribute("vnfInfo.vnf-type"));
100 ctx.setAttribute(prefix + "vnf.prov-status", vnfCtx.getAttribute("vnfInfo.prov-status"));
101 ctx.setAttribute(prefix + "vnf.orchestration-status", vnfCtx.getAttribute("vnfInfo.orchestration-status"));
102 ctx.setAttribute(prefix + "vnf.ipv4-oam-address", vnfCtx.getAttribute("vnfInfo.ipv4-oam-address"));
105 String relLen = vnfCtx.getAttribute("vnfInfo.relationship-list.relationship_length");
106 int relationshipLength = 0;
107 if (relLen != null) {
108 relationshipLength = Integer.parseInt(relLen);
110 log.info("RELLEN " + relationshipLength);
111 for (int i = 0; i < relationshipLength; i++) {
112 String vserverId = getRelationshipValue(i, vnfCtx, PARAM_VSERVER, "vserver.vserver-id", PARAM_VNF_INFO);
113 String tenantId = getRelationshipValue(i, vnfCtx, PARAM_VSERVER, "tenant.tenant-id", PARAM_VNF_INFO);
114 String cloudOwner = getRelationshipValue(i, vnfCtx, PARAM_VSERVER, "cloud-region.cloud-owner",
116 String cloudRegionId =
117 getRelationshipValue(i, vnfCtx, PARAM_VSERVER, "cloud-region.cloud-region-id", PARAM_VNF_INFO);
118 if (vserverId != null) {
119 log.info("VSERVER KEYS " + vserverId + " " + tenantId + " " + cloudOwner + " " + cloudRegionId);
120 String vnfPrefix = prefix + "vm[" + vmCount + "].";
121 ctx.setAttribute(vnfPrefix + ATTR_VSERVER_ID, vserverId);
122 ctx.setAttribute(vnfPrefix + ATTR_TENANT_ID, tenantId);
123 ctx.setAttribute(vnfPrefix + ATTR_CLOUD_OWNER, cloudOwner);
124 ctx.setAttribute(vnfPrefix + ATTR_CLOUD_REGION_ID, cloudRegionId);
128 ctx.setAttribute(prefix + "vm-count", String.valueOf(vmCount));
129 log.info("VMCOUNT FROM VNF INFO " + ctx.getAttribute(prefix + "vm-count"));
132 public void getVMInfo(Map<String, String> params, SvcLogicContext ctx)
133 throws SvcLogicException {
136 log.info("Received getVmInfo call with params : " + params);
137 String prefix = params.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
138 prefix = StringUtils.isNotBlank(prefix) ? (prefix + ".") : "";
140 ctx.setAttribute(prefix + ATTR_VNFC_COUNT, String.valueOf(vnfcCount)); // In case no vnfcs are found
142 VServerInfo vServerInfo = new VServerInfo(params);
145 "vserver.vserver-id = '" + vServerInfo.getVserverId() + "' AND tenant.tenant-id = '" + vServerInfo
147 + "' AND cloud-region.cloud-owner = '" + vServerInfo.getCloudOwner()
148 + "' AND cloud-region.cloud-region-id = '"
149 + vServerInfo.getCloudRegionId() + "'";
151 SvcLogicContext vmCtx = readResource(resourceKey, PARAM_VM_INFO, PARAM_VSERVER);
152 ctx.setAttribute(prefix + "vm.prov-status", vmCtx.getAttribute("vmInfo.prov-status"));
153 ctx.setAttribute(prefix + "vm.vserver-name", vmCtx.getAttribute("vmInfo.vserver-name"));
154 ctx.setAttribute(prefix + "vm.vserver-selflink", vmCtx.getAttribute("vmInfo.vserver-selflink"));
156 String relLen = vmCtx.getAttribute("vmInfo.relationship-list.relationship_length");
157 int relationshipLength = 0;
158 if (relLen != null) {
159 relationshipLength = Integer.parseInt(relLen);
161 log.info("RELLEN" + relationshipLength);
162 for (int i = 0; i < relationshipLength; i++) {
163 String vfModuleId = getRelationshipValue(i, vmCtx, "vf-module", "vf-module.vf-module-id",
165 if (vfModuleId != null) {
166 ctx.setAttribute(prefix + "vm.vf-module-id", vfModuleId);
169 String vnfcName = getRelationshipValue(i, vmCtx, "vnfc", "vnfc.vnfc-name", PARAM_VM_INFO);
170 if (vnfcName != null) {
171 ctx.setAttribute(prefix + "vm.vnfc[" + vnfcCount + "].vnfc-name", vnfcName);
175 } // relationshipLength
176 ctx.setAttribute(prefix + ATTR_VNFC_COUNT, String.valueOf(vnfcCount));
177 log.info("VSERVERNAME " + ctx.getAttribute(prefix + "vm.vserver-name") + " HAS NUM VNFCS = "
178 + ctx.getAttribute(prefix + ATTR_VNFC_COUNT));
179 } catch (Exception e) {
180 log.error("An error occurred when fetching Vm info", e);
181 throw new SvcLogicException("Failed to fetch VM info", e);
185 private String getRelationshipValue(int i, SvcLogicContext ctx, String relatedTo, String relationshipKey,
188 if (relatedTo.equals(ctx.getAttribute(prefix + STR_RELATIONSHIP_LIST + i + "].related-to"))) {
189 log.info("RELATEDTO " + relatedTo);
190 int relationshipDataLength = 0;
192 ctx.getAttribute(prefix + STR_RELATIONSHIP_LIST + i + "].relationship-data_length");
193 if (relDataLen != null) {
194 relationshipDataLength = Integer.parseInt(relDataLen);
197 for (int j = 0; j < relationshipDataLength; j++) {
199 String key = ctx.getAttribute(prefix + STR_RELATIONSHIP_LIST + i + "].relationship-data["
200 + j + "].relationship-key");
202 String value = ctx.getAttribute(prefix + STR_RELATIONSHIP_LIST + i + "].relationship-data["
203 + j + "].relationship-value");
205 log.info("GENERIC KEY " + key);
206 log.info("GENERIC VALUE " + value);
208 if (relationshipKey.equals(key)) {
212 } // relationshipDataLength
218 public void getVnfcInfo(Map<String, String> params, SvcLogicContext ctx)
219 throws AaiServiceInternalException, SvcLogicException {
220 log.info("Received getVnfc call with params : " + params);
222 String prefix = params.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
223 prefix = StringUtils.isNotBlank(prefix) ? (prefix + ".") : "";
225 String vnfcName = params.get("vnfcName");
226 if (StringUtils.isBlank(vnfcName)) {
227 throw new AaiServiceInternalException("Vnfc Name is missing");
230 String resourceKey = STR_VNFC_NAME + vnfcName + "'";
231 SvcLogicContext vnfcCtx = readResource(resourceKey, "vnfcInfo", "vnfc");
233 // Changes for US 315820 for 1710 vnfc-type renamed to nfc-function,vnfc-function-code renamed to
236 ctx.setAttribute(prefix + "vnfc.vnfc-type", vnfcCtx.getAttribute("vnfcInfo.nfc-function"));
237 ctx.setAttribute(prefix + "vnfc.vnfc-function-code", vnfcCtx.getAttribute("vnfcInfo.nfc-naming-code"));
238 ctx.setAttribute(prefix + "vnfc.group-notation", vnfcCtx.getAttribute("vnfcInfo.group-notation"));
239 ctx.setAttribute(prefix + "vnfc.ipaddress-v4-oam-vip", vnfcCtx.getAttribute("vnfcInfo.ipaddress-v4-oam-vip"));
243 public void insertVnfcs(Map<String, String> params, SvcLogicContext ctx, int vnfcRefLen, int vmCount,
244 String vfModuleIdFromRequest)
245 throws AaiServiceInternalException, SvcLogicException {
246 log.info("Received insertVnfcs call with params : " + params);
248 String prefix = params.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
250 prefix = StringUtils.isNotBlank(prefix) ? (prefix + ".") : "";
252 log.debug("vnfcRefIndx" + vnfcRefIndx);
253 for (int i = 0; i < vmCount; i++) {
254 String aaiRefKey = prefix + "vm[" + i + "].";
256 //ConfigScaleOut - Do not process vms that are not associated with vfmodule id if vfmodule id is present
257 if (StringUtils.isNotBlank(vfModuleIdFromRequest)) {
258 String vmVfModuleId = ctx.getAttribute(aaiRefKey + "vf-module-id");
259 log.info("insertVnfcs():::vfModule for vm is=" + vmVfModuleId);
260 if (StringUtils.isBlank(vmVfModuleId) || !StringUtils
261 .equalsIgnoreCase(vmVfModuleId, vfModuleIdFromRequest)) {
266 log.info(QUERY_STR_VNFC_NAME + ctx.getAttribute(aaiRefKey + ATTR_VNFC_NAME));
267 String vnfcNameAAI = ctx.getAttribute(aaiRefKey + ATTR_VNFC_NAME);
268 // Get Vnfc_reference data from the table
269 String vnfcRefKey = STR_VNFC_REF + vnfcRefIndx + "].";
271 log.info(STR_VNFC_REF_KEY + vnfcRefKey);
272 log.info(STR_AAI_REF_KEY + aaiRefKey);
274 String groupNotationType = ctx.getAttribute(vnfcRefKey + "GROUP-NOTATION-TYPE");
275 String groupNotationValue = ctx.getAttribute(vnfcRefKey + "GROUP-NOTATION-VALUE");
276 String vnfcType = ctx.getAttribute(vnfcRefKey + "VNFC-TYPE");
277 String vnfcFuncCode = ctx.getAttribute(vnfcRefKey + ATTR_VNFC_FUNC_CODE);
278 String populateIpAddressV4OamVip = ctx.getAttribute(vnfcRefKey + "IPADDRESS-V4-OAM-VIP");
280 // Get vnfc Data to be added
281 String vserverName = ctx.getAttribute(aaiRefKey + ATTR_VSERVER_NAME);
282 String vnfcName = vserverName + vnfcFuncCode + "001";
283 String groupNotation = getGroupNotation(groupNotationType, groupNotationValue, vnfcName, vserverName,
284 prefix, ctx, vnfcType, vnfcFuncCode, vmCount);
286 String ipAddressV4OamVip = null;
287 if ("Y".equals(populateIpAddressV4OamVip)) {
288 ipAddressV4OamVip = ctx.getAttribute("vnf-host-ip-address"); // from input
290 Map<String, String> vnfcParams =
291 populateVnfcParams(ctx, aaiRefKey, ipAddressV4OamVip, groupNotation, vnfcType, vnfcFuncCode);
293 log.info("Vnfc name from AAI: " + vnfcNameAAI);
294 log.info("Vnfc name generated: " + vnfcName);
296 if (StringUtils.isNotBlank(vnfcNameAAI)) {
297 if (vnfcName.equalsIgnoreCase(vnfcNameAAI)) {
298 updateVnfcStatus(vnfcNameAAI, params, prefix);
304 addVnfc(vnfcName, vnfcParams, prefix);
306 // Add VNFC Info to context for current added VNFC
307 ctx.setAttribute(aaiRefKey + ATTR_VNFC_NAME, vnfcName);
308 ctx.setAttribute(aaiRefKey + "vnfc-type", vnfcType);
309 ctx.setAttribute(aaiRefKey + "vnfc-function-code", vnfcFuncCode);
310 ctx.setAttribute(aaiRefKey + "group-notation", groupNotation);
314 public List<String> getVnfcData(Map<String, String> params, SvcLogicContext ctx, int vnfcRefLen, int vmCount) {
316 String prefix = params.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
317 prefix = StringUtils.isNotBlank(prefix) ? (prefix + ".") : "";
318 List<String> vnfcNames = new ArrayList<>();
319 int vnfcRefIndx = -1;
320 for (int i = 0; i < vmCount; i++) {
321 String aaiRefKey = prefix + "vm[" + i + "].";
322 log.info(QUERY_STR_VNFC_NAME + ctx.getAttribute(aaiRefKey + ATTR_VNFC_NAME));
323 if (ctx.getAttribute(aaiRefKey + ATTR_VNFC_NAME) != null) {
328 String vnfcRefKey = STR_VNFC_REF + vnfcRefIndx + "].";
329 log.info(STR_VNFC_REF_KEY + vnfcRefKey);
330 log.info(STR_AAI_REF_KEY + aaiRefKey);
331 String vnfcFuncCode = ctx.getAttribute(vnfcRefKey + ATTR_VNFC_FUNC_CODE);
333 // Get vnfc Data to be added
334 String vserverName = ctx.getAttribute(aaiRefKey + ATTR_VSERVER_NAME);
335 String vnfcName = vserverName + vnfcFuncCode + "001";
337 vnfcNames.add(vnfcName);
343 private Map<String, String> populateVnfcParams(SvcLogicContext ctx, String aaiRefKey, String ipAddressV4OamVip,
344 String groupNotation, String vnfcType, String vnfcFuncCode) {
346 Map<String, String> vnfcParams = new HashMap<>();
348 // Changes for vnfc-type renamed to nfc-function,vnfc-function-code renamed to
350 vnfcParams.put("nfc-naming-code", vnfcFuncCode);
351 vnfcParams.put("nfc-function", vnfcType);
352 vnfcParams.put("ipaddress-v4-oam-vip", ipAddressV4OamVip);
353 vnfcParams.put(PARAM_PROV_STATUS, PARAM_VAL_NVTPROV);
354 vnfcParams.put("orchestration-status", "CONFIGURED");
355 vnfcParams.put("in-maint", "false");
356 vnfcParams.put("is-closed-loop", "false");
357 vnfcParams.put("group-notation", groupNotation);
358 vnfcParams.put("relationship-list.relationship[0].related-to", PARAM_VSERVER);
359 vnfcParams.put("relationship-list.relationship[0].relationship-data[0].relationship-key", "vserver.vserver-id");
360 vnfcParams.put("relationship-list.relationship[0].relationship-data[0].relationship-value",
361 ctx.getAttribute(aaiRefKey + ATTR_VSERVER_ID));
362 vnfcParams.put("relationship-list.relationship[0].relationship-data[1].relationship-key", "tenant.tenant-id");
363 vnfcParams.put("relationship-list.relationship[0].relationship-data[1].relationship-value",
364 ctx.getAttribute(aaiRefKey + ATTR_TENANT_ID));
365 vnfcParams.put("relationship-list.relationship[0].relationship-data[2].relationship-key",
366 "cloud-region.cloud-owner");
367 vnfcParams.put("relationship-list.relationship[0].relationship-data[2].relationship-value",
368 ctx.getAttribute(aaiRefKey + ATTR_CLOUD_OWNER));
369 vnfcParams.put("relationship-list.relationship[0].relationship-data[3].relationship-key",
370 "cloud-region.cloud-region-id");
371 vnfcParams.put("relationship-list.relationship[0].relationship-data[3].relationship-value",
372 ctx.getAttribute(aaiRefKey + ATTR_CLOUD_REGION_ID));
373 vnfcParams.put("relationship-list.relationship[1].related-to", PARAM_GENERIC_VNF);
374 vnfcParams.put("relationship-list.relationship[1].relationship-data[0].relationship-key", "generic-vnf.vnf-id");
375 vnfcParams.put("relationship-list.relationship[1].relationship-data[0].relationship-value",
376 ctx.getAttribute(ATTR_VNF_ID));
377 vnfcParams.put("relationship-list.relationship[2].related-to", "vf-module");
378 vnfcParams.put("relationship-list.relationship[2].relationship-data[0].relationship-key", "generic-vnf.vnf-id");
379 vnfcParams.put("relationship-list.relationship[2].relationship-data[0].relationship-value",
380 ctx.getAttribute(ATTR_VNF_ID));
381 vnfcParams.put("relationship-list.relationship[2].relationship-data[1].relationship-key",
382 "vf-module.vf-module-id");
383 vnfcParams.put("relationship-list.relationship[2].relationship-data[1].relationship-value",
384 ctx.getAttribute(aaiRefKey + "vf-module-id"));
389 public void addVnfc(String vnfcName, Map<String, String> params, String prefix)
390 throws AaiServiceInternalException, SvcLogicException {
392 log.info("Received addVnfc call with vnfcName : " + vnfcName);
393 log.info("Received addVnfc call with params : " + params);
394 String resourceKey = STR_VNFC_NAME + vnfcName + "'";
395 log.info("Received addVnfc call with resourceKey : " + resourceKey);
397 SvcLogicContext vnfcCtx = new SvcLogicContext();
398 SvcLogicResource.QueryStatus response =
399 aaiClient.save("vnfc", true, false, resourceKey, params, prefix, vnfcCtx);
401 if (SvcLogicResource.QueryStatus.SUCCESS.equals(response)) {
402 log.info("Added VNFC SUCCESSFULLY " + vnfcName);
403 } else if (SvcLogicResource.QueryStatus.FAILURE.equals(response)) {
404 throw new AaiServiceInternalException("VNFC Add failed for vnfc_name " + vnfcName);
408 public String getGroupNotation(String groupNotationType, String groupNotationValue, String vnfcName,
409 String vserverName, String prefix, SvcLogicContext ctx, String vnfcRefVnfcType, String vnfcFuncCode,
412 String groupNotation = null;
414 if ("fixed-value".equals(groupNotationType)) {
415 groupNotation = groupNotationValue;
416 } else if ("first-vnfc-name".equals(groupNotationType)) {
419 * If the group-notation-type value = ?first-vnfc-name?,
420 * then populate the group-notation value with the concatenation of
421 * [vnfc name associated with the first vnfc for the vnfc-type (e.g., *******)]
422 * and [the value in group-notation-value (e.g., pair)].
423 * There may be several vnfc-types associated with the VM?s.
425 /* Vnfc-type should be from refrence data */
427 /* vDBE has 2 VNFCs with same VNFC type . The pair name should be same for both . */
429 * When first VNFC is added details should be added to context so FirstVnfcName doesnt return null second
432 String tmpVnfcName = getFirstVnfcNameForVnfcType(ctx, prefix, vnfcRefVnfcType);
434 log.info("RETURNED FIRSTVNFCNAME" + tmpVnfcName);
435 log.info("CURRENTVNFCNAME" + vnfcName);
436 groupNotation = resolveGroupNotation(groupNotationValue, vnfcName, tmpVnfcName);
437 } else if ("relative-value".equals(groupNotationType)) {
440 * If the group-notation-type = ?relative-value?, then find the group-notation value
441 * from the prior vnfc (where prior means the vnfc with where the last three digits of the
442 * vm-name is one lower than the current one; note that this vnfc may have been previously configured.)
443 * 1. If the group-notation-value = next, then add 1 to the group-notation value from the prior vnfc and use
445 * 2. If the group-notation-value = same, then use the group-notation-value from the prior vnfc record
448 // next and same cant be defined for first VM. if next will not generate grpNotation if Prior is not a
450 String tmpVserverName;
451 if (vserverName != null) {
453 String vmNamePrefix = vserverName.substring(0, vserverName.length() - 3);
454 String lastThreeChars = vserverName.substring(vserverName.length() - 3);
456 if (NumberUtils.isDigits(lastThreeChars)) {
457 int vmNum = Integer.parseInt(lastThreeChars) - 1;
458 String formatted = String.format("%03d", vmNum);
460 log.info("FORMATTED " + formatted);
462 tmpVserverName = vmNamePrefix + formatted;
464 String priorGroupNotation = getGroupNotationForVServer(ctx, prefix, tmpVserverName);
465 groupNotation = resolveGroupNotation(groupNotationValue, priorGroupNotation);
468 } else if ("existing-value".equals(groupNotationType)) {
469 /* 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.
470 * Verify that the group-notation value is the same for all such records found in inventory.
471 * 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.
472 * If all records match, use the same group-notation value for the new vnfc record as found in the existing vnfc records.
474 groupNotation = getGroupNotationForExistigValue(ctx, prefix, vnfcFuncCode, vmCount);
477 log.info("RETURNED GROUPNOTATION " + groupNotation);
478 return groupNotation;
481 private String resolveGroupNotation(String groupNotationValue, String vnfcName, String tmpVnfcName) {
482 if (tmpVnfcName == null) {
483 log.info("CURRENTVNFCNAME" + vnfcName);
484 // No Vnfcs currently exist. Use Current vnfcName
485 return vnfcName + groupNotationValue;
487 return tmpVnfcName + groupNotationValue;
491 private String resolveGroupNotation(String groupNotationValue, String priorGroupNotation) {
492 if ("same".equals(groupNotationValue)) {
493 return priorGroupNotation;
494 } else if ("next".equals(groupNotationValue) && priorGroupNotation != null
495 && NumberUtils.isDigits(priorGroupNotation)) {
497 int nextGrpNotation = Integer.parseInt(priorGroupNotation) + 1;
498 return String.valueOf(nextGrpNotation);
503 public String getGroupNotationForExistigValue(SvcLogicContext ctx, String prefix, String vnfcFuncCode,
505 String vfModuleId = ctx.getAttribute("req-vf-module-id"); //Coming from request-params
506 boolean first = true;
507 String aaiGroupNotationValue = null;
508 for (int i = 0; i < vmCount; i++) {
509 String ind = "tmp.vnfInfo.vm[" + i + "].";
510 String aaiFuncCode = ctx.getAttribute(ind + "vnfc-function-code");
511 String aaiGroupNotation = ctx.getAttribute(ind + "group-notation");
512 String aaiVfModuleId = ctx.getAttribute(ind + "vf-module-id");
514 log.info("getGroupNotationForExistigValue()::: vfModuleId=" + vfModuleId + ", aaiFuncCode=" + aaiFuncCode
515 + ", aaiGroupNotation=" + aaiGroupNotation + ",aaiVfMOduleId=" + aaiVfModuleId);
517 if (StringUtils.isNotBlank(aaiFuncCode) && aaiFuncCode.equals(vnfcFuncCode) &&
518 (StringUtils.isNotBlank(vfModuleId) && StringUtils.isNotBlank(aaiVfModuleId) && aaiVfModuleId
519 .equals(vfModuleId))) {
520 if (null == aaiGroupNotationValue && first) {
521 if (null == aaiGroupNotation) {//Return if null
524 aaiGroupNotationValue = ctx.getAttribute(ind + "group-notation");
527 if (!StringUtils.equals(aaiGroupNotationValue, ctx.getAttribute(ind + "group-notation"))) {
528 log.info("Values are different, returning null");
535 return aaiGroupNotationValue;
538 public String getGroupNotationForVServer(SvcLogicContext ctx, String prefix, String vserverName) {
540 String vmCountStr = ctx.getAttribute(prefix + "vnf.vm-count");
542 if (vmCountStr == null) {
546 int vmCount = Integer.parseInt(vmCountStr);
547 for (int i = 0; i < vmCount; i++) {
549 String tmpVserver = ctx.getAttribute(prefix + "vm[" + i + "].vserver-name");
551 if (vserverName.equals(tmpVserver)) {
552 return ctx.getAttribute(prefix + "vm[" + i + "].group-notation");
558 public String getFirstVnfcNameForVnfcType(SvcLogicContext ctx, String prefix, String vnfcRefVnfcType) {
560 String vmCountStr = ctx.getAttribute(prefix + "vnf.vm-count");
561 if (vmCountStr == null) {
564 int vmCount = Integer.parseInt(vmCountStr);
565 for (int i = 0; i < vmCount; i++) {
567 String tmpvnfcType = ctx.getAttribute(prefix + "vm[" + i + "].vnfc-type");
569 if (vnfcRefVnfcType.equals(tmpvnfcType)) {
570 return ctx.getAttribute(prefix + "vm[" + i + "].vnfc-name");
576 public void updateVServerStatus(Map<String, String> params, SvcLogicContext ctx, int vmCount)
577 throws AaiServiceInternalException, SvcLogicException {
578 log.info("Received updateVServerStatus call with params : " + params);
580 String prefix = params.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
582 prefix = StringUtils.isNotBlank(prefix) ? (prefix + ".") : "";
583 Map<String, String> vServerParams = new HashMap<>();
585 // TODO - Should this just update prov-status or both? What about generic-vnf status? Will that be updated by
588 vServerParams.put(PARAM_PROV_STATUS, PARAM_VAL_NVTPROV);
590 for (int i = 0; i < vmCount; i++) {
591 String aaiRefKey = prefix + "vm[" + i + "].";
593 log.info("VNFCNAME IN UpdateVServer " + ctx.getAttribute(aaiRefKey + ATTR_VNFC_NAME));
595 if (ctx.getAttribute(aaiRefKey + ATTR_VNFC_NAME) != null) {
599 String resourceKey = "vserver.vserver-id = '" + ctx.getAttribute(aaiRefKey + ATTR_VSERVER_ID) + "'"
600 + " AND tenant.tenant-id = '" + ctx.getAttribute(aaiRefKey + ATTR_TENANT_ID) + "'"
601 + " AND cloud-region.cloud-owner = '" + ctx.getAttribute(aaiRefKey + ATTR_CLOUD_OWNER) + "'"
602 + " AND cloud-region.cloud-region-id = '" + ctx.getAttribute(aaiRefKey + ATTR_CLOUD_REGION_ID) + "'";
604 updateResource(PARAM_VSERVER, resourceKey, vServerParams);
608 public void updateVnfStatus(Map<String, String> params, SvcLogicContext ctx)
609 throws AaiServiceInternalException, SvcLogicException {
610 log.info("Received updateVnfStatus call with params : " + params);
612 Map<String, String> vnfParams = new HashMap<>();
614 // TODO - Should this just update prov-status or both? What about generic-vnf status? Will that be updated by
617 vnfParams.put(PARAM_PROV_STATUS, PARAM_VAL_NVTPROV);
619 String resourceKey = STR_VNF_ID + ctx.getAttribute(ATTR_VNF_ID) + "'";
621 updateResource(PARAM_GENERIC_VNF, resourceKey, vnfParams);
624 public void updateResource(String resource, String resourceKey, Map<String, String> params)
625 throws AaiServiceInternalException, SvcLogicException {
627 log.info("Received updateResource call with Key : " + resourceKey);
629 SvcLogicContext ctx = new SvcLogicContext();
630 SvcLogicResource.QueryStatus response = aaiClient.update(resource, resourceKey, params, "tmp.update", ctx);
632 if (SvcLogicResource.QueryStatus.SUCCESS.equals(response)) {
633 log.info("Updated " + resource + " SUCCESSFULLY for " + resourceKey);
635 } else if (SvcLogicResource.QueryStatus.FAILURE.equals(response)) {
636 throw new AaiServiceInternalException(resource + " Update failed for " + resourceKey);
640 public SvcLogicContext readResource(String query, String prefix, String resourceType)
641 throws AaiServiceInternalException, SvcLogicException {
642 SvcLogicContext resourceContext = new SvcLogicContext();
644 SvcLogicResource.QueryStatus response =
645 aaiClient.query(resourceType, false, null, query, prefix, null, resourceContext);
646 log.info("AAIResponse: " + response.toString());
647 if (!SvcLogicResource.QueryStatus.SUCCESS.equals(response)) {
648 throw new AaiServiceInternalException("Error Retrieving " + resourceType + " from A&AI");
650 return resourceContext;
653 // Added for Backward Compatibility
654 public void checkAndUpdateVnfc(Map<String, String> params, SvcLogicContext ctx, int vnfcRefLen, int vmCount)
655 throws AaiServiceInternalException, SvcLogicException {
656 log.info("Received checkAndUpdateVnfcStatus call with params : " + params);
658 String prefix = params.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
660 prefix = StringUtils.isNotBlank(prefix) ? (prefix + ".") : "";
662 for (int i = 0; i < vmCount; i++) {
663 String aaiRefKey = prefix + "vm[" + i + "].";
665 log.info(QUERY_STR_VNFC_NAME + aaiRefKey + "vnfc-name:" + ctx.getAttribute(aaiRefKey + ATTR_VNFC_NAME));
667 String vnfcNameAai = ctx.getAttribute(aaiRefKey + ATTR_VNFC_NAME);
669 if (StringUtils.isNotBlank(vnfcNameAai)) {
670 // Get Vnfc_reference data
671 for (int vnfcRefIndx = 0; vnfcRefIndx < vnfcRefLen; vnfcRefIndx++) {
673 String vnfcRefKey = STR_VNFC_REF + vnfcRefIndx + "].";
675 log.info(STR_VNFC_REF_KEY + vnfcRefKey);
676 log.info(STR_AAI_REF_KEY + aaiRefKey);
678 String vnfcFuncCode = ctx.getAttribute(vnfcRefKey + ATTR_VNFC_FUNC_CODE);
679 String vserverName = ctx.getAttribute(aaiRefKey + ATTR_VSERVER_NAME);
680 String vnfcNameReference = vserverName + vnfcFuncCode + "001";
681 tryUpdateVnfcStatus(params, prefix, vnfcNameAai, vnfcNameReference);
687 private void tryUpdateVnfcStatus(Map<String, String> params, String prefix, String vnfcNameAai,
688 String vnfcNameReference) throws AaiServiceInternalException, SvcLogicException {
689 if (vnfcNameAai.equals(vnfcNameReference)) {
690 updateVnfcStatus(vnfcNameAai, params, prefix);
694 public void updateVnfcStatus(String vnfcName, Map<String, String> params, String prefix)
695 throws AaiServiceInternalException, SvcLogicException {
697 log.info("Received updateVnfcStatus call with vnfcName : " + vnfcName);
698 log.info("Received updateVnfcStatus call with params : " + params);
700 String resourceKey = STR_VNFC_NAME + vnfcName + "'";
701 log.info("Received updateVnfcStatus call with resourceKey : " + resourceKey);
703 Map<String, String> vnfcParams = new HashMap<>();
704 vnfcParams.put(PARAM_PROV_STATUS, PARAM_VAL_NVTPROV);
705 vnfcParams.put("orchestration-status", "CONFIGURED");
707 log.info("In updateVnfcStatus call with vnfcParams : " + vnfcParams);
709 updateResource("vnfc", resourceKey, vnfcParams);
711 log.info("End of updateVnfcStatus");
714 public void updateVnfStatusWithOAMAddress(Map<String, String> params, SvcLogicContext ctx)
715 throws AaiServiceInternalException, SvcLogicException {
716 log.info("Received updateVnfStatusWithOAMAddress call with params : " + params);
718 String ipAddress = ctx.getAttribute("vnf-host-ip-address");
719 log.debug("Vnf-host-ip-address" + ipAddress);
721 Map<String, String> vnfParams = new HashMap<>();
722 vnfParams.put("ipv4-oam-address", ipAddress);
723 String resourceKey = STR_VNF_ID + ctx.getAttribute(ATTR_VNF_ID) + "'";
724 updateResource(PARAM_GENERIC_VNF, resourceKey, vnfParams);
727 public void getVfModuleInfo(Map<String, String> params, SvcLogicContext vfModuleCtx) throws Exception {
728 log.info("Received getVfModuleInfo call with params : " + params);
729 String prefix = params.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
730 prefix = StringUtils.isNotBlank(prefix) ? (prefix + ".") : "";
732 String vnfId = params.get("vnfId");
733 String vfModuleId = params.get("vfModuleId");
734 String resourceKey = "generic-vnf.vnf-id = '" + vnfId +
735 "' AND vf-module.vf-module-id = '" + vfModuleId + "'";
736 String queryPrefix = "vfModuleInfo";
737 String resourceType = "vf-module";
738 SvcLogicContext vfmCtx = readResource(resourceKey, queryPrefix, resourceType);
739 String modelInvariantId = vfmCtx.getAttribute("vfModuleInfo.model-invariant-id");
740 log.info("getVfModuleInfo():::modelInvariant=" + modelInvariantId);
741 vfModuleCtx.setAttribute(prefix + "vfModule.model-invariant-id",
742 vfmCtx.getAttribute("vfModuleInfo.model-invariant-id"));
744 .setAttribute(prefix + "vfModule.model-version-id", vfmCtx.getAttribute("vfModuleInfo.model-version-id"));
745 log.info("End - getVfModuleInfo");
748 public void getModelVersionInfo(Map<String, String> modelParams, SvcLogicContext modelCtx) throws Exception {
749 log.info("Received getModelVersionInfo call with params : " + modelParams);
750 String prefix = modelParams.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
751 prefix = StringUtils.isNotBlank(prefix) ? (prefix + ".") : "";
753 String modelInvariantId = modelParams.get("model-invariant-id");
754 String modelVersionId = modelParams.get("model-version-id");
755 String resourceKey = "model.model-invariant-id = '" + modelInvariantId +
756 "' AND model-ver.model-version-id = '" + modelVersionId + "'";
757 String queryPrefix = "modelInfo";
758 String resourceType = "model-ver";
759 SvcLogicContext vfmCtx = readResource(resourceKey, queryPrefix, resourceType);
760 log.info("getModelVersionInfo():::modelname=" + vfmCtx.getAttribute("modelInfo.model-name"));
761 modelCtx.setAttribute(prefix + "vfModule.model-name", vfmCtx.getAttribute("modelInfo.model-name"));
762 log.info("End - getModelVersionInfo");
766 public void getIdentityUrl(Map<String, String> params, SvcLogicContext ctx) throws Exception{
767 log.info("Recieved getIdentityUrl call with params : "+params);
768 String prefix = params.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
769 prefix = StringUtils.isNotBlank(prefix) ? (prefix + ".") : "";
771 String cloudOwner = params.get("cloudOwner");
772 String cloudRegionId = params.get("cloudRegionId");
773 // per comment from git review, we need to sanitize the two parameters
774 // to avoid security issues
775 cloudOwner = cloudOwner.replaceAll("'", "");
776 cloudRegionId = cloudRegionId.replaceAll("'", "");
777 log.debug("cloudOwner" +cloudOwner +"," +"cloudRegionId"+ cloudRegionId);
778 String resourceKey = "depth = '" + 0 + "' AND cloud-region.cloud-owner = '" + cloudOwner +
779 "' AND cloud-region.cloud-region-id = '" + cloudRegionId + "'";
780 String queryPrefix ="urlInfo";
781 String resourceType = "cloud-region";
782 SvcLogicContext urlCtx = readResource(resourceKey, queryPrefix, resourceType);
783 log.info("IdentityUrl: "+urlCtx.getAttribute("urlInfo.identity-url"));
784 ctx.setAttribute(prefix+"cloud-region.identity-url", urlCtx.getAttribute("urlInfo.identity-url"));