Modify the Service Name
[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 MSBRegisterUtil msbRegisterUtil;\r
68     @Inject\r
69     private RuleMgtWrapper ruleMgtWrapper;\r
70 \r
71     @PostConstruct\r
72     public void init() {\r
73         try {\r
74             msbRegisterUtil.register(initServiceEntity());\r
75         } catch (IOException e) {\r
76             log.warn("Micro service registry httpclient close failure",e);\r
77         }\r
78     }\r
79 \r
80     @PUT\r
81     @Produces(MediaType.APPLICATION_JSON)\r
82     @ApiOperation(value = "Save the alarm+ rule to the database, and deployed to the engine when the enable to open.", response = RuleAddAndUpdateResponse.class)\r
83     @Timed\r
84     public RuleAddAndUpdateResponse addCorrelationRule(@Context HttpServletRequest request,\r
85             @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
86         Locale locale = LanguageUtil.getLocale(request);\r
87         RuleAddAndUpdateResponse ruleChangeResponse;\r
88         try {\r
89             ruleChangeResponse = ruleMgtWrapper\r
90                     .addCorrelationRule(UserUtil.getUserName(request), ruleCreateRequest);\r
91             log.info("create rule:" + ruleCreateRequest.getRuleName() + " success.");\r
92             return ruleChangeResponse;\r
93         } catch (CorrelationException e) {\r
94             log.error("create rule:" + ruleCreateRequest.getRuleName() + " failed", e);\r
95             throw ExceptionUtil.buildExceptionResponse(I18nProxy.getInstance().getValue(locale,\r
96                     e.getMessage()));\r
97         }\r
98     }\r
99 \r
100     @POST\r
101     @Produces(MediaType.APPLICATION_JSON)\r
102     @ApiOperation(value = "Update the alarm+ rule and deployed to the engine when the enable to open.", response = RuleAddAndUpdateResponse.class)\r
103     @Timed\r
104     public RuleAddAndUpdateResponse updateCorrelationRule(@Context HttpServletRequest request,\r
105             @ApiParam(value = "alarm+ rule update request.<br>[ruleid]:<font color=\"red\">required</font>", required = true) RuleUpdateRequest ruleUpdateRequest) {\r
106         Locale locale = LanguageUtil.getLocale(request);\r
107         RuleAddAndUpdateResponse ruleChangeResponse;\r
108         try {\r
109             ruleChangeResponse = ruleMgtWrapper.updateCorrelationRule(UserUtil.getUserName(request), ruleUpdateRequest);\r
110             log.info("update rule:" + ruleUpdateRequest.getRuleId() + " successful");\r
111             return ruleChangeResponse;\r
112         } catch (CorrelationException e) {\r
113             log.error("update rule:" + ruleUpdateRequest.getContent() + " failed", e);\r
114             throw ExceptionUtil.buildExceptionResponse(I18nProxy.getInstance().getValue(locale,\r
115                     e.getMessage()));\r
116         }\r
117     }\r
118 \r
119     @DELETE\r
120     @Produces(MediaType.APPLICATION_JSON)\r
121     @ApiOperation(value = "Delete the alarm+ rule,and when the enable is open also removed from the engine.")\r
122     @Timed\r
123     public boolean deleteCorrelationRule(@Context HttpServletRequest request,\r
124             @ApiParam(value = "alarm+ rule delete request.<br>[ruleid]:<font color=\"red\">required</font>", required = true) RuleDeleteRequest ruleDeleteRequest) {\r
125         Locale locale = LanguageUtil.getLocale(request);\r
126         try {\r
127             ruleMgtWrapper.deleteCorrelationRule(ruleDeleteRequest);\r
128             log.info("delete rule:" + ruleDeleteRequest.getRuleId() + " successful");\r
129             return true;\r
130         } catch (CorrelationException e) {\r
131             log.error("delete rule:" + ruleDeleteRequest.getRuleId() + " failed", e);\r
132             throw ExceptionUtil.buildExceptionResponse(I18nProxy.getInstance().getValue(locale,\r
133                     e.getMessage()));\r
134         }\r
135     }\r
136 \r
137     @GET\r
138     @Produces(MediaType.APPLICATION_JSON)\r
139     @ApiOperation(value = "According to the conditions query the alarm + rules", response = RuleQueryListResponse.class)\r
140     @Timed\r
141     public RuleQueryListResponse getCorrelationRules(@Context HttpServletRequest request,\r
142             @ApiParam(value = "query condition:<br>" + " <b>[ruleid]</b>:Rule ID;<br>"\r
143                     + "<b>[rulename]</b>:Rule name;<br>" + "<b>[creator]</b>:creator of the rule;<br>"\r
144                     + "<b>[modifier]</b>:modifier of the rule;<br>"\r
145                     + "<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
146         Locale locale = LanguageUtil.getLocale(request);\r
147         RuleQueryListResponse ruleQueryListResponse;\r
148         RuleQueryCondition ruleQueryCondition = getRuleQueryCondition(ruleQueryRequest, request);\r
149         try {\r
150             ruleQueryListResponse = ruleMgtWrapper\r
151                     .getCorrelationRuleByCondition(ruleQueryCondition);\r
152             log.info("query rule successful by condition:" + JSONObject.fromObject(ruleQueryCondition));\r
153             return ruleQueryListResponse;\r
154         } catch (CorrelationException e) {\r
155             log.error("query rule failed,cause query condition conversion failure", e);\r
156             throw ExceptionUtil.buildExceptionResponse(I18nProxy.getInstance().getValue(locale,\r
157                     e.getMessage()));\r
158         }\r
159     }\r
160 \r
161     private RuleQueryCondition getRuleQueryCondition(String queryRequest,\r
162             HttpServletRequest request) {\r
163         Locale locale = LanguageUtil.getLocale(request);\r
164         try {\r
165             RuleQueryCondition ruleQueryCondition = JacksonUtil\r
166                     .jsonToBean(queryRequest, RuleQueryCondition.class);\r
167             if (queryRequest == null) {\r
168                 ruleQueryCondition.setEnabled(RuleMgtConstant.STATUS_RULE_ALL);\r
169             } else if (queryRequest.indexOf("enabled") == -1) {\r
170                 ruleQueryCondition.setEnabled(RuleMgtConstant.STATUS_RULE_ALL);\r
171             }\r
172             return ruleQueryCondition;\r
173         } catch (IOException e) {\r
174             log.warn("queryRequest convert to json failed", e);\r
175             throw ExceptionUtil.buildExceptionResponse(I18nProxy.getInstance().getValue(locale,\r
176                     I18nProxy.RULE_MANAGEMENT_DATA_FORMAT_ERROR));\r
177         }\r
178     }\r
179 \r
180     private ServiceRegisterEntity initServiceEntity() {\r
181         ServiceRegisterEntity serviceRegisterEntity = new ServiceRegisterEntity();\r
182         serviceRegisterEntity.setServiceName("holmes-rule-management");\r
183         serviceRegisterEntity.setProtocol("REST");\r
184         serviceRegisterEntity.setVersion("v1");\r
185         serviceRegisterEntity.setUrl("/api/holmes/v1");\r
186         serviceRegisterEntity.setSingleNode(MicroServiceConfig.getServiceIp(), "9101", 0);\r
187         serviceRegisterEntity.setVisualRange("1");\r
188         return serviceRegisterEntity;\r
189     }\r
190 }\r