Merge "AAI id mapping in SOL002 Adapter"
[so.git] / adapters / mso-ve-vnfm-adapter / src / main / java / org / onap / so / adapters / vevnfm / aai / AaiConnection.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SO
4  * ================================================================================
5  * Copyright (C) 2020 Samsung. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.adapters.vevnfm.aai;
22
23 import java.util.Collections;
24 import java.util.LinkedList;
25 import java.util.List;
26 import java.util.Optional;
27 import org.apache.logging.log4j.util.Strings;
28 import org.onap.aai.domain.yang.*;
29 import org.onap.so.adapters.vevnfm.exception.VeVnfmException;
30 import org.onap.so.client.aai.AAIObjectType;
31 import org.onap.so.client.aai.AAIResourcesClient;
32 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
33 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
34 import org.onap.so.client.graphinventory.entities.uri.Depth;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.stereotype.Service;
38
39 @Service
40 public class AaiConnection {
41
42     private static final Logger logger = LoggerFactory.getLogger(AaiConnection.class);
43
44     private static final String SELFLINK = "selflink";
45     private static final int FIRST_INDEX = 0;
46
47     private AAIResourcesClient resourcesClient = null;
48
49     private static void isValid(final List<EsrSystemInfo> infos) throws VeVnfmException {
50         if (infos == null || infos.isEmpty() || Strings.isBlank(infos.get(FIRST_INDEX).getServiceUrl())) {
51             throw new VeVnfmException("No 'url' field in VNFM info");
52         }
53     }
54
55     private AAIResourcesClient getResourcesClient() {
56         if (resourcesClient == null) {
57             resourcesClient = new AAIResourcesClient();
58         }
59
60         return resourcesClient;
61     }
62
63     public List<EsrSystemInfo> receiveVnfm() throws VeVnfmException {
64         List<EsrSystemInfo> infos;
65
66         try {
67             infos = receiveVnfmInternal();
68         } catch (Exception e) {
69             throw new VeVnfmException(e);
70         }
71
72         isValid(infos);
73
74         return infos;
75     }
76
77     private List<EsrSystemInfo> receiveVnfmInternal() {
78         final AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.VNFM_LIST);
79         final Optional<EsrVnfmList> response = getResourcesClient().get(EsrVnfmList.class, resourceUri);
80
81         if (response.isPresent()) {
82             final EsrVnfmList esrVnfmList = response.get();
83             logger.info("The AAI ESR replied with: {}", esrVnfmList);
84             final List<EsrVnfm> esrVnfm = esrVnfmList.getEsrVnfm();
85
86             final List<EsrSystemInfo> infos = new LinkedList<>();
87
88             for (final EsrVnfm vnfm : esrVnfm) {
89                 final String vnfmId = vnfm.getVnfmId();
90                 infos.addAll(receiveVnfmServiceUrl(vnfmId));
91             }
92
93             return infos;
94         }
95
96         return null;
97     }
98
99     private List<EsrSystemInfo> receiveVnfmServiceUrl(final String vnfmId) {
100         final Optional<EsrVnfm> response = getResourcesClient().get(EsrVnfm.class,
101                 AAIUriFactory.createResourceUri(AAIObjectType.VNFM, vnfmId).depth(Depth.ONE));
102
103         if (response.isPresent()) {
104             final EsrVnfm esrVnfm = response.get();
105             logger.info("The AAI ESR replied with: {}", esrVnfm);
106             final EsrSystemInfoList esrSystemInfoList = esrVnfm.getEsrSystemInfoList();
107
108             if (esrSystemInfoList != null) {
109                 return esrSystemInfoList.getEsrSystemInfo();
110             }
111         }
112
113         return Collections.emptyList();
114     }
115
116     public String receiveGenericVnfId(final String href) {
117         final AAIResourceUri resourceUri =
118                 AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNFS).queryParam(SELFLINK, href);
119         final Optional<GenericVnfs> response = getResourcesClient().get(GenericVnfs.class, resourceUri);
120
121         if (response.isPresent()) {
122             final GenericVnfs vnfs = response.get();
123             logger.info("The AAI replied with: {}", vnfs);
124             final List<GenericVnf> genericVnfList = vnfs.getGenericVnf();
125             final int size = genericVnfList.size();
126
127             if (size == 1) {
128                 return genericVnfList.get(FIRST_INDEX).getVnfId();
129             } else if (size > 1) {
130                 logger.warn("more generic vnfs available");
131             }
132         }
133
134         return null;
135     }
136 }