Change the groupid from openo to onap
[holmes/rule-management.git] / rulemgt / src / main / java / org / onap / 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.onap.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.onap.holmes.common.api.entity.ServiceRegisterEntity;\r
40 import org.onap.holmes.common.config.MicroServiceConfig;\r
41 import org.onap.holmes.common.exception.CorrelationException;\r
42 import org.onap.holmes.common.utils.ExceptionUtil;\r
43 import org.onap.holmes.common.utils.JacksonUtil;\r
44 import org.onap.holmes.common.utils.LanguageUtil;\r
45 import org.onap.holmes.common.utils.MSBRegisterUtil;\r
46 import org.onap.holmes.common.utils.UserUtil;\r
47 import org.onap.holmes.rulemgt.bean.request.RuleCreateRequest;\r
48 import org.onap.holmes.rulemgt.bean.request.RuleDeleteRequest;\r
49 import org.onap.holmes.rulemgt.bean.request.RuleQueryCondition;\r
50 import org.onap.holmes.rulemgt.bean.request.RuleUpdateRequest;\r
51 import org.onap.holmes.rulemgt.bean.response.RuleAddAndUpdateResponse;\r
52 import org.onap.holmes.rulemgt.bean.response.RuleQueryListResponse;\r
53 import org.onap.holmes.rulemgt.constant.RuleMgtConstant;\r
54 import org.onap.holmes.rulemgt.wrapper.RuleMgtWrapper;\r
55 \r
56 @Service\r
57 @SwaggerDefinition\r
58 @Path("/rule")\r
59 @Api(tags = {"CorrelationRules"})\r
60 @Produces(MediaType.APPLICATION_JSON)\r
61 @Slf4j\r
62 public class RuleMgtResources {\r
63 \r
64     @Inject\r
65     private RuleMgtWrapper ruleMgtWrapper;\r
66 \r
67     @PUT\r
68     @Produces(MediaType.APPLICATION_JSON)\r
69     @ApiOperation(value = "Save the alarm+ rule to the database, and deployed to the engine when the enable to open.", response = RuleAddAndUpdateResponse.class)\r
70     @Timed\r
71     public RuleAddAndUpdateResponse addCorrelationRule(@Context HttpServletRequest request,\r
72             @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
73         Locale locale = LanguageUtil.getLocale(request);\r
74         RuleAddAndUpdateResponse ruleChangeResponse;\r
75         try {\r
76             ruleChangeResponse = ruleMgtWrapper\r
77                     .addCorrelationRule(UserUtil.getUserName(request), ruleCreateRequest);\r
78             log.info("create rule:" + ruleCreateRequest.getRuleName() + " success.");\r
79             return ruleChangeResponse;\r
80         } catch (CorrelationException e) {\r
81             log.error("create rule:" + ruleCreateRequest.getRuleName() + " failed", e);\r
82             throw ExceptionUtil.buildExceptionResponse(e.getMessage());\r
83         }\r
84     }\r
85 \r
86     @POST\r
87     @Produces(MediaType.APPLICATION_JSON)\r
88     @ApiOperation(value = "Update the alarm+ rule and deployed to the engine when the enable to open.", response = RuleAddAndUpdateResponse.class)\r
89     @Timed\r
90     public RuleAddAndUpdateResponse updateCorrelationRule(@Context HttpServletRequest request,\r
91             @ApiParam(value = "alarm+ rule update request.<br>[ruleid]:<font color=\"red\">required</font>", required = true) RuleUpdateRequest ruleUpdateRequest) {\r
92         Locale locale = LanguageUtil.getLocale(request);\r
93         RuleAddAndUpdateResponse ruleChangeResponse;\r
94         try {\r
95             ruleChangeResponse = ruleMgtWrapper.updateCorrelationRule(UserUtil.getUserName(request), ruleUpdateRequest);\r
96             log.info("update rule:" + ruleUpdateRequest.getRuleId() + " successful");\r
97             return ruleChangeResponse;\r
98         } catch (CorrelationException e) {\r
99             log.error("update rule:" + ruleUpdateRequest.getContent() + " failed", e);\r
100             throw ExceptionUtil.buildExceptionResponse(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(e.getMessage());\r
118         }\r
119     }\r
120 \r
121     @GET\r
122     @Produces(MediaType.APPLICATION_JSON)\r
123     @ApiOperation(value = "According to the conditions query the alarm + rules", response = RuleQueryListResponse.class)\r
124     @Timed\r
125     public RuleQueryListResponse getCorrelationRules(@Context HttpServletRequest request,\r
126             @ApiParam(value = "query condition:<br>" + " <b>[ruleid]</b>:Rule ID;<br>"\r
127                     + "<b>[rulename]</b>:Rule name;<br>" + "<b>[creator]</b>:creator of the rule;<br>"\r
128                     + "<b>[modifier]</b>:modifier of the rule;<br>"\r
129                     + "<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
130         Locale locale = LanguageUtil.getLocale(request);\r
131         RuleQueryListResponse ruleQueryListResponse;\r
132         RuleQueryCondition ruleQueryCondition = getRuleQueryCondition(ruleQueryRequest, request);\r
133         try {\r
134             ruleQueryListResponse = ruleMgtWrapper\r
135                     .getCorrelationRuleByCondition(ruleQueryCondition);\r
136             log.info("query rule successful by condition:" + JSONObject.fromObject(ruleQueryCondition));\r
137             return ruleQueryListResponse;\r
138         } catch (CorrelationException e) {\r
139             log.error("query rule failed,cause query condition conversion failure", e);\r
140             throw ExceptionUtil.buildExceptionResponse(e.getMessage());\r
141         }\r
142     }\r
143 \r
144     private RuleQueryCondition getRuleQueryCondition(String queryRequest,\r
145             HttpServletRequest request) {\r
146         Locale locale = LanguageUtil.getLocale(request);\r
147         try {\r
148             RuleQueryCondition ruleQueryCondition = JacksonUtil\r
149                     .jsonToBean(queryRequest, RuleQueryCondition.class);\r
150             if (queryRequest == null) {\r
151                 ruleQueryCondition.setEnabled(RuleMgtConstant.STATUS_RULE_ALL);\r
152             } else if (queryRequest.indexOf("enabled") == -1) {\r
153                 ruleQueryCondition.setEnabled(RuleMgtConstant.STATUS_RULE_ALL);\r
154             }\r
155             return ruleQueryCondition;\r
156         } catch (IOException e) {\r
157             log.warn("queryRequest convert to json failed", e);\r
158             throw ExceptionUtil.buildExceptionResponse("The request format is invalid!");\r
159         }\r
160     }\r
161 }\r