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.so.adapters.vnfmadapter.rest.exceptions.VnfmNotFoundException;
33 import org.onap.so.client.aai.AAIObjectType;
34 import org.onap.so.client.aai.AAIVersion;
35 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.stereotype.Service;
42 * Provides helper methods for interactions with AAI.
45 public class AaiHelper {
47 private static final Logger logger = LoggerFactory.getLogger(AaiHelper.class);
48 private final AaiServiceProvider aaiServiceProvider;
51 public AaiHelper(final AaiServiceProvider aaiServiceProvider) {
52 this.aaiServiceProvider = aaiServiceProvider;
56 * Add a relationship to the given generic VNF to the given VNFM.
58 * @param vnf the generic VNF
59 * @param vnfmId the ID of the VNFM
61 public void addRelationshipFromGenericVnfToVnfm(final GenericVnf vnf, final String vnfmId) {
62 if (vnf.getRelationshipList() == null) {
63 vnf.setRelationshipList(new RelationshipList());
65 final RelationshipList vnfmRelationshiplist = vnf.getRelationshipList();
66 vnfmRelationshiplist.getRelationship().add(createRelationshipToVnfm(vnfmId));
68 aaiServiceProvider.invokePutGenericVnf(vnf);
71 private Relationship createRelationshipToVnfm(final String vnfmId) {
72 final Relationship relationship = new Relationship();
73 relationship.setRelatedTo("esr-vnfm");
74 relationship.setRelationshipLabel("tosca.relationships.DependsOn");
75 relationship.setRelatedLink("/aai/" + AAIVersion.LATEST
76 + AAIUriFactory.createResourceUri(AAIObjectType.VNFM, vnfmId).build().toString());
77 relationship.getRelationshipData().add(createRelationshipData("esr-vnfm.vnfm-id", vnfmId));
81 private RelationshipData createRelationshipData(final String key, final String value) {
82 final RelationshipData data = new RelationshipData();
83 data.setRelationshipKey(key);
84 data.setRelationshipValue(value);
89 * Get the VNFM assigned for use for the given generic VNF.
91 * @param vnf the generic VNF
92 * @return the VNFM to use, or <code>null</code> if no VNFM has been assigned yet
94 public EsrVnfm getAssignedVnfm(final GenericVnf vnf) {
95 for (final Relationship relationship : vnf.getRelationshipList() == null ? Collections.<Relationship>emptyList()
96 : vnf.getRelationshipList().getRelationship()) {
97 if ("esr-vnfm".equals(relationship.getRelatedTo())) {
98 return getRelatedVnfmId(relationship);
104 private EsrVnfm getRelatedVnfmId(final Relationship relationship) {
105 for (final RelationshipData relationshipData : relationship.getRelationshipData()) {
106 if ("esr-vnfm.vnfm-id".equals(relationshipData.getRelationshipKey())) {
107 logger.debug("VNFM URL from GenericVnf relataionship: " + relationshipData.getRelationshipValue());
108 return aaiServiceProvider.invokeGetVnfm(relationshipData.getRelationshipValue());
115 * Select a VNFM to use for the given generic VNF. Should only be used when no VNFM has already been
116 * assigned to the VNF.
118 * @param vnf the generic VNF
119 * @return the VNFM to use
121 public EsrVnfm selectVnfm(final GenericVnf vnf) {
122 final EsrVnfmList vnfmsInEsr = aaiServiceProvider.invokeGetVnfms();
124 if (vnfmsInEsr == null) {
125 throw new VnfmNotFoundException("No VNFMs found in AAI ESR");
127 logger.debug("VNFMs in ESR: " + vnfmsInEsr);
129 for (final EsrVnfm vnfm : vnfmsInEsr.getEsrVnfm()) {
130 if (vnfmHasMatchingEsrSystemInfoType(vnfm, vnf.getNfType())) {
134 throw new VnfmNotFoundException("No matching VNFM found in AAI ESR");
137 private boolean vnfmHasMatchingEsrSystemInfoType(final EsrVnfm vnfm, final String type) {
138 logger.debug("Checking VNFM ID: " + vnfm + ": " + vnfm.getVnfmId());
140 final EsrSystemInfoList systemInfolist = aaiServiceProvider.invokeGetVnfmEsrSystemInfoList(vnfm.getVnfmId());
141 if (systemInfolist != null) {
142 for (final EsrSystemInfo esrSystemInfo : systemInfolist.getEsrSystemInfo()) {
143 if (esrSystemInfo.getType().equals(type)) {
144 logger.debug("Matched VNFM ID: " + vnfm + ", based on type");