2 * Copyright 2016-2017, Nokia Corporation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct;
18 import com.google.common.annotations.VisibleForTesting;
19 import okhttp3.Credentials;
20 import okhttp3.Request;
21 import org.onap.aai.ApiClient;
22 import org.onap.aai.api.CloudInfrastructureApi;
23 import org.onap.aai.api.ExternalSystemApi;
24 import org.onap.aai.api.NetworkApi;
25 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.MsbApiProvider;
26 import org.springframework.beans.factory.annotation.Autowired;
27 import org.springframework.beans.factory.annotation.Value;
28 import org.springframework.stereotype.Component;
30 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManager.SERVICE_NAME;
33 * Responsible for providing access to AAI APIs.
34 * Handles authentication and mandatory parameters.
37 public class AAIRestApiProvider {
38 private final MsbApiProvider msbApiProvider;
39 private final AaiSecurityProvider aaiSecurityProvider;
40 @Value("${aaiUsername}")
41 private String aaiUsername;
42 @Value("${aaiPassword}")
43 private String aaiPassword;
46 AAIRestApiProvider(MsbApiProvider msbApiProvider, AaiSecurityProvider aaiSecurityProvider) {
47 this.msbApiProvider = msbApiProvider;
48 this.aaiSecurityProvider = aaiSecurityProvider;
52 * @return API to access the cloud infrastructure
54 public CloudInfrastructureApi getCloudInfrastructureApi() {
55 return buildApiClient(AAIService.CLOUD).createService(CloudInfrastructureApi.class);
59 * @return API to access the external systems
61 public ExternalSystemApi getExternalSystemApi() {
62 return buildApiClient(AAIService.ESR).createService(ExternalSystemApi.class);
66 * @return API to access the networking
68 public NetworkApi getNetworkApi() {
69 return buildApiClient(AAIService.NETWORK).createService(NetworkApi.class);
74 ApiClient buildApiClient(AAIService service) {
75 ApiClient apiClient = new ApiClient();
76 apiClient.getOkBuilder().sslSocketFactory(aaiSecurityProvider.buildSSLSocketFactory(), aaiSecurityProvider.buildTrustManager());
77 apiClient.getOkBuilder().hostnameVerifier(aaiSecurityProvider.buildHostnameVerifier());
78 apiClient.getOkBuilder().addInterceptor(chain -> {
79 Request request = chain.request().newBuilder().addHeader("X-FromAppId", SERVICE_NAME).build();
80 return chain.proceed(request);
82 apiClient.getOkBuilder().authenticator((route, response) -> {
83 String credential = Credentials.basic(aaiUsername, aaiPassword);
84 return response.request().newBuilder().header("Authorization", credential).build();
86 String url = msbApiProvider.getMicroServiceUrl(service.getServiceName(), "v11");
87 if (!url.endsWith("/")) {
90 apiClient.getAdapterBuilder().baseUrl(url);
96 String getServiceName() {
101 String getServiceName() {
102 return "aai-externalSystem";
106 String getServiceName() {
107 return "aai-cloudInfrastructure";
111 abstract String getServiceName();