Fixed issue in extracting package name from rule
[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.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;
29
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;
50
51 @Service
52 @Path("/rule")
53 @Api(tags = {"Holmes Engine Management"})
54 @Produces(MediaType.APPLICATION_JSON)
55 @Slf4j
56 public class EngineResources {
57         private Pattern packagePattern = Pattern.compile("package[\\s]+([^;]+)[;\\s]*");
58     
59     @Inject
60     DroolsEngine droolsEngine;
61
62     @PUT
63     @ApiOperation(value = "Deploy a rule into the Drools engine.", response = CorrelationRuleResponse.class)
64     @Produces(MediaType.APPLICATION_JSON)
65     @Timed
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) {
72
73         CorrelationRuleResponse crResponse = new CorrelationRuleResponse();
74         Locale locale = LanguageUtil.getLocale(httpRequest);
75         try {
76             String packageName = getPackageName(deployRuleRequest.getContent());
77             if(packageName == null) {
78                 throw new CorrelationException("Could not find package name in rule: "+deployRuleRequest.getContent());
79             }
80             
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());
89             }
90             log.info("Rule deployed. Package name: " + packageNameRet);
91             crResponse.setPackageName(packageNameRet);
92
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());
99         }
100
101         return crResponse;
102     }
103
104     @DELETE
105     @ApiOperation(value = "Undeploy a rule from the Drools engine.")
106     @Produces(MediaType.APPLICATION_JSON)
107     @Timed
108     @Path("/{packageName}")
109     public boolean undeployRule(@PathParam("packageName") String packageName,
110             @Context HttpServletRequest httpRequest) {
111
112         Locale locale = LanguageUtil.getLocale(httpRequest);
113
114         try {
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());
120         }
121         return true;
122     }
123
124
125     @POST
126     @ApiOperation(value = "Check the validity of a rule.")
127     @Produces(MediaType.APPLICATION_JSON)
128     @Timed
129     public boolean compileRule(CompileRuleRequest compileRuleRequest,
130             @Context HttpServletRequest httpRequest) {
131
132         Locale locale = LanguageUtil.getLocale(httpRequest);
133
134         try {
135             droolsEngine.compileRule(compileRuleRequest.getContent(), locale);
136         } catch (CorrelationException correlationException) {
137             log.error(correlationException.getMessage(), correlationException);
138             throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
139         }
140         return true;
141     }
142     
143     private String getPackageName(String contents){
144         Matcher m = packagePattern.matcher(contents);
145         
146         if (m.find( )) {
147            return m.group(1);
148         }else {
149            return null;
150         }
151     }
152 }