1374e89a192d6a0941fd951f9215258f9f4eda8d
[so.git] / adapters / mso-vnfm-adapter / mso-vnfm-etsi-adapter / src / main / java / org / onap / so / adapters / vnfmadapter / extclients / aai / AaiHelper.java
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.Iterator;
26 import java.util.Map;
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.RelationshipList;
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.TenantNotFoundException;
38 import org.onap.so.adapters.vnfmadapter.rest.exceptions.VnfmNotFoundException;
39 import org.onap.so.client.aai.AAIObjectType;
40 import org.onap.so.client.aai.AAIVersion;
41 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
42 import org.onap.vnfmadapter.v1.model.Tenant;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.stereotype.Service;
47
48 /**
49  * Provides helper methods for interactions with AAI.
50  */
51 @Service
52 public class AaiHelper {
53
54     private static final Logger logger = LoggerFactory.getLogger(AaiHelper.class);
55     private final AaiServiceProvider aaiServiceProvider;
56     private final Map<String, OamIpAddressSource> mapOfVnfIdToOamIpAddressHolder = new HashMap<>();
57
58     @Autowired
59     public AaiHelper(final AaiServiceProvider aaiServiceProvider) {
60         this.aaiServiceProvider = aaiServiceProvider;
61     }
62
63     /**
64      * Add a relationship to the given generic VNF to the given VNFM.
65      *
66      * @param vnf the generic VNF
67      * @param vnfmId the ID of the VNFM
68      */
69     public void addRelationshipFromGenericVnfToVnfm(final GenericVnf vnf, final String vnfmId) {
70         if (vnf.getRelationshipList() == null) {
71             vnf.setRelationshipList(new RelationshipList());
72         }
73         final RelationshipList vnfmRelationshiplist = vnf.getRelationshipList();
74         vnfmRelationshiplist.getRelationship().add(createRelationshipToVnfm(vnfmId));
75
76     }
77
78     private Relationship createRelationshipToVnfm(final String vnfmId) {
79         final Relationship relationship = new Relationship();
80         relationship.setRelatedTo("esr-vnfm");
81         relationship.setRelationshipLabel("tosca.relationships.DependsOn");
82         relationship.setRelatedLink("/aai/" + AAIVersion.LATEST
83                 + AAIUriFactory.createResourceUri(AAIObjectType.VNFM, vnfmId).build().toString());
84         relationship.getRelationshipData().add(createRelationshipData("esr-vnfm.vnfm-id", vnfmId));
85         return relationship;
86     }
87
88     private RelationshipData createRelationshipData(final String key, final String value) {
89         final RelationshipData data = new RelationshipData();
90         data.setRelationshipKey(key);
91         data.setRelationshipValue(value);
92         return data;
93     }
94
95     /**
96      * Get the VNFM assigned for use for the given generic VNF.
97      *
98      * @param vnf the generic VNF
99      * @return the VNFM to use, or <code>null</code> if no VNFM has been assigned yet
100      */
101     public EsrVnfm getAssignedVnfm(final GenericVnf vnf) {
102         final String vnfmId = getIdOfAssignedVnfm(vnf);
103         return vnfmId == null ? null : aaiServiceProvider.invokeGetVnfm(vnfmId);
104     }
105
106     /**
107      * Get the ID of the VNFM assigned for use for the given generic VNF.
108      *
109      * @param vnf the generic VNF
110      * @return the ID of the VNFM to use, or <code>null</code> if no VNFM has been assigned yet
111      */
112     public String getIdOfAssignedVnfm(final GenericVnf vnf) {
113         final Relationship relationship = getRelationship(vnf, "esr-vnfm");
114         return getRelationshipData(relationship, "esr-vnfm.vnfm-id");
115     }
116
117     /**
118      * Get the tenant assigned for use for the given generic VNF.
119      *
120      * @param vnf the generic VNF
121      * @return the tenant to use, or <code>null</code> if no tenant has been assigned yet
122      */
123     public Tenant getAssignedTenant(final GenericVnf vnf) {
124         final Relationship relationship = getRelationship(vnf, "tenant");
125         final String cloudOwner = getRelationshipData(relationship, "cloud-region.cloud-owner");
126         final String cloudRegion = getRelationshipData(relationship, "cloud-region.cloud-region-id");
127         final String tenantId = getRelationshipData(relationship, "tenant.tenant-id");
128         if (cloudOwner == null || cloudRegion == null || tenantId == null) {
129             throw new TenantNotFoundException("No matching Tenant found in AAI. VNFID: " + vnf.getVnfId());
130         } else {
131             return new Tenant().cloudOwner(cloudOwner).regionName(cloudRegion).tenantId(tenantId);
132         }
133     }
134
135     private Relationship getRelationship(final GenericVnf vnf, final String relationshipRelatedToValue) {
136         for (final Relationship relationship : vnf.getRelationshipList() == null ? Collections.<Relationship>emptyList()
137                 : vnf.getRelationshipList().getRelationship()) {
138             if (relationship.getRelatedTo().equals(relationshipRelatedToValue)) {
139                 return relationship;
140             }
141         }
142         return null;
143     }
144
145     /**
146      * Get the value of the relationship data with the given key in the given relationship.
147      *
148      * @param relationship the relationship
149      * @param relationshipDataKey the key for the relationship data
150      * @return the value of the relationship data for the given key
151      */
152     public String getRelationshipData(final Relationship relationship, final String relationshipDataKey) {
153         if (relationship != null) {
154             for (final RelationshipData relationshipData : relationship.getRelationshipData()) {
155                 if (relationshipData.getRelationshipKey().equals(relationshipDataKey)) {
156                     return relationshipData.getRelationshipValue();
157                 }
158             }
159         }
160         return null;
161     }
162
163     /**
164      * Delete from the given VNF the relationship matching the given criteria.
165      *
166      * @param vnf the VNF
167      * @param relationshipRelatedToValue the related-to value for the relationship
168      * @param dataKey the relationship data key to match on
169      * @param dataValue the value the relationship data with the given key must match
170      * @return the deleted relationship or <code>null</code> if none found matching the given criteria
171      */
172     public Relationship deleteRelationshipWithDataValue(final GenericVnf vnf, final String relationshipRelatedToValue,
173             final String dataKey, final String dataValue) {
174         final Iterator<Relationship> relationships =
175                 vnf.getRelationshipList() == null ? Collections.<Relationship>emptyList().iterator()
176                         : vnf.getRelationshipList().getRelationship().iterator();
177
178         while (relationships.hasNext()) {
179             final Relationship relationship = relationships.next();
180             if (relationship.getRelatedTo().equals(relationshipRelatedToValue)
181                     && dataValue.equals(getRelationshipData(relationship, dataKey))) {
182                 relationships.remove();
183                 return relationship;
184             }
185         }
186         return null;
187     }
188
189     /**
190      * Select a VNFM to use for the given generic VNF. Should only be used when no VNFM has already been assigned to the
191      * VNF.
192      *
193      * @param vnf the generic VNF
194      * @return the VNFM to use
195      */
196     public EsrVnfm selectVnfm(final GenericVnf vnf) {
197         final EsrVnfmList vnfmsInEsr = aaiServiceProvider.invokeGetVnfms();
198
199         if (vnfmsInEsr == null) {
200             throw new VnfmNotFoundException("No VNFMs found in AAI ESR");
201         }
202         logger.debug("VNFMs in ESR: " + vnfmsInEsr);
203
204         for (final EsrVnfm vnfm : vnfmsInEsr.getEsrVnfm()) {
205             if (vnfmHasMatchingEsrSystemInfoType(vnfm, vnf.getNfType())) {
206                 return vnfm;
207             }
208         }
209         throw new VnfmNotFoundException("No matching VNFM found in AAI ESR");
210     }
211
212     private boolean vnfmHasMatchingEsrSystemInfoType(final EsrVnfm vnfm, final String type) {
213         logger.debug("Checking VNFM ID: " + vnfm + ": " + vnfm.getVnfmId());
214
215         final EsrSystemInfoList systemInfolist = aaiServiceProvider.invokeGetVnfmEsrSystemInfoList(vnfm.getVnfmId());
216         if (systemInfolist != null) {
217             for (final EsrSystemInfo esrSystemInfo : systemInfolist.getEsrSystemInfo()) {
218                 if (esrSystemInfo.getType().equals(type)) {
219                     logger.debug("Matched VNFM ID: " + vnfm + ", based on type");
220                     return true;
221                 }
222             }
223         }
224         return false;
225     }
226
227     /**
228      * Create a vserver.
229      *
230      * @param vnfc the VNFC to base the vserver on
231      * @return the vserver
232      */
233     public Vserver createVserver(final LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs vnfc) {
234         final Vserver vserver = new Vserver();
235         vserver.setVserverId(vnfc.getComputeResource().getResourceId());
236         vserver.setVserverName(vnfc.getId());
237         vserver.setProvStatus("active");
238         vserver.setVserverSelflink("Not available");
239         return vserver;
240     }
241
242     /**
243      * Add a relationship to the given vserver to the given VNF.
244      *
245      * @param vnf the vserver
246      * @param vnfmId the ID of the VNF
247      */
248     public void addRelationshipFromVserverVnfToGenericVnf(final Vserver vserver, final String vnfId) {
249         if (vserver.getRelationshipList() == null) {
250             vserver.setRelationshipList(new RelationshipList());
251         }
252         final RelationshipList vserverRelationshiplist = vserver.getRelationshipList();
253         vserverRelationshiplist.getRelationship().add(createRelationshipToGenericVnf(vnfId));
254     }
255
256     private Relationship createRelationshipToGenericVnf(final String vnfId) {
257         final Relationship relationship = new Relationship();
258         relationship.setRelatedTo("generic-vnf");
259         relationship.setRelationshipLabel("tosca.relationships.HostedOn");
260         relationship.setRelatedLink("/aai/" + AAIVersion.LATEST
261                 + AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId).build().toString());
262         relationship.getRelationshipData().add(createRelationshipData("generic-vnf.vnf-id", vnfId));
263         return relationship;
264     }
265
266     public void setOamIpAddressSource(final String vnfId, final OamIpAddressSource oamIpAddressSource) {
267         mapOfVnfIdToOamIpAddressHolder.put(vnfId, oamIpAddressSource);
268     }
269
270     public OamIpAddressSource getOamIpAddressSource(final String vnfId) {
271         return mapOfVnfIdToOamIpAddressHolder.get(vnfId);
272     }
273
274     /**
275      * Add a relationship to the given tenant to the given VNF.
276      *
277      * @param vnf the generic vnf
278      * @param tenant the Tenant
279      */
280
281     public void addRelationshipFromGenericVnfToTenant(final GenericVnf vnf, final Tenant tenant) {
282         if (vnf.getRelationshipList() == null) {
283             vnf.setRelationshipList(new RelationshipList());
284         }
285         final RelationshipList vnfmRelationshiplist = vnf.getRelationshipList();
286         vnfmRelationshiplist.getRelationship().add(createRelationshipToTenant(tenant));
287     }
288
289     private Relationship createRelationshipToTenant(final Tenant tenant) {
290         final Relationship relationship = new Relationship();
291         relationship.setRelatedTo("tenant");
292         relationship.setRelatedLink("/aai/" + AAIVersion.LATEST + AAIUriFactory.createResourceUri(AAIObjectType.TENANT,
293                 tenant.getCloudOwner(), tenant.getRegionName(), tenant.getTenantId()).build().toString());
294         relationship.getRelationshipData()
295                 .add(createRelationshipData("cloud-region.cloud-owner", tenant.getCloudOwner()));
296         relationship.getRelationshipData()
297                 .add(createRelationshipData("cloud-region.cloud-region-id", tenant.getRegionName()));
298         relationship.getRelationshipData().add(createRelationshipData("tenant.tenant-id", tenant.getTenantId()));
299         return relationship;
300     }
301
302 }