/**\r
- * Copyright 2017 ZTE Corporation.\r
- *\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
+ * Copyright 2017-2022 ZTE Corporation.\r
+ * <p>\r
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except\r
+ * in compliance with the License. You may obtain a copy of the License at\r
+ * <p>\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ * <p>\r
+ * Unless required by applicable law or agreed to in writing, software distributed under the License\r
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express\r
+ * or implied. See the License for the specific language governing permissions and limitations under\r
+ * the License.\r
*/\r
package org.onap.holmes.rulemgt.resources;\r
\r
-import com.codahale.metrics.annotation.Timed;\r
import io.swagger.annotations.Api;\r
import io.swagger.annotations.ApiOperation;\r
import io.swagger.annotations.ApiParam;\r
import io.swagger.annotations.SwaggerDefinition;\r
-import java.util.ArrayList;\r
-import java.util.Date;\r
-import java.util.List;\r
-import java.util.Locale;\r
-import javax.inject.Inject;\r
-import javax.servlet.http.HttpServletRequest;\r
-import javax.ws.rs.DELETE;\r
-import javax.ws.rs.GET;\r
-import javax.ws.rs.POST;\r
-import javax.ws.rs.PUT;\r
-import javax.ws.rs.Path;\r
-import javax.ws.rs.PathParam;\r
-import javax.ws.rs.Produces;\r
-import javax.ws.rs.QueryParam;\r
-import javax.ws.rs.core.Context;\r
-import javax.ws.rs.core.MediaType;\r
+import jakarta.ws.rs.core.MediaType;\r
import lombok.extern.slf4j.Slf4j;\r
-import net.sf.json.JSONObject;\r
-import org.jvnet.hk2.annotations.Service;\r
import org.onap.holmes.common.exception.CorrelationException;\r
import org.onap.holmes.common.utils.ExceptionUtil;\r
import org.onap.holmes.common.utils.GsonUtil;\r
-import org.onap.holmes.common.utils.LanguageUtil;\r
import org.onap.holmes.common.utils.UserUtil;\r
import org.onap.holmes.rulemgt.bean.request.RuleCreateRequest;\r
import org.onap.holmes.rulemgt.bean.request.RuleDeleteRequest;\r
import org.onap.holmes.rulemgt.bean.request.RuleUpdateRequest;\r
import org.onap.holmes.rulemgt.bean.response.RuleAddAndUpdateResponse;\r
import org.onap.holmes.rulemgt.bean.response.RuleQueryListResponse;\r
-import org.onap.holmes.rulemgt.bean.response.RuleResult4API;\r
import org.onap.holmes.rulemgt.constant.RuleMgtConstant;\r
import org.onap.holmes.rulemgt.wrapper.RuleMgtWrapper;\r
+import org.springframework.beans.factory.annotation.Autowired;\r
+import org.springframework.http.HttpStatus;\r
+import org.springframework.http.ResponseEntity;\r
+import org.springframework.web.bind.annotation.*;\r
+\r
+import javax.servlet.http.HttpServletRequest;\r
\r
-@Service\r
+@Slf4j\r
+@RestController\r
@SwaggerDefinition\r
-@Path("/rule")\r
+@RequestMapping("/rule")\r
@Api(tags = {"Holmes Rule Management"})\r
-@Produces(MediaType.APPLICATION_JSON)\r
-@Slf4j\r
public class RuleMgtResources {\r
\r
- @Inject\r
+ @Autowired\r
private RuleMgtWrapper ruleMgtWrapper;\r
\r
- @PUT\r
- @Produces(MediaType.APPLICATION_JSON)\r
+ @ResponseBody\r
+ @PutMapping(produces = MediaType.APPLICATION_JSON)\r
@ApiOperation(value = "Save a rule into the database; deploy it to the Drools engine if it is enabled.",\r
response = RuleAddAndUpdateResponse.class)\r
- @Timed\r
- public RuleAddAndUpdateResponse addCorrelationRule(@Context HttpServletRequest request,\r
- @ApiParam(value = "The request entity of the HTTP call, which comprises \"ruleName\"(required), "\r
- + "\"loopControlName\"(required), \"content\"(required), \"enabled\"(required) "\r
- + "and \"description\"(optional)", required = true)\r
- RuleCreateRequest ruleCreateRequest) {\r
- Locale locale = LanguageUtil.getLocale(request);\r
+ public RuleAddAndUpdateResponse addCorrelationRule(HttpServletRequest request,\r
+ @ApiParam(value =\r
+ "The request entity of the HTTP call, which comprises \"ruleName\"(required), "\r
+ + "\"loopControlName\"(required), \"content\"(required), \"enabled\"(required) "\r
+ + "and \"description\"(optional)", required = true)\r
+ @RequestBody RuleCreateRequest ruleCreateRequest) {\r
RuleAddAndUpdateResponse ruleChangeResponse;\r
try {\r
ruleChangeResponse = ruleMgtWrapper\r
.addCorrelationRule(UserUtil.getUserName(request), ruleCreateRequest);\r
- log.info("create rule:" + ruleCreateRequest.getRuleName() + " success.");\r
return ruleChangeResponse;\r
} catch (CorrelationException e) {\r
- log.error("create rule:" + ruleCreateRequest.getRuleName() + " failed", e);\r
+ log.error(String.format("failed to create the rule: %s", ruleCreateRequest.getRuleName()), e);\r
throw ExceptionUtil.buildExceptionResponse(e.getMessage());\r
}\r
}\r
\r
- @POST\r
- @Produces(MediaType.APPLICATION_JSON)\r
+ @ResponseBody\r
+ @PostMapping(produces = MediaType.APPLICATION_JSON)\r
@ApiOperation(value = "Update an existing rule; deploy it to the Drools engine if it is enabled.", response = RuleAddAndUpdateResponse.class)\r
- @Timed\r
- public RuleAddAndUpdateResponse updateCorrelationRule(@Context HttpServletRequest request,\r
- @ApiParam(value = "The request entity of the HTTP call, which comprises \"ruleId\"(required), "\r
- + "\"content\"(required), \"enabled\"(required) and \"description\"(optional)", required = true)\r
- RuleUpdateRequest ruleUpdateRequest) {\r
- Locale locale = LanguageUtil.getLocale(request);\r
+ public RuleAddAndUpdateResponse updateCorrelationRule(HttpServletRequest request,\r
+ @ApiParam(value =\r
+ "The request entity of the HTTP call, which comprises \"ruleId\"(required), "\r
+ + "\"content\"(required), \"enabled\"(required) and \"description\"(optional)", required = true)\r
+ @RequestBody RuleUpdateRequest ruleUpdateRequest) {\r
RuleAddAndUpdateResponse ruleChangeResponse;\r
try {\r
- ruleChangeResponse = ruleMgtWrapper.updateCorrelationRule(UserUtil.getUserName(request), ruleUpdateRequest);\r
+ ruleChangeResponse = ruleMgtWrapper\r
+ .updateCorrelationRule(UserUtil.getUserName(request), ruleUpdateRequest);\r
log.info("update rule:" + ruleUpdateRequest.getRuleId() + " successful");\r
return ruleChangeResponse;\r
} catch (CorrelationException e) {\r
- log.error("update rule:" + ruleUpdateRequest.getContent() + " failed", e);\r
+ log.error(String.format("failed to update the rule: %s", ruleUpdateRequest.getRuleId()), e);\r
throw ExceptionUtil.buildExceptionResponse(e.getMessage());\r
}\r
}\r
\r
- @DELETE\r
- @Produces(MediaType.APPLICATION_JSON)\r
+ @DeleteMapping("/{ruleid}")\r
@ApiOperation(value = "Remove a rule from Holmes.")\r
- @Timed\r
- @Path("/{ruleid}")\r
- public boolean deleteCorrelationRule(@Context HttpServletRequest request,\r
- @PathParam("ruleid") String ruleId) {\r
- Locale locale = LanguageUtil.getLocale(request);\r
+ public ResponseEntity deleteCorrelationRule(@PathVariable("ruleid") String ruleId) {\r
try {\r
ruleMgtWrapper.deleteCorrelationRule(new RuleDeleteRequest(ruleId));\r
- log.info("delete rule:" + ruleId + " successful");\r
- return true;\r
+ return ResponseEntity.status(HttpStatus.NO_CONTENT).build();\r
} catch (CorrelationException e) {\r
- log.error("delete rule:" + ruleId + " failed", e);\r
+ log.error(String.format("failed to delete the rule: %s", ruleId), e);\r
throw ExceptionUtil.buildExceptionResponse(e.getMessage());\r
}\r
}\r
\r
- @GET\r
- @Produces(MediaType.APPLICATION_JSON)\r
+ @ResponseBody\r
+ @GetMapping(produces = MediaType.APPLICATION_JSON)\r
@ApiOperation(value = "Query rules using certain criteria.", response = RuleQueryListResponse.class)\r
- @Timed\r
- public RuleQueryListResponse getCorrelationRules(@Context HttpServletRequest request,\r
- @ApiParam(value = "A JSON string used as a query parameter, which comprises \"ruleid\"(optional), "\r
- + "\"rulename\"(optional), \"creator\"(optional), "\r
- + "\"modifier\"(optional) and \"enabled\"(optional). E.g. {\"ruleid\":\"rule_1484727187317\"}",\r
- required = false) @QueryParam("queryrequest") String ruleQueryRequest) {\r
- Locale locale = LanguageUtil.getLocale(request);\r
+ public RuleQueryListResponse getCorrelationRules(\r
+ @ApiParam(value =\r
+ "A JSON string used as a query parameter, which comprises \"ruleid\"(optional), "\r
+ + "\"rulename\"(optional), \"creator\"(optional), "\r
+ + "\"modifier\"(optional) and \"enabled\"(optional). E.g. {\"ruleid\":\"rule_1484727187317\"}")\r
+ @RequestParam(value = "queryrequest", required = false) String ruleQueryRequest) {\r
RuleQueryListResponse ruleQueryListResponse;\r
\r
- RuleQueryCondition ruleQueryCondition = getRuleQueryCondition(ruleQueryRequest, request);\r
+ RuleQueryCondition ruleQueryCondition = getRuleQueryCondition(ruleQueryRequest);\r
try {\r
ruleQueryListResponse = ruleMgtWrapper\r
.getCorrelationRuleByCondition(ruleQueryCondition);\r
- log.info("query rule successful by condition:" + JSONObject.fromObject(ruleQueryCondition));\r
return ruleQueryListResponse;\r
} catch (CorrelationException e) {\r
- log.error("query rule failed,cause query condition conversion failure", e);\r
+ log.error(String.format("failed to query the rule: %s", ruleQueryCondition.getName()), e);\r
throw ExceptionUtil.buildExceptionResponse(e.getMessage());\r
}\r
}\r
\r
- private RuleQueryCondition getRuleQueryCondition(String queryRequest,\r
- HttpServletRequest request) {\r
- Locale locale = LanguageUtil.getLocale(request);\r
- RuleQueryCondition ruleQueryCondition = GsonUtil.jsonToBean(queryRequest, RuleQueryCondition.class);\r
+ private RuleQueryCondition getRuleQueryCondition(String queryRequest) {\r
+ RuleQueryCondition ruleQueryCondition = GsonUtil\r
+ .jsonToBean(queryRequest, RuleQueryCondition.class);\r
if (queryRequest == null) {\r
- if(ruleQueryCondition==null){\r
+ if (ruleQueryCondition == null) {\r
ruleQueryCondition = new RuleQueryCondition();\r
}\r
ruleQueryCondition.setEnabled(RuleMgtConstant.STATUS_RULE_ALL);\r