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 String vnfmId = getIdOfAssignedVnfm(vnf);
99 return vnfmId == null ? null : aaiServiceProvider.invokeGetVnfm(vnfmId);
103 * Get the ID of the VNFM assigned for use for the given generic VNF.
105 * @param vnf the generic VNF
106 * @return the ID of the VNFM to use, or <code>null</code> if no VNFM has been assigned yet
108 public String getIdOfAssignedVnfm(final GenericVnf vnf) {
109 final Relationship relationship = getRelationship(vnf, "esr-vnfm");
110 return getRelationshipKey(relationship, "esr-vnfm.vnfm-id");
114 * Get the tenant assigned for use for the given generic VNF.
116 * @param vnf the generic VNF
117 * @return the tenant to use, or <code>null</code> if no tenant has been assigned yet
119 public Tenant getAssignedTenant(final GenericVnf vnf) {
120 final Relationship relationship = getRelationship(vnf, "tenant");
121 final String cloudOwner = getRelationshipKey(relationship, "cloud-region.cloud-owner");
122 final String cloudRegion = getRelationshipKey(relationship, "cloud-region.cloud-region-id");
123 final String tenantId = getRelationshipKey(relationship, "tenant.tenant-id");
124 return cloudOwner == null || cloudRegion == null || tenantId == null ? null
125 : aaiServiceProvider.invokeGetTenant(cloudOwner, cloudRegion, tenantId);
128 private Relationship getRelationship(final GenericVnf vnf, final String relationshipRelatedToValue) {
129 for (final Relationship relationship : vnf.getRelationshipList() == null ? Collections.<Relationship>emptyList()
130 : vnf.getRelationshipList().getRelationship()) {
131 if (relationship.getRelatedTo().equals(relationshipRelatedToValue)) {
138 private String getRelationshipKey(final Relationship relationship, final String relationshipKey) {
139 if (relationship != null) {
140 for (final RelationshipData relationshipData : relationship.getRelationshipData()) {
141 if (relationshipData.getRelationshipKey().equals(relationshipKey)) {
142 return relationshipData.getRelationshipValue();
150 * Select a VNFM to use for the given generic VNF. Should only be used when no VNFM has already been
151 * assigned to the VNF.
153 * @param vnf the generic VNF
154 * @return the VNFM to use
156 public EsrVnfm selectVnfm(final GenericVnf vnf) {
157 final EsrVnfmList vnfmsInEsr = aaiServiceProvider.invokeGetVnfms();
159 if (vnfmsInEsr == null) {
160 throw new VnfmNotFoundException("No VNFMs found in AAI ESR");
162 logger.debug("VNFMs in ESR: " + vnfmsInEsr);
164 for (final EsrVnfm vnfm : vnfmsInEsr.getEsrVnfm()) {
165 if (vnfmHasMatchingEsrSystemInfoType(vnfm, vnf.getNfType())) {
169 throw new VnfmNotFoundException("No matching VNFM found in AAI ESR");
172 private boolean vnfmHasMatchingEsrSystemInfoType(final EsrVnfm vnfm, final String type) {
173 logger.debug("Checking VNFM ID: " + vnfm + ": " + vnfm.getVnfmId());
175 final EsrSystemInfoList systemInfolist = aaiServiceProvider.invokeGetVnfmEsrSystemInfoList(vnfm.getVnfmId());
176 if (systemInfolist != null) {
177 for (final EsrSystemInfo esrSystemInfo : systemInfolist.getEsrSystemInfo()) {
178 if (esrSystemInfo.getType().equals(type)) {
179 logger.debug("Matched VNFM ID: " + vnfm + ", based on type");
190 * @param vnfc the VNFC to base the vserver on
191 * @return the vserver
193 public Vserver createVserver(final LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs vnfc) {
194 final Vserver vserver = new Vserver();
195 vserver.setVserverId(vnfc.getComputeResource().getResourceId());
196 vserver.setVserverName(vnfc.getId());
197 vserver.setProvStatus("active");
198 vserver.setVserverSelflink("Not available");
203 * Add a relationship to the given vserver to the given VNF.
205 * @param vnf the vserver
206 * @param vnfmId the ID of the VNF
208 public void addRelationshipFromVserverVnfToGenericVnf(final Vserver vserver, final String vnfId) {
209 if (vserver.getRelationshipList() == null) {
210 vserver.setRelationshipList(new RelationshipList());
212 final RelationshipList vserverRelationshiplist = vserver.getRelationshipList();
213 vserverRelationshiplist.getRelationship().add(createRelationshipToGenericVnf(vnfId));
216 private Relationship createRelationshipToGenericVnf(final String vnfId) {
217 final Relationship relationship = new Relationship();
218 relationship.setRelatedTo("generic-vnf");
219 relationship.setRelationshipLabel("tosca.relationships.HostedOn");
220 relationship.setRelatedLink("/aai/" + AAIVersion.LATEST
221 + AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId).build().toString());
222 relationship.getRelationshipData().add(createRelationshipData("generic-vnf.vnf-id", vnfId));