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 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 {
56 DroolsEngine droolsEngine;
59 @ApiOperation(value = "Deploy a rule into the Drools engine.", response = CorrelationRuleResponse.class)
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 DmaapService.loopControlNames
74 .put(packageName, deployRuleRequest.getLoopControlName());
75 String packageNameRet = droolsEngine.deployRule(deployRuleRequest, locale);
76 if (!packageName.equals(packageNameRet)) {
77 log.info("The parsed package name is different from that returned by the engine.");
78 DmaapService.loopControlNames.remove(packageName);
79 DmaapService.loopControlNames
80 .put(packageNameRet, deployRuleRequest.getLoopControlName());
82 log.info("Rule deployed. Package name: " + packageName);
83 crResponse.setPackageName(packageName);
85 } catch (CorrelationException correlationException) {
86 log.error(correlationException.getMessage(), correlationException);
87 throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
88 } catch (RuntimeException e) {
89 log.error(e.getMessage(), e);
90 throw ExceptionUtil.buildExceptionResponse(e.getMessage());
97 @ApiOperation(value = "Undeploy a rule from the Drools engine.")
98 @Produces(MediaType.APPLICATION_JSON)
100 @Path("/{packageName}")
101 public boolean undeployRule(@PathParam("packageName") String packageName,
102 @Context HttpServletRequest httpRequest) {
104 Locale locale = LanguageUtil.getLocale(httpRequest);
107 droolsEngine.undeployRule(packageName, locale);
108 DmaapService.loopControlNames.remove(packageName);
109 } catch (CorrelationException correlationException) {
110 log.error(correlationException.getMessage(), correlationException);
111 throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
118 @ApiOperation(value = "Check the validity of a rule.")
119 @Produces(MediaType.APPLICATION_JSON)
121 public boolean compileRule(CompileRuleRequest compileRuleRequest,
122 @Context HttpServletRequest httpRequest) {
124 Locale locale = LanguageUtil.getLocale(httpRequest);
127 droolsEngine.compileRule(compileRuleRequest.getContent(), locale);
128 } catch (CorrelationException correlationException) {
129 log.error(correlationException.getMessage(), correlationException);
130 throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
135 private String getPackageName(String contents){
136 String ret = contents.trim();
137 ret = ret.substring(7, ret.indexOf("import")).trim();
138 return ret.endsWith(";") ? ret.substring(0, ret.length() - 1) : ret;