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.dg.aai.impl;
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
30 import com.att.eelf.i18n.EELFResourceManager;
31 import java.util.HashMap;
32 import java.util.HashSet;
35 import java.util.stream.Collectors;
36 import org.onap.appc.dg.aai.AAIPlugin;
37 import org.onap.appc.dg.aai.exception.AAIQueryException;
38 import org.onap.appc.dg.aai.objects.AAIQueryResult;
39 import org.onap.appc.dg.aai.objects.Relationship;
40 import org.onap.appc.domainmodel.Vnf;
41 import org.onap.appc.domainmodel.Vnfc;
42 import org.onap.appc.domainmodel.Vserver;
43 import org.onap.appc.exceptions.APPCException;
44 import org.onap.appc.i18n.Msg;
45 import org.onap.ccsdk.sli.adaptors.aai.AAIClient;
46 import org.onap.ccsdk.sli.adaptors.aai.AAIService;
47 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
48 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
49 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
50 import org.osgi.framework.BundleContext;
51 import org.osgi.framework.FrameworkUtil;
52 import org.osgi.framework.ServiceReference;
55 public class AAIPluginImpl implements AAIPlugin {
57 private static final String PARAM_GENERIC_VNF = "generic-vnf";
58 private static final String PARAM_RESOURCE_KEY = "resourceKey";
60 private static final String STR_AAI_RESPONSE = "AAIResponse: ";
61 private static final String STR_VNF_ID = "VNF ID ";
62 private static final String STR_VNF_VNFC = "vnf.vnfc[";
64 private static final String PROPERTY_IN_MAINT = "in-maint";
65 private static final String PROPERTY_PROV_STATUS = "prov-status";
66 private static final String PROPERTY_LOOP_DISABLED = "is-closed-loop-disabled";
67 private static final String PROPERTY_RESOURCE_VERSION = "resource-version";
68 private static final String PROPERTY_VNFC_FUNC_CODE = "vnfc-function-code";
69 private static final String PROPERTY_ORCHESTRATION_STATUS = "orchestration-status";
70 private static final String PROPERTY_VNFC_TYPE = "vnfc-type";
71 private static final String PROPERTY_VNFC_NAME = "vnfc-name";
72 private static final String PROPERTY_VSERVER_ID = "vserver-id";
73 private static final String PROPERTY_VSERVER_SLINK = "vserver-selflink";
74 private static final String PROPERTY_VSERVER_NAME = "vserver-name";
75 private static final String PROPERTY_VSERVER_NAME_2 = "vserver-name2";
76 private static final String PROPERTY_HEAT_STACK_ID = "heat-stack-id";
77 private static final String PROPERTY_VNF_TYPE = "vnf-type";
78 private static final String PROPERTY_VNF_NEM = "vnf-name";
79 private static final String PARAM_RESOURCE_TYPE = "resourceType";
81 private AAIClient aaiClient;
83 private final EELFLogger logger = EELFManager.getInstance().getLogger(AAIPluginImpl.class);
85 public void initialize() {
86 BundleContext bctx = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
87 ServiceReference sref = bctx.getServiceReference(AAIService.class);
88 aaiClient = (AAIClient) bctx.getService(sref);
92 public void postGenericVnfData(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
93 String vnfId = ctx.getAttribute(Constants.VNF_ID_PARAM_NAME);
94 String prefix = ctx.getAttribute(Constants.AAI_PREFIX_PARAM_NAME);
96 String key = "vnf-id = '" + vnfId + "'";
98 Map<String, String> data = new HashMap<>();
99 for (Map.Entry<String, String> entry : params.entrySet()) {
100 String paramKey = entry.getKey();
101 int pos = paramKey.indexOf(Constants.AAI_INPUT_DATA);
103 data.put(paramKey.substring(Constants.AAI_INPUT_DATA.length() + 1), entry.getValue());
108 SvcLogicResource.QueryStatus response = aaiClient.update(PARAM_GENERIC_VNF, key, data, prefix, ctx);
109 if (SvcLogicResource.QueryStatus.NOT_FOUND.equals(response)) {
110 String msg = EELFResourceManager.format(Msg.VNF_NOT_FOUND, vnfId);
111 ctx.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
112 throw new APPCException(msg);
114 logger.info(STR_AAI_RESPONSE + response.toString());
115 if (SvcLogicResource.QueryStatus.FAILURE.equals(response)) {
116 String msg = EELFResourceManager.format(Msg.AAI_QUERY_FAILED, vnfId);
117 ctx.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
118 throw new APPCException(msg);
120 String msg = EELFResourceManager
121 .format(Msg.SUCCESS_EVENT_MESSAGE, "PostGenericVnfData", STR_VNF_ID + vnfId);
122 ctx.setAttribute(org.onap.appc.Constants.ATTRIBUTE_SUCCESS_MESSAGE, msg);
124 } catch (SvcLogicException e) {
125 String msg = EELFResourceManager.format(Msg.AAI_QUERY_FAILED, vnfId);
126 ctx.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
128 throw new APPCException(e);
133 public void getGenericVnfData(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
134 String vnfId = ctx.getAttribute(Constants.VNF_ID_PARAM_NAME);
135 String prefix = ctx.getAttribute(Constants.AAI_PREFIX_PARAM_NAME);
137 String key = "vnf-id = '" + vnfId + "'";
139 SvcLogicResource.QueryStatus response = aaiClient
140 .query(PARAM_GENERIC_VNF, false, null, key, prefix, null, ctx);
141 if (SvcLogicResource.QueryStatus.NOT_FOUND.equals(response)) {
142 String msg = EELFResourceManager.format(Msg.VNF_NOT_FOUND, vnfId);
143 ctx.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
144 throw new APPCException(msg);
145 } else if (SvcLogicResource.QueryStatus.FAILURE.equals(response)) {
146 String msg = EELFResourceManager.format(Msg.AAI_QUERY_FAILED, vnfId);
147 ctx.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
148 throw new APPCException(msg);
150 String aaiEntitlementPoolUuid = ctx.getAttribute(Constants.AAI_ENTITLMENT_POOL_UUID_NAME);
151 if (null == aaiEntitlementPoolUuid) {
152 aaiEntitlementPoolUuid = "";
154 String aaiLicenseKeyGroupUuid = ctx.getAttribute(Constants.AAI_LICENSE_KEY_UUID_NAME);
155 if (null == aaiLicenseKeyGroupUuid) {
156 aaiLicenseKeyGroupUuid = "";
159 ctx.setAttribute(Constants.IS_RELEASE_ENTITLEMENT_REQUIRE,
160 Boolean.toString(!aaiEntitlementPoolUuid.isEmpty()));
161 ctx.setAttribute(Constants.IS_RELEASE_LICENSE_REQUIRE, Boolean.toString(!aaiLicenseKeyGroupUuid.isEmpty()));
162 String msg = EELFResourceManager.format(Msg.SUCCESS_EVENT_MESSAGE, "GetGenericVnfData", STR_VNF_ID + vnfId);
163 ctx.setAttribute(org.onap.appc.Constants.ATTRIBUTE_SUCCESS_MESSAGE, msg);
165 logger.info(STR_AAI_RESPONSE + response.toString());
166 } catch (SvcLogicException e) {
167 String msg = EELFResourceManager.format(Msg.AAI_QUERY_FAILED, vnfId);
168 ctx.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
170 throw new APPCException(e);
175 public void getVnfHierarchy(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
177 Set<Vnfc> vnfcSet = new HashSet<>();
180 String vnfId = params.get(PARAM_RESOURCE_KEY);
181 AAIQueryResult vnfQueryResult;
184 vnfQueryResult = readVnf(vnfId);
186 vnfType = vnfQueryResult.getAdditionProperties().get(PROPERTY_VNF_TYPE);
187 vnfVersion = vnfQueryResult.getAdditionProperties().get(Constants.AAI_VNF_MODEL_VERSION_ID);
189 Vnf vnf = createVnf(vnfType, vnfVersion, vnfId);
191 for (Relationship vnfRelationship : vnfQueryResult.getRelationshipList()) {
192 if ("vserver".equalsIgnoreCase(vnfRelationship.getRelatedTo())) {
194 String tenantId = vnfRelationship.getRelationShipDataMap().get("tenant.tenant-id");
195 String vmId = vnfRelationship.getRelationShipDataMap().get("vserver.vserver-id");
196 String vmRelatedLink = vnfRelationship.getRelatedLink();
197 String vmName = vnfRelationship.getRelatedProperties().get("vserver.vserver-name");
198 String cloudOwner = vnfRelationship.getRelationShipDataMap().get("cloud-region.cloud-owner");
199 String cloudRegionId = vnfRelationship.getRelationShipDataMap().get("cloud-region.cloud-region-id");
201 AAIQueryResult vmQueryResult = readVM(vmId, tenantId, cloudOwner, cloudRegionId);
202 String vmURL = vmQueryResult.getAdditionProperties().get(PROPERTY_VSERVER_SLINK);
204 Vserver vm = createVserver(tenantId, vmId, vmRelatedLink, vmName, vmURL);
206 for (Relationship vmRelation : vmQueryResult.getRelationshipList()) {
208 if ("vnfc".equalsIgnoreCase(vmRelation.getRelatedTo())) {
209 String vnfcName = vmRelation.getRelationShipDataMap().get("vnfc.vnfc-name");
210 AAIQueryResult vnfcQueryResult = readVnfc(vnfcName);
211 String vnfcType = vnfcQueryResult.getAdditionProperties().get(PROPERTY_VNFC_TYPE);
213 Vnfc newVnfc = createVnfc(vnfcName, vnfcType);
215 if (vnfcSet.contains(newVnfc)) {
216 Vnfc vnfcFromSet = vnfcSet.stream().filter(vnfc -> vnfc.equals(newVnfc))
217 .collect(Collectors.toList()).get(0);
218 vnfcFromSet.addVserver(vm);
219 vm.setVnfc(vnfcFromSet);
222 newVnfc.addVserver(vm);
223 vnfcSet.add(newVnfc);
229 ctx.setAttribute("VNF.VMCount", String.valueOf(vmCount));
230 populateContext(vnf, ctx);
231 } catch (AAIQueryException e) {
232 ctx.setAttribute("getVnfHierarchy_result", "FAILURE");
233 String msg = EELFResourceManager.format(Msg.AAI_QUERY_FAILED, vnfId);
234 ctx.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
235 logger.error("Failed in getVnfHierarchy, Error retrieving VNF details. Error message: " + ctx
236 .getAttribute("getResource_result"), e);
237 logger.warn("Incorrect or Incomplete VNF Hierarchy");
238 throw new APPCException("Error Retrieving VNF hierarchy");
240 ctx.setAttribute("getVnfHierarchy_result", "SUCCESS");
241 String msg = EELFResourceManager.format(Msg.SUCCESS_EVENT_MESSAGE, "GetVNFHierarchy", STR_VNF_ID + vnfId);
242 ctx.setAttribute(org.onap.appc.Constants.ATTRIBUTE_SUCCESS_MESSAGE, msg);
246 private Vnf createVnf(String vnfType, String vnfVersion, String vnfId) {
249 vnf.setVnfType(vnfType);
250 vnf.setVnfVersion(vnfVersion);
254 private Vnfc createVnfc(String vnfcName, String vnfcType) {
255 Vnfc vnfc = new Vnfc();
256 vnfc.setVnfcName(vnfcName);
257 vnfc.setVnfcType(vnfcType);
261 private Vserver createVserver(String tenantId, String vmId, String vmRelatedLink, String vmName, String vmURL) {
262 Vserver vserver = new Vserver();
263 vserver.setTenantId(tenantId);
265 vserver.setRelatedLink(vmRelatedLink);
266 vserver.setName(vmName);
267 vserver.setUrl(vmURL);
271 private void populateContext(Vnf vnf, SvcLogicContext ctx) {
272 ctx.setAttribute("vnf.type", vnf.getVnfType());
273 ctx.setAttribute("vnf.version", vnf.getVnfVersion());
274 ctx.setAttribute("vnf.vnfcCount", String.valueOf(vnf.getVnfcs().size()));
276 for (Vnfc vnfc : vnf.getVnfcs()) {
277 ctx.setAttribute(STR_VNF_VNFC + vnfcCount + "].name", vnfc.getVnfcName());
278 ctx.setAttribute(STR_VNF_VNFC + vnfcCount + "].type", vnfc.getVnfcType());
279 ctx.setAttribute(STR_VNF_VNFC + vnfcCount + "].vm_count", String.valueOf(vnfc.getVserverList().size()));
281 for (Vserver vm : vnfc.getVserverList()) {
282 ctx.setAttribute(STR_VNF_VNFC + vnfcCount + "].vm[" + vmCount++ + "].url", vm.getUrl());
288 private AAIQueryResult readVnfc(String vnfcName) throws AAIQueryException {
289 String query = "vnfc.vnfc-name = '" + vnfcName + "'";
290 String prefix = "VNFC";
291 String resourceType = "vnfc";
292 SvcLogicContext vnfContext = readResource(query, prefix, resourceType);
293 String[] additionalProperties = new String[]{PROPERTY_VNFC_TYPE, PROPERTY_VNFC_NAME,
294 PROPERTY_VNFC_FUNC_CODE, PROPERTY_IN_MAINT, PROPERTY_PROV_STATUS,
295 PROPERTY_LOOP_DISABLED, PROPERTY_ORCHESTRATION_STATUS, PROPERTY_RESOURCE_VERSION};
296 return readRelationDataAndProperties(prefix, vnfContext, additionalProperties);
299 protected AAIQueryResult readVM(String vmId, String tenantId, String cloudOwner, String cloudRegionId)
300 throws AAIQueryException {
301 String query = "vserver.vserver-id = '" + vmId + "' AND tenant.tenant_id = '" + tenantId
302 + "' AND cloud-region.cloud-owner = '"
303 + cloudOwner + "' AND cloud-region.cloud-region-id = '" + cloudRegionId + "'";
304 String prefix = "VM";
305 String resourceType = "vserver";
306 SvcLogicContext vnfContext = readResource(query, prefix, resourceType);
307 String[] additionalProperties = new String[]{PROPERTY_VSERVER_ID, PROPERTY_VSERVER_SLINK,
308 PROPERTY_VSERVER_NAME, PROPERTY_IN_MAINT, PROPERTY_PROV_STATUS, PROPERTY_LOOP_DISABLED,
309 PROPERTY_VSERVER_NAME_2, PROPERTY_RESOURCE_VERSION,};
311 return readRelationDataAndProperties(prefix, vnfContext, additionalProperties);
314 protected AAIQueryResult readVnf(String vnfId) throws AAIQueryException {
315 String query = "generic-vnf.vnf-id = '" + vnfId + "'";
316 String prefix = "VNF";
317 SvcLogicContext vnfContext = readResource(query, prefix, PARAM_GENERIC_VNF);
319 String[] additionalProperties = new String[]{PROPERTY_VNF_TYPE, PROPERTY_VNF_NEM,
320 PROPERTY_IN_MAINT, PROPERTY_PROV_STATUS, PROPERTY_HEAT_STACK_ID,
321 PROPERTY_LOOP_DISABLED, PROPERTY_ORCHESTRATION_STATUS, PROPERTY_RESOURCE_VERSION, Constants.AAI_VNF_MODEL_VERSION_ID};
323 return readRelationDataAndProperties(prefix, vnfContext, additionalProperties);
326 private AAIQueryResult readRelationDataAndProperties(String prefix, SvcLogicContext context,
327 String[] additionalProperties) {
328 AAIQueryResult result = new AAIQueryResult();
329 if (context != null && context.getAttribute(prefix + ".relationship-list.relationship_length") != null) {
330 Integer relationsCount = Integer.parseInt(context.getAttribute(
331 prefix + ".relationship-list.relationship_length"));
332 for (int i = 0; i < relationsCount; i++) {
333 String rsKey = prefix + ".relationship-list.relationship[" + i + "]";
334 Relationship relationShip = new Relationship();
335 relationShip.setRelatedLink(context.getAttribute(rsKey + ".related-link"));
336 relationShip.setRelatedTo(context.getAttribute(rsKey + ".related-to"));
337 Integer relationDataCount = Integer.parseInt(context.getAttribute(rsKey + ".relationship-data_length"));
338 for (int j = 0; j < relationDataCount; j++) {
339 String rsDataKey = rsKey + ".relationship-data[" + j + "]";
340 String key = context.getAttribute(rsDataKey + ".relationship-key");
341 String value = context.getAttribute(rsDataKey + ".relationship-value");
342 relationShip.getRelationShipDataMap().put(key, value);
344 Integer relatedPropertyCount = 0;
345 String relatedPropertyCountStr = null;
347 relatedPropertyCountStr = context.getAttribute(rsKey + ".related-to-property_length");
348 relatedPropertyCount = Integer.parseInt(relatedPropertyCountStr);
349 } catch (NumberFormatException e) {
350 logger.debug("Invalid value in the context for Related Property Count " + relatedPropertyCountStr);
353 for (int j = 0; j < relatedPropertyCount; j++) {
354 String rsPropKey = rsKey + ".related-to-property[" + j + "]";
355 String key = context.getAttribute(rsPropKey + ".property-key");
356 String value = context.getAttribute(rsPropKey + ".property-value");
357 relationShip.getRelatedProperties().put(key, value);
359 result.getRelationshipList().add(relationShip);
362 logger.error("Relationship-list not present in the SvcLogicContext attributes set."
363 + (context == null ? "" : "Attribute KeySet = " + context.getAttributeKeySet()));
366 if (context != null) {
367 for (String key : additionalProperties) {
368 result.getAdditionProperties().put(key, context.getAttribute(prefix + "." + key));
374 protected SvcLogicContext readResource(String query, String prefix, String resourceType) throws AAIQueryException {
375 SvcLogicContext resourceContext = new SvcLogicContext();
377 SvcLogicResource.QueryStatus response = aaiClient
378 .query(resourceType, false, null, query, prefix, null, resourceContext);
379 logger.info(STR_AAI_RESPONSE + response.toString());
380 if (!SvcLogicResource.QueryStatus.SUCCESS.equals(response)) {
381 throw new AAIQueryException("Error Retrieving VNF hierarchy from A&AI");
383 } catch (SvcLogicException e) {
384 logger.error(EELFResourceManager.format(Msg.AAI_GET_DATA_FAILED, query), e);
385 throw new AAIQueryException("Error Retrieving VNF hierarchy from A&AI");
387 return resourceContext;
391 public void getResource(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
392 String resourceType = params.get(PARAM_RESOURCE_TYPE);
393 String ctxPrefix = params.get("prefix");
394 String resourceKey = params.get(PARAM_RESOURCE_KEY);
395 if (logger.isDebugEnabled()) {
396 logger.debug("inside getResorce");
397 logger.debug("Retrieving " + resourceType + " details from A&AI for Key : " + resourceKey);
400 SvcLogicResource.QueryStatus response =
401 aaiClient.query(resourceType, false, null, resourceKey, ctxPrefix, null, ctx);
402 logger.info(STR_AAI_RESPONSE + response.toString());
403 ctx.setAttribute("getResource_result", response.toString());
404 } catch (SvcLogicException e) {
405 logger.error(EELFResourceManager.format(Msg.AAI_GET_DATA_FAILED, resourceKey), e);
407 if (logger.isDebugEnabled()) {
408 logger.debug("exiting getResource======");
413 public void postResource(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
414 String resourceType = params.get(PARAM_RESOURCE_TYPE);
415 String ctxPrefix = params.get("prefix");
416 String resourceKey = params.get(PARAM_RESOURCE_KEY);
417 String attributeName = params.get("attributeName");
418 String attributeValue = params.get("attributeValue");
419 if (logger.isDebugEnabled()) {
420 logger.debug("inside postResource");
421 logger.debug("Updating " + resourceType + " details in A&AI for Key : " + resourceKey);
422 logger.debug("Updating " + attributeName + " to : " + attributeValue);
424 Map<String, String> data = new HashMap<>();
425 data.put(attributeName, attributeValue);
428 SvcLogicResource.QueryStatus response = aaiClient.update(resourceType, resourceKey, data, ctxPrefix, ctx);
429 logger.info(STR_AAI_RESPONSE + response.toString());
430 ctx.setAttribute("postResource_result", response.toString());
431 } catch (SvcLogicException e) {
432 logger.error(EELFResourceManager.format(Msg.AAI_UPDATE_FAILED, resourceKey, attributeValue), e);
434 if (logger.isDebugEnabled()) {
435 logger.debug("exiting postResource======");
440 public void deleteResource(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
441 String resourceType = params.get(PARAM_RESOURCE_TYPE);
442 String resourceKey = params.get(PARAM_RESOURCE_KEY);
444 if (logger.isDebugEnabled()) {
445 logger.debug("inside deleteResource");
446 logger.debug("Deleting " + resourceType + " details From A&AI for Key : " + resourceKey);
449 SvcLogicResource.QueryStatus response = aaiClient.delete(resourceType, resourceKey, ctx);
450 logger.info(STR_AAI_RESPONSE + response.toString());
451 ctx.setAttribute("deleteResource_result", response.toString());
452 } catch (SvcLogicException e) {
453 logger.error(EELFResourceManager.format(Msg.AAI_DELETE_FAILED, resourceKey), e);
455 if (logger.isDebugEnabled()) {
456 logger.debug("exiting deleteResource======");