ee04819ca499134ceb71a3b919ca46d543845f41
[so.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.adapters.vnfmadapter.extclients.aai;
22
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.Map;
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.Tenant;
35 import org.onap.aai.domain.yang.Vserver;
36 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs;
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.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.stereotype.Service;
45
46 /**
47  * Provides helper methods for interactions with AAI.
48  */
49 @Service
50 public class AaiHelper {
51
52     private static final Logger logger = LoggerFactory.getLogger(AaiHelper.class);
53     private final AaiServiceProvider aaiServiceProvider;
54     private final Map<String, OamIpAddressSource> mapOfVnfIdToOamIpAddressHolder = new HashMap<>();
55
56     @Autowired
57     public AaiHelper(final AaiServiceProvider aaiServiceProvider) {
58         this.aaiServiceProvider = aaiServiceProvider;
59     }
60
61     /**
62      * Add a relationship to the given generic VNF to the given VNFM.
63      *
64      * @param vnf the generic VNF
65      * @param vnfmId the ID of the VNFM
66      */
67     public void addRelationshipFromGenericVnfToVnfm(final GenericVnf vnf, final String vnfmId) {
68         if (vnf.getRelationshipList() == null) {
69             vnf.setRelationshipList(new RelationshipList());
70         }
71         final RelationshipList vnfmRelationshiplist = vnf.getRelationshipList();
72         vnfmRelationshiplist.getRelationship().add(createRelationshipToVnfm(vnfmId));
73
74         aaiServiceProvider.invokePutGenericVnf(vnf);
75     }
76
77     private Relationship createRelationshipToVnfm(final String vnfmId) {
78         final Relationship relationship = new Relationship();
79         relationship.setRelatedTo("esr-vnfm");
80         relationship.setRelationshipLabel("tosca.relationships.DependsOn");
81         relationship.setRelatedLink("/aai/" + AAIVersion.LATEST
82                 + AAIUriFactory.createResourceUri(AAIObjectType.VNFM, vnfmId).build().toString());
83         relationship.getRelationshipData().add(createRelationshipData("esr-vnfm.vnfm-id", vnfmId));
84         return relationship;
85     }
86
87     private RelationshipData createRelationshipData(final String key, final String value) {
88         final RelationshipData data = new RelationshipData();
89         data.setRelationshipKey(key);
90         data.setRelationshipValue(value);
91         return data;
92     }
93
94     /**
95      * Get the VNFM assigned for use for the given generic VNF.
96      *
97      * @param vnf the generic VNF
98      * @return the VNFM to use, or <code>null</code> if no VNFM has been assigned yet
99      */
100     public EsrVnfm getAssignedVnfm(final GenericVnf vnf) {
101         final String vnfmId = getIdOfAssignedVnfm(vnf);
102         return vnfmId == null ? null : aaiServiceProvider.invokeGetVnfm(vnfmId);
103     }
104
105     /**
106      * Get the ID of the VNFM assigned for use for the given generic VNF.
107      *
108      * @param vnf the generic VNF
109      * @return the ID of the VNFM to use, or <code>null</code> if no VNFM has been assigned yet
110      */
111     public String getIdOfAssignedVnfm(final GenericVnf vnf) {
112         final Relationship relationship = getRelationship(vnf, "esr-vnfm");
113         return getRelationshipKey(relationship, "esr-vnfm.vnfm-id");
114     }
115
116     /**
117      * Get the tenant assigned for use for the given generic VNF.
118      *
119      * @param vnf the generic VNF
120      * @return the tenant to use, or <code>null</code> if no tenant has been assigned yet
121      */
122     public Tenant getAssignedTenant(final GenericVnf vnf) {
123         final Relationship relationship = getRelationship(vnf, "tenant");
124         final String cloudOwner = getRelationshipKey(relationship, "cloud-region.cloud-owner");
125         final String cloudRegion = getRelationshipKey(relationship, "cloud-region.cloud-region-id");
126         final String tenantId = getRelationshipKey(relationship, "tenant.tenant-id");
127         return cloudOwner == null || cloudRegion == null || tenantId == null ? null
128                 : aaiServiceProvider.invokeGetTenant(cloudOwner, cloudRegion, tenantId);
129     }
130
131     private Relationship getRelationship(final GenericVnf vnf, final String relationshipRelatedToValue) {
132         for (final Relationship relationship : vnf.getRelationshipList() == null ? Collections.<Relationship>emptyList()
133                 : vnf.getRelationshipList().getRelationship()) {
134             if (relationship.getRelatedTo().equals(relationshipRelatedToValue)) {
135                 return relationship;
136             }
137         }
138         return null;
139     }
140
141     private String getRelationshipKey(final Relationship relationship, final String relationshipKey) {
142         if (relationship != null) {
143             for (final RelationshipData relationshipData : relationship.getRelationshipData()) {
144                 if (relationshipData.getRelationshipKey().equals(relationshipKey)) {
145                     return relationshipData.getRelationshipValue();
146                 }
147             }
148         }
149         return null;
150     }
151
152     /**
153      * Select a VNFM to use for the given generic VNF. Should only be used when no VNFM has already been assigned to the
154      * VNF.
155      *
156      * @param vnf the generic VNF
157      * @return the VNFM to use
158      */
159     public EsrVnfm selectVnfm(final GenericVnf vnf) {
160         final EsrVnfmList vnfmsInEsr = aaiServiceProvider.invokeGetVnfms();
161
162         if (vnfmsInEsr == null) {
163             throw new VnfmNotFoundException("No VNFMs found in AAI ESR");
164         }
165         logger.debug("VNFMs in ESR: " + vnfmsInEsr);
166
167         for (final EsrVnfm vnfm : vnfmsInEsr.getEsrVnfm()) {
168             if (vnfmHasMatchingEsrSystemInfoType(vnfm, vnf.getNfType())) {
169                 return vnfm;
170             }
171         }
172         throw new VnfmNotFoundException("No matching VNFM found in AAI ESR");
173     }
174
175     private boolean vnfmHasMatchingEsrSystemInfoType(final EsrVnfm vnfm, final String type) {
176         logger.debug("Checking VNFM ID: " + vnfm + ": " + vnfm.getVnfmId());
177
178         final EsrSystemInfoList systemInfolist = aaiServiceProvider.invokeGetVnfmEsrSystemInfoList(vnfm.getVnfmId());
179         if (systemInfolist != null) {
180             for (final EsrSystemInfo esrSystemInfo : systemInfolist.getEsrSystemInfo()) {
181                 if (esrSystemInfo.getType().equals(type)) {
182                     logger.debug("Matched VNFM ID: " + vnfm + ", based on type");
183                     return true;
184                 }
185             }
186         }
187         return false;
188     }
189
190     /**
191      * Create a vserver.
192      *
193      * @param vnfc the VNFC to base the vserver on
194      * @return the vserver
195      */
196     public Vserver createVserver(final LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs vnfc) {
197         final Vserver vserver = new Vserver();
198         vserver.setVserverId(vnfc.getComputeResource().getResourceId());
199         vserver.setVserverName(vnfc.getId());
200         vserver.setProvStatus("active");
201         vserver.setVserverSelflink("Not available");
202         return vserver;
203     }
204
205     /**
206      * Add a relationship to the given vserver to the given VNF.
207      *
208      * @param vnf the vserver
209      * @param vnfmId the ID of the VNF
210      */
211     public void addRelationshipFromVserverVnfToGenericVnf(final Vserver vserver, final String vnfId) {
212         if (vserver.getRelationshipList() == null) {
213             vserver.setRelationshipList(new RelationshipList());
214         }
215         final RelationshipList vserverRelationshiplist = vserver.getRelationshipList();
216         vserverRelationshiplist.getRelationship().add(createRelationshipToGenericVnf(vnfId));
217     }
218
219     private Relationship createRelationshipToGenericVnf(final String vnfId) {
220         final Relationship relationship = new Relationship();
221         relationship.setRelatedTo("generic-vnf");
222         relationship.setRelationshipLabel("tosca.relationships.HostedOn");
223         relationship.setRelatedLink("/aai/" + AAIVersion.LATEST
224                 + AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId).build().toString());
225         relationship.getRelationshipData().add(createRelationshipData("generic-vnf.vnf-id", vnfId));
226         return relationship;
227     }
228
229     public void setOamIpAddressSource(final String vnfId, final OamIpAddressSource oamIpAddressSource) {
230         mapOfVnfIdToOamIpAddressHolder.put(vnfId, oamIpAddressSource);
231     }
232
233     public OamIpAddressSource getOamIpAddressSource(final String vnfId) {
234         return mapOfVnfIdToOamIpAddressHolder.get(vnfId);
235     }
236
237 }