Migrate from DW to Springboot
[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             log.info("create rule:" + ruleCreateRequest.getRuleName() + " success.");\r
66             return ruleChangeResponse;\r
67         } catch (CorrelationException e) {\r
68             log.error("create rule:" + ruleCreateRequest.getRuleName() + " failed", e);\r
69             throw ExceptionUtil.buildExceptionResponse(e.getMessage());\r
70         }\r
71     }\r
72 \r
73     @ResponseBody\r
74     @PostMapping(produces = MediaType.APPLICATION_JSON)\r
75     @ApiOperation(value = "Update an existing rule; deploy it to the Drools engine if it is enabled.", response = RuleAddAndUpdateResponse.class)\r
76     public RuleAddAndUpdateResponse updateCorrelationRule(HttpServletRequest request,\r
77               @ApiParam(value =\r
78                       "The request entity of the HTTP call, which comprises \"ruleId\"(required), "\r
79                               + "\"content\"(required), \"enabled\"(required) and \"description\"(optional)", required = true)\r
80               @RequestBody RuleUpdateRequest ruleUpdateRequest) {\r
81         RuleAddAndUpdateResponse ruleChangeResponse;\r
82         try {\r
83             ruleChangeResponse = ruleMgtWrapper\r
84                     .updateCorrelationRule(UserUtil.getUserName(request), ruleUpdateRequest);\r
85             log.info("update rule:" + ruleUpdateRequest.getRuleId() + " successful");\r
86             return ruleChangeResponse;\r
87         } catch (CorrelationException e) {\r
88             log.error("update rule:" + ruleUpdateRequest.getContent() + " failed", e);\r
89             throw ExceptionUtil.buildExceptionResponse(e.getMessage());\r
90         }\r
91     }\r
92 \r
93     @DeleteMapping("/{ruleid}")\r
94     @ApiOperation(value = "Remove a rule from Holmes.")\r
95     public ResponseEntity deleteCorrelationRule(@PathVariable("ruleid") String ruleId) {\r
96         try {\r
97             ruleMgtWrapper.deleteCorrelationRule(new RuleDeleteRequest(ruleId));\r
98             log.info("delete rule:" + ruleId + " successful");\r
99             return ResponseEntity.status(HttpStatus.NO_CONTENT).build();\r
100         } catch (CorrelationException e) {\r
101             log.error("delete rule:" + ruleId + " failed", e);\r
102             throw ExceptionUtil.buildExceptionResponse(e.getMessage());\r
103         }\r
104     }\r
105 \r
106     @ResponseBody\r
107     @GetMapping(produces = MediaType.APPLICATION_JSON)\r
108     @ApiOperation(value = "Query rules using certain criteria.", response = RuleQueryListResponse.class)\r
109     public RuleQueryListResponse getCorrelationRules(\r
110              @ApiParam(value =\r
111                      "A JSON string used as a query parameter, which comprises \"ruleid\"(optional), "\r
112                              + "\"rulename\"(optional), \"creator\"(optional), "\r
113                              + "\"modifier\"(optional) and \"enabled\"(optional). E.g. {\"ruleid\":\"rule_1484727187317\"}")\r
114              @RequestParam(value = "queryrequest", required = false) String ruleQueryRequest) {\r
115         RuleQueryListResponse ruleQueryListResponse;\r
116 \r
117         RuleQueryCondition ruleQueryCondition = getRuleQueryCondition(ruleQueryRequest);\r
118         try {\r
119             ruleQueryListResponse = ruleMgtWrapper\r
120                     .getCorrelationRuleByCondition(ruleQueryCondition);\r
121             log.info("query rule successful by condition:" + ruleQueryCondition);\r
122             return ruleQueryListResponse;\r
123         } catch (CorrelationException e) {\r
124             log.error("query rule failed,cause query condition conversion failure", e);\r
125             throw ExceptionUtil.buildExceptionResponse(e.getMessage());\r
126         }\r
127     }\r
128 \r
129     private RuleQueryCondition getRuleQueryCondition(String queryRequest) {\r
130         RuleQueryCondition ruleQueryCondition = GsonUtil\r
131                 .jsonToBean(queryRequest, RuleQueryCondition.class);\r
132         if (queryRequest == null) {\r
133             if (ruleQueryCondition == null) {\r
134                 ruleQueryCondition = new RuleQueryCondition();\r
135             }\r
136             ruleQueryCondition.setEnabled(RuleMgtConstant.STATUS_RULE_ALL);\r
137         } else if (queryRequest.indexOf("enabled") == -1) {\r
138             ruleQueryCondition.setEnabled(RuleMgtConstant.STATUS_RULE_ALL);\r
139         }\r
140         return ruleQueryCondition;\r
141     }\r
142 }\r