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