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.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.spring.Conditions;
 
  27 import org.springframework.beans.factory.annotation.Autowired;
 
  28 import org.springframework.beans.factory.annotation.Value;
 
  29 import org.springframework.context.annotation.Conditional;
 
  30 import org.springframework.stereotype.Component;
 
  32 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManager.SERVICE_NAME;
 
  35  * Responsible for providing access to AAI APIs.
 
  36  * Handles authentication and mandatory parameters.
 
  39 @Conditional(value = Conditions.UseForDirect.class)
 
  40 public class AAIRestApiProvider {
 
  41     private final MsbApiProvider msbApiProvider;
 
  42     private final AaiSecurityProvider aaiSecurityProvider;
 
  43     @Value("${aaiUsername}")
 
  44     private String aaiUsername;
 
  45     @Value("${aaiPassword}")
 
  46     private String aaiPassword;
 
  49     AAIRestApiProvider(MsbApiProvider msbApiProvider, AaiSecurityProvider aaiSecurityProvider) {
 
  50         this.msbApiProvider = msbApiProvider;
 
  51         this.aaiSecurityProvider = aaiSecurityProvider;
 
  55      * @return API to access the cloud infrastructure
 
  57     public CloudInfrastructureApi getCloudInfrastructureApi() {
 
  58         return buildApiClient(AAIService.CLOUD).createService(CloudInfrastructureApi.class);
 
  62      * @return API to access the external systems
 
  64     public ExternalSystemApi getExternalSystemApi() {
 
  65         return buildApiClient(AAIService.ESR).createService(ExternalSystemApi.class);
 
  69      * @return API to access the networking
 
  71     public NetworkApi getNetworkApi() {
 
  72         return buildApiClient(AAIService.NETWORK).createService(NetworkApi.class);
 
  77     ApiClient buildApiClient(AAIService service) {
 
  78         ApiClient apiClient = new ApiClient();
 
  79         apiClient.getOkBuilder().sslSocketFactory(aaiSecurityProvider.buildSSLSocketFactory(), aaiSecurityProvider.buildTrustManager());
 
  80         apiClient.getOkBuilder().hostnameVerifier(aaiSecurityProvider.buildHostnameVerifier());
 
  81         apiClient.getOkBuilder().addInterceptor(chain -> {
 
  82             Request request = chain.request().newBuilder().addHeader("X-FromAppId", SERVICE_NAME).build();
 
  83             return chain.proceed(request);
 
  85         apiClient.getOkBuilder().authenticator((route, response) -> {
 
  86             String credential = Credentials.basic(aaiUsername, aaiPassword);
 
  87             return response.request().newBuilder().header("Authorization", credential).build();
 
  89         String url = msbApiProvider.getMicroServiceUrl(service.getServiceName(), "v11");
 
  90         if (!url.endsWith("/")) {
 
  93         apiClient.getAdapterBuilder().baseUrl(url);
 
  99             String getServiceName() {
 
 100                 return "aai-network";
 
 104             String getServiceName() {
 
 105                 return "aai-externalSystem";
 
 109             String getServiceName() {
 
 110                 return "aai-cloudInfrastructure";
 
 114         abstract String getServiceName();