2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 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.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.dg.aai.impl;
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import com.att.eelf.i18n.EELFResourceManager;
30 import java.util.HashMap;
31 import java.util.HashSet;
34 import java.util.stream.Collectors;
35 import org.onap.appc.dg.aai.AAIPlugin;
36 import org.onap.appc.dg.aai.exception.AAIQueryException;
37 import org.onap.appc.dg.aai.objects.AAIQueryResult;
38 import org.onap.appc.dg.aai.objects.Relationship;
39 import org.onap.appc.domainmodel.Vnf;
40 import org.onap.appc.domainmodel.Vnfc;
41 import org.onap.appc.domainmodel.Vserver;
42 import org.onap.appc.exceptions.APPCException;
43 import org.onap.appc.i18n.Msg;
44 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
45 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
46 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
47 import org.onap.ccsdk.sli.adaptors.aai.AAIClient;
48 import org.onap.ccsdk.sli.adaptors.aai.AAIService;
49 import org.osgi.framework.BundleContext;
50 import org.osgi.framework.FrameworkUtil;
51 import org.osgi.framework.ServiceReference;
54 public class AAIPluginImpl implements AAIPlugin {
56 private static final String PARAM_GENERIC_VNF = "generic-vnf";
57 private static final String PARAM_RESOURCE_KEY = "resourceKey";
59 private static final String STR_AAI_RESPONSE = "AAIResponse: ";
60 private static final String STR_VNF_ID = "VNF ID ";
61 private static final String STR_VNF_VNFC = "vnf.vnfc[";
63 private static final String PROPERTY_IN_MAINT = "in-maint";
64 private static final String PROPERTY_PROV_STATUS = "prov-status";
65 private static final String PROPERTY_LOOP_DISABLED = "is-closed-loop-disabled";
66 private static final String PROPERTY_RESOURCE_VERSION = "resource-version";
67 private static final String PROPERTY_VNFC_FUNC_CODE = "vnfc-function-code";
68 private static final String PROPERTY_ORCHESTRATION_STATUS = "orchestration-status";
69 private static final String PROPERTY_VNFC_TYPE = "vnfc-type";
70 private static final String PROPERTY_VNFC_NAME = "vnfc-name";
71 private static final String PROPERTY_VSERVER_ID = "vserver-id";
72 private static final String PROPERTY_VSERVER_SLINK = "vserver-selflink";
73 private static final String PROPERTY_VSERVER_NAME = "vserver-name";
74 private static final String PROPERTY_VSERVER_NAME_2 = "vserver-name2";
75 private static final String PROPERTY_HEAT_STACK_ID = "heat-stack-id";
76 private static final String PROPERTY_VNF_TYPE = "vnf-type";
77 private static final String PROPERTY_VNF_NEM = "vnf-name";
78 private static final String PARAM_RESOURCE_TYPE = "resourceType";
80 private AAIClient aaiClient;
82 private final EELFLogger logger = EELFManager.getInstance().getLogger(AAIPluginImpl.class);
84 public void initialize() {
85 BundleContext bctx = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
86 ServiceReference sref = bctx.getServiceReference(AAIService.class);
87 aaiClient = (AAIClient) bctx.getService(sref);
91 public void postGenericVnfData(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
92 String vnfId = ctx.getAttribute(Constants.VNF_ID_PARAM_NAME);
93 String prefix = ctx.getAttribute(Constants.AAI_PREFIX_PARAM_NAME);
95 String key = "vnf-id = '" + vnfId + "'";
97 Map<String, String> data = new HashMap<>();
98 for (Map.Entry<String, String> entry : params.entrySet()) {
99 String paramKey = entry.getKey();
100 int pos = paramKey.indexOf(Constants.AAI_INPUT_DATA);
102 data.put(paramKey.substring(Constants.AAI_INPUT_DATA.length() + 1), entry.getValue());
107 SvcLogicResource.QueryStatus response = aaiClient.update(PARAM_GENERIC_VNF, key, data, prefix, ctx);
108 if (SvcLogicResource.QueryStatus.NOT_FOUND.equals(response)) {
109 String msg = EELFResourceManager.format(Msg.VNF_NOT_FOUND, vnfId);
110 ctx.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
111 throw new APPCException(msg);
113 logger.info(STR_AAI_RESPONSE + response.toString());
114 if (SvcLogicResource.QueryStatus.FAILURE.equals(response)) {
115 String msg = EELFResourceManager.format(Msg.AAI_QUERY_FAILED, vnfId);
116 ctx.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
117 throw new APPCException(msg);
120 EELFResourceManager.format(Msg.SUCCESS_EVENT_MESSAGE, "PostGenericVnfData", STR_VNF_ID + vnfId);
121 ctx.setAttribute(org.onap.appc.Constants.ATTRIBUTE_SUCCESS_MESSAGE, msg);
123 } catch (SvcLogicException e) {
124 String msg = EELFResourceManager.format(Msg.AAI_QUERY_FAILED, vnfId);
125 ctx.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
127 throw new APPCException(e);
132 public void getGenericVnfData(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
133 String vnfId = ctx.getAttribute(Constants.VNF_ID_PARAM_NAME);
134 String prefix = ctx.getAttribute(Constants.AAI_PREFIX_PARAM_NAME);
136 String key = "vnf-id = '" + vnfId + "'";
138 SvcLogicResource.QueryStatus response =
139 aaiClient.query(PARAM_GENERIC_VNF, false, null, key, prefix, null, ctx);
140 if (SvcLogicResource.QueryStatus.NOT_FOUND.equals(response)) {
141 String msg = EELFResourceManager.format(Msg.VNF_NOT_FOUND, vnfId);
142 ctx.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
143 throw new APPCException(msg);
144 } else if (SvcLogicResource.QueryStatus.FAILURE.equals(response)) {
145 String msg = EELFResourceManager.format(Msg.AAI_QUERY_FAILED, vnfId);
146 ctx.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
147 throw new APPCException(msg);
149 String aaiEntitlementPoolUuid = ctx.getAttribute(Constants.AAI_ENTITLMENT_POOL_UUID_NAME);
150 if (null == aaiEntitlementPoolUuid) {
151 aaiEntitlementPoolUuid = "";
153 String aaiLicenseKeyGroupUuid = ctx.getAttribute(Constants.AAI_LICENSE_KEY_UUID_NAME);
154 if (null == aaiLicenseKeyGroupUuid) {
155 aaiLicenseKeyGroupUuid = "";
158 ctx.setAttribute(Constants.IS_RELEASE_ENTITLEMENT_REQUIRE,
159 Boolean.toString(!aaiEntitlementPoolUuid.isEmpty()));
160 ctx.setAttribute(Constants.IS_RELEASE_LICENSE_REQUIRE, Boolean.toString(!aaiLicenseKeyGroupUuid.isEmpty()));
161 String msg = EELFResourceManager.format(Msg.SUCCESS_EVENT_MESSAGE, "GetGenericVnfData", STR_VNF_ID + vnfId);
162 ctx.setAttribute(org.onap.appc.Constants.ATTRIBUTE_SUCCESS_MESSAGE, msg);
164 logger.info(STR_AAI_RESPONSE + response.toString());
165 } catch (SvcLogicException e) {
166 String msg = EELFResourceManager.format(Msg.AAI_QUERY_FAILED, vnfId);
167 ctx.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
169 throw new APPCException(e);
174 public void getVnfHierarchy(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
176 Set<Vnfc> vnfcSet = new HashSet<>();
179 String vnfId = params.get(PARAM_RESOURCE_KEY);
180 AAIQueryResult vnfQueryResult;
183 vnfQueryResult = readVnf(vnfId);
185 vnfType = vnfQueryResult.getAdditionProperties().get(PROPERTY_VNF_TYPE);
186 vnfVersion = vnfQueryResult.getAdditionProperties().get(Constants.AAI_VNF_MODEL_VERSION_ID);
188 Vnf vnf = createVnf(vnfType, vnfVersion, vnfId);
190 for (Relationship vnfRelationship : vnfQueryResult.getRelationshipList()) {
191 if ("vserver".equalsIgnoreCase(vnfRelationship.getRelatedTo())) {
193 String tenantId = vnfRelationship.getRelationShipDataMap().get("tenant.tenant-id");
194 String vmId = vnfRelationship.getRelationShipDataMap().get("vserver.vserver-id");
195 String vmRelatedLink = vnfRelationship.getRelatedLink();
196 String vmName = vnfRelationship.getRelatedProperties().get("vserver.vserver-name");
197 String cloudOwner = vnfRelationship.getRelationShipDataMap().get("cloud-region.cloud-owner");
198 String cloudRegionId = vnfRelationship.getRelationShipDataMap().get("cloud-region.cloud-region-id");
200 AAIQueryResult vmQueryResult = readVM(vmId, tenantId, cloudOwner, cloudRegionId);
201 String vmURL = vmQueryResult.getAdditionProperties().get(PROPERTY_VSERVER_SLINK);
203 Vserver vm = createVserver(tenantId, vmId, vmRelatedLink, vmName, vmURL);
205 for (Relationship vmRelation : vmQueryResult.getRelationshipList()) {
207 if ("vnfc".equalsIgnoreCase(vmRelation.getRelatedTo())) {
208 String vnfcName = vmRelation.getRelationShipDataMap().get("vnfc.vnfc-name");
209 AAIQueryResult vnfcQueryResult = readVnfc(vnfcName);
210 String vnfcType = vnfcQueryResult.getAdditionProperties().get(PROPERTY_VNFC_TYPE);
212 Vnfc newVnfc = createVnfc(vnfcName, vnfcType);
214 if (vnfcSet.contains(newVnfc)) {
216 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: "
236 + ctx.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 = '" + cloudOwner
303 + "' 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,
322 Constants.AAI_VNF_MODEL_VERSION_ID};
324 return readRelationDataAndProperties(prefix, vnfContext, additionalProperties);
327 private AAIQueryResult readRelationDataAndProperties(String prefix, SvcLogicContext context,
328 String[] additionalProperties) {
329 AAIQueryResult result = new AAIQueryResult();
330 if (context != null && context.getAttribute(prefix + ".relationship-list.relationship_length") != null) {
331 Integer relationsCount =
332 Integer.parseInt(context.getAttribute(prefix + ".relationship-list.relationship_length"));
333 for (int i = 0; i < relationsCount; i++) {
334 String rsKey = prefix + ".relationship-list.relationship[" + i + "]";
335 Relationship relationShip = new Relationship();
336 relationShip.setRelatedLink(context.getAttribute(rsKey + ".related-link"));
337 relationShip.setRelatedTo(context.getAttribute(rsKey + ".related-to"));
338 Integer relationDataCount = Integer.parseInt(context.getAttribute(rsKey + ".relationship-data_length"));
339 for (int j = 0; j < relationDataCount; j++) {
340 String rsDataKey = rsKey + ".relationship-data[" + j + "]";
341 String key = context.getAttribute(rsDataKey + ".relationship-key");
342 String value = context.getAttribute(rsDataKey + ".relationship-value");
343 relationShip.getRelationShipDataMap().put(key, value);
345 Integer relatedPropertyCount = 0;
346 String relatedPropertyCountStr = null;
348 relatedPropertyCountStr = context.getAttribute(rsKey + ".related-to-property_length");
349 relatedPropertyCount = Integer.parseInt(relatedPropertyCountStr);
350 } catch (NumberFormatException e) {
351 logger.debug("Invalid value in the context for Related Property Count " + relatedPropertyCountStr);
354 for (int j = 0; j < relatedPropertyCount; j++) {
355 String rsPropKey = rsKey + ".related-to-property[" + j + "]";
356 String key = context.getAttribute(rsPropKey + ".property-key");
357 String value = context.getAttribute(rsPropKey + ".property-value");
358 relationShip.getRelatedProperties().put(key, value);
360 result.getRelationshipList().add(relationShip);
363 logger.error("Relationship-list not present in the SvcLogicContext attributes set."
364 + (context == null ? "" : " Attribute KeySet = " + context.getAttributeKeySet()));
367 if (context != null) {
368 for (String key : additionalProperties) {
369 result.getAdditionProperties().put(key, context.getAttribute(prefix + "." + key));
375 protected SvcLogicContext readResource(String query, String prefix, String resourceType) throws AAIQueryException {
376 SvcLogicContext resourceContext = new SvcLogicContext();
378 SvcLogicResource.QueryStatus response =
379 aaiClient.query(resourceType, false, null, query, prefix, null, resourceContext);
380 logger.info(STR_AAI_RESPONSE + response.toString());
381 if (!SvcLogicResource.QueryStatus.SUCCESS.equals(response)) {
382 throw new AAIQueryException("Error Retrieving VNF hierarchy from A&AI");
384 } catch (SvcLogicException e) {
385 logger.error(EELFResourceManager.format(Msg.AAI_GET_DATA_FAILED, query), e);
386 throw new AAIQueryException("Error Retrieving VNF hierarchy from A&AI");
388 return resourceContext;
392 public void getResource(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
393 String resourceType = params.get(PARAM_RESOURCE_TYPE);
394 String ctxPrefix = params.get("prefix");
395 String resourceKey = params.get(PARAM_RESOURCE_KEY);
396 if (logger.isDebugEnabled()) {
397 logger.debug("inside getResource");
398 logger.debug("Retrieving " + resourceType + " details from A&AI for Key: " + resourceKey);
401 SvcLogicResource.QueryStatus response =
402 aaiClient.query(resourceType, false, null, resourceKey, ctxPrefix, null, ctx);
403 logger.info(STR_AAI_RESPONSE + response.toString());
404 ctx.setAttribute("getResource_result", response.toString());
405 } catch (SvcLogicException e) {
406 logger.error(EELFResourceManager.format(Msg.AAI_GET_DATA_FAILED, resourceKey), e);
408 logger.debug("exiting getResource======");
412 public void postResource(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
413 String resourceType = params.get(PARAM_RESOURCE_TYPE);
414 String ctxPrefix = params.get("prefix");
415 String resourceKey = params.get(PARAM_RESOURCE_KEY);
416 String attributeName = params.get("attributeName");
417 String attributeValue = params.get("attributeValue");
418 if (logger.isDebugEnabled()) {
419 logger.debug("inside postResource");
420 logger.debug("Updating " + resourceType + " details in A&AI for Key: " + resourceKey);
421 logger.debug("Updating " + attributeName + " to: " + attributeValue);
423 Map<String, String> data = new HashMap<>();
424 data.put(attributeName, attributeValue);
427 SvcLogicResource.QueryStatus response = aaiClient.update(resourceType, resourceKey, data, ctxPrefix, ctx);
428 logger.info(STR_AAI_RESPONSE + response.toString());
429 ctx.setAttribute("postResource_result", response.toString());
430 } catch (SvcLogicException e) {
431 logger.error(EELFResourceManager.format(Msg.AAI_UPDATE_FAILED, resourceKey, attributeValue), e);
433 logger.debug("exiting postResource======");
437 public void deleteResource(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
438 String resourceType = params.get(PARAM_RESOURCE_TYPE);
439 String resourceKey = params.get(PARAM_RESOURCE_KEY);
441 if (logger.isDebugEnabled()) {
442 logger.debug("inside deleteResource");
443 logger.debug("Deleting " + resourceType + " details from A&AI for Key: " + resourceKey);
446 SvcLogicResource.QueryStatus response = aaiClient.delete(resourceType, resourceKey, ctx);
447 logger.info(STR_AAI_RESPONSE + response.toString());
448 ctx.setAttribute("deleteResource_result", response.toString());
449 } catch (SvcLogicException e) {
450 logger.error(EELFResourceManager.format(Msg.AAI_DELETE_FAILED, resourceKey), e);
452 logger.debug("exiting deleteResource======");