9ef29a537000aa5826e6bda45937b89eea6d234b
[sdnc/apps.git] /
1 /*
2  * ============LICENSE_START===================================================
3  * Copyright (c) 2018 Amdocs
4  * ============================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END=====================================================
17  */
18 package org.onap.sdnc.apps.pomba.servicedecomposition;
19
20 import java.util.Base64;
21 import org.eclipse.jetty.util.security.Password;
22 import org.onap.aai.restclient.client.RestClient;
23 import org.springframework.beans.factory.annotation.Value;
24 import org.springframework.context.annotation.Bean;
25 import org.springframework.context.annotation.Conditional;
26 import org.springframework.stereotype.Component;
27
28 @Component
29 public class AAIConfiguration {
30     @Value("${aai.serviceName}")
31     private String host;
32
33     @Value("${aai.servicePort}")
34     private String port;
35
36     @Value("${aai.username}")
37     private String aaiUsername;
38
39     @Value("${aai.password}")
40     private String aaiPassword;
41
42     @Value("${aai.httpProtocol}")
43     private String httpProtocol;
44
45     @Value("${aai.securityProtocol}")
46     private String securityProtocol;
47
48     @Value("${aai.authentication}")
49     private String authenticationMode;
50
51     @Value("${aai.trustStorePath}")
52     private String trustStorePath;
53
54     @Value("${aai.keyStorePath}")
55     private String keyStorePath;
56
57     @Value("${aai.keyStorePassword}")
58     private String keyStorePassword;
59
60     @Value("${aai.connectionTimeout}")
61     private Integer connectionTimeout;
62
63     @Value("${aai.readTimeout}")
64     private Integer readTimeout;
65
66     @Value("${aai.serviceInstancePath}")
67     private String serviceInstancePath;
68
69     @Value("${aai.resourceList}")
70     private String resourceList;
71
72     @Value("${basicAuth.username:admin}")
73     private String username;
74
75     @Value("${basicAuth.password:OBF:1u2a1toa1w8v1tok1u30}")
76     private String password;
77
78     @Bean(name="aaiBasicAuthorization")
79     public String getAAIBasicAuth() {
80         return "Basic " + Base64.getEncoder().encodeToString((this.aaiUsername + ":" + Password.deobfuscate(this.aaiPassword)).getBytes());
81     }
82
83     @Bean(name="basicAuthHeader")
84     public String getSdBasicAuthHeader() {
85         return "Basic " + Base64.getEncoder().encodeToString((this.username + ":" + Password.deobfuscate(this.password)).getBytes());
86     }
87
88     @Conditional(AAIBasicAuthCondition.class)
89     @Bean(name="aaiClient")
90     public RestClient restClientWithBasicAuth() {
91         return new RestClient()
92                 .validateServerHostname(false)
93                 .validateServerCertChain(false)
94                 .basicAuthPassword(aaiUsername)
95                 .basicAuthPassword(Password.deobfuscate(aaiPassword))
96                 .connectTimeoutMs(this.connectionTimeout)
97                 .readTimeoutMs(this.readTimeout);
98     }
99
100     @Conditional(AAIClientCertCondition.class)
101     @Bean(name="aaiClient")
102     public RestClient restClientWithClientCert() {
103         RestClient restClient = new RestClient();
104         if ("https".equals(httpProtocol))
105             restClient.validateServerHostname(false).validateServerCertChain(false).trustStore(trustStorePath).clientCertFile(keyStorePath).clientCertPassword(keyStorePassword).connectTimeoutMs(connectionTimeout).readTimeoutMs(readTimeout);
106         else
107             restClient.validateServerHostname(false).validateServerCertChain(false).connectTimeoutMs(connectionTimeout).readTimeoutMs(readTimeout);
108         return restClient;
109     }
110
111     @Bean(name="aaiBaseUrl")
112     public String getURL() {
113         return this.httpProtocol + "://" + this.host + ":" + this.port;
114     }
115
116     @Bean(name="aaiServiceInstancePath")
117     public String getServiceInstancePath() {
118         return this.serviceInstancePath;
119     }
120
121     @Bean(name="aaiResourceList")
122     public String getResourceList() {
123         return this.resourceList;
124     }
125 }