Optimized Rule Deployment Logic
[holmes/engine-management.git] / engine-d / src / main / java / org / onap / holmes / engine / resources / EngineResources.java
1 /**
2  * Copyright 2017 ZTE Corporation.
3  *
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  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 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.util.Locale;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
26
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;
47
48 @Service
49 @Path("/rule")
50 @Api(tags = {"Holmes Engine Management"})
51 @Produces(MediaType.APPLICATION_JSON)
52 @Slf4j
53 public class EngineResources {
54         private Pattern packagePattern = Pattern.compile("package[\\s]+([^;]+)[;\\s]*");
55     
56     @Inject
57     DroolsEngine droolsEngine;
58
59     @PUT
60     @ApiOperation(value = "Deploy a rule into the Drools engine.", response = CorrelationRuleResponse.class)
61     @Produces(MediaType.APPLICATION_JSON)
62     @Timed
63     public CorrelationRuleResponse deployRule(
64             @ApiParam(value = "The request entity of the HTTP call, which comprises three "
65                     + "fields: \"content\" , \"loopControlName\" and \"engineId\". "
66                     + "The \"content\" should be a valid Drools rule string and the \"engineId\" "
67                     + "has to be \"engine-d\" in the Amsterdam release.", required = true) DeployRuleRequest deployRuleRequest,
68             @Context HttpServletRequest httpRequest) {
69
70         CorrelationRuleResponse crResponse = new CorrelationRuleResponse();
71         Locale locale = LanguageUtil.getLocale(httpRequest);
72         try {
73             String packageName = getPackageName(deployRuleRequest.getContent());
74             if(packageName == null) {
75                 throw new CorrelationException("Could not find package name in rule: "+deployRuleRequest.getContent());
76             }
77             
78             DmaapService.loopControlNames
79                     .put(packageName, deployRuleRequest.getLoopControlName());
80             String packageNameRet = droolsEngine.deployRule(deployRuleRequest);
81             if (!packageName.equals(packageNameRet)) {
82                 log.info("The parsed package name is different from that returned by the engine.");
83                 DmaapService.loopControlNames.remove(packageName);
84                 DmaapService.loopControlNames
85                         .put(packageNameRet, deployRuleRequest.getLoopControlName());
86             }
87             log.info("Rule deployed. Package name: " + packageNameRet);
88             crResponse.setPackageName(packageNameRet);
89
90         } catch (CorrelationException correlationException) {
91             log.error(correlationException.getMessage(), correlationException);
92             throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
93         } catch (RuntimeException e) {
94             log.error(e.getMessage(), e);
95             throw ExceptionUtil.buildExceptionResponse(e.getMessage());
96         }
97
98         return crResponse;
99     }
100
101     @DELETE
102     @ApiOperation(value = "Undeploy a rule from the Drools engine.")
103     @Produces(MediaType.APPLICATION_JSON)
104     @Timed
105     @Path("/{packageName}")
106     public boolean undeployRule(@PathParam("packageName") String packageName,
107             @Context HttpServletRequest httpRequest) {
108
109         Locale locale = LanguageUtil.getLocale(httpRequest);
110
111         try {
112             droolsEngine.undeployRule(packageName);
113             DmaapService.loopControlNames.remove(packageName);
114         } catch (CorrelationException correlationException) {
115             log.error(correlationException.getMessage(), correlationException);
116             throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
117         }
118
119         return true;
120     }
121
122
123     @POST
124     @ApiOperation(value = "Check the validity of a rule.")
125     @Produces(MediaType.APPLICATION_JSON)
126     @Timed
127     public boolean compileRule(CompileRuleRequest compileRuleRequest,
128             @Context HttpServletRequest httpRequest) {
129
130         Locale locale = LanguageUtil.getLocale(httpRequest);
131
132         try {
133             droolsEngine.compileRule(compileRuleRequest.getContent());
134         } catch (CorrelationException correlationException) {
135             log.error(correlationException.getMessage(), correlationException);
136             throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
137         }
138         return true;
139     }
140     
141     private String getPackageName(String contents){
142         Matcher m = packagePattern.matcher(contents);
143         
144         if (m.find( )) {
145            return m.group(1);
146         }else {
147            return null;
148         }
149     }
150 }