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.dmaap.DmaapService;
37 import org.onap.holmes.common.exception.CorrelationException;
38 import org.onap.holmes.common.utils.ExceptionUtil;
39 import org.onap.holmes.common.utils.LanguageUtil;
40 import org.onap.holmes.engine.manager.DroolsEngine;
41 import org.onap.holmes.engine.request.CompileRuleRequest;
42 import org.onap.holmes.engine.request.DeployRuleRequest;
43 import org.onap.holmes.engine.response.CorrelationRuleResponse;
47 @Api(tags = {"Holmes Engine Management"})
48 @Produces(MediaType.APPLICATION_JSON)
50 public class EngineResources {
53 DroolsEngine droolsEngine;
56 @ApiOperation(value = "Deploy a rule into the Drools engine.", response = CorrelationRuleResponse.class)
57 @Produces(MediaType.APPLICATION_JSON)
59 public CorrelationRuleResponse deployRule(
60 @ApiParam(value = "The request entity of the HTTP call, which comprises three "
61 + "fields: \"content\" , \"loopcontrolname\" and \"engineid\". "
62 + "The \"content\" should be a valid Drools rule string and the \"engineid\" "
63 + "has to be \"engine-d\" in the Amsterdam release.", required = true) DeployRuleRequest deployRuleRequest,
64 @Context HttpServletRequest httpRequest) {
66 CorrelationRuleResponse crResponse = new CorrelationRuleResponse();
67 Locale locale = LanguageUtil.getLocale(httpRequest);
69 String packageName = droolsEngine.deployRule(deployRuleRequest, locale);
70 DmaapService.loopControlNames
71 .put(packageName, deployRuleRequest.getLoopControlName());
72 log.info("Rule deployed. Package name: " + packageName);
73 crResponse.setPackageName(packageName);
75 } catch (CorrelationException correlationException) {
76 log.error(correlationException.getMessage(), correlationException);
77 throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
84 @ApiOperation(value = "Undeploy a rule from the Drools engine.")
85 @Produces(MediaType.APPLICATION_JSON)
87 @Path("/{packageName}")
88 public boolean undeployRule(@PathParam("packageName") String packageName,
89 @Context HttpServletRequest httpRequest) {
91 Locale locale = LanguageUtil.getLocale(httpRequest);
95 droolsEngine.undeployRule(packageName, locale);
97 } catch (CorrelationException correlationException) {
98 log.error(correlationException.getMessage(), correlationException);
99 throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
106 @ApiOperation(value = "Check the validity of a rule.")
107 @Produces(MediaType.APPLICATION_JSON)
109 public boolean compileRule(CompileRuleRequest compileRuleRequest,
110 @Context HttpServletRequest httpRequest) {
112 Locale locale = LanguageUtil.getLocale(httpRequest);
115 droolsEngine.compileRule(compileRuleRequest.getContent(), locale);
116 } catch (CorrelationException correlationException) {
117 log.error(correlationException.getMessage(), correlationException);
118 throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());