Add Swagger Related Configurations
[holmes/engine-management.git] / engine-d / src / main / java / org / onap / holmes / engine / resources / EngineResources.java
1 /**
2  * Copyright 2017 ZTE Corporation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.holmes.engine.resources;
17
18
19 import com.codahale.metrics.annotation.Timed;
20 import io.swagger.annotations.Api;
21 import io.swagger.annotations.ApiOperation;
22 import io.swagger.annotations.ApiParam;
23 import java.util.Locale;
24 import javax.inject.Inject;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.ws.rs.DELETE;
27 import javax.ws.rs.POST;
28 import javax.ws.rs.PUT;
29 import javax.ws.rs.Path;
30 import javax.ws.rs.PathParam;
31 import javax.ws.rs.Produces;
32 import javax.ws.rs.core.Context;
33 import javax.ws.rs.core.MediaType;
34 import lombok.extern.slf4j.Slf4j;
35 import org.jvnet.hk2.annotations.Service;
36 import org.onap.holmes.common.exception.CorrelationException;
37 import org.onap.holmes.common.utils.ExceptionUtil;
38 import org.onap.holmes.common.utils.LanguageUtil;
39 import org.onap.holmes.engine.manager.DroolsEngine;
40 import org.onap.holmes.engine.request.CompileRuleRequest;
41 import org.onap.holmes.engine.request.DeployRuleRequest;
42 import org.onap.holmes.engine.response.CorrelationRuleResponse;
43
44 @Service
45 @Path("/rule")
46 @Api(tags = {"Holmes Engine Management"})
47 @Produces(MediaType.APPLICATION_JSON)
48 @Slf4j
49 public class EngineResources {
50
51     @Inject
52     DroolsEngine droolsEngine;
53
54     @PUT
55     @ApiOperation(value = "Deploy a rule into the Drools engine.", response = CorrelationRuleResponse.class)
56     @Produces(MediaType.APPLICATION_JSON)
57     @Timed
58     public CorrelationRuleResponse deployRule(
59             @ApiParam(value = "The request entity of the HTTP call, which comprises two "
60                     + "fields: \"content\" and \"engineid\". "
61                     + "The \"content\" should be a valid Drools rule string and the \"engineid\" "
62                     + "has to be \"engine-d\" in the Amsterdam release.", required = true) DeployRuleRequest deployRuleRequest,
63             @Context HttpServletRequest httpRequest) {
64
65         CorrelationRuleResponse crResponse = new CorrelationRuleResponse();
66         Locale locale = LanguageUtil.getLocale(httpRequest);
67         try {
68
69             String packageName = droolsEngine.deployRule(deployRuleRequest, locale);
70             log.info("Rule deployed. Package name: " + packageName);
71             crResponse.setPackageName(packageName);
72
73         } catch (CorrelationException correlationException) {
74             log.error(correlationException.getMessage(), correlationException);
75             throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
76         }
77
78         return crResponse;
79     }
80
81     @DELETE
82     @ApiOperation(value = "Undeploy a rule from the Drools engine.")
83     @Produces(MediaType.APPLICATION_JSON)
84     @Timed
85     @Path("/{packageName}")
86     public boolean undeployRule(@PathParam("packageName") String packageName,
87             @Context HttpServletRequest httpRequest) {
88
89         Locale locale = LanguageUtil.getLocale(httpRequest);
90
91         try {
92
93             droolsEngine.undeployRule(packageName, locale);
94
95         } catch (CorrelationException correlationException) {
96             log.error(correlationException.getMessage(), correlationException);
97             throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
98         }
99         return true;
100     }
101
102
103     @POST
104     @ApiOperation(value = "Check the validity of a rule.")
105     @Produces(MediaType.APPLICATION_JSON)
106     @Timed
107     public boolean compileRule(CompileRuleRequest compileRuleRequest,
108             @Context HttpServletRequest httpRequest) {
109
110         Locale locale = LanguageUtil.getLocale(httpRequest);
111
112         try {
113             droolsEngine.compileRule(compileRuleRequest.getContent(), locale);
114         } catch (CorrelationException correlationException) {
115             log.error(correlationException.getMessage(), correlationException);
116             throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
117         }
118         return true;
119     }
120 }