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 java.util.regex.Matcher;
25 import java.util.regex.Pattern;
27 import javax.inject.Inject;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.ws.rs.DELETE;
30 import javax.ws.rs.POST;
31 import javax.ws.rs.PUT;
32 import javax.ws.rs.Path;
33 import javax.ws.rs.PathParam;
34 import javax.ws.rs.Produces;
35 import javax.ws.rs.core.Context;
36 import javax.ws.rs.core.MediaType;
37 import lombok.extern.slf4j.Slf4j;
38 import org.jvnet.hk2.annotations.Service;
39 import org.onap.holmes.common.dmaap.DmaapService;
40 import org.onap.holmes.common.exception.CorrelationException;
41 import org.onap.holmes.common.utils.ExceptionUtil;
42 import org.onap.holmes.common.utils.LanguageUtil;
43 import org.onap.holmes.engine.manager.DroolsEngine;
44 import org.onap.holmes.engine.request.CompileRuleRequest;
45 import org.onap.holmes.engine.request.DeployRuleRequest;
46 import org.onap.holmes.engine.response.CorrelationRuleResponse;
50 @Api(tags = {"Holmes Engine Management"})
51 @Produces(MediaType.APPLICATION_JSON)
53 public class EngineResources {
54 private Pattern packagePattern = Pattern.compile("package[\\s]+([^;]+)[;\\s]*");
57 DroolsEngine droolsEngine;
60 @ApiOperation(value = "Deploy a rule into the Drools engine.", response = CorrelationRuleResponse.class)
61 @Produces(MediaType.APPLICATION_JSON)
63 public CorrelationRuleResponse deployRule(
64 @ApiParam(value = "The request entity of the HTTP call, which comprises three "
65 + "fields: \"content\" , \"loopControlName\" and \"engineId\". "
66 + "The \"content\" should be a valid Drools rule string and the \"engineId\" "
67 + "has to be \"engine-d\" in the Amsterdam release.", required = true) DeployRuleRequest deployRuleRequest,
68 @Context HttpServletRequest httpRequest) {
70 CorrelationRuleResponse crResponse = new CorrelationRuleResponse();
71 Locale locale = LanguageUtil.getLocale(httpRequest);
73 String packageName = getPackageName(deployRuleRequest.getContent());
74 if(packageName == null) {
75 throw new CorrelationException("Could not find package name in rule: "+deployRuleRequest.getContent());
78 DmaapService.loopControlNames
79 .put(packageName, deployRuleRequest.getLoopControlName());
80 String packageNameRet = droolsEngine.deployRule(deployRuleRequest, locale);
81 if (!packageName.equals(packageNameRet)) {
82 log.info("The parsed package name is different from that returned by the engine.");
83 DmaapService.loopControlNames.remove(packageName);
84 DmaapService.loopControlNames
85 .put(packageNameRet, deployRuleRequest.getLoopControlName());
87 log.info("Rule deployed. Package name: " + packageNameRet);
88 crResponse.setPackageName(packageNameRet);
90 } catch (CorrelationException correlationException) {
91 log.error(correlationException.getMessage(), correlationException);
92 throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
93 } catch (RuntimeException e) {
94 log.error(e.getMessage(), e);
95 throw ExceptionUtil.buildExceptionResponse(e.getMessage());
102 @ApiOperation(value = "Undeploy a rule from the Drools engine.")
103 @Produces(MediaType.APPLICATION_JSON)
105 @Path("/{packageName}")
106 public boolean undeployRule(@PathParam("packageName") String packageName,
107 @Context HttpServletRequest httpRequest) {
109 Locale locale = LanguageUtil.getLocale(httpRequest);
112 droolsEngine.undeployRule(packageName, locale);
113 DmaapService.loopControlNames.remove(packageName);
114 } catch (CorrelationException correlationException) {
115 log.error(correlationException.getMessage(), correlationException);
116 throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
123 @ApiOperation(value = "Check the validity of a rule.")
124 @Produces(MediaType.APPLICATION_JSON)
126 public boolean compileRule(CompileRuleRequest compileRuleRequest,
127 @Context HttpServletRequest httpRequest) {
129 Locale locale = LanguageUtil.getLocale(httpRequest);
132 droolsEngine.compileRule(compileRuleRequest.getContent(), locale);
133 } catch (CorrelationException correlationException) {
134 log.error(correlationException.getMessage(), correlationException);
135 throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
140 private String getPackageName(String contents){
141 Matcher m = packagePattern.matcher(contents);