2 * Copyright 2017 ZTE Corporation.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.holmes.engine.resources;
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;
46 @Api(tags = {"Holmes Engine Management"})
47 @Produces(MediaType.APPLICATION_JSON)
49 public class EngineResources {
52 DroolsEngine droolsEngine;
55 @ApiOperation(value = "Deploy a rule into the Drools engine.", response = CorrelationRuleResponse.class)
56 @Produces(MediaType.APPLICATION_JSON)
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) {
65 CorrelationRuleResponse crResponse = new CorrelationRuleResponse();
66 Locale locale = LanguageUtil.getLocale(httpRequest);
69 String packageName = droolsEngine.deployRule(deployRuleRequest, locale);
70 log.info("Rule deployed. Package name: " + packageName);
71 crResponse.setPackageName(packageName);
73 } catch (CorrelationException correlationException) {
74 log.error(correlationException.getMessage(), correlationException);
75 throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
82 @ApiOperation(value = "Undeploy a rule from the Drools engine.")
83 @Produces(MediaType.APPLICATION_JSON)
85 @Path("/{packageName}")
86 public boolean undeployRule(@PathParam("packageName") String packageName,
87 @Context HttpServletRequest httpRequest) {
89 Locale locale = LanguageUtil.getLocale(httpRequest);
93 droolsEngine.undeployRule(packageName, locale);
95 } catch (CorrelationException correlationException) {
96 log.error(correlationException.getMessage(), correlationException);
97 throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
104 @ApiOperation(value = "Check the validity of a rule.")
105 @Produces(MediaType.APPLICATION_JSON)
107 public boolean compileRule(CompileRuleRequest compileRuleRequest,
108 @Context HttpServletRequest httpRequest) {
110 Locale locale = LanguageUtil.getLocale(httpRequest);
113 droolsEngine.compileRule(compileRuleRequest.getContent(), locale);
114 } catch (CorrelationException correlationException) {
115 log.error(correlationException.getMessage(), correlationException);
116 throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());