X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=rulemgt%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fholmes%2Frulemgt%2Fresources%2FRuleMgtResources.java;h=133e8592625be3665725ed50be2a03b723da0f9d;hb=a796bda2f8e2db093ce9f528f6e5960e4aa1b22c;hp=dc21e0d1fe9191ae9fb9d55a0641d5463fd6d823;hpb=646a660b1a7828013006619b4f7ec2d2f2f02f36;p=holmes%2Frule-management.git diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/resources/RuleMgtResources.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/resources/RuleMgtResources.java index dc21e0d..133e859 100644 --- a/rulemgt/src/main/java/org/onap/holmes/rulemgt/resources/RuleMgtResources.java +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/resources/RuleMgtResources.java @@ -1,46 +1,27 @@ /** - * Copyright 2017 ZTE Corporation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2017-2022 ZTE Corporation. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. */ package org.onap.holmes.rulemgt.resources; -import com.codahale.metrics.annotation.Timed; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import io.swagger.annotations.SwaggerDefinition; -import java.io.IOException; -import java.util.Locale; -import javax.inject.Inject; -import javax.servlet.http.HttpServletRequest; -import javax.ws.rs.DELETE; -import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.PUT; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; -import javax.ws.rs.Produces; -import javax.ws.rs.QueryParam; -import javax.ws.rs.core.Context; -import javax.ws.rs.core.MediaType; +import jakarta.ws.rs.core.MediaType; import lombok.extern.slf4j.Slf4j; -import net.sf.json.JSONObject; -import org.jvnet.hk2.annotations.Service; import org.onap.holmes.common.exception.CorrelationException; import org.onap.holmes.common.utils.ExceptionUtil; -import org.onap.holmes.common.utils.JacksonUtil; -import org.onap.holmes.common.utils.LanguageUtil; +import org.onap.holmes.common.utils.GsonUtil; import org.onap.holmes.common.utils.UserUtil; import org.onap.holmes.rulemgt.bean.request.RuleCreateRequest; import org.onap.holmes.rulemgt.bean.request.RuleDeleteRequest; @@ -50,29 +31,33 @@ import org.onap.holmes.rulemgt.bean.response.RuleAddAndUpdateResponse; import org.onap.holmes.rulemgt.bean.response.RuleQueryListResponse; import org.onap.holmes.rulemgt.constant.RuleMgtConstant; import org.onap.holmes.rulemgt.wrapper.RuleMgtWrapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import javax.servlet.http.HttpServletRequest; -@Service +@Slf4j +@RestController @SwaggerDefinition -@Path("/rule") +@RequestMapping("/rule") @Api(tags = {"Holmes Rule Management"}) -@Produces(MediaType.APPLICATION_JSON) -@Slf4j public class RuleMgtResources { - @Inject + @Autowired private RuleMgtWrapper ruleMgtWrapper; - @PUT - @Produces(MediaType.APPLICATION_JSON) + @ResponseBody + @PutMapping(produces = MediaType.APPLICATION_JSON) @ApiOperation(value = "Save a rule into the database; deploy it to the Drools engine if it is enabled.", response = RuleAddAndUpdateResponse.class) - @Timed - public RuleAddAndUpdateResponse addCorrelationRule(@Context HttpServletRequest request, - @ApiParam(value = "The request entity of the HTTP call, which comprises \"rulename\"(required), " - + "\"loopcontrolname\"(required), \"content\"(required), \"enabled\"(required) " - + "and \"description\"(optional)", required = true) - RuleCreateRequest ruleCreateRequest) { - Locale locale = LanguageUtil.getLocale(request); + public RuleAddAndUpdateResponse addCorrelationRule(HttpServletRequest request, + @ApiParam(value = + "The request entity of the HTTP call, which comprises \"ruleName\"(required), " + + "\"loopControlName\"(required), \"content\"(required), \"enabled\"(required) " + + "and \"description\"(optional)", required = true) + @RequestBody RuleCreateRequest ruleCreateRequest) { RuleAddAndUpdateResponse ruleChangeResponse; try { ruleChangeResponse = ruleMgtWrapper @@ -85,18 +70,18 @@ public class RuleMgtResources { } } - @POST - @Produces(MediaType.APPLICATION_JSON) + @ResponseBody + @PostMapping(produces = MediaType.APPLICATION_JSON) @ApiOperation(value = "Update an existing rule; deploy it to the Drools engine if it is enabled.", response = RuleAddAndUpdateResponse.class) - @Timed - public RuleAddAndUpdateResponse updateCorrelationRule(@Context HttpServletRequest request, - @ApiParam(value = "The request entity of the HTTP call, which comprises \"ruleid\"(required), " - + "\"content\"(required), \"enabled\"(required) and \"description\"(optional)", required = true) - RuleUpdateRequest ruleUpdateRequest) { - Locale locale = LanguageUtil.getLocale(request); + public RuleAddAndUpdateResponse updateCorrelationRule(HttpServletRequest request, + @ApiParam(value = + "The request entity of the HTTP call, which comprises \"ruleId\"(required), " + + "\"content\"(required), \"enabled\"(required) and \"description\"(optional)", required = true) + @RequestBody RuleUpdateRequest ruleUpdateRequest) { RuleAddAndUpdateResponse ruleChangeResponse; try { - ruleChangeResponse = ruleMgtWrapper.updateCorrelationRule(UserUtil.getUserName(request), ruleUpdateRequest); + ruleChangeResponse = ruleMgtWrapper + .updateCorrelationRule(UserUtil.getUserName(request), ruleUpdateRequest); log.info("update rule:" + ruleUpdateRequest.getRuleId() + " successful"); return ruleChangeResponse; } catch (CorrelationException e) { @@ -105,40 +90,35 @@ public class RuleMgtResources { } } - @DELETE - @Produces(MediaType.APPLICATION_JSON) + @DeleteMapping("/{ruleid}") @ApiOperation(value = "Remove a rule from Holmes.") - @Timed - @Path("/{ruleid}") - public boolean deleteCorrelationRule(@Context HttpServletRequest request, - @PathParam("ruleid") String ruleId) { - Locale locale = LanguageUtil.getLocale(request); + public ResponseEntity deleteCorrelationRule(@PathVariable("ruleid") String ruleId) { try { ruleMgtWrapper.deleteCorrelationRule(new RuleDeleteRequest(ruleId)); log.info("delete rule:" + ruleId + " successful"); - return true; + return ResponseEntity.status(HttpStatus.NO_CONTENT).build(); } catch (CorrelationException e) { log.error("delete rule:" + ruleId + " failed", e); throw ExceptionUtil.buildExceptionResponse(e.getMessage()); } } - @GET - @Produces(MediaType.APPLICATION_JSON) + @ResponseBody + @GetMapping(produces = MediaType.APPLICATION_JSON) @ApiOperation(value = "Query rules using certain criteria.", response = RuleQueryListResponse.class) - @Timed - public RuleQueryListResponse getCorrelationRules(@Context HttpServletRequest request, - @ApiParam(value = "A JSON string used as a query parameter, which comprises \"ruleid\"(optional), " - + "\"rulename\"(optional), \"creator\"(optional), " - + "\"modifier\"(optional) and \"enabled\"(optional). E.g. {\"ruleid\":\"rule_1484727187317\"}", - required = false) @QueryParam("queryrequest") String ruleQueryRequest) { - Locale locale = LanguageUtil.getLocale(request); + public RuleQueryListResponse getCorrelationRules( + @ApiParam(value = + "A JSON string used as a query parameter, which comprises \"ruleid\"(optional), " + + "\"rulename\"(optional), \"creator\"(optional), " + + "\"modifier\"(optional) and \"enabled\"(optional). E.g. {\"ruleid\":\"rule_1484727187317\"}") + @RequestParam(value = "queryrequest", required = false) String ruleQueryRequest) { RuleQueryListResponse ruleQueryListResponse; - RuleQueryCondition ruleQueryCondition = getRuleQueryCondition(ruleQueryRequest, request); + + RuleQueryCondition ruleQueryCondition = getRuleQueryCondition(ruleQueryRequest); try { ruleQueryListResponse = ruleMgtWrapper .getCorrelationRuleByCondition(ruleQueryCondition); - log.info("query rule successful by condition:" + JSONObject.fromObject(ruleQueryCondition)); + log.info("query rule successful by condition:" + ruleQueryCondition); return ruleQueryListResponse; } catch (CorrelationException e) { log.error("query rule failed,cause query condition conversion failure", e); @@ -146,21 +126,17 @@ public class RuleMgtResources { } } - private RuleQueryCondition getRuleQueryCondition(String queryRequest, - HttpServletRequest request) { - Locale locale = LanguageUtil.getLocale(request); - try { - RuleQueryCondition ruleQueryCondition = JacksonUtil - .jsonToBean(queryRequest, RuleQueryCondition.class); - if (queryRequest == null) { - ruleQueryCondition.setEnabled(RuleMgtConstant.STATUS_RULE_ALL); - } else if (queryRequest.indexOf("enabled") == -1) { - ruleQueryCondition.setEnabled(RuleMgtConstant.STATUS_RULE_ALL); + private RuleQueryCondition getRuleQueryCondition(String queryRequest) { + RuleQueryCondition ruleQueryCondition = GsonUtil + .jsonToBean(queryRequest, RuleQueryCondition.class); + if (queryRequest == null) { + if (ruleQueryCondition == null) { + ruleQueryCondition = new RuleQueryCondition(); } - return ruleQueryCondition; - } catch (IOException e) { - log.warn("queryRequest convert to json failed", e); - throw ExceptionUtil.buildExceptionResponse("The request format is invalid!"); + ruleQueryCondition.setEnabled(RuleMgtConstant.STATUS_RULE_ALL); + } else if (queryRequest.indexOf("enabled") == -1) { + ruleQueryCondition.setEnabled(RuleMgtConstant.STATUS_RULE_ALL); } + return ruleQueryCondition; } }