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.etsisol003adapter.lcm.extclients.aai;
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.Iterator;
27 import org.onap.aai.domain.yang.EsrSystemInfo;
28 import org.onap.aai.domain.yang.EsrSystemInfoList;
29 import org.onap.aai.domain.yang.EsrVnfm;
30 import org.onap.aai.domain.yang.EsrVnfmList;
31 import org.onap.aai.domain.yang.GenericVnf;
32 import org.onap.aai.domain.yang.Relationship;
33 import org.onap.aai.domain.yang.RelationshipData;
34 import org.onap.aai.domain.yang.Vserver;
35 import org.onap.so.adapters.etsisol003adapter.lcm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs;
36 import org.onap.so.adapters.etsisol003adapter.lcm.rest.exceptions.TenantNotFoundException;
37 import org.onap.so.adapters.etsisol003adapter.lcm.rest.exceptions.VnfmNotFoundException;
38 import org.onap.so.adapters.etsisol003adapter.lcm.v1.model.Tenant;
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;
52 private final Map<String, OamIpAddressSource> mapOfVnfIdToOamIpAddressHolder = new HashMap<>();
55 public AaiHelper(final AaiServiceProvider aaiServiceProvider) {
56 this.aaiServiceProvider = aaiServiceProvider;
60 * Get the VNFM assigned for use for the given generic VNF.
62 * @param vnf the generic VNF
63 * @return the VNFM to use, or <code>null</code> if no VNFM has been assigned yet
65 public EsrVnfm getAssignedVnfm(final GenericVnf vnf) {
66 final String vnfmId = getIdOfAssignedVnfm(vnf);
67 return vnfmId == null ? null : aaiServiceProvider.invokeGetVnfm(vnfmId);
71 * Get the ID of the VNFM assigned for use for the given generic VNF.
73 * @param vnf the generic VNF
74 * @return the ID of the VNFM to use, or <code>null</code> if no VNFM has been assigned yet
76 public String getIdOfAssignedVnfm(final GenericVnf vnf) {
77 final Relationship relationship = getRelationship(vnf, "esr-vnfm");
78 return getRelationshipData(relationship, "esr-vnfm.vnfm-id");
82 * Get the tenant assigned for use for the given generic VNF.
84 * @param vnf the generic VNF
85 * @return the tenant to use, or <code>null</code> if no tenant has been assigned yet
87 public Tenant getAssignedTenant(final GenericVnf vnf) {
88 final Relationship relationship = getRelationship(vnf, "tenant");
89 final String cloudOwner = getRelationshipData(relationship, "cloud-region.cloud-owner");
90 final String cloudRegion = getRelationshipData(relationship, "cloud-region.cloud-region-id");
91 final String tenantId = getRelationshipData(relationship, "tenant.tenant-id");
92 if (cloudOwner == null || cloudRegion == null || tenantId == null) {
93 throw new TenantNotFoundException("No matching Tenant found in AAI. VNFID: " + vnf.getVnfId());
95 return new Tenant().cloudOwner(cloudOwner).regionName(cloudRegion).tenantId(tenantId);
99 private Relationship getRelationship(final GenericVnf vnf, final String relationshipRelatedToValue) {
100 for (final Relationship relationship : vnf.getRelationshipList() == null ? Collections.<Relationship>emptyList()
101 : vnf.getRelationshipList().getRelationship()) {
102 if (relationship.getRelatedTo().equals(relationshipRelatedToValue)) {
110 * Get the value of the relationship data with the given key in the given relationship.
112 * @param relationship the relationship
113 * @param relationshipDataKey the key for the relationship data
114 * @return the value of the relationship data for the given key
116 public String getRelationshipData(final Relationship relationship, final String relationshipDataKey) {
117 if (relationship != null) {
118 for (final RelationshipData relationshipData : relationship.getRelationshipData()) {
119 if (relationshipData.getRelationshipKey().equals(relationshipDataKey)) {
120 return relationshipData.getRelationshipValue();
128 * Delete from the given VNF the relationship matching the given criteria.
131 * @param relationshipRelatedToValue the related-to value for the relationship
132 * @param dataKey the relationship data key to match on
133 * @param dataValue the value the relationship data with the given key must match
134 * @return the deleted relationship or <code>null</code> if none found matching the given criteria
136 public Relationship deleteRelationshipWithDataValue(final GenericVnf vnf, final String relationshipRelatedToValue,
137 final String dataKey, final String dataValue) {
138 final Iterator<Relationship> relationships =
139 vnf.getRelationshipList() == null ? Collections.<Relationship>emptyList().iterator()
140 : vnf.getRelationshipList().getRelationship().iterator();
142 while (relationships.hasNext()) {
143 final Relationship relationship = relationships.next();
144 if (relationship.getRelatedTo().equals(relationshipRelatedToValue)
145 && dataValue.equals(getRelationshipData(relationship, dataKey))) {
146 relationships.remove();
154 * Select a VNFM to use for the given generic VNF. Should only be used when no VNFM has already been assigned to the
157 * @param vnf the generic VNF
158 * @return the VNFM to use
160 public EsrVnfm selectVnfm(final GenericVnf vnf) {
161 final EsrVnfmList vnfmsInEsr = aaiServiceProvider.invokeGetVnfms();
163 if (vnfmsInEsr == null) {
164 throw new VnfmNotFoundException("No VNFMs found in AAI ESR");
166 logger.debug("VNFMs in ESR: " + vnfmsInEsr);
168 for (final EsrVnfm vnfm : vnfmsInEsr.getEsrVnfm()) {
169 final EsrSystemInfoList systemInfolist =
170 aaiServiceProvider.invokeGetVnfmEsrSystemInfoList(vnfm.getVnfmId());
171 vnfm.setEsrSystemInfoList(systemInfolist);
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 = vnfm.getEsrSystemInfoList();
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");
209 public void setOamIpAddressSource(final String vnfId, final OamIpAddressSource oamIpAddressSource) {
210 mapOfVnfIdToOamIpAddressHolder.put(vnfId, oamIpAddressSource);
213 public OamIpAddressSource getOamIpAddressSource(final String vnfId) {
214 return mapOfVnfIdToOamIpAddressHolder.get(vnfId);