Refactor SOL003 Adapter to organize its modules
[so.git] / adapters / etsi-sol003-adapter / etsi-sol003-lcm / etsi-sol003-lcm-adapter / src / main / java / org / onap / so / adapters / etsi / sol003 / adapter / lcm / 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.etsi.sol003.adapter.lcm.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.Vserver;
35 import org.onap.etsi.sol003.adapter.lcm.v1.model.Tenant;
36 import org.onap.so.adapters.etsi.sol003.adapter.lcm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs;
37 import org.onap.so.adapters.etsi.sol003.adapter.lcm.rest.exceptions.TenantNotFoundException;
38 import org.onap.so.adapters.etsi.sol003.adapter.lcm.rest.exceptions.VnfmNotFoundException;
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     private final Map<String, OamIpAddressSource> mapOfVnfIdToOamIpAddressHolder = new HashMap<>();
53
54     @Autowired
55     public AaiHelper(final AaiServiceProvider aaiServiceProvider) {
56         this.aaiServiceProvider = aaiServiceProvider;
57     }
58
59     /**
60      * Get the VNFM assigned for use for the given generic VNF.
61      *
62      * @param vnf the generic VNF
63      * @return the VNFM to use, or <code>null</code> if no VNFM has been assigned yet
64      */
65     public EsrVnfm getAssignedVnfm(final GenericVnf vnf) {
66         final String vnfmId = getIdOfAssignedVnfm(vnf);
67         return vnfmId == null ? null : aaiServiceProvider.invokeGetVnfm(vnfmId);
68     }
69
70     /**
71      * Get the ID of the VNFM assigned for use for the given generic VNF.
72      *
73      * @param vnf the generic VNF
74      * @return the ID of the VNFM to use, or <code>null</code> if no VNFM has been assigned yet
75      */
76     public String getIdOfAssignedVnfm(final GenericVnf vnf) {
77         final Relationship relationship = getRelationship(vnf, "esr-vnfm");
78         return getRelationshipData(relationship, "esr-vnfm.vnfm-id");
79     }
80
81     /**
82      * Get the tenant assigned for use for the given generic VNF.
83      *
84      * @param vnf the generic VNF
85      * @return the tenant to use, or <code>null</code> if no tenant has been assigned yet
86      */
87     public Tenant getAssignedTenant(final GenericVnf vnf) {
88         final Relationship relationship = getRelationship(vnf, "tenant");
89         final String cloudOwner = getRelationshipData(relationship, "cloud-region.cloud-owner");
90         final String cloudRegion = getRelationshipData(relationship, "cloud-region.cloud-region-id");
91         final String tenantId = getRelationshipData(relationship, "tenant.tenant-id");
92         if (cloudOwner == null || cloudRegion == null || tenantId == null) {
93             throw new TenantNotFoundException("No matching Tenant found in AAI. VNFID: " + vnf.getVnfId());
94         } else {
95             return new Tenant().cloudOwner(cloudOwner).regionName(cloudRegion).tenantId(tenantId);
96         }
97     }
98
99     private Relationship getRelationship(final GenericVnf vnf, final String relationshipRelatedToValue) {
100         for (final Relationship relationship : vnf.getRelationshipList() == null ? Collections.<Relationship>emptyList()
101                 : vnf.getRelationshipList().getRelationship()) {
102             if (relationship.getRelatedTo().equals(relationshipRelatedToValue)) {
103                 return relationship;
104             }
105         }
106         return null;
107     }
108
109     /**
110      * Get the value of the relationship data with the given key in the given relationship.
111      *
112      * @param relationship the relationship
113      * @param relationshipDataKey the key for the relationship data
114      * @return the value of the relationship data for the given key
115      */
116     public String getRelationshipData(final Relationship relationship, final String relationshipDataKey) {
117         if (relationship != null) {
118             for (final RelationshipData relationshipData : relationship.getRelationshipData()) {
119                 if (relationshipData.getRelationshipKey().equals(relationshipDataKey)) {
120                     return relationshipData.getRelationshipValue();
121                 }
122             }
123         }
124         return null;
125     }
126
127     /**
128      * Delete from the given VNF the relationship matching the given criteria.
129      *
130      * @param vnf the VNF
131      * @param relationshipRelatedToValue the related-to value for the relationship
132      * @param dataKey the relationship data key to match on
133      * @param dataValue the value the relationship data with the given key must match
134      * @return the deleted relationship or <code>null</code> if none found matching the given criteria
135      */
136     public Relationship deleteRelationshipWithDataValue(final GenericVnf vnf, final String relationshipRelatedToValue,
137             final String dataKey, final String dataValue) {
138         final Iterator<Relationship> relationships =
139                 vnf.getRelationshipList() == null ? Collections.<Relationship>emptyList().iterator()
140                         : vnf.getRelationshipList().getRelationship().iterator();
141
142         while (relationships.hasNext()) {
143             final Relationship relationship = relationships.next();
144             if (relationship.getRelatedTo().equals(relationshipRelatedToValue)
145                     && dataValue.equals(getRelationshipData(relationship, dataKey))) {
146                 relationships.remove();
147                 return relationship;
148             }
149         }
150         return null;
151     }
152
153     /**
154      * Select a VNFM to use for the given generic VNF. Should only be used when no VNFM has already been assigned to the
155      * VNF.
156      *
157      * @param vnf the generic VNF
158      * @return the VNFM to use
159      */
160     public EsrVnfm selectVnfm(final GenericVnf vnf) {
161         final EsrVnfmList vnfmsInEsr = aaiServiceProvider.invokeGetVnfms();
162
163         if (vnfmsInEsr == null) {
164             throw new VnfmNotFoundException("No VNFMs found in AAI ESR");
165         }
166         logger.debug("VNFMs in ESR: " + vnfmsInEsr);
167
168         for (final EsrVnfm vnfm : vnfmsInEsr.getEsrVnfm()) {
169             final EsrSystemInfoList systemInfolist =
170                     aaiServiceProvider.invokeGetVnfmEsrSystemInfoList(vnfm.getVnfmId());
171             vnfm.setEsrSystemInfoList(systemInfolist);
172             if (vnfmHasMatchingEsrSystemInfoType(vnfm, vnf.getNfType())) {
173                 return vnfm;
174             }
175         }
176         throw new VnfmNotFoundException("No matching VNFM found in AAI ESR");
177     }
178
179     private boolean vnfmHasMatchingEsrSystemInfoType(final EsrVnfm vnfm, final String type) {
180         logger.debug("Checking VNFM ID: " + vnfm + ": " + vnfm.getVnfmId());
181
182         final EsrSystemInfoList systemInfolist = vnfm.getEsrSystemInfoList();
183         if (systemInfolist != null) {
184             for (final EsrSystemInfo esrSystemInfo : systemInfolist.getEsrSystemInfo()) {
185                 if (esrSystemInfo.getType().equals(type)) {
186                     logger.debug("Matched VNFM ID: " + vnfm + ", based on type");
187                     return true;
188                 }
189             }
190         }
191         return false;
192     }
193
194     /**
195      * Create a vserver.
196      *
197      * @param vnfc the VNFC to base the vserver on
198      * @return the vserver
199      */
200     public Vserver createVserver(final LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs vnfc) {
201         final Vserver vserver = new Vserver();
202         vserver.setVserverId(vnfc.getComputeResource().getResourceId());
203         vserver.setVserverName(vnfc.getId());
204         vserver.setProvStatus("active");
205         vserver.setVserverSelflink("Not available");
206         return vserver;
207     }
208
209     public void setOamIpAddressSource(final String vnfId, final OamIpAddressSource oamIpAddressSource) {
210         mapOfVnfIdToOamIpAddressHolder.put(vnfId, oamIpAddressSource);
211     }
212
213     public OamIpAddressSource getOamIpAddressSource(final String vnfId) {
214         return mapOfVnfIdToOamIpAddressHolder.get(vnfId);
215     }
216
217 }