08bf2c4104ca08091e730a16ee5c276af9e1241e
[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 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;
29
30 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManager.SERVICE_NAME;
31
32 /**
33  * Responsible for providing access to AAI APIs.
34  * Handles authentication and mandatory parameters.
35  */
36 @Component
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;
44
45     @Autowired
46     AAIRestApiProvider(MsbApiProvider msbApiProvider, AaiSecurityProvider aaiSecurityProvider) {
47         this.msbApiProvider = msbApiProvider;
48         this.aaiSecurityProvider = aaiSecurityProvider;
49     }
50
51     /**
52      * @return API to access the cloud infrastructure
53      */
54     public CloudInfrastructureApi getCloudInfrastructureApi() {
55         return buildApiClient(AAIService.CLOUD).createService(CloudInfrastructureApi.class);
56     }
57
58     /**
59      * @return API to access the external systems
60      */
61     public ExternalSystemApi getExternalSystemApi() {
62         return buildApiClient(AAIService.ESR).createService(ExternalSystemApi.class);
63     }
64
65     /**
66      * @return API to access the networking
67      */
68     public NetworkApi getNetworkApi() {
69         return buildApiClient(AAIService.NETWORK).createService(NetworkApi.class);
70
71     }
72
73     @VisibleForTesting
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);
81         });
82         apiClient.getOkBuilder().authenticator((route, response) -> {
83             String credential = Credentials.basic(aaiUsername, aaiPassword);
84             return response.request().newBuilder().header("Authorization", credential).build();
85         });
86         String url = msbApiProvider.getMicroServiceUrl(service.getServiceName(), "v11");
87         if (!url.endsWith("/")) {
88             url = url + "/";
89         }
90         apiClient.getAdapterBuilder().baseUrl(url);
91         return apiClient;
92     }
93
94     enum AAIService {
95         NETWORK {
96             String getServiceName() {
97                 return "aai-network";
98             }
99         },
100         ESR {
101             String getServiceName() {
102                 return "aai-externalSystem";
103             }
104         },
105         CLOUD {
106             String getServiceName() {
107                 return "aai-cloudInfrastructure";
108             }
109         };
110
111         abstract String getServiceName();
112     }
113 }