2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
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.adaptors.aai.AAIClient;
45 import org.onap.ccsdk.sli.adaptors.aai.AAIService;
46 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
47 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
48 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
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);
119 String msg = EELFResourceManager
120 .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 = aaiClient
139 .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);
213 if (vnfcSet.contains(newVnfc)) {
214 Vnfc vnfcFromSet = vnfcSet.stream().filter(vnfc -> vnfc.equals(newVnfc))
215 .collect(Collectors.toList()).get(0);
216 vnfcFromSet.addVserver(vm);
217 vm.setVnfc(vnfcFromSet);
220 newVnfc.addVserver(vm);
221 vnfcSet.add(newVnfc);
227 ctx.setAttribute("VNF.VMCount", String.valueOf(vmCount));
228 populateContext(vnf, ctx);
229 } catch (AAIQueryException e) {
230 ctx.setAttribute("getVnfHierarchy_result", "FAILURE");
231 String msg = EELFResourceManager.format(Msg.AAI_QUERY_FAILED, vnfId);
232 ctx.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
233 logger.error("Failed in getVnfHierarchy, Error retrieving VNF details. Error message: " + ctx
234 .getAttribute("getResource_result"), e);
235 logger.warn("Incorrect or Incomplete VNF Hierarchy");
236 throw new APPCException("Error Retrieving VNF hierarchy");
238 ctx.setAttribute("getVnfHierarchy_result", "SUCCESS");
239 String msg = EELFResourceManager.format(Msg.SUCCESS_EVENT_MESSAGE, "GetVNFHierarchy", STR_VNF_ID + vnfId);
240 ctx.setAttribute(org.onap.appc.Constants.ATTRIBUTE_SUCCESS_MESSAGE, msg);
244 private Vnf createVnf(String vnfType, String vnfVersion, String vnfId) {
247 vnf.setVnfType(vnfType);
248 vnf.setVnfVersion(vnfVersion);
252 private Vnfc createVnfc(String vnfcName, String vnfcType) {
253 Vnfc vnfc = new Vnfc();
254 vnfc.setVnfcName(vnfcName);
255 vnfc.setVnfcType(vnfcType);
259 private Vserver createVserver(String tenantId, String vmId, String vmRelatedLink, String vmName, String vmURL) {
260 Vserver vserver = new Vserver();
261 vserver.setTenantId(tenantId);
263 vserver.setRelatedLink(vmRelatedLink);
264 vserver.setName(vmName);
265 vserver.setUrl(vmURL);
269 private void populateContext(Vnf vnf, SvcLogicContext ctx) {
270 ctx.setAttribute("vnf.type", vnf.getVnfType());
271 ctx.setAttribute("vnf.version", vnf.getVnfVersion());
272 ctx.setAttribute("vnf.vnfcCount", String.valueOf(vnf.getVnfcs().size()));
274 for (Vnfc vnfc : vnf.getVnfcs()) {
275 ctx.setAttribute(STR_VNF_VNFC + vnfcCount + "].name", vnfc.getVnfcName());
276 ctx.setAttribute(STR_VNF_VNFC + vnfcCount + "].type", vnfc.getVnfcType());
277 ctx.setAttribute(STR_VNF_VNFC + vnfcCount + "].vm_count", String.valueOf(vnfc.getVserverList().size()));
279 for (Vserver vm : vnfc.getVserverList()) {
280 ctx.setAttribute(STR_VNF_VNFC + vnfcCount + "].vm[" + vmCount++ + "].url", vm.getUrl());
286 private AAIQueryResult readVnfc(String vnfcName) throws AAIQueryException {
287 String query = "vnfc.vnfc-name = '" + vnfcName + "'";
288 String prefix = "VNFC";
289 String resourceType = "vnfc";
290 SvcLogicContext vnfContext = readResource(query, prefix, resourceType);
291 String[] additionalProperties = new String[]{PROPERTY_VNFC_TYPE, PROPERTY_VNFC_NAME,
292 PROPERTY_VNFC_FUNC_CODE, PROPERTY_IN_MAINT, PROPERTY_PROV_STATUS,
293 PROPERTY_LOOP_DISABLED, PROPERTY_ORCHESTRATION_STATUS, PROPERTY_RESOURCE_VERSION};
294 return readRelationDataAndProperties(prefix, vnfContext, additionalProperties);
297 private AAIQueryResult readVM(String vmId, String tenantId, String cloudOwner, String cloudRegionId)
298 throws AAIQueryException {
299 String query = "vserver.vserver-id = '" + vmId + "' AND tenant.tenant_id = '" + tenantId
300 + "' AND cloud-region.cloud-owner = '"
301 + cloudOwner + "' AND cloud-region.cloud-region-id = '" + cloudRegionId + "'";
302 String prefix = "VM";
303 String resourceType = "vserver";
304 SvcLogicContext vnfContext = readResource(query, prefix, resourceType);
305 String[] additionalProperties = new String[]{PROPERTY_VSERVER_ID, PROPERTY_VSERVER_SLINK,
306 PROPERTY_VSERVER_NAME, PROPERTY_IN_MAINT, PROPERTY_PROV_STATUS, PROPERTY_LOOP_DISABLED,
307 PROPERTY_VSERVER_NAME_2, PROPERTY_RESOURCE_VERSION,};
309 return readRelationDataAndProperties(prefix, vnfContext, additionalProperties);
312 private AAIQueryResult readVnf(String vnfId) throws AAIQueryException {
313 String query = "generic-vnf.vnf-id = '" + vnfId + "'";
314 String prefix = "VNF";
315 SvcLogicContext vnfContext = readResource(query, prefix, PARAM_GENERIC_VNF);
317 String[] additionalProperties = new String[]{PROPERTY_VNF_TYPE, PROPERTY_VNF_NEM,
318 PROPERTY_IN_MAINT, PROPERTY_PROV_STATUS, PROPERTY_HEAT_STACK_ID,
319 PROPERTY_LOOP_DISABLED, PROPERTY_ORCHESTRATION_STATUS, PROPERTY_RESOURCE_VERSION, Constants.AAI_VNF_MODEL_VERSION_ID};
321 return readRelationDataAndProperties(prefix, vnfContext, additionalProperties);
324 private AAIQueryResult readRelationDataAndProperties(String prefix, SvcLogicContext context,
325 String[] additionalProperties) {
326 AAIQueryResult result = new AAIQueryResult();
328 if (context != null && context.getAttribute(prefix + ".relationship-list.relationship_length") != null) {
329 Integer relationsCount = Integer.parseInt(context.getAttribute(
330 prefix + ".relationship-list.relationship_length"));
331 for (int i = 0; i < relationsCount; i++) {
332 String rsKey = prefix + ".relationship-list.relationship[" + i + "]";
333 Relationship relationShip = new Relationship();
334 relationShip.setRelatedLink(context.getAttribute(rsKey + ".related-link"));
335 relationShip.setRelatedTo(context.getAttribute(rsKey + ".related-to"));
336 Integer relationDataCount = Integer.parseInt(context.getAttribute(rsKey + ".relationship-data_length"));
337 for (int j = 0; j < relationDataCount; j++) {
338 String rsDataKey = rsKey + ".relationship-data[" + j + "]";
339 String key = context.getAttribute(rsDataKey + ".relationship-key");
340 String value = context.getAttribute(rsDataKey + ".relationship-value");
341 relationShip.getRelationShipDataMap().put(key, value);
343 Integer relatedPropertyCount = 0;
344 String relatedPropertyCountStr = null;
346 relatedPropertyCountStr = context.getAttribute(rsKey + ".related-to-property_length");
347 relatedPropertyCount = Integer.parseInt(relatedPropertyCountStr);
348 } catch (NumberFormatException e) {
349 logger.debug("Invalid value in the context for Related Property Count " + relatedPropertyCountStr);
352 for (int j = 0; j < relatedPropertyCount; j++) {
353 String rsPropKey = rsKey + ".related-to-property[" + j + "]";
354 String key = context.getAttribute(rsPropKey + ".property-key");
355 String value = context.getAttribute(rsPropKey + ".property-value");
356 relationShip.getRelatedProperties().put(key, value);
358 result.getRelationshipList().add(relationShip);
361 logger.error("Relationship-list not present in the SvcLogicContext attributes set."
362 + (context == null ? "" : "Attribute KeySet = " + context.getAttributeKeySet()));
365 if (context != null) {
366 for (String key : additionalProperties) {
367 result.getAdditionProperties().put(key, context.getAttribute(prefix + "." + key));
373 private SvcLogicContext readResource(String query, String prefix, String resourceType) throws AAIQueryException {
374 SvcLogicContext resourceContext = new SvcLogicContext();
376 SvcLogicResource.QueryStatus response = aaiClient
377 .query(resourceType, false, null, query, prefix, null, resourceContext);
378 logger.info(STR_AAI_RESPONSE + response.toString());
379 if (!SvcLogicResource.QueryStatus.SUCCESS.equals(response)) {
380 throw new AAIQueryException("Error Retrieving VNF hierarchy from A&AI");
382 } catch (SvcLogicException e) {
383 logger.error(EELFResourceManager.format(Msg.AAI_GET_DATA_FAILED, query), e);
384 throw new AAIQueryException("Error Retrieving VNF hierarchy from A&AI");
386 return resourceContext;
390 public void getResource(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
391 String resourceType = params.get(PARAM_RESOURCE_TYPE);
392 String ctxPrefix = params.get("prefix");
393 String resourceKey = params.get(PARAM_RESOURCE_KEY);
394 if (logger.isDebugEnabled()) {
395 logger.debug("inside getResorce");
396 logger.debug("Retrieving " + resourceType + " details from A&AI for Key : " + resourceKey);
399 SvcLogicResource.QueryStatus response =
400 aaiClient.query(resourceType, false, null, resourceKey, ctxPrefix, null, ctx);
401 logger.info(STR_AAI_RESPONSE + response.toString());
402 ctx.setAttribute("getResource_result", response.toString());
403 } catch (SvcLogicException e) {
404 logger.error(EELFResourceManager.format(Msg.AAI_GET_DATA_FAILED, resourceKey), e);
406 if (logger.isDebugEnabled()) {
407 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 if (logger.isDebugEnabled()) {
434 logger.debug("exiting postResource======");
439 public void deleteResource(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
440 String resourceType = params.get(PARAM_RESOURCE_TYPE);
441 String resourceKey = params.get(PARAM_RESOURCE_KEY);
443 if (logger.isDebugEnabled()) {
444 logger.debug("inside deleteResource");
445 logger.debug("Deleting " + resourceType + " details From A&AI for Key : " + resourceKey);
448 SvcLogicResource.QueryStatus response = aaiClient.delete(resourceType, resourceKey, ctx);
449 logger.info(STR_AAI_RESPONSE + response.toString());
450 ctx.setAttribute("deleteResource_result", response.toString());
451 } catch (SvcLogicException e) {
452 logger.error(EELFResourceManager.format(Msg.AAI_DELETE_FAILED, resourceKey), e);
454 if (logger.isDebugEnabled()) {
455 logger.debug("exiting deleteResource======");