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