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 lombok.extern.slf4j.Slf4j;
24 import org.jvnet.hk2.annotations.Service;
25 import org.onap.holmes.common.dmaap.store.ClosedLoopControlNameCache;
26 import org.onap.holmes.common.exception.CorrelationException;
27 import org.onap.holmes.common.utils.ExceptionUtil;
28 import org.onap.holmes.common.utils.LanguageUtil;
29 import org.onap.holmes.engine.manager.DroolsEngine;
30 import org.onap.holmes.engine.request.CompileRuleRequest;
31 import org.onap.holmes.engine.request.DeployRuleRequest;
32 import org.onap.holmes.engine.response.CorrelationRuleResponse;
34 import javax.inject.Inject;
35 import javax.servlet.http.HttpServletRequest;
37 import javax.ws.rs.core.Context;
38 import javax.ws.rs.core.MediaType;
39 import java.util.Locale;
40 import java.util.regex.Matcher;
41 import java.util.regex.Pattern;
45 @Api(tags = {"Holmes Engine Management"})
46 @Produces(MediaType.APPLICATION_JSON)
48 public class EngineResources {
50 DroolsEngine droolsEngine;
51 private Pattern packagePattern = Pattern.compile("package[\\s]+([^;]+)[;\\s]*");
52 private ClosedLoopControlNameCache closedLoopControlNameCache;
55 public void setClosedLoopControlNameCache(ClosedLoopControlNameCache closedLoopControlNameCache) {
56 this.closedLoopControlNameCache = closedLoopControlNameCache;
60 @Produces(MediaType.APPLICATION_JSON)
62 public CorrelationRuleResponse deployRule(
63 @ApiParam(value = "The request entity of the HTTP call, which comprises three "
64 + "fields: \"content\" , \"loopControlName\" and \"engineId\". "
65 + "The \"content\" should be a valid Drools rule string and the \"engineId\" "
66 + "has to be \"engine-d\" in the Amsterdam release.", required = true) DeployRuleRequest deployRuleRequest,
67 @Context HttpServletRequest httpRequest) {
69 CorrelationRuleResponse crResponse = new CorrelationRuleResponse();
70 Locale locale = LanguageUtil.getLocale(httpRequest);
72 String packageName = getPackageName(deployRuleRequest.getContent());
73 if(packageName == null) {
74 throw new CorrelationException("Could not find package name in rule: "+deployRuleRequest.getContent());
77 closedLoopControlNameCache
78 .put(packageName, deployRuleRequest.getLoopControlName());
79 String packageNameRet = droolsEngine.deployRule(deployRuleRequest);
80 if (!packageName.equals(packageNameRet)) {
81 log.info("The parsed package name is different from that returned by the engine.");
82 closedLoopControlNameCache.remove(packageName);
83 closedLoopControlNameCache
84 .put(packageNameRet, deployRuleRequest.getLoopControlName());
86 log.info("Rule deployed. Package name: " + packageNameRet);
87 crResponse.setPackageName(packageNameRet);
89 } catch (CorrelationException correlationException) {
90 log.error(correlationException.getMessage(), correlationException);
91 throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
92 } catch (RuntimeException e) {
93 log.error(e.getMessage(), e);
94 throw ExceptionUtil.buildExceptionResponse(e.getMessage());
101 @Produces(MediaType.APPLICATION_JSON)
103 @Path("/{packageName}")
104 public boolean undeployRule(@PathParam("packageName") String packageName,
105 @Context HttpServletRequest httpRequest) {
107 Locale locale = LanguageUtil.getLocale(httpRequest);
110 droolsEngine.undeployRule(packageName);
111 closedLoopControlNameCache.remove(packageName);
112 } catch (CorrelationException correlationException) {
113 log.error(correlationException.getMessage(), correlationException);
114 throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
122 @ApiOperation(value = "Check the validity of a rule.")
123 @Produces(MediaType.APPLICATION_JSON)
125 public boolean compileRule(CompileRuleRequest compileRuleRequest,
126 @Context HttpServletRequest httpRequest) {
128 Locale locale = LanguageUtil.getLocale(httpRequest);
131 droolsEngine.compileRule(compileRuleRequest.getContent());
132 } catch (CorrelationException correlationException) {
133 log.error(correlationException.getMessage(), correlationException);
134 throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
139 private String getPackageName(String contents){
140 Matcher m = packagePattern.matcher(contents);