2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.adapters.vevnfm.aai;
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;
40 public class AaiConnection {
42 private static final Logger logger = LoggerFactory.getLogger(AaiConnection.class);
44 private static final String SELFLINK = "selflink";
45 private static final int FIRST_INDEX = 0;
47 private AAIResourcesClient resourcesClient = null;
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");
55 private AAIResourcesClient getResourcesClient() {
56 if (resourcesClient == null) {
57 resourcesClient = new AAIResourcesClient();
60 return resourcesClient;
63 public List<EsrSystemInfo> receiveVnfm() throws VeVnfmException {
64 List<EsrSystemInfo> infos;
67 infos = receiveVnfmInternal();
68 } catch (Exception e) {
69 throw new VeVnfmException(e);
77 private List<EsrSystemInfo> receiveVnfmInternal() {
78 final AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.VNFM_LIST);
79 final Optional<EsrVnfmList> response = getResourcesClient().get(EsrVnfmList.class, resourceUri);
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();
86 final List<EsrSystemInfo> infos = new LinkedList<>();
88 for (final EsrVnfm vnfm : esrVnfm) {
89 final String vnfmId = vnfm.getVnfmId();
90 infos.addAll(receiveVnfmServiceUrl(vnfmId));
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));
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();
108 if (esrSystemInfoList != null) {
109 return esrSystemInfoList.getEsrSystemInfo();
113 return Collections.emptyList();
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);
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();
128 return genericVnfList.get(FIRST_INDEX).getVnfId();
129 } else if (size > 1) {
130 logger.warn("more generic vnfs available");