682faf0de2726fd18bcf9aa1e145889610929d3a
[holmes/rule-management.git] / rulemgt / src / main / java / org / openo / holmes / rulemgt / resources / RuleMgtResources.java
1 /**\r
2  * Copyright 2017 ZTE Corporation.\r
3  *\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  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\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.openo.holmes.rulemgt.resources;\r
17 \r
18 import com.codahale.metrics.annotation.Timed;\r
19 import io.swagger.annotations.Api;\r
20 import io.swagger.annotations.ApiOperation;\r
21 import io.swagger.annotations.ApiParam;\r
22 import io.swagger.annotations.SwaggerDefinition;\r
23 import java.io.IOException;\r
24 import java.util.Locale;\r
25 import javax.inject.Inject;\r
26 import javax.servlet.http.HttpServletRequest;\r
27 import javax.ws.rs.DELETE;\r
28 import javax.ws.rs.GET;\r
29 import javax.ws.rs.POST;\r
30 import javax.ws.rs.PUT;\r
31 import javax.ws.rs.Path;\r
32 import javax.ws.rs.Produces;\r
33 import javax.ws.rs.QueryParam;\r
34 import javax.ws.rs.core.Context;\r
35 import javax.ws.rs.core.MediaType;\r
36 import lombok.extern.slf4j.Slf4j;\r
37 import net.sf.json.JSONObject;\r
38 import org.jvnet.hk2.annotations.Service;\r
39 import org.openo.holmes.common.exception.CorrelationException;\r
40 import org.openo.holmes.common.utils.ExceptionUtil;\r
41 import org.openo.holmes.common.utils.I18nProxy;\r
42 import org.openo.holmes.common.utils.JacksonUtil;\r
43 import org.openo.holmes.common.utils.LanguageUtil;\r
44 import org.openo.holmes.common.utils.UserUtil;\r
45 import org.openo.holmes.rulemgt.bean.request.RuleCreateRequest;\r
46 import org.openo.holmes.rulemgt.bean.request.RuleDeleteRequest;\r
47 import org.openo.holmes.rulemgt.bean.request.RuleQueryCondition;\r
48 import org.openo.holmes.rulemgt.bean.request.RuleUpdateRequest;\r
49 import org.openo.holmes.rulemgt.bean.response.RuleAddAndUpdateResponse;\r
50 import org.openo.holmes.rulemgt.bean.response.RuleQueryListResponse;\r
51 import org.openo.holmes.rulemgt.constant.RuleMgtConstant;\r
52 import org.openo.holmes.rulemgt.wrapper.RuleMgtWrapper;\r
53 \r
54 @Service\r
55 @SwaggerDefinition\r
56 @Path("/rule")\r
57 @Api(tags = {"CorrelationRules"})\r
58 @Produces(MediaType.APPLICATION_JSON)\r
59 @Slf4j\r
60 public class RuleMgtResources {\r
61 \r
62     @Inject\r
63     private RuleMgtWrapper ruleMgtWrapper;\r
64 \r
65     @PUT\r
66     @Produces(MediaType.APPLICATION_JSON)\r
67     @ApiOperation(value = "Save the alarm+ rule to the database, and deployed to the engine when the enable to open.", response = RuleAddAndUpdateResponse.class)\r
68     @Timed\r
69     public RuleAddAndUpdateResponse addCorrelationRule(@Context HttpServletRequest request,\r
70             @ApiParam(value = "alarm+ rule create request.<br>[rulename]:<font color=\"red\">required</font><br>[content]:<font color=\"red\">required</font><br>[enabled]:<font color=\"red\">required</font>", required = true) RuleCreateRequest ruleCreateRequest) {\r
71         Locale locale = LanguageUtil.getLocale(request);\r
72         RuleAddAndUpdateResponse ruleChangeResponse;\r
73         try {\r
74             ruleChangeResponse = ruleMgtWrapper\r
75                     .addCorrelationRule(UserUtil.getUserName(request), ruleCreateRequest);\r
76             log.info("create rule:" + ruleCreateRequest.getRuleName() + " success.");\r
77             return ruleChangeResponse;\r
78         } catch (CorrelationException e) {\r
79             log.error("create rule:" + ruleCreateRequest.getRuleName() + " failed", e);\r
80             throw ExceptionUtil.buildExceptionResponse(I18nProxy.getInstance().getValue(locale,\r
81                     e.getMessage()));\r
82         }\r
83     }\r
84 \r
85     @POST\r
86     @Produces(MediaType.APPLICATION_JSON)\r
87     @ApiOperation(value = "Update the alarm+ rule and deployed to the engine when the enable to open.", response = RuleAddAndUpdateResponse.class)\r
88     @Timed\r
89     public RuleAddAndUpdateResponse updateCorrelationRule(@Context HttpServletRequest request,\r
90             @ApiParam(value = "alarm+ rule update request.<br>[ruleid]:<font color=\"red\">required</font>", required = true) RuleUpdateRequest ruleUpdateRequest) {\r
91         Locale locale = LanguageUtil.getLocale(request);\r
92         RuleAddAndUpdateResponse ruleChangeResponse;\r
93         try {\r
94             ruleChangeResponse = ruleMgtWrapper.updateCorrelationRule(UserUtil.getUserName(request), ruleUpdateRequest);\r
95             log.info("update rule:" + ruleUpdateRequest.getRuleId() + " successful");\r
96             return ruleChangeResponse;\r
97         } catch (CorrelationException e) {\r
98             log.error("update rule:" + ruleUpdateRequest.getContent() + " failed", e);\r
99             throw ExceptionUtil.buildExceptionResponse(I18nProxy.getInstance().getValue(locale,\r
100                     e.getMessage()));\r
101         }\r
102     }\r
103 \r
104     @DELETE\r
105     @Produces(MediaType.APPLICATION_JSON)\r
106     @ApiOperation(value = "Delete the alarm+ rule,and when the enable is open also removed from the engine.")\r
107     @Timed\r
108     public boolean deleteCorrelationRule(@Context HttpServletRequest request,\r
109             @ApiParam(value = "alarm+ rule delete request.<br>[ruleid]:<font color=\"red\">required</font>", required = true) RuleDeleteRequest ruleDeleteRequest) {\r
110         Locale locale = LanguageUtil.getLocale(request);\r
111         try {\r
112             ruleMgtWrapper.deleteCorrelationRule(ruleDeleteRequest);\r
113             log.info("delete rule:" + ruleDeleteRequest.getRuleId() + " successful");\r
114             return true;\r
115         } catch (CorrelationException e) {\r
116             log.error("delete rule:" + ruleDeleteRequest.getRuleId() + " failed", e);\r
117             throw ExceptionUtil.buildExceptionResponse(I18nProxy.getInstance().getValue(locale,\r
118                     e.getMessage()));\r
119         }\r
120     }\r
121 \r
122     @GET\r
123     @Produces(MediaType.APPLICATION_JSON)\r
124     @ApiOperation(value = "According to the conditions query the alarm + rules", response = RuleQueryListResponse.class)\r
125     @Timed\r
126     public RuleQueryListResponse getCorrelationRules(@Context HttpServletRequest request,\r
127             @ApiParam(value = "query condition:<br>" + " <b>[ruleid]</b>:Rule ID;<br>"\r
128                     + "<b>[rulename]</b>:Rule name;<br>" + "<b>[creator]</b>:creator of the rule;<br>"\r
129                     + "<b>[modifier]</b>:modifier of the rule;<br>"\r
130                     + "<b>[enabled]</b>: 0 is Enabled,1 is disabled;<br><font color=\"red\">for example:</font><br>{\"ruleid\":\"rule_1484727187317\"}", required = false) @QueryParam("queryrequest") String ruleQueryRequest) {\r
131         Locale locale = LanguageUtil.getLocale(request);\r
132         RuleQueryListResponse ruleQueryListResponse;\r
133         RuleQueryCondition ruleQueryCondition = getRuleQueryCondition(ruleQueryRequest, request);\r
134         try {\r
135             ruleQueryListResponse = ruleMgtWrapper\r
136                     .getCorrelationRuleByCondition(ruleQueryCondition);\r
137             log.info("query rule successful by condition:" + JSONObject.fromObject(ruleQueryCondition));\r
138             return ruleQueryListResponse;\r
139         } catch (CorrelationException e) {\r
140             log.error("query rule failed,cause query condition conversion failure", e);\r
141             throw ExceptionUtil.buildExceptionResponse(I18nProxy.getInstance().getValue(locale,\r
142                     e.getMessage()));\r
143         }\r
144     }\r
145 \r
146     private RuleQueryCondition getRuleQueryCondition(String queryRequest,\r
147             HttpServletRequest request) {\r
148         Locale locale = LanguageUtil.getLocale(request);\r
149         try {\r
150             RuleQueryCondition ruleQueryCondition = JacksonUtil\r
151                     .jsonToBean(queryRequest, RuleQueryCondition.class);\r
152             if (queryRequest == null) {\r
153                 ruleQueryCondition.setEnabled(RuleMgtConstant.STATUS_RULE_ALL);\r
154             } else if (queryRequest.indexOf("enabled") == -1) {\r
155                 ruleQueryCondition.setEnabled(RuleMgtConstant.STATUS_RULE_ALL);\r
156             }\r
157             return ruleQueryCondition;\r
158         } catch (IOException e) {\r
159             log.warn("queryRequest convert to json failed", e);\r
160             throw ExceptionUtil.buildExceptionResponse(I18nProxy.getInstance().getValue(locale,\r
161                     I18nProxy.RULE_MANAGEMENT_DATA_FORMAT_ERROR));\r
162         }\r
163     }\r
164 }\r