Fixed MSB Invocation Issues
[holmes/rule-management.git] / rulemgt / src / main / java / org / onap / holmes / rulemgt / bolt / enginebolt / EngineService.java
1 /**\r
2  * Copyright 2017-2020 ZTE Corporation.\r
3  * <p>\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  * <p>\r
8  * http://www.apache.org/licenses/LICENSE-2.0\r
9  * <p>\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 package org.onap.holmes.rulemgt.bolt.enginebolt;\r
17 \r
18 import lombok.extern.slf4j.Slf4j;\r
19 import org.apache.http.HttpResponse;\r
20 import org.apache.http.client.methods.HttpDelete;\r
21 import org.apache.http.client.methods.HttpPost;\r
22 import org.apache.http.client.methods.HttpPut;\r
23 import org.apache.http.entity.StringEntity;\r
24 import org.apache.http.impl.client.CloseableHttpClient;\r
25 import org.jvnet.hk2.annotations.Service;\r
26 import org.onap.holmes.common.utils.GsonUtil;\r
27 import org.onap.holmes.common.utils.HttpsUtils;\r
28 import org.onap.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine;\r
29 import org.onap.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine;\r
30 import org.onap.holmes.rulemgt.constant.RuleMgtConstant;\r
31 \r
32 import javax.ws.rs.core.MediaType;\r
33 import java.io.IOException;\r
34 import java.util.HashMap;\r
35 \r
36 @Slf4j\r
37 @Service\r
38 public class EngineService {\r
39 \r
40     private static final String HTTPS = "https://";\r
41     private static final String HTTP = "http://";\r
42     private static final String PORT = ":9102";\r
43 \r
44     protected HttpResponse delete(String packageName, String ip) throws Exception {\r
45         HashMap headers = createHeaders();\r
46         String url = getRequestPref() + ip + PORT + RuleMgtConstant.ENGINE_PATH + "/" + packageName;\r
47         CloseableHttpClient httpClient = null;\r
48         HttpDelete httpDelete = new HttpDelete(url);\r
49         try {\r
50             httpClient = HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT);\r
51             return HttpsUtils.delete(httpDelete, headers, httpClient);\r
52         } finally {\r
53             httpDelete.releaseConnection();\r
54             closeHttpClient(httpClient);\r
55         }\r
56     }\r
57 \r
58     protected HttpResponse check(CorrelationCheckRule4Engine correlationCheckRule4Engine, String ip)\r
59             throws Exception {\r
60         String content = GsonUtil.beanToJson(correlationCheckRule4Engine);\r
61         HashMap headers = createHeaders();\r
62         String url = getRequestPref() + ip + PORT + RuleMgtConstant.ENGINE_PATH;\r
63         CloseableHttpClient httpClient = null;\r
64         HttpPost httpPost = new HttpPost(url);\r
65         try {\r
66             httpClient = HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT);\r
67             return HttpsUtils.post(httpPost, headers, new HashMap<>(), new StringEntity(content), httpClient);\r
68         } finally {\r
69             httpPost.releaseConnection();\r
70             closeHttpClient(httpClient);\r
71         }\r
72     }\r
73 \r
74     protected HttpResponse deploy(CorrelationDeployRule4Engine correlationDeployRule4Engine, String ip) throws Exception {\r
75         String content = GsonUtil.beanToJson(correlationDeployRule4Engine);\r
76         HashMap headers = createHeaders();\r
77         String url = getRequestPref() + ip + PORT + RuleMgtConstant.ENGINE_PATH;\r
78         CloseableHttpClient httpClient = null;\r
79         HttpPut httpPut = new HttpPut(url);\r
80         try {\r
81             httpClient = HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT);\r
82             return HttpsUtils.put(httpPut, headers, new HashMap<>(), new StringEntity(content), httpClient);\r
83         } finally {\r
84             closeHttpClient(httpClient);\r
85         }\r
86     }\r
87 \r
88     private void closeHttpClient(CloseableHttpClient httpClient) {\r
89         if (httpClient != null) {\r
90             try {\r
91                 httpClient.close();\r
92             } catch (IOException e) {\r
93                 log.warn("Failed to close http client!");\r
94             }\r
95         }\r
96     }\r
97 \r
98     private HashMap<String, String> createHeaders() {\r
99         HashMap<String, String> headers = new HashMap<>();\r
100         headers.put("Content-Type", MediaType.APPLICATION_JSON);\r
101         headers.put("Accept", MediaType.APPLICATION_JSON);\r
102         return headers;\r
103     }\r
104 \r
105     private String getRequestPref() {\r
106         return HttpsUtils.isHttpsEnabled() ? HTTPS : HTTP;\r
107     }\r
108 }\r