Change the groupid from openo to onap
[holmes/rule-management.git] / rulemgt / src / main / java / org / onap / holmes / rulemgt / resources / RuleMgtResources.java
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
new file mode 100644 (file)
index 0000000..04578f7
--- /dev/null
@@ -0,0 +1,161 @@
+/**\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
+ */\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.io.IOException;\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.Produces;\r
+import javax.ws.rs.QueryParam;\r
+import javax.ws.rs.core.Context;\r
+import javax.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.api.entity.ServiceRegisterEntity;\r
+import org.onap.holmes.common.config.MicroServiceConfig;\r
+import org.onap.holmes.common.exception.CorrelationException;\r
+import org.onap.holmes.common.utils.ExceptionUtil;\r
+import org.onap.holmes.common.utils.JacksonUtil;\r
+import org.onap.holmes.common.utils.LanguageUtil;\r
+import org.onap.holmes.common.utils.MSBRegisterUtil;\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.RuleQueryCondition;\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.constant.RuleMgtConstant;\r
+import org.onap.holmes.rulemgt.wrapper.RuleMgtWrapper;\r
+\r
+@Service\r
+@SwaggerDefinition\r
+@Path("/rule")\r
+@Api(tags = {"CorrelationRules"})\r
+@Produces(MediaType.APPLICATION_JSON)\r
+@Slf4j\r
+public class RuleMgtResources {\r
+\r
+    @Inject\r
+    private RuleMgtWrapper ruleMgtWrapper;\r
+\r
+    @PUT\r
+    @Produces(MediaType.APPLICATION_JSON)\r
+    @ApiOperation(value = "Save the alarm+ rule to the database, and deployed to the engine when the enable to open.", response = RuleAddAndUpdateResponse.class)\r
+    @Timed\r
+    public RuleAddAndUpdateResponse addCorrelationRule(@Context HttpServletRequest request,\r
+            @ApiParam(value = "alarm+ rule create request.<br>[rulename]:<font color=\"red\">required</font><br>[content]:<font color=\"red\">required</font><br>[enabled]:<font color=\"red\">required</font>", required = true) RuleCreateRequest ruleCreateRequest) {\r
+        Locale locale = LanguageUtil.getLocale(request);\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
+            throw ExceptionUtil.buildExceptionResponse(e.getMessage());\r
+        }\r
+    }\r
+\r
+    @POST\r
+    @Produces(MediaType.APPLICATION_JSON)\r
+    @ApiOperation(value = "Update the alarm+ rule and deployed to the engine when the enable to open.", response = RuleAddAndUpdateResponse.class)\r
+    @Timed\r
+    public RuleAddAndUpdateResponse updateCorrelationRule(@Context HttpServletRequest request,\r
+            @ApiParam(value = "alarm+ rule update request.<br>[ruleid]:<font color=\"red\">required</font>", required = true) RuleUpdateRequest ruleUpdateRequest) {\r
+        Locale locale = LanguageUtil.getLocale(request);\r
+        RuleAddAndUpdateResponse ruleChangeResponse;\r
+        try {\r
+            ruleChangeResponse = ruleMgtWrapper.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
+            throw ExceptionUtil.buildExceptionResponse(e.getMessage());\r
+        }\r
+    }\r
+\r
+    @DELETE\r
+    @Produces(MediaType.APPLICATION_JSON)\r
+    @ApiOperation(value = "Delete the alarm+ rule,and when the enable is open also removed from the engine.")\r
+    @Timed\r
+    public boolean deleteCorrelationRule(@Context HttpServletRequest request,\r
+            @ApiParam(value = "alarm+ rule delete request.<br>[ruleid]:<font color=\"red\">required</font>", required = true) RuleDeleteRequest ruleDeleteRequest) {\r
+        Locale locale = LanguageUtil.getLocale(request);\r
+        try {\r
+            ruleMgtWrapper.deleteCorrelationRule(ruleDeleteRequest);\r
+            log.info("delete rule:" + ruleDeleteRequest.getRuleId() + " successful");\r
+            return true;\r
+        } catch (CorrelationException e) {\r
+            log.error("delete rule:" + ruleDeleteRequest.getRuleId() + " failed", e);\r
+            throw ExceptionUtil.buildExceptionResponse(e.getMessage());\r
+        }\r
+    }\r
+\r
+    @GET\r
+    @Produces(MediaType.APPLICATION_JSON)\r
+    @ApiOperation(value = "According to the conditions query the alarm + rules", response = RuleQueryListResponse.class)\r
+    @Timed\r
+    public RuleQueryListResponse getCorrelationRules(@Context HttpServletRequest request,\r
+            @ApiParam(value = "query condition:<br>" + " <b>[ruleid]</b>:Rule ID;<br>"\r
+                    + "<b>[rulename]</b>:Rule name;<br>" + "<b>[creator]</b>:creator of the rule;<br>"\r
+                    + "<b>[modifier]</b>:modifier of the rule;<br>"\r
+                    + "<b>[enabled]</b>: 0 is Enabled,1 is disabled;<br><font color=\"red\">for example:</font><br>{\"ruleid\":\"rule_1484727187317\"}", required = false) @QueryParam("queryrequest") String ruleQueryRequest) {\r
+        Locale locale = LanguageUtil.getLocale(request);\r
+        RuleQueryListResponse ruleQueryListResponse;\r
+        RuleQueryCondition ruleQueryCondition = getRuleQueryCondition(ruleQueryRequest, request);\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
+            throw ExceptionUtil.buildExceptionResponse(e.getMessage());\r
+        }\r
+    }\r
+\r
+    private RuleQueryCondition getRuleQueryCondition(String queryRequest,\r
+            HttpServletRequest request) {\r
+        Locale locale = LanguageUtil.getLocale(request);\r
+        try {\r
+            RuleQueryCondition ruleQueryCondition = JacksonUtil\r
+                    .jsonToBean(queryRequest, RuleQueryCondition.class);\r
+            if (queryRequest == null) {\r
+                ruleQueryCondition.setEnabled(RuleMgtConstant.STATUS_RULE_ALL);\r
+            } else if (queryRequest.indexOf("enabled") == -1) {\r
+                ruleQueryCondition.setEnabled(RuleMgtConstant.STATUS_RULE_ALL);\r
+            }\r
+            return ruleQueryCondition;\r
+        } catch (IOException e) {\r
+            log.warn("queryRequest convert to json failed", e);\r
+            throw ExceptionUtil.buildExceptionResponse("The request format is invalid!");\r
+        }\r
+    }\r
+}\r