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 java.util.HashMap;
26 import org.onap.aai.domain.yang.EsrSystemInfo;
27 import org.onap.aai.domain.yang.EsrSystemInfoList;
28 import org.onap.aai.domain.yang.EsrVnfm;
29 import org.onap.aai.domain.yang.EsrVnfmList;
30 import org.onap.aai.domain.yang.GenericVnf;
31 import org.onap.aai.domain.yang.Relationship;
32 import org.onap.aai.domain.yang.RelationshipData;
33 import org.onap.aai.domain.yang.RelationshipList;
34 import org.onap.aai.domain.yang.Vserver;
35 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs;
36 import org.onap.so.adapters.vnfmadapter.rest.exceptions.TenantNotFoundException;
37 import org.onap.so.adapters.vnfmadapter.rest.exceptions.VnfmNotFoundException;
38 import org.onap.so.client.aai.AAIObjectType;
39 import org.onap.so.client.aai.AAIVersion;
40 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
41 import org.onap.vnfmadapter.v1.model.Tenant;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.stereotype.Service;
48 * Provides helper methods for interactions with AAI.
51 public class AaiHelper {
53 private static final Logger logger = LoggerFactory.getLogger(AaiHelper.class);
54 private final AaiServiceProvider aaiServiceProvider;
55 private final Map<String, OamIpAddressSource> mapOfVnfIdToOamIpAddressHolder = new HashMap<>();
58 public AaiHelper(final AaiServiceProvider aaiServiceProvider) {
59 this.aaiServiceProvider = aaiServiceProvider;
63 * Add a relationship to the given generic VNF to the given VNFM.
65 * @param vnf the generic VNF
66 * @param vnfmId the ID of the VNFM
68 public void addRelationshipFromGenericVnfToVnfm(final GenericVnf vnf, final String vnfmId) {
69 if (vnf.getRelationshipList() == null) {
70 vnf.setRelationshipList(new RelationshipList());
72 final RelationshipList vnfmRelationshiplist = vnf.getRelationshipList();
73 vnfmRelationshiplist.getRelationship().add(createRelationshipToVnfm(vnfmId));
75 aaiServiceProvider.invokePutGenericVnf(vnf);
78 private Relationship createRelationshipToVnfm(final String vnfmId) {
79 final Relationship relationship = new Relationship();
80 relationship.setRelatedTo("esr-vnfm");
81 relationship.setRelationshipLabel("tosca.relationships.DependsOn");
82 relationship.setRelatedLink("/aai/" + AAIVersion.LATEST
83 + AAIUriFactory.createResourceUri(AAIObjectType.VNFM, vnfmId).build().toString());
84 relationship.getRelationshipData().add(createRelationshipData("esr-vnfm.vnfm-id", vnfmId));
88 private RelationshipData createRelationshipData(final String key, final String value) {
89 final RelationshipData data = new RelationshipData();
90 data.setRelationshipKey(key);
91 data.setRelationshipValue(value);
96 * Get the VNFM assigned for use for the given generic VNF.
98 * @param vnf the generic VNF
99 * @return the VNFM to use, or <code>null</code> if no VNFM has been assigned yet
101 public EsrVnfm getAssignedVnfm(final GenericVnf vnf) {
102 final String vnfmId = getIdOfAssignedVnfm(vnf);
103 return vnfmId == null ? null : aaiServiceProvider.invokeGetVnfm(vnfmId);
107 * Get the ID of the VNFM assigned for use for the given generic VNF.
109 * @param vnf the generic VNF
110 * @return the ID of the VNFM to use, or <code>null</code> if no VNFM has been assigned yet
112 public String getIdOfAssignedVnfm(final GenericVnf vnf) {
113 final Relationship relationship = getRelationship(vnf, "esr-vnfm");
114 return getRelationshipKey(relationship, "esr-vnfm.vnfm-id");
118 * Get the tenant assigned for use for the given generic VNF.
120 * @param vnf the generic VNF
121 * @return the tenant to use, or <code>null</code> if no tenant has been assigned yet
123 public Tenant getAssignedTenant(final GenericVnf vnf) {
124 final Relationship relationship = getRelationship(vnf, "tenant");
125 final String cloudOwner = getRelationshipKey(relationship, "cloud-region.cloud-owner");
126 final String cloudRegion = getRelationshipKey(relationship, "cloud-region.cloud-region-id");
127 final String tenantId = getRelationshipKey(relationship, "tenant.tenant-id");
128 if (cloudOwner == null || cloudRegion == null || tenantId == null) {
129 throw new TenantNotFoundException("No matching Tenant found in AAI. VNFID: " + vnf.getVnfId());
131 return new Tenant().cloudOwner(cloudOwner).regionName(cloudRegion).tenantId(tenantId);
135 private Relationship getRelationship(final GenericVnf vnf, final String relationshipRelatedToValue) {
136 for (final Relationship relationship : vnf.getRelationshipList() == null ? Collections.<Relationship>emptyList()
137 : vnf.getRelationshipList().getRelationship()) {
138 if (relationship.getRelatedTo().equals(relationshipRelatedToValue)) {
145 private String getRelationshipKey(final Relationship relationship, final String relationshipKey) {
146 if (relationship != null) {
147 for (final RelationshipData relationshipData : relationship.getRelationshipData()) {
148 if (relationshipData.getRelationshipKey().equals(relationshipKey)) {
149 return relationshipData.getRelationshipValue();
157 * Select a VNFM to use for the given generic VNF. Should only be used when no VNFM has already been assigned to the
160 * @param vnf the generic VNF
161 * @return the VNFM to use
163 public EsrVnfm selectVnfm(final GenericVnf vnf) {
164 final EsrVnfmList vnfmsInEsr = aaiServiceProvider.invokeGetVnfms();
166 if (vnfmsInEsr == null) {
167 throw new VnfmNotFoundException("No VNFMs found in AAI ESR");
169 logger.debug("VNFMs in ESR: " + vnfmsInEsr);
171 for (final EsrVnfm vnfm : vnfmsInEsr.getEsrVnfm()) {
172 if (vnfmHasMatchingEsrSystemInfoType(vnfm, vnf.getNfType())) {
176 throw new VnfmNotFoundException("No matching VNFM found in AAI ESR");
179 private boolean vnfmHasMatchingEsrSystemInfoType(final EsrVnfm vnfm, final String type) {
180 logger.debug("Checking VNFM ID: " + vnfm + ": " + vnfm.getVnfmId());
182 final EsrSystemInfoList systemInfolist = aaiServiceProvider.invokeGetVnfmEsrSystemInfoList(vnfm.getVnfmId());
183 if (systemInfolist != null) {
184 for (final EsrSystemInfo esrSystemInfo : systemInfolist.getEsrSystemInfo()) {
185 if (esrSystemInfo.getType().equals(type)) {
186 logger.debug("Matched VNFM ID: " + vnfm + ", based on type");
197 * @param vnfc the VNFC to base the vserver on
198 * @return the vserver
200 public Vserver createVserver(final LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs vnfc) {
201 final Vserver vserver = new Vserver();
202 vserver.setVserverId(vnfc.getComputeResource().getResourceId());
203 vserver.setVserverName(vnfc.getId());
204 vserver.setProvStatus("active");
205 vserver.setVserverSelflink("Not available");
210 * Add a relationship to the given vserver to the given VNF.
212 * @param vnf the vserver
213 * @param vnfmId the ID of the VNF
215 public void addRelationshipFromVserverVnfToGenericVnf(final Vserver vserver, final String vnfId) {
216 if (vserver.getRelationshipList() == null) {
217 vserver.setRelationshipList(new RelationshipList());
219 final RelationshipList vserverRelationshiplist = vserver.getRelationshipList();
220 vserverRelationshiplist.getRelationship().add(createRelationshipToGenericVnf(vnfId));
223 private Relationship createRelationshipToGenericVnf(final String vnfId) {
224 final Relationship relationship = new Relationship();
225 relationship.setRelatedTo("generic-vnf");
226 relationship.setRelationshipLabel("tosca.relationships.HostedOn");
227 relationship.setRelatedLink("/aai/" + AAIVersion.LATEST
228 + AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId).build().toString());
229 relationship.getRelationshipData().add(createRelationshipData("generic-vnf.vnf-id", vnfId));
233 public void setOamIpAddressSource(final String vnfId, final OamIpAddressSource oamIpAddressSource) {
234 mapOfVnfIdToOamIpAddressHolder.put(vnfId, oamIpAddressSource);
237 public OamIpAddressSource getOamIpAddressSource(final String vnfId) {
238 return mapOfVnfIdToOamIpAddressHolder.get(vnfId);