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 org.onap.aai.domain.yang.*;
24 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs;
25 import org.onap.so.adapters.vnfmadapter.rest.exceptions.TenantNotFoundException;
26 import org.onap.so.adapters.vnfmadapter.rest.exceptions.VnfmNotFoundException;
27 import org.onap.so.client.aai.AAIObjectType;
28 import org.onap.so.client.aai.AAIVersion;
29 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
30 import org.onap.vnfmadapter.v1.model.Tenant;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.stereotype.Service;
35 import java.util.Collections;
36 import java.util.HashMap;
40 * Provides helper methods for interactions with AAI.
43 public class AaiHelper {
45 private static final Logger logger = LoggerFactory.getLogger(AaiHelper.class);
46 private final AaiServiceProvider aaiServiceProvider;
47 private final Map<String, OamIpAddressSource> mapOfVnfIdToOamIpAddressHolder = new HashMap<>();
50 public AaiHelper(final AaiServiceProvider aaiServiceProvider) {
51 this.aaiServiceProvider = aaiServiceProvider;
55 * Add a relationship to the given generic VNF to the given VNFM.
57 * @param vnf the generic VNF
58 * @param vnfmId the ID of the VNFM
60 public void addRelationshipFromGenericVnfToVnfm(final GenericVnf vnf, final String vnfmId) {
61 if (vnf.getRelationshipList() == null) {
62 vnf.setRelationshipList(new RelationshipList());
64 final RelationshipList vnfmRelationshiplist = vnf.getRelationshipList();
65 vnfmRelationshiplist.getRelationship().add(createRelationshipToVnfm(vnfmId));
69 private Relationship createRelationshipToVnfm(final String vnfmId) {
70 final Relationship relationship = new Relationship();
71 relationship.setRelatedTo("esr-vnfm");
72 relationship.setRelationshipLabel("tosca.relationships.DependsOn");
73 relationship.setRelatedLink("/aai/" + AAIVersion.LATEST
74 + AAIUriFactory.createResourceUri(AAIObjectType.VNFM, vnfmId).build().toString());
75 relationship.getRelationshipData().add(createRelationshipData("esr-vnfm.vnfm-id", vnfmId));
79 private RelationshipData createRelationshipData(final String key, final String value) {
80 final RelationshipData data = new RelationshipData();
81 data.setRelationshipKey(key);
82 data.setRelationshipValue(value);
87 * Get the VNFM assigned for use for the given generic VNF.
89 * @param vnf the generic VNF
90 * @return the VNFM to use, or <code>null</code> if no VNFM has been assigned yet
92 public EsrVnfm getAssignedVnfm(final GenericVnf vnf) {
93 final String vnfmId = getIdOfAssignedVnfm(vnf);
94 return vnfmId == null ? null : aaiServiceProvider.invokeGetVnfm(vnfmId);
98 * Get the ID of the VNFM assigned for use for the given generic VNF.
100 * @param vnf the generic VNF
101 * @return the ID of the VNFM to use, or <code>null</code> if no VNFM has been assigned yet
103 public String getIdOfAssignedVnfm(final GenericVnf vnf) {
104 final Relationship relationship = getRelationship(vnf, "esr-vnfm");
105 return getRelationshipKey(relationship, "esr-vnfm.vnfm-id");
109 * Get the tenant assigned for use for the given generic VNF.
111 * @param vnf the generic VNF
112 * @return the tenant to use, or <code>null</code> if no tenant has been assigned yet
114 public Tenant getAssignedTenant(final GenericVnf vnf) {
115 final Relationship relationship = getRelationship(vnf, "tenant");
116 final String cloudOwner = getRelationshipKey(relationship, "cloud-region.cloud-owner");
117 final String cloudRegion = getRelationshipKey(relationship, "cloud-region.cloud-region-id");
118 final String tenantId = getRelationshipKey(relationship, "tenant.tenant-id");
119 if (cloudOwner == null || cloudRegion == null || tenantId == null) {
120 throw new TenantNotFoundException("No matching Tenant found in AAI. VNFID: " + vnf.getVnfId());
122 return new Tenant().cloudOwner(cloudOwner).regionName(cloudRegion).tenantId(tenantId);
126 private Relationship getRelationship(final GenericVnf vnf, final String relationshipRelatedToValue) {
127 for (final Relationship relationship : vnf.getRelationshipList() == null ? Collections.<Relationship>emptyList()
128 : vnf.getRelationshipList().getRelationship()) {
129 if (relationship.getRelatedTo().equals(relationshipRelatedToValue)) {
136 private String getRelationshipKey(final Relationship relationship, final String relationshipKey) {
137 if (relationship != null) {
138 for (final RelationshipData relationshipData : relationship.getRelationshipData()) {
139 if (relationshipData.getRelationshipKey().equals(relationshipKey)) {
140 return relationshipData.getRelationshipValue();
148 * Select a VNFM to use for the given generic VNF. Should only be used when no VNFM has already been assigned to the
151 * @param vnf the generic VNF
152 * @return the VNFM to use
154 public EsrVnfm selectVnfm(final GenericVnf vnf) {
155 final EsrVnfmList vnfmsInEsr = aaiServiceProvider.invokeGetVnfms();
157 if (vnfmsInEsr == null) {
158 throw new VnfmNotFoundException("No VNFMs found in AAI ESR");
160 logger.debug("VNFMs in ESR: " + vnfmsInEsr);
162 for (final EsrVnfm vnfm : vnfmsInEsr.getEsrVnfm()) {
163 if (vnfmHasMatchingEsrSystemInfoType(vnfm, vnf.getNfType())) {
167 throw new VnfmNotFoundException("No matching VNFM found in AAI ESR");
170 private boolean vnfmHasMatchingEsrSystemInfoType(final EsrVnfm vnfm, final String type) {
171 logger.debug("Checking VNFM ID: " + vnfm + ": " + vnfm.getVnfmId());
173 final EsrSystemInfoList systemInfolist = aaiServiceProvider.invokeGetVnfmEsrSystemInfoList(vnfm.getVnfmId());
174 if (systemInfolist != null) {
175 for (final EsrSystemInfo esrSystemInfo : systemInfolist.getEsrSystemInfo()) {
176 if (esrSystemInfo.getType().equals(type)) {
177 logger.debug("Matched VNFM ID: " + vnfm + ", based on type");
188 * @param vnfc the VNFC to base the vserver on
189 * @return the vserver
191 public Vserver createVserver(final LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs vnfc) {
192 final Vserver vserver = new Vserver();
193 vserver.setVserverId(vnfc.getComputeResource().getResourceId());
194 vserver.setVserverName(vnfc.getId());
195 vserver.setProvStatus("active");
196 vserver.setVserverSelflink("Not available");
201 * Add a relationship to the given vserver to the given VNF.
203 * @param vnf the vserver
204 * @param vnfmId the ID of the VNF
206 public void addRelationshipFromVserverVnfToGenericVnf(final Vserver vserver, final String vnfId) {
207 if (vserver.getRelationshipList() == null) {
208 vserver.setRelationshipList(new RelationshipList());
210 final RelationshipList vserverRelationshiplist = vserver.getRelationshipList();
211 vserverRelationshiplist.getRelationship().add(createRelationshipToGenericVnf(vnfId));
214 private Relationship createRelationshipToGenericVnf(final String vnfId) {
215 final Relationship relationship = new Relationship();
216 relationship.setRelatedTo("generic-vnf");
217 relationship.setRelationshipLabel("tosca.relationships.HostedOn");
218 relationship.setRelatedLink("/aai/" + AAIVersion.LATEST
219 + AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId).build().toString());
220 relationship.getRelationshipData().add(createRelationshipData("generic-vnf.vnf-id", vnfId));
224 public void setOamIpAddressSource(final String vnfId, final OamIpAddressSource oamIpAddressSource) {
225 mapOfVnfIdToOamIpAddressHolder.put(vnfId, oamIpAddressSource);
228 public OamIpAddressSource getOamIpAddressSource(final String vnfId) {
229 return mapOfVnfIdToOamIpAddressHolder.get(vnfId);
233 * Add a relationship to the given tenant to the given VNF.
235 * @param vnf the generic vnf
236 * @param tenant the Tenant
239 public void addRelationshipFromGenericVnfToTenant(final GenericVnf vnf, final Tenant tenant) {
240 if (vnf.getRelationshipList() == null) {
241 vnf.setRelationshipList(new RelationshipList());
243 final RelationshipList vnfmRelationshiplist = vnf.getRelationshipList();
244 vnfmRelationshiplist.getRelationship().add(createRelationshipToTenant(tenant));
247 private Relationship createRelationshipToTenant(final Tenant tenant) {
248 final Relationship relationship = new Relationship();
249 relationship.setRelatedTo("tenant");
250 relationship.setRelatedLink("/aai/" + AAIVersion.LATEST + AAIUriFactory.createResourceUri(AAIObjectType.TENANT,
251 tenant.getCloudOwner(), tenant.getRegionName(), tenant.getTenantId()).build().toString());
252 relationship.getRelationshipData().add(createRelationshipData("tenant.tenant-id", tenant.getTenantId()));