2c9a4620f27d09ace9caacc66cb187239adec8d7
[holmes/rule-management.git] / rulemgt / src / main / java / org / onap / holmes / rulemgt / resources / RuleMgtResources.java
1 /**\r
2  * Copyright 2017-2022 ZTE Corporation.\r
3  * <p>\r
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except\r
5  * in compliance with the License. You may obtain a copy of the License at\r
6  * <p>\r
7  * http://www.apache.org/licenses/LICENSE-2.0\r
8  * <p>\r
9  * Unless required by applicable law or agreed to in writing, software distributed under the License\r
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express\r
11  * or implied. See the License for the specific language governing permissions and limitations under\r
12  * the License.\r
13  */\r
14 package org.onap.holmes.rulemgt.resources;\r
15 \r
16 import io.swagger.annotations.Api;\r
17 import io.swagger.annotations.ApiOperation;\r
18 import io.swagger.annotations.ApiParam;\r
19 import io.swagger.annotations.SwaggerDefinition;\r
20 import jakarta.ws.rs.core.MediaType;\r
21 import lombok.extern.slf4j.Slf4j;\r
22 import org.onap.holmes.common.exception.CorrelationException;\r
23 import org.onap.holmes.common.utils.ExceptionUtil;\r
24 import org.onap.holmes.common.utils.GsonUtil;\r
25 import org.onap.holmes.common.utils.UserUtil;\r
26 import org.onap.holmes.rulemgt.bean.request.RuleCreateRequest;\r
27 import org.onap.holmes.rulemgt.bean.request.RuleDeleteRequest;\r
28 import org.onap.holmes.rulemgt.bean.request.RuleQueryCondition;\r
29 import org.onap.holmes.rulemgt.bean.request.RuleUpdateRequest;\r
30 import org.onap.holmes.rulemgt.bean.response.RuleAddAndUpdateResponse;\r
31 import org.onap.holmes.rulemgt.bean.response.RuleQueryListResponse;\r
32 import org.onap.holmes.rulemgt.constant.RuleMgtConstant;\r
33 import org.onap.holmes.rulemgt.wrapper.RuleMgtWrapper;\r
34 import org.springframework.beans.factory.annotation.Autowired;\r
35 import org.springframework.http.HttpStatus;\r
36 import org.springframework.http.ResponseEntity;\r
37 import org.springframework.web.bind.annotation.*;\r
38 \r
39 import javax.servlet.http.HttpServletRequest;\r
40 \r
41 @Slf4j\r
42 @RestController\r
43 @SwaggerDefinition\r
44 @RequestMapping("/rule")\r
45 @Api(tags = {"Holmes Rule Management"})\r
46 public class RuleMgtResources {\r
47 \r
48     @Autowired\r
49     private RuleMgtWrapper ruleMgtWrapper;\r
50 \r
51     @ResponseBody\r
52     @PutMapping(produces = MediaType.APPLICATION_JSON)\r
53     @ApiOperation(value = "Save a rule into the database; deploy it to the Drools engine if it is enabled.",\r
54             response = RuleAddAndUpdateResponse.class)\r
55     public RuleAddAndUpdateResponse addCorrelationRule(HttpServletRequest request,\r
56                @ApiParam(value =\r
57                        "The request entity of the HTTP call, which comprises \"ruleName\"(required), "\r
58                                + "\"loopControlName\"(required), \"content\"(required), \"enabled\"(required) "\r
59                                + "and \"description\"(optional)", required = true)\r
60                @RequestBody RuleCreateRequest ruleCreateRequest) {\r
61         RuleAddAndUpdateResponse ruleChangeResponse;\r
62         try {\r
63             ruleChangeResponse = ruleMgtWrapper\r
64                     .addCorrelationRule(UserUtil.getUserName(request), ruleCreateRequest);\r
65             return ruleChangeResponse;\r
66         } catch (CorrelationException e) {\r
67             log.error(String.format("failed to create the rule: %s", ruleCreateRequest.getRuleName()), e);\r
68             throw ExceptionUtil.buildExceptionResponse(e.getMessage());\r
69         }\r
70     }\r
71 \r
72     @ResponseBody\r
73     @PostMapping(produces = MediaType.APPLICATION_JSON)\r
74     @ApiOperation(value = "Update an existing rule; deploy it to the Drools engine if it is enabled.", response = RuleAddAndUpdateResponse.class)\r
75     public RuleAddAndUpdateResponse updateCorrelationRule(HttpServletRequest request,\r
76               @ApiParam(value =\r
77                       "The request entity of the HTTP call, which comprises \"ruleId\"(required), "\r
78                               + "\"content\"(required), \"enabled\"(required) and \"description\"(optional)", required = true)\r
79               @RequestBody RuleUpdateRequest ruleUpdateRequest) {\r
80         RuleAddAndUpdateResponse ruleChangeResponse;\r
81         try {\r
82             ruleChangeResponse = ruleMgtWrapper\r
83                     .updateCorrelationRule(UserUtil.getUserName(request), ruleUpdateRequest);\r
84             log.info("update rule:" + ruleUpdateRequest.getRuleId() + " successful");\r
85             return ruleChangeResponse;\r
86         } catch (CorrelationException e) {\r
87             log.error(String.format("failed to update the rule: %s", ruleUpdateRequest.getRuleId()), e);\r
88             throw ExceptionUtil.buildExceptionResponse(e.getMessage());\r
89         }\r
90     }\r
91 \r
92     @DeleteMapping("/{ruleid}")\r
93     @ApiOperation(value = "Remove a rule from Holmes.")\r
94     public ResponseEntity deleteCorrelationRule(@PathVariable("ruleid") String ruleId) {\r
95         try {\r
96             ruleMgtWrapper.deleteCorrelationRule(new RuleDeleteRequest(ruleId));\r
97             return ResponseEntity.status(HttpStatus.NO_CONTENT).build();\r
98         } catch (CorrelationException e) {\r
99             log.error(String.format("failed to delete the rule: %s", ruleId), e);\r
100             throw ExceptionUtil.buildExceptionResponse(e.getMessage());\r
101         }\r
102     }\r
103 \r
104     @ResponseBody\r
105     @GetMapping(produces = MediaType.APPLICATION_JSON)\r
106     @ApiOperation(value = "Query rules using certain criteria.", response = RuleQueryListResponse.class)\r
107     public RuleQueryListResponse getCorrelationRules(\r
108              @ApiParam(value =\r
109                      "A JSON string used as a query parameter, which comprises \"ruleid\"(optional), "\r
110                              + "\"rulename\"(optional), \"creator\"(optional), "\r
111                              + "\"modifier\"(optional) and \"enabled\"(optional). E.g. {\"ruleid\":\"rule_1484727187317\"}")\r
112              @RequestParam(value = "queryrequest", required = false) String ruleQueryRequest) {\r
113         RuleQueryListResponse ruleQueryListResponse;\r
114 \r
115         RuleQueryCondition ruleQueryCondition = getRuleQueryCondition(ruleQueryRequest);\r
116         try {\r
117             ruleQueryListResponse = ruleMgtWrapper\r
118                     .getCorrelationRuleByCondition(ruleQueryCondition);\r
119             return ruleQueryListResponse;\r
120         } catch (CorrelationException e) {\r
121             log.error(String.format("failed to query the rule: %s", ruleQueryCondition.getName()), e);\r
122             throw ExceptionUtil.buildExceptionResponse(e.getMessage());\r
123         }\r
124     }\r
125 \r
126     private RuleQueryCondition getRuleQueryCondition(String queryRequest) {\r
127         RuleQueryCondition ruleQueryCondition = GsonUtil\r
128                 .jsonToBean(queryRequest, RuleQueryCondition.class);\r
129         if (queryRequest == null) {\r
130             if (ruleQueryCondition == null) {\r
131                 ruleQueryCondition = new RuleQueryCondition();\r
132             }\r
133             ruleQueryCondition.setEnabled(RuleMgtConstant.STATUS_RULE_ALL);\r
134         } else if (queryRequest.indexOf("enabled") == -1) {\r
135             ruleQueryCondition.setEnabled(RuleMgtConstant.STATUS_RULE_ALL);\r
136         }\r
137         return ruleQueryCondition;\r
138     }\r
139 }\r