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.aai.domain.yang.Vserver;
34 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs;
35 import org.onap.so.adapters.vnfmadapter.rest.exceptions.VnfmNotFoundException;
36 import org.onap.so.client.aai.AAIObjectType;
37 import org.onap.so.client.aai.AAIVersion;
38 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.stereotype.Service;
45 * Provides helper methods for interactions with AAI.
48 public class AaiHelper {
50 private static final Logger logger = LoggerFactory.getLogger(AaiHelper.class);
51 private final AaiServiceProvider aaiServiceProvider;
54 public AaiHelper(final AaiServiceProvider aaiServiceProvider) {
55 this.aaiServiceProvider = aaiServiceProvider;
59 * Add a relationship to the given generic VNF to the given VNFM.
61 * @param vnf the generic VNF
62 * @param vnfmId the ID of the VNFM
64 public void addRelationshipFromGenericVnfToVnfm(final GenericVnf vnf, final String vnfmId) {
65 if (vnf.getRelationshipList() == null) {
66 vnf.setRelationshipList(new RelationshipList());
68 final RelationshipList vnfmRelationshiplist = vnf.getRelationshipList();
69 vnfmRelationshiplist.getRelationship().add(createRelationshipToVnfm(vnfmId));
71 aaiServiceProvider.invokePutGenericVnf(vnf);
74 private Relationship createRelationshipToVnfm(final String vnfmId) {
75 final Relationship relationship = new Relationship();
76 relationship.setRelatedTo("esr-vnfm");
77 relationship.setRelationshipLabel("tosca.relationships.DependsOn");
78 relationship.setRelatedLink("/aai/" + AAIVersion.LATEST
79 + AAIUriFactory.createResourceUri(AAIObjectType.VNFM, vnfmId).build().toString());
80 relationship.getRelationshipData().add(createRelationshipData("esr-vnfm.vnfm-id", vnfmId));
84 private RelationshipData createRelationshipData(final String key, final String value) {
85 final RelationshipData data = new RelationshipData();
86 data.setRelationshipKey(key);
87 data.setRelationshipValue(value);
92 * Get the VNFM assigned for use for the given generic VNF.
94 * @param vnf the generic VNF
95 * @return the VNFM to use, or <code>null</code> if no VNFM has been assigned yet
97 public EsrVnfm getAssignedVnfm(final GenericVnf vnf) {
98 final Relationship relationship = getRelationship(vnf, "esr-vnfm");
99 final String vnfmId = getRelationshipKey(relationship, "esr-vnfm.vnfm-id");
100 return vnfmId == null ? null : aaiServiceProvider.invokeGetVnfm(vnfmId);
104 * Get the tenant assigned for use for the given generic VNF.
106 * @param vnf the generic VNF
107 * @return the tenant to use, or <code>null</code> if no tenant has been assigned yet
109 public Tenant getAssignedTenant(final GenericVnf vnf) {
110 final Relationship relationship = getRelationship(vnf, "tenant");
111 final String cloudOwner = getRelationshipKey(relationship, "cloud-region.cloud-owner");
112 final String cloudRegion = getRelationshipKey(relationship, "cloud-region.cloud-region-id");
113 final String tenantId = getRelationshipKey(relationship, "tenant.tenant-id");
114 return cloudOwner == null || cloudRegion == null || tenantId == null ? null
115 : aaiServiceProvider.invokeGetTenant(cloudOwner, cloudRegion, tenantId);
118 private Relationship getRelationship(final GenericVnf vnf, final String relationshipRelatedToValue) {
119 for (final Relationship relationship : vnf.getRelationshipList() == null ? Collections.<Relationship>emptyList()
120 : vnf.getRelationshipList().getRelationship()) {
121 if (relationship.getRelatedTo().equals(relationshipRelatedToValue)) {
128 private String getRelationshipKey(final Relationship relationship, final String relationshipKey) {
129 if (relationship != null) {
130 for (final RelationshipData relationshipData : relationship.getRelationshipData()) {
131 if (relationshipData.getRelationshipKey().equals(relationshipKey)) {
132 return relationshipData.getRelationshipValue();
140 * Select a VNFM to use for the given generic VNF. Should only be used when no VNFM has already been
141 * assigned to the VNF.
143 * @param vnf the generic VNF
144 * @return the VNFM to use
146 public EsrVnfm selectVnfm(final GenericVnf vnf) {
147 final EsrVnfmList vnfmsInEsr = aaiServiceProvider.invokeGetVnfms();
149 if (vnfmsInEsr == null) {
150 throw new VnfmNotFoundException("No VNFMs found in AAI ESR");
152 logger.debug("VNFMs in ESR: " + vnfmsInEsr);
154 for (final EsrVnfm vnfm : vnfmsInEsr.getEsrVnfm()) {
155 if (vnfmHasMatchingEsrSystemInfoType(vnfm, vnf.getNfType())) {
159 throw new VnfmNotFoundException("No matching VNFM found in AAI ESR");
162 private boolean vnfmHasMatchingEsrSystemInfoType(final EsrVnfm vnfm, final String type) {
163 logger.debug("Checking VNFM ID: " + vnfm + ": " + vnfm.getVnfmId());
165 final EsrSystemInfoList systemInfolist = aaiServiceProvider.invokeGetVnfmEsrSystemInfoList(vnfm.getVnfmId());
166 if (systemInfolist != null) {
167 for (final EsrSystemInfo esrSystemInfo : systemInfolist.getEsrSystemInfo()) {
168 if (esrSystemInfo.getType().equals(type)) {
169 logger.debug("Matched VNFM ID: " + vnfm + ", based on type");
180 * @param vnfc the VNFC to base the vserver on
181 * @return the vserver
183 public Vserver createVserver(final LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs vnfc) {
184 final Vserver vserver = new Vserver();
185 vserver.setVserverId(vnfc.getComputeResource().getResourceId());
186 vserver.setVserverName(vnfc.getId());
187 vserver.setProvStatus("active");
188 vserver.setVserverSelflink("Not available");
193 * Add a relationship to the given vserver to the given VNF.
195 * @param vnf the vserver
196 * @param vnfmId the ID of the VNF
198 public void addRelationshipFromVserverVnfToGenericVnf(final Vserver vserver, final String vnfId) {
199 if (vserver.getRelationshipList() == null) {
200 vserver.setRelationshipList(new RelationshipList());
202 final RelationshipList vserverRelationshiplist = vserver.getRelationshipList();
203 vserverRelationshiplist.getRelationship().add(createRelationshipToGenericVnf(vnfId));
206 private Relationship createRelationshipToGenericVnf(final String vnfId) {
207 final Relationship relationship = new Relationship();
208 relationship.setRelatedTo("generic-vnf");
209 relationship.setRelationshipLabel("tosca.relationships.HostedOn");
210 relationship.setRelatedLink("/aai/" + AAIVersion.LATEST
211 + AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId).build().toString());
212 relationship.getRelationshipData().add(createRelationshipData("generic-vnf.vnf-id", vnfId));