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