update link to upper-constraints.txt
[holmes/engine-management.git] / engine-d / src / main / java / org / onap / holmes / engine / resources / EngineResources.java
1 /**
2  * Copyright 2017 ZTE Corporation.
3  * <p>
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
7  * <p>
8  * http://www.apache.org/licenses/LICENSE-2.0
9  * <p>
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.
15  */
16 package org.onap.holmes.engine.resources;
17
18
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.*;
32
33 import jakarta.ws.rs.core.MediaType;
34 import java.util.regex.Matcher;
35 import java.util.regex.Pattern;
36
37 @Slf4j
38 @RestController
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;
45
46     @Autowired
47     public void setDroolsEngine(DroolsEngine droolsEngine) {
48         this.droolsEngine = droolsEngine;
49     }
50
51     @Autowired
52     public void setClosedLoopControlNameCache(ClosedLoopControlNameCache closedLoopControlNameCache) {
53         this.closedLoopControlNameCache = closedLoopControlNameCache;
54     }
55
56     @ResponseBody
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) {
64
65         CorrelationRuleResponse crResponse = new CorrelationRuleResponse();
66         try {
67             String packageName = getPackageName(deployRuleRequest.getContent());
68             if (packageName == null) {
69                 throw new CorrelationException("Could not find package name in rule: " + deployRuleRequest.getContent());
70             }
71
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());
80             }
81             log.info("Rule deployed. Package name: " + packageNameRet);
82             crResponse.setPackageName(packageNameRet);
83
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());
90         }
91
92         return crResponse;
93     }
94
95     @DeleteMapping(value = "/{packageName}")
96     public void undeployRule(@PathVariable("packageName") String packageName) {
97         try {
98             droolsEngine.undeployRule(packageName);
99             closedLoopControlNameCache.remove(packageName);
100         } catch (CorrelationException correlationException) {
101             log.error(correlationException.getMessage(), correlationException);
102             throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
103         }
104     }
105
106     @PostMapping
107     @ApiOperation(value = "Check the validity of a rule.")
108     public void compileRule(@RequestBody CompileRuleRequest compileRuleRequest) {
109         try {
110             droolsEngine.compileRule(compileRuleRequest.getContent());
111         } catch (CorrelationException correlationException) {
112             log.error(correlationException.getMessage(), correlationException);
113             throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
114         }
115     }
116
117     private String getPackageName(String contents) {
118         Matcher m = packagePattern.matcher(contents);
119
120         if (m.find()) {
121             return m.group(1);
122         } else {
123             return null;
124         }
125     }
126 }