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 io.swagger.annotations.Api;
20 import io.swagger.annotations.ApiOperation;
21 import io.swagger.annotations.ApiParam;
22 import lombok.extern.slf4j.Slf4j;
23 import org.onap.holmes.common.dmaap.store.ClosedLoopControlNameCache;
24 import org.onap.holmes.common.exception.CorrelationException;
25 import org.onap.holmes.common.utils.ExceptionUtil;
26 import org.onap.holmes.engine.manager.DroolsEngine;
27 import org.onap.holmes.engine.request.CompileRuleRequest;
28 import org.onap.holmes.engine.request.DeployRuleRequest;
29 import org.onap.holmes.engine.response.CorrelationRuleResponse;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.web.bind.annotation.*;
33 import jakarta.ws.rs.core.MediaType;
34 import java.util.regex.Matcher;
35 import java.util.regex.Pattern;
39 @RequestMapping("/rule")
40 @Api(tags = {"Holmes Engine Management"})
41 public class EngineResources {
42 private Pattern packagePattern = Pattern.compile("package[\\s]+([^;]+)[;\\s]*");
43 private ClosedLoopControlNameCache closedLoopControlNameCache;
44 private DroolsEngine droolsEngine;
47 public void setDroolsEngine(DroolsEngine droolsEngine) {
48 this.droolsEngine = droolsEngine;
52 public void setClosedLoopControlNameCache(ClosedLoopControlNameCache closedLoopControlNameCache) {
53 this.closedLoopControlNameCache = closedLoopControlNameCache;
57 @PutMapping(produces = MediaType.APPLICATION_JSON)
58 public CorrelationRuleResponse deployRule(
59 @ApiParam(value = "The request entity of the HTTP call, which comprises three "
60 + "fields: \"content\" , \"loopControlName\" 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)
63 @RequestBody DeployRuleRequest deployRuleRequest) {
65 CorrelationRuleResponse crResponse = new CorrelationRuleResponse();
67 String packageName = getPackageName(deployRuleRequest.getContent());
68 if (packageName == null) {
69 throw new CorrelationException("Could not find package name in rule: " + deployRuleRequest.getContent());
72 closedLoopControlNameCache
73 .put(packageName, deployRuleRequest.getLoopControlName());
74 String packageNameRet = droolsEngine.deployRule(deployRuleRequest);
75 if (!packageName.equals(packageNameRet)) {
76 log.info("The parsed package name is different from that returned by the engine.");
77 closedLoopControlNameCache.remove(packageName);
78 closedLoopControlNameCache
79 .put(packageNameRet, deployRuleRequest.getLoopControlName());
81 log.info("Rule deployed. Package name: " + packageNameRet);
82 crResponse.setPackageName(packageNameRet);
84 } catch (CorrelationException correlationException) {
85 log.error(correlationException.getMessage(), correlationException);
86 throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
87 } catch (RuntimeException e) {
88 log.error(e.getMessage(), e);
89 throw ExceptionUtil.buildExceptionResponse(e.getMessage());
95 @DeleteMapping(value = "/{packageName}")
96 public void undeployRule(@PathVariable("packageName") String packageName) {
98 droolsEngine.undeployRule(packageName);
99 closedLoopControlNameCache.remove(packageName);
100 } catch (CorrelationException correlationException) {
101 log.error(correlationException.getMessage(), correlationException);
102 throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
107 @ApiOperation(value = "Check the validity of a rule.")
108 public void compileRule(@RequestBody CompileRuleRequest compileRuleRequest) {
110 droolsEngine.compileRule(compileRuleRequest.getContent());
111 } catch (CorrelationException correlationException) {
112 log.error(correlationException.getMessage(), correlationException);
113 throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
117 private String getPackageName(String contents) {
118 Matcher m = packagePattern.matcher(contents);