d3c01ae7be5e516636cac4a0f60137ad43af449a
[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.util.ArrayList;\r
24 import java.util.Date;\r
25 import java.util.List;\r
26 import java.util.Locale;\r
27 import javax.inject.Inject;\r
28 import javax.servlet.http.HttpServletRequest;\r
29 import javax.ws.rs.DELETE;\r
30 import javax.ws.rs.GET;\r
31 import javax.ws.rs.POST;\r
32 import javax.ws.rs.PUT;\r
33 import javax.ws.rs.Path;\r
34 import javax.ws.rs.PathParam;\r
35 import javax.ws.rs.Produces;\r
36 import javax.ws.rs.QueryParam;\r
37 import javax.ws.rs.core.Context;\r
38 import javax.ws.rs.core.MediaType;\r
39 import lombok.extern.slf4j.Slf4j;\r
40 import net.sf.json.JSONObject;\r
41 import org.jvnet.hk2.annotations.Service;\r
42 import org.onap.holmes.common.exception.CorrelationException;\r
43 import org.onap.holmes.common.utils.ExceptionUtil;\r
44 import org.onap.holmes.common.utils.GsonUtil;\r
45 import org.onap.holmes.common.utils.LanguageUtil;\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.bean.response.RuleResult4API;\r
54 import org.onap.holmes.rulemgt.constant.RuleMgtConstant;\r
55 import org.onap.holmes.rulemgt.wrapper.RuleMgtWrapper;\r
56 \r
57 @Service\r
58 @SwaggerDefinition\r
59 @Path("/rule")\r
60 @Api(tags = {"Holmes Rule Management"})\r
61 @Produces(MediaType.APPLICATION_JSON)\r
62 @Slf4j\r
63 public class RuleMgtResources {\r
64 \r
65     @Inject\r
66     private RuleMgtWrapper ruleMgtWrapper;\r
67 \r
68     @PUT\r
69     @Produces(MediaType.APPLICATION_JSON)\r
70     @ApiOperation(value = "Save a rule into the database; deploy it to the Drools engine if it is enabled.",\r
71             response = RuleAddAndUpdateResponse.class)\r
72     @Timed\r
73     public RuleAddAndUpdateResponse addCorrelationRule(@Context HttpServletRequest request,\r
74             @ApiParam(value = "The request entity of the HTTP call, which comprises \"ruleName\"(required), "\r
75                     + "\"loopControlName\"(required), \"content\"(required), \"enabled\"(required) "\r
76                     + "and \"description\"(optional)", required = true)\r
77                     RuleCreateRequest ruleCreateRequest) {\r
78         Locale locale = LanguageUtil.getLocale(request);\r
79         RuleAddAndUpdateResponse ruleChangeResponse;\r
80         try {\r
81             ruleChangeResponse = ruleMgtWrapper\r
82                     .addCorrelationRule(UserUtil.getUserName(request), ruleCreateRequest);\r
83             log.info("create rule:" + ruleCreateRequest.getRuleName() + " success.");\r
84             return ruleChangeResponse;\r
85         } catch (CorrelationException e) {\r
86             log.error("create rule:" + ruleCreateRequest.getRuleName() + " failed", e);\r
87             throw ExceptionUtil.buildExceptionResponse(e.getMessage());\r
88         }\r
89     }\r
90 \r
91     @POST\r
92     @Produces(MediaType.APPLICATION_JSON)\r
93     @ApiOperation(value = "Update an existing rule; deploy it to the Drools engine if it is enabled.", response = RuleAddAndUpdateResponse.class)\r
94     @Timed\r
95     public RuleAddAndUpdateResponse updateCorrelationRule(@Context HttpServletRequest request,\r
96             @ApiParam(value = "The request entity of the HTTP call, which comprises \"ruleId\"(required), "\r
97                     + "\"content\"(required), \"enabled\"(required) and \"description\"(optional)", required = true)\r
98                     RuleUpdateRequest ruleUpdateRequest) {\r
99         Locale locale = LanguageUtil.getLocale(request);\r
100         RuleAddAndUpdateResponse ruleChangeResponse;\r
101         try {\r
102             ruleChangeResponse = ruleMgtWrapper.updateCorrelationRule(UserUtil.getUserName(request), ruleUpdateRequest);\r
103             log.info("update rule:" + ruleUpdateRequest.getRuleId() + " successful");\r
104             return ruleChangeResponse;\r
105         } catch (CorrelationException e) {\r
106             log.error("update rule:" + ruleUpdateRequest.getContent() + " failed", e);\r
107             throw ExceptionUtil.buildExceptionResponse(e.getMessage());\r
108         }\r
109     }\r
110 \r
111     @DELETE\r
112     @Produces(MediaType.APPLICATION_JSON)\r
113     @ApiOperation(value = "Remove a rule from Holmes.")\r
114     @Timed\r
115     @Path("/{ruleid}")\r
116     public boolean deleteCorrelationRule(@Context HttpServletRequest request,\r
117             @PathParam("ruleid") String ruleId) {\r
118         Locale locale = LanguageUtil.getLocale(request);\r
119         try {\r
120             ruleMgtWrapper.deleteCorrelationRule(new RuleDeleteRequest(ruleId));\r
121             log.info("delete rule:" + ruleId + " successful");\r
122             return true;\r
123         } catch (CorrelationException e) {\r
124             log.error("delete rule:" + ruleId + " failed", e);\r
125             throw ExceptionUtil.buildExceptionResponse(e.getMessage());\r
126         }\r
127     }\r
128 \r
129     @GET\r
130     @Produces(MediaType.APPLICATION_JSON)\r
131     @ApiOperation(value = "Query rules using certain criteria.", response = RuleQueryListResponse.class)\r
132     @Timed\r
133     public RuleQueryListResponse getCorrelationRules(@Context HttpServletRequest request,\r
134             @ApiParam(value = "A JSON string used as a query parameter, which comprises \"ruleid\"(optional), "\r
135                     + "\"rulename\"(optional), \"creator\"(optional), "\r
136                     + "\"modifier\"(optional) and \"enabled\"(optional). E.g. {\"ruleid\":\"rule_1484727187317\"}",\r
137                     required = false) @QueryParam("queryrequest") String ruleQueryRequest) {\r
138         Locale locale = LanguageUtil.getLocale(request);\r
139         RuleQueryListResponse ruleQueryListResponse;\r
140 \r
141         RuleQueryCondition ruleQueryCondition = getRuleQueryCondition(ruleQueryRequest, request);\r
142         try {\r
143             ruleQueryListResponse = ruleMgtWrapper\r
144                     .getCorrelationRuleByCondition(ruleQueryCondition);\r
145             log.info("query rule successful by condition:" + JSONObject.fromObject(ruleQueryCondition));\r
146             return ruleQueryListResponse;\r
147         } catch (CorrelationException e) {\r
148             log.error("query rule failed,cause query condition conversion failure", e);\r
149             throw ExceptionUtil.buildExceptionResponse(e.getMessage());\r
150         }\r
151     }\r
152 \r
153     private RuleQueryCondition getRuleQueryCondition(String queryRequest,\r
154             HttpServletRequest request) {\r
155         Locale locale = LanguageUtil.getLocale(request);\r
156         RuleQueryCondition ruleQueryCondition = GsonUtil.jsonToBean(queryRequest, RuleQueryCondition.class);\r
157         if (queryRequest == null) {\r
158             if(ruleQueryCondition==null){\r
159                 ruleQueryCondition = new RuleQueryCondition();\r
160             }\r
161             ruleQueryCondition.setEnabled(RuleMgtConstant.STATUS_RULE_ALL);\r
162         } else if (queryRequest.indexOf("enabled") == -1) {\r
163             ruleQueryCondition.setEnabled(RuleMgtConstant.STATUS_RULE_ALL);\r
164         }\r
165         return ruleQueryCondition;\r
166     }\r
167 }\r