2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (c) 2020 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.dcaegen2.platform.mod.util;
23 import io.netty.handler.ssl.SslContext;
24 import io.netty.handler.ssl.SslContextBuilder;
25 import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
26 import org.onap.dcaegen2.platform.mod.model.exceptions.policymodel.PolicyModelDistributionEnvNotFoundException;
27 import org.onap.dcaegen2.platform.mod.model.policymodel.EnvInfo;
28 import org.onap.dcaegen2.platform.mod.model.policymodel.PolicyModelDistributionEnv;
29 import org.springframework.beans.factory.annotation.Value;
30 import org.springframework.http.HttpHeaders;
31 import org.springframework.http.client.reactive.ReactorClientHttpConnector;
32 import org.springframework.stereotype.Component;
33 import org.springframework.web.reactive.function.client.ExchangeFilterFunctions;
34 import org.springframework.web.reactive.function.client.WebClient;
35 import reactor.netty.http.client.HttpClient;
37 import javax.annotation.PostConstruct;
38 import javax.net.ssl.SSLException;
39 import java.util.HashMap;
43 * Policy Model Service Utils to get URL, Username, Password, Webclient
47 public class PolicyModelUtils {
50 private String urlpath;
52 @Value("${dev.server}")
53 private String devServer;
56 private String devServerPort;
59 private String devServerUser;
61 @Value("${dev.password}")
62 private String devServerUserPassword;
64 @Value("${pst.server}")
65 private String pstServer;
68 private String pstServerPort;
71 private String pstServerUser;
73 @Value("${pst.password}")
74 private String pstServerUserPassword;
76 @Value("${ete.server}")
77 private String eteServer;
80 private String eteServerPort;
83 private String eteServerUser;
85 @Value("${ete.password}")
86 private String eteServerUserPassword;
88 @Value("${prod.server}")
89 private String prodServer;
91 @Value("${prod.port}")
92 private String prodServerPort;
94 @Value("${prod.user}")
95 private String prodServerUser;
97 @Value("${prod.password}")
98 private String prodServerUserPassword;
100 Map<String, EnvInfo> envMap;
103 * Creates a Policy Model Distribution Engine URL for the Environment requested
107 envMap = new HashMap<>();
108 envMap.put(PolicyModelDistributionEnv.DEV.name(), EnvInfo.builder().url("https://"+ devServer + ":" + devServerPort + urlpath).username(devServerUser).password(devServerUserPassword).build());
109 envMap.put(PolicyModelDistributionEnv.PST.name(), EnvInfo.builder().url("https://"+ pstServer + ":" + pstServerPort + urlpath).username(pstServerUser).password(pstServerUserPassword).build());
110 envMap.put(PolicyModelDistributionEnv.ETE.name(), EnvInfo.builder().url("https://"+ eteServer + ":" + eteServerPort + urlpath).username(eteServerUser).password(eteServerUserPassword).build());
111 envMap.put(PolicyModelDistributionEnv.PROD.name(), EnvInfo.builder().url("https://"+ prodServer + ":" + prodServerPort + urlpath).username(prodServerUser).password(prodServerUserPassword).build());
115 * Generates a Policy Model Distribution Engine URL for the Environment
120 public String getPolicyEngineURL(String env) {
121 if(!envMap.containsKey(env)) throw new PolicyModelDistributionEnvNotFoundException(String.format("Policy Model Environment with env %s invalid", env));
122 return envMap.get(env).getUrl();
128 * Generates a Policy Model Distribution Engine UserName for the Environment
134 public String getUserName(String env) {
135 if(!envMap.containsKey(env)) throw new PolicyModelDistributionEnvNotFoundException(String.format("Policy Model Environment with env %s invalid", env));
136 return envMap.get(env).getUsername();
141 * Generates a Policy Model Distribution Engine Password for the Environment
147 public String getPassword(String env) {
148 if(!envMap.containsKey(env)) throw new PolicyModelDistributionEnvNotFoundException(String.format("Policy Model Environment with env %s invalid", env));
149 return envMap.get(env).getPassword();
153 * Generates a Policy Model Distribution Engine Webclient for the Environment
158 public WebClient getWebClient(String env) throws SSLException {
159 String userName = getUserName(env);
160 String password = getPassword(env);
162 SslContext sslContext = SslContextBuilder.forClient()
163 .trustManager(InsecureTrustManagerFactory.INSTANCE)
165 HttpClient httpClient = HttpClient.create().secure(t -> t.sslContext(sslContext));
166 return WebClient.builder().clientConnector(new ReactorClientHttpConnector(httpClient))
167 .defaultHeader(HttpHeaders.CONTENT_TYPE, "application/yaml")
168 .filter(ExchangeFilterFunctions.basicAuthentication(userName, password))