Distributing Blueprint to DCAE Dashboard Issue-ID: DCAEGEN2-2385>
[dcaegen2/platform.git] / mod2 / catalog-service / src / main / java / org / onap / dcaegen2 / platform / mod / util / PolicyModelUtils.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  org.onap.dcae
4  *  ================================================================================
5  *  Copyright (c) 2020-2021 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
10  *
11  *       http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.dcaegen2.platform.mod.util;
22
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;
36
37 import javax.annotation.PostConstruct;
38 import javax.net.ssl.SSLException;
39 import java.util.HashMap;
40 import java.util.Map;
41
42 /**
43  * Policy Model Service Utils to get URL, Username, Password, Webclient
44  */
45
46 @Component
47 public class PolicyModelUtils {
48
49     @Value("${policymodel.url.path}")
50     private String urlpath;
51
52     @Value("${policymodel.dev.server}")
53     private String devServer;
54
55     @Value("${policymodel.dev.port}")
56     private String devServerPort;
57
58     @Value("${policymodel.dev.user}")
59     private String devServerUser;
60
61     @Value("${policymodel.dev.password}")
62     private String devServerUserPassword;
63
64     @Value("${policymodel.pst.server}")
65     private String pstServer;
66
67     @Value("${policymodel.pst.port}")
68     private String pstServerPort;
69
70     @Value("${policymodel.pst.user}")
71     private String pstServerUser;
72
73     @Value("${policymodel.pst.password}")
74     private String pstServerUserPassword;
75
76     @Value("${policymodel.ete.server}")
77     private String eteServer;
78
79     @Value("${policymodel.ete.port}")
80     private String eteServerPort;
81
82     @Value("${policymodel.ete.user}")
83     private String eteServerUser;
84
85     @Value("${policymodel.ete.password}")
86     private String eteServerUserPassword;
87
88     @Value("${policymodel.prod.server}")
89     private String prodServer;
90
91     @Value("${policymodel.prod.port}")
92     private String prodServerPort;
93
94     @Value("${policymodel.prod.user}")
95     private String prodServerUser;
96
97     @Value("${policymodel.prod.password}")
98     private String prodServerUserPassword;
99
100     Map<String, EnvInfo> envMap;
101
102     /**
103      * Creates a Policy Model Distribution Engine URL for the Environment requested
104      */
105     @PostConstruct
106     public void init() {
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());
112     }
113
114     /**
115      * Generates a Policy Model Distribution Engine URL for the Environment
116      *
117      * @param env
118      * @return
119      */
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();
123     }
124
125
126
127     /**
128      * Generates a Policy Model Distribution Engine UserName for the Environment
129      *
130      * @param env
131      * @return
132      */
133
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();
137     }
138
139
140     /**
141      * Generates a Policy Model Distribution Engine Password for the Environment
142      *
143      * @param env
144      * @return
145      */
146
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();
150     }
151
152     /**
153      * Generates a Policy Model Distribution Engine Webclient for the Environment
154      *
155      * @param env
156      * @return
157      */
158     public WebClient getWebClient(String env) throws SSLException {
159         String userName = getUserName(env);
160         String password = getPassword(env);
161
162         SslContext sslContext = SslContextBuilder.forClient()
163                 .trustManager(InsecureTrustManagerFactory.INSTANCE)
164                 .build();
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))
169                 .build();
170     }
171
172
173
174 }