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