2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.dg.aai.impl;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.att.eelf.i18n.EELFResourceManager;
29 import java.util.HashMap;
30 import java.util.HashSet;
33 import java.util.stream.Collectors;
34 import org.onap.appc.dg.aai.AAIPlugin;
35 import org.onap.appc.dg.aai.exception.AAIQueryException;
36 import org.onap.appc.dg.aai.objects.AAIQueryResult;
37 import org.onap.appc.dg.aai.objects.Relationship;
38 import org.onap.appc.domainmodel.Vnf;
39 import org.onap.appc.domainmodel.Vnfc;
40 import org.onap.appc.domainmodel.Vserver;
41 import org.onap.appc.exceptions.APPCException;
42 import org.onap.appc.i18n.Msg;
43 import org.onap.ccsdk.sli.adaptors.aai.AAIClient;
44 import org.onap.ccsdk.sli.adaptors.aai.AAIService;
45 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
46 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
47 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
48 import org.osgi.framework.BundleContext;
49 import org.osgi.framework.FrameworkUtil;
50 import org.osgi.framework.ServiceReference;
53 public class AAIPluginImpl implements AAIPlugin {
55 private static final String PARAM_GENERIC_VNF = "generic-vnf";
56 private static final String PARAM_RESOURCE_KEY = "resourceKey";
58 private static final String STR_AAI_RESPONSE = "AAIResponse: ";
59 private static final String STR_VNF_ID = "VNF ID ";
60 private static final String STR_VNF_VNFC = "vnf.vnfc[";
62 private static final String PROPERTY_IN_MAINT = "in-maint";
63 private static final String PROPERTY_PROV_STATUS = "prov-status";
64 private static final String PROPERTY_LOOP_DISABLED = "is-closed-loop-disabled";
65 private static final String PROPERTY_RESOURCE_VERSION = "resource-version";
66 private static final String PROPERTY_VNFC_FUNC_CODE = "vnfc-function-code";
67 private static final String PROPERTY_ORCHESTRATION_STATUS = "orchestration-status";
68 private static final String PROPERTY_VNFC_TYPE = "vnfc-type";
69 private static final String PROPERTY_VNFC_NAME = "vnfc-name";
70 private static final String PROPERTY_VSERVER_ID = "vserver-id";
71 private static final String PROPERTY_VSERVER_SLINK = "vserver-selflink";
72 private static final String PROPERTY_VSERVER_NAME = "vserver-name";
73 private static final String PROPERTY_VSERVER_NAME_2 = "vserver-name2";
74 private static final String PROPERTY_HEAT_STACK_ID = "heat-stack-id";
75 private static final String PROPERTY_VNF_TYPE = "vnf-type";
76 private static final String PROPERTY_VNF_NEM = "vnf-name";
77 private static final String PARAM_RESOURCE_TYPE = "resourceType";
79 private AAIClient aaiClient;
81 private final EELFLogger logger = EELFManager.getInstance().getLogger(AAIPluginImpl.class);
83 public void initialize() {
84 BundleContext bctx = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
85 ServiceReference sref = bctx.getServiceReference(AAIService.class);
86 aaiClient = (AAIClient) bctx.getService(sref);
90 public void postGenericVnfData(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
91 String vnfId = ctx.getAttribute(Constants.VNF_ID_PARAM_NAME);
92 String prefix = ctx.getAttribute(Constants.AAI_PREFIX_PARAM_NAME);
94 String key = "vnf-id = '" + vnfId + "'";
96 Map<String, String> data = new HashMap<>();
97 for (Map.Entry<String, String> entry : params.entrySet()) {
98 String paramKey = entry.getKey();
99 int pos = paramKey.indexOf(Constants.AAI_INPUT_DATA);
101 data.put(paramKey.substring(Constants.AAI_INPUT_DATA.length() + 1), entry.getValue());
106 SvcLogicResource.QueryStatus response = aaiClient.update(PARAM_GENERIC_VNF, key, data, prefix, ctx);
107 if (SvcLogicResource.QueryStatus.NOT_FOUND.equals(response)) {
108 String msg = EELFResourceManager.format(Msg.VNF_NOT_FOUND, vnfId);
109 ctx.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
110 throw new APPCException(msg);
112 logger.info(STR_AAI_RESPONSE + response.toString());
113 if (SvcLogicResource.QueryStatus.FAILURE.equals(response)) {
114 String msg = EELFResourceManager.format(Msg.AAI_QUERY_FAILED, vnfId);
115 ctx.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
116 throw new APPCException(msg);
118 String msg = EELFResourceManager
119 .format(Msg.SUCCESS_EVENT_MESSAGE, "PostGenericVnfData", STR_VNF_ID + vnfId);
120 ctx.setAttribute(org.onap.appc.Constants.ATTRIBUTE_SUCCESS_MESSAGE, msg);
122 } catch (SvcLogicException e) {
123 String msg = EELFResourceManager.format(Msg.AAI_QUERY_FAILED, vnfId);
124 ctx.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
126 throw new APPCException(e);
131 public void getGenericVnfData(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
132 String vnfId = ctx.getAttribute(Constants.VNF_ID_PARAM_NAME);
133 String prefix = ctx.getAttribute(Constants.AAI_PREFIX_PARAM_NAME);
135 String key = "vnf-id = '" + vnfId + "'";
137 SvcLogicResource.QueryStatus response = aaiClient
138 .query(PARAM_GENERIC_VNF, false, null, key, prefix, null, ctx);
139 if (SvcLogicResource.QueryStatus.NOT_FOUND.equals(response)) {
140 String msg = EELFResourceManager.format(Msg.VNF_NOT_FOUND, vnfId);
141 ctx.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
142 throw new APPCException(msg);
143 } else if (SvcLogicResource.QueryStatus.FAILURE.equals(response)) {
144 String msg = EELFResourceManager.format(Msg.AAI_QUERY_FAILED, vnfId);
145 ctx.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
146 throw new APPCException(msg);
148 String aaiEntitlementPoolUuid = ctx.getAttribute(Constants.AAI_ENTITLMENT_POOL_UUID_NAME);
149 if (null == aaiEntitlementPoolUuid) {
150 aaiEntitlementPoolUuid = "";
152 String aaiLicenseKeyGroupUuid = ctx.getAttribute(Constants.AAI_LICENSE_KEY_UUID_NAME);
153 if (null == aaiLicenseKeyGroupUuid) {
154 aaiLicenseKeyGroupUuid = "";
157 ctx.setAttribute(Constants.IS_RELEASE_ENTITLEMENT_REQUIRE,
158 Boolean.toString(!aaiEntitlementPoolUuid.isEmpty()));
159 ctx.setAttribute(Constants.IS_RELEASE_LICENSE_REQUIRE, Boolean.toString(!aaiLicenseKeyGroupUuid.isEmpty()));
160 String msg = EELFResourceManager.format(Msg.SUCCESS_EVENT_MESSAGE, "GetGenericVnfData", STR_VNF_ID + vnfId);
161 ctx.setAttribute(org.onap.appc.Constants.ATTRIBUTE_SUCCESS_MESSAGE, msg);
163 logger.info(STR_AAI_RESPONSE + response.toString());
164 } catch (SvcLogicException e) {
165 String msg = EELFResourceManager.format(Msg.AAI_QUERY_FAILED, vnfId);
166 ctx.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
168 throw new APPCException(e);
173 public void getVnfHierarchy(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
175 Set<Vnfc> vnfcSet = new HashSet<>();
178 String vnfId = params.get(PARAM_RESOURCE_KEY);
179 AAIQueryResult vnfQueryResult;
182 vnfQueryResult = readVnf(vnfId);
184 vnfType = vnfQueryResult.getAdditionProperties().get(PROPERTY_VNF_TYPE);
185 vnfVersion = vnfQueryResult.getAdditionProperties().get(Constants.AAI_VNF_MODEL_VERSION_ID);
187 Vnf vnf = createVnf(vnfType, vnfVersion, vnfId);
189 for (Relationship vnfRelationship : vnfQueryResult.getRelationshipList()) {
190 if ("vserver".equalsIgnoreCase(vnfRelationship.getRelatedTo())) {
192 String tenantId = vnfRelationship.getRelationShipDataMap().get("tenant.tenant-id");
193 String vmId = vnfRelationship.getRelationShipDataMap().get("vserver.vserver-id");
194 String vmRelatedLink = vnfRelationship.getRelatedLink();
195 String vmName = vnfRelationship.getRelatedProperties().get("vserver.vserver-name");
196 String cloudOwner = vnfRelationship.getRelationShipDataMap().get("cloud-region.cloud-owner");
197 String cloudRegionId = vnfRelationship.getRelationShipDataMap().get("cloud-region.cloud-region-id");
199 AAIQueryResult vmQueryResult = readVM(vmId, tenantId, cloudOwner, cloudRegionId);
200 String vmURL = vmQueryResult.getAdditionProperties().get(PROPERTY_VSERVER_SLINK);
202 Vserver vm = createVserver(tenantId, vmId, vmRelatedLink, vmName, vmURL);
204 for (Relationship vmRelation : vmQueryResult.getRelationshipList()) {
206 if ("vnfc".equalsIgnoreCase(vmRelation.getRelatedTo())) {
207 String vnfcName = vmRelation.getRelationShipDataMap().get("vnfc.vnfc-name");
208 AAIQueryResult vnfcQueryResult = readVnfc(vnfcName);
209 String vnfcType = vnfcQueryResult.getAdditionProperties().get(PROPERTY_VNFC_TYPE);
211 Vnfc newVnfc = createVnfc(vnfcName, vnfcType);
212 if (vnfcSet.contains(newVnfc)) {
213 Vnfc vnfcFromSet = vnfcSet.stream().filter(vnfc -> vnfc.equals(newVnfc))
214 .collect(Collectors.toList()).get(0);
215 vnfcFromSet.addVserver(vm);
216 vm.setVnfc(vnfcFromSet);
219 newVnfc.addVserver(vm);
220 vnfcSet.add(newVnfc);
226 ctx.setAttribute("VNF.VMCount", String.valueOf(vmCount));
227 populateContext(vnf, ctx);
228 } catch (AAIQueryException e) {
229 ctx.setAttribute("getVnfHierarchy_result", "FAILURE");
230 String msg = EELFResourceManager.format(Msg.AAI_QUERY_FAILED, vnfId);
231 ctx.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
232 logger.error("Failed in getVnfHierarchy, Error retrieving VNF details. Error message: " + ctx
233 .getAttribute("getResource_result"), e);
234 logger.warn("Incorrect or Incomplete VNF Hierarchy");
235 throw new APPCException("Error Retrieving VNF hierarchy");
237 ctx.setAttribute("getVnfHierarchy_result", "SUCCESS");
238 String msg = EELFResourceManager.format(Msg.SUCCESS_EVENT_MESSAGE, "GetVNFHierarchy", STR_VNF_ID + vnfId);
239 ctx.setAttribute(org.onap.appc.Constants.ATTRIBUTE_SUCCESS_MESSAGE, msg);
243 private Vnf createVnf(String vnfType, String vnfVersion, String vnfId) {
246 vnf.setVnfType(vnfType);
247 vnf.setVnfVersion(vnfVersion);
251 private Vnfc createVnfc(String vnfcName, String vnfcType) {
252 Vnfc vnfc = new Vnfc();
253 vnfc.setVnfcName(vnfcName);
254 vnfc.setVnfcType(vnfcType);
258 private Vserver createVserver(String tenantId, String vmId, String vmRelatedLink, String vmName, String vmURL) {
259 Vserver vserver = new Vserver();
260 vserver.setTenantId(tenantId);
262 vserver.setRelatedLink(vmRelatedLink);
263 vserver.setName(vmName);
264 vserver.setUrl(vmURL);
268 private void populateContext(Vnf vnf, SvcLogicContext ctx) {
269 ctx.setAttribute("vnf.type", vnf.getVnfType());
270 ctx.setAttribute("vnf.version", vnf.getVnfVersion());
271 ctx.setAttribute("vnf.vnfcCount", String.valueOf(vnf.getVnfcs().size()));
273 for (Vnfc vnfc : vnf.getVnfcs()) {
274 ctx.setAttribute(STR_VNF_VNFC + vnfcCount + "].name", vnfc.getVnfcName());
275 ctx.setAttribute(STR_VNF_VNFC + vnfcCount + "].type", vnfc.getVnfcType());
276 ctx.setAttribute(STR_VNF_VNFC + vnfcCount + "].vm_count", String.valueOf(vnfc.getVserverList().size()));
278 for (Vserver vm : vnfc.getVserverList()) {
279 ctx.setAttribute(STR_VNF_VNFC + vnfcCount + "].vm[" + vmCount++ + "].url", vm.getUrl());
285 private AAIQueryResult readVnfc(String vnfcName) throws AAIQueryException {
286 String query = "vnfc.vnfc-name = '" + vnfcName + "'";
287 String prefix = "VNFC";
288 String resourceType = "vnfc";
289 SvcLogicContext vnfContext = readResource(query, prefix, resourceType);
290 String[] additionalProperties = new String[]{PROPERTY_VNFC_TYPE, PROPERTY_VNFC_NAME,
291 PROPERTY_VNFC_FUNC_CODE, PROPERTY_IN_MAINT, PROPERTY_PROV_STATUS,
292 PROPERTY_LOOP_DISABLED, PROPERTY_ORCHESTRATION_STATUS, PROPERTY_RESOURCE_VERSION};
293 return readRelationDataAndProperties(prefix, vnfContext, additionalProperties);
296 private AAIQueryResult readVM(String vmId, String tenantId, String cloudOwner, String cloudRegionId)
297 throws AAIQueryException {
298 String query = "vserver.vserver-id = '" + vmId + "' AND tenant.tenant_id = '" + tenantId
299 + "' AND cloud-region.cloud-owner = '"
300 + cloudOwner + "' AND cloud-region.cloud-region-id = '" + cloudRegionId + "'";
301 String prefix = "VM";
302 String resourceType = "vserver";
303 SvcLogicContext vnfContext = readResource(query, prefix, resourceType);
304 String[] additionalProperties = new String[]{PROPERTY_VSERVER_ID, PROPERTY_VSERVER_SLINK,
305 PROPERTY_VSERVER_NAME, PROPERTY_IN_MAINT, PROPERTY_PROV_STATUS, PROPERTY_LOOP_DISABLED,
306 PROPERTY_VSERVER_NAME_2, PROPERTY_RESOURCE_VERSION,};
308 return readRelationDataAndProperties(prefix, vnfContext, additionalProperties);
311 private AAIQueryResult readVnf(String vnfId) throws AAIQueryException {
312 String query = "generic-vnf.vnf-id = '" + vnfId + "'";
313 String prefix = "VNF";
314 SvcLogicContext vnfContext = readResource(query, prefix, PARAM_GENERIC_VNF);
316 String[] additionalProperties = new String[]{PROPERTY_VNF_TYPE, PROPERTY_VNF_NEM,
317 PROPERTY_IN_MAINT, PROPERTY_PROV_STATUS, PROPERTY_HEAT_STACK_ID,
318 PROPERTY_LOOP_DISABLED, PROPERTY_ORCHESTRATION_STATUS, PROPERTY_RESOURCE_VERSION, Constants.AAI_VNF_MODEL_VERSION_ID};
320 return readRelationDataAndProperties(prefix, vnfContext, additionalProperties);
323 private AAIQueryResult readRelationDataAndProperties(String prefix, SvcLogicContext context,
324 String[] additionalProperties) {
325 AAIQueryResult result = new AAIQueryResult();
327 if (context != null && context.getAttribute(prefix + ".relationship-list.relationship_length") != null) {
328 Integer relationsCount = Integer.parseInt(context.getAttribute(
329 prefix + ".relationship-list.relationship_length"));
330 for (int i = 0; i < relationsCount; i++) {
331 String rsKey = prefix + ".relationship-list.relationship[" + i + "]";
332 Relationship relationShip = new Relationship();
333 relationShip.setRelatedLink(context.getAttribute(rsKey + ".related-link"));
334 relationShip.setRelatedTo(context.getAttribute(rsKey + ".related-to"));
335 Integer relationDataCount = Integer.parseInt(context.getAttribute(rsKey + ".relationship-data_length"));
336 for (int j = 0; j < relationDataCount; j++) {
337 String rsDataKey = rsKey + ".relationship-data[" + j + "]";
338 String key = context.getAttribute(rsDataKey + ".relationship-key");
339 String value = context.getAttribute(rsDataKey + ".relationship-value");
340 relationShip.getRelationShipDataMap().put(key, value);
342 Integer relatedPropertyCount = 0;
343 String relatedPropertyCountStr = null;
345 relatedPropertyCountStr = context.getAttribute(rsKey + ".related-to-property_length");
346 relatedPropertyCount = Integer.parseInt(relatedPropertyCountStr);
347 } catch (NumberFormatException e) {
348 logger.debug("Invalid value in the context for Related Property Count " + relatedPropertyCountStr);
351 for (int j = 0; j < relatedPropertyCount; j++) {
352 String rsPropKey = rsKey + ".related-to-property[" + j + "]";
353 String key = context.getAttribute(rsPropKey + ".property-key");
354 String value = context.getAttribute(rsPropKey + ".property-value");
355 relationShip.getRelatedProperties().put(key, value);
357 result.getRelationshipList().add(relationShip);
360 logger.error("Relationship-list not present in the SvcLogicContext attributes set."
361 + (context == null ? "" : "Attribute KeySet = " + context.getAttributeKeySet()));
364 if (context != null) {
365 for (String key : additionalProperties) {
366 result.getAdditionProperties().put(key, context.getAttribute(prefix + "." + key));
372 private SvcLogicContext readResource(String query, String prefix, String resourceType) throws AAIQueryException {
373 SvcLogicContext resourceContext = new SvcLogicContext();
375 SvcLogicResource.QueryStatus response = aaiClient
376 .query(resourceType, false, null, query, prefix, null, resourceContext);
377 logger.info(STR_AAI_RESPONSE + response.toString());
378 if (!SvcLogicResource.QueryStatus.SUCCESS.equals(response)) {
379 throw new AAIQueryException("Error Retrieving VNF hierarchy from A&AI");
381 } catch (SvcLogicException e) {
382 logger.error(EELFResourceManager.format(Msg.AAI_GET_DATA_FAILED, query), e);
383 throw new AAIQueryException("Error Retrieving VNF hierarchy from A&AI");
385 return resourceContext;
389 public void getResource(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
390 String resourceType = params.get(PARAM_RESOURCE_TYPE);
391 String ctxPrefix = params.get("prefix");
392 String resourceKey = params.get(PARAM_RESOURCE_KEY);
393 if (logger.isDebugEnabled()) {
394 logger.debug("inside getResorce");
395 logger.debug("Retrieving " + resourceType + " details from A&AI for Key : " + resourceKey);
398 SvcLogicResource.QueryStatus response =
399 aaiClient.query(resourceType, false, null, resourceKey, ctxPrefix, null, ctx);
400 logger.info(STR_AAI_RESPONSE + response.toString());
401 ctx.setAttribute("getResource_result", response.toString());
402 } catch (SvcLogicException e) {
403 logger.error(EELFResourceManager.format(Msg.AAI_GET_DATA_FAILED, resourceKey), e);
405 if (logger.isDebugEnabled()) {
406 logger.debug("exiting getResource======");
411 public void postResource(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
412 String resourceType = params.get(PARAM_RESOURCE_TYPE);
413 String ctxPrefix = params.get("prefix");
414 String resourceKey = params.get(PARAM_RESOURCE_KEY);
415 String attributeName = params.get("attributeName");
416 String attributeValue = params.get("attributeValue");
417 if (logger.isDebugEnabled()) {
418 logger.debug("inside postResource");
419 logger.debug("Updating " + resourceType + " details in A&AI for Key : " + resourceKey);
420 logger.debug("Updating " + attributeName + " to : " + attributeValue);
422 Map<String, String> data = new HashMap<>();
423 data.put(attributeName, attributeValue);
426 SvcLogicResource.QueryStatus response = aaiClient.update(resourceType, resourceKey, data, ctxPrefix, ctx);
427 logger.info(STR_AAI_RESPONSE + response.toString());
428 ctx.setAttribute("postResource_result", response.toString());
429 } catch (SvcLogicException e) {
430 logger.error(EELFResourceManager.format(Msg.AAI_UPDATE_FAILED, resourceKey, attributeValue), e);
432 if (logger.isDebugEnabled()) {
433 logger.debug("exiting postResource======");
438 public void deleteResource(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
439 String resourceType = params.get(PARAM_RESOURCE_TYPE);
440 String resourceKey = params.get(PARAM_RESOURCE_KEY);
442 if (logger.isDebugEnabled()) {
443 logger.debug("inside deleteResource");
444 logger.debug("Deleting " + resourceType + " details From A&AI for Key : " + resourceKey);
447 SvcLogicResource.QueryStatus response = aaiClient.delete(resourceType, resourceKey, ctx);
448 logger.info(STR_AAI_RESPONSE + response.toString());
449 ctx.setAttribute("deleteResource_result", response.toString());
450 } catch (SvcLogicException e) {
451 logger.error(EELFResourceManager.format(Msg.AAI_DELETE_FAILED, resourceKey), e);
453 if (logger.isDebugEnabled()) {
454 logger.debug("exiting deleteResource======");