ed437841f31d15947d84bef6062dd3263156f4cc
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / onap / direct / AAIRestApiProvider.java
1 /*
2  * Copyright 2016-2017, Nokia Corporation
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct;
17
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;
30
31 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManager.SERVICE_NAME;
32
33 /**
34  * Responsible for providing access to AAI APIs.
35  * Handles authentication and mandatory parameters.
36  */
37 @Component
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;
45
46     @Autowired
47     AAIRestApiProvider(MsbApiProvider msbApiProvider, AaiSecurityProvider aaiSecurityProvider) {
48         this.msbApiProvider = msbApiProvider;
49         this.aaiSecurityProvider = aaiSecurityProvider;
50     }
51
52     /**
53      * @return API to access the cloud infrastructure
54      */
55     public CloudInfrastructureApi getCloudInfrastructureApi() {
56         return buildApiClient(AAIService.CLOUD).createService(CloudInfrastructureApi.class);
57     }
58
59     /**
60      * @return API to access the external systems
61      */
62     public ExternalSystemApi getExternalSystemApi() {
63         return buildApiClient(AAIService.ESR).createService(ExternalSystemApi.class);
64     }
65
66     /**
67      * @return API to access the networking
68      */
69     public NetworkApi getNetworkApi() {
70         return buildApiClient(AAIService.NETWORK).createService(NetworkApi.class);
71
72     }
73
74     @VisibleForTesting
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").
85                             build();
86             return chain.proceed(request);
87         });
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("/")) {
97             url = url + "/";
98         }
99         apiClient.getAdapterBuilder().baseUrl(url);
100         return apiClient;
101     }
102
103     enum AAIService {
104         NETWORK {
105             String getServiceName() {
106                 return "aai-network";
107             }
108         },
109         ESR {
110             String getServiceName() {
111                 return "aai-externalSystem";
112             }
113         },
114         CLOUD {
115             String getServiceName() {
116                 return "aai-cloudInfrastructure";
117             }
118         };
119
120         abstract String getServiceName();
121     }
122 }