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