2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.so.adapters.vnfmadapter.extclients.aai;
23 import java.util.Collections;
24 import org.onap.aai.domain.yang.EsrSystemInfo;
25 import org.onap.aai.domain.yang.EsrSystemInfoList;
26 import org.onap.aai.domain.yang.EsrVnfm;
27 import org.onap.aai.domain.yang.EsrVnfmList;
28 import org.onap.aai.domain.yang.GenericVnf;
29 import org.onap.aai.domain.yang.Relationship;
30 import org.onap.aai.domain.yang.RelationshipData;
31 import org.onap.aai.domain.yang.RelationshipList;
32 import org.onap.aai.domain.yang.Tenant;
33 import org.onap.so.adapters.vnfmadapter.rest.exceptions.VnfmNotFoundException;
34 import org.onap.so.client.aai.AAIObjectType;
35 import org.onap.so.client.aai.AAIVersion;
36 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.stereotype.Service;
43 * Provides helper methods for interactions with AAI.
46 public class AaiHelper {
48 private static final Logger logger = LoggerFactory.getLogger(AaiHelper.class);
49 private final AaiServiceProvider aaiServiceProvider;
52 public AaiHelper(final AaiServiceProvider aaiServiceProvider) {
53 this.aaiServiceProvider = aaiServiceProvider;
57 * Add a relationship to the given generic VNF to the given VNFM.
59 * @param vnf the generic VNF
60 * @param vnfmId the ID of the VNFM
62 public void addRelationshipFromGenericVnfToVnfm(final GenericVnf vnf, final String vnfmId) {
63 if (vnf.getRelationshipList() == null) {
64 vnf.setRelationshipList(new RelationshipList());
66 final RelationshipList vnfmRelationshiplist = vnf.getRelationshipList();
67 vnfmRelationshiplist.getRelationship().add(createRelationshipToVnfm(vnfmId));
69 aaiServiceProvider.invokePutGenericVnf(vnf);
72 private Relationship createRelationshipToVnfm(final String vnfmId) {
73 final Relationship relationship = new Relationship();
74 relationship.setRelatedTo("esr-vnfm");
75 relationship.setRelationshipLabel("tosca.relationships.DependsOn");
76 relationship.setRelatedLink("/aai/" + AAIVersion.LATEST
77 + AAIUriFactory.createResourceUri(AAIObjectType.VNFM, vnfmId).build().toString());
78 relationship.getRelationshipData().add(createRelationshipData("esr-vnfm.vnfm-id", vnfmId));
82 private RelationshipData createRelationshipData(final String key, final String value) {
83 final RelationshipData data = new RelationshipData();
84 data.setRelationshipKey(key);
85 data.setRelationshipValue(value);
90 * Get the VNFM assigned for use for the given generic VNF.
92 * @param vnf the generic VNF
93 * @return the VNFM to use, or <code>null</code> if no VNFM has been assigned yet
95 public EsrVnfm getAssignedVnfm(final GenericVnf vnf) {
96 final Relationship relationship = getRelationship(vnf, "esr-vnfm");
97 final String vnfmId = getRelationshipKey(relationship, "esr-vnfm.vnfm-id");
98 return vnfmId == null ? null : aaiServiceProvider.invokeGetVnfm(vnfmId);
102 * Get the tenant assigned for use for the given generic VNF.
104 * @param vnf the generic VNF
105 * @return the tenant to use, or <code>null</code> if no tenant has been assigned yet
107 public Tenant getAssignedTenant(final GenericVnf vnf) {
108 final Relationship relationship = getRelationship(vnf, "tenant");
109 final String cloudOwner = getRelationshipKey(relationship, "cloud-region.cloud-owner");
110 final String cloudRegion = getRelationshipKey(relationship, "cloud-region.cloud-region-id");
111 final String tenantId = getRelationshipKey(relationship, "tenant.tenant-id");
112 return cloudOwner == null || cloudRegion == null || tenantId == null ? null
113 : aaiServiceProvider.invokeGetTenant(cloudOwner, cloudRegion, tenantId);
116 private Relationship getRelationship(final GenericVnf vnf, final String relationshipRelatedToValue) {
117 for (final Relationship relationship : vnf.getRelationshipList() == null ? Collections.<Relationship>emptyList()
118 : vnf.getRelationshipList().getRelationship()) {
119 if (relationship.getRelatedTo().equals(relationshipRelatedToValue)) {
126 private String getRelationshipKey(final Relationship relationship, final String relationshipKey) {
127 if (relationship != null) {
128 for (final RelationshipData relationshipData : relationship.getRelationshipData()) {
129 if (relationshipData.getRelationshipKey().equals(relationshipKey)) {
130 return relationshipData.getRelationshipValue();
138 * Select a VNFM to use for the given generic VNF. Should only be used when no VNFM has already been
139 * assigned to the VNF.
141 * @param vnf the generic VNF
142 * @return the VNFM to use
144 public EsrVnfm selectVnfm(final GenericVnf vnf) {
145 final EsrVnfmList vnfmsInEsr = aaiServiceProvider.invokeGetVnfms();
147 if (vnfmsInEsr == null) {
148 throw new VnfmNotFoundException("No VNFMs found in AAI ESR");
150 logger.debug("VNFMs in ESR: " + vnfmsInEsr);
152 for (final EsrVnfm vnfm : vnfmsInEsr.getEsrVnfm()) {
153 if (vnfmHasMatchingEsrSystemInfoType(vnfm, vnf.getNfType())) {
157 throw new VnfmNotFoundException("No matching VNFM found in AAI ESR");
160 private boolean vnfmHasMatchingEsrSystemInfoType(final EsrVnfm vnfm, final String type) {
161 logger.debug("Checking VNFM ID: " + vnfm + ": " + vnfm.getVnfmId());
163 final EsrSystemInfoList systemInfolist = aaiServiceProvider.invokeGetVnfmEsrSystemInfoList(vnfm.getVnfmId());
164 if (systemInfolist != null) {
165 for (final EsrSystemInfo esrSystemInfo : systemInfolist.getEsrSystemInfo()) {
166 if (esrSystemInfo.getType().equals(type)) {
167 logger.debug("Matched VNFM ID: " + vnfm + ", based on type");