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 java.util.UUID;
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.aai.auth.HttpBasicAuth;
26 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.MsbApiProvider;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.beans.factory.annotation.Value;
29 import org.springframework.stereotype.Component;
31 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManager.SERVICE_NAME;
34 * Responsible for providing access to AAI APIs.
35 * Handles authentication and mandatory parameters.
38 public class AAIRestApiProvider {
39 private final MsbApiProvider msbApiProvider;
40 private final AaiSecurityProvider aaiSecurityProvider;
41 @Value("${aaiUsername}")
42 private String aaiUsername;
43 @Value("${aaiPassword}")
44 private String aaiPassword;
47 AAIRestApiProvider(MsbApiProvider msbApiProvider, AaiSecurityProvider aaiSecurityProvider) {
48 this.msbApiProvider = msbApiProvider;
49 this.aaiSecurityProvider = aaiSecurityProvider;
53 * @return API to access the cloud infrastructure
55 public CloudInfrastructureApi getCloudInfrastructureApi() {
56 return buildApiClient(AAIService.CLOUD).createService(CloudInfrastructureApi.class);
60 * @return API to access the external systems
62 public ExternalSystemApi getExternalSystemApi() {
63 return buildApiClient(AAIService.ESR).createService(ExternalSystemApi.class);
67 * @return API to access the networking
69 public NetworkApi getNetworkApi() {
70 return buildApiClient(AAIService.NETWORK).createService(NetworkApi.class);
75 ApiClient buildApiClient(AAIService service) {
76 ApiClient apiClient = new ApiClient();
77 apiClient.getOkBuilder().sslSocketFactory(aaiSecurityProvider.buildSSLSocketFactory(), aaiSecurityProvider.buildTrustManager());
78 apiClient.getOkBuilder().hostnameVerifier(aaiSecurityProvider.buildHostnameVerifier());
79 apiClient.getOkBuilder().addInterceptor(chain -> {
80 Request request = chain.request().newBuilder()
81 .addHeader("X-FromAppId", SERVICE_NAME)
82 //backward incompatibe API change in Beijing release
83 .addHeader("X-TransactionId", UUID.randomUUID().toString())
84 .addHeader("Accept", "application/json").
86 return chain.proceed(request);
88 HttpBasicAuth httpBasicAuth = new HttpBasicAuth();
89 httpBasicAuth.setCredentials(aaiUsername, aaiPassword);
90 apiClient.addAuthorization("basic", httpBasicAuth);
91 String url = msbApiProvider.getMicroServiceUrl(service.getServiceName(), "v11");
92 //the returned swagger schema is inconsistent with base URL
93 url = url.replaceAll("/external-system$", "");
94 url = url.replaceAll("/cloud-infrastructure$", "");
95 url = url.replaceAll("/network$", "");
96 if (!url.endsWith("/")) {
99 apiClient.getAdapterBuilder().baseUrl(url);
105 String getServiceName() {
106 return "aai-network";
110 String getServiceName() {
111 return "aai-externalSystem";
115 String getServiceName() {
116 return "aai-cloudInfrastructure";
120 abstract String getServiceName();