Trying to fix the Jenkins Job
[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 lombok.extern.slf4j.Slf4j;
24 import org.jvnet.hk2.annotations.Service;
25 import org.onap.holmes.common.dmaap.store.ClosedLoopControlNameCache;
26 import org.onap.holmes.common.exception.CorrelationException;
27 import org.onap.holmes.common.utils.ExceptionUtil;
28 import org.onap.holmes.common.utils.LanguageUtil;
29 import org.onap.holmes.engine.manager.DroolsEngine;
30 import org.onap.holmes.engine.request.CompileRuleRequest;
31 import org.onap.holmes.engine.request.DeployRuleRequest;
32 import org.onap.holmes.engine.response.CorrelationRuleResponse;
33
34 import javax.inject.Inject;
35 import javax.servlet.http.HttpServletRequest;
36 import javax.ws.rs.*;
37 import javax.ws.rs.core.Context;
38 import javax.ws.rs.core.MediaType;
39 import java.util.Locale;
40 import java.util.regex.Matcher;
41 import java.util.regex.Pattern;
42
43 @Service
44 @Path("/rule")
45 @Api(tags = {"Holmes Engine Management"})
46 @Produces(MediaType.APPLICATION_JSON)
47 @Slf4j
48 public class EngineResources {
49     @Inject
50     DroolsEngine droolsEngine;
51         private Pattern packagePattern = Pattern.compile("package[\\s]+([^;]+)[;\\s]*");
52     private ClosedLoopControlNameCache closedLoopControlNameCache;
53
54     @Inject
55     public void setClosedLoopControlNameCache(ClosedLoopControlNameCache closedLoopControlNameCache) {
56         this.closedLoopControlNameCache = closedLoopControlNameCache;
57     }
58
59     @PUT
60     @Produces(MediaType.APPLICATION_JSON)
61     @Timed
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) {
68
69         CorrelationRuleResponse crResponse = new CorrelationRuleResponse();
70         Locale locale = LanguageUtil.getLocale(httpRequest);
71         try {
72             String packageName = getPackageName(deployRuleRequest.getContent());
73             if(packageName == null) {
74                 throw new CorrelationException("Could not find package name in rule: "+deployRuleRequest.getContent());
75             }
76             
77             closedLoopControlNameCache
78                     .put(packageName, deployRuleRequest.getLoopControlName());
79             String packageNameRet = droolsEngine.deployRule(deployRuleRequest);
80             if (!packageName.equals(packageNameRet)) {
81                 log.info("The parsed package name is different from that returned by the engine.");
82                 closedLoopControlNameCache.remove(packageName);
83                 closedLoopControlNameCache
84                         .put(packageNameRet, deployRuleRequest.getLoopControlName());
85             }
86             log.info("Rule deployed. Package name: " + packageNameRet);
87             crResponse.setPackageName(packageNameRet);
88
89         } catch (CorrelationException correlationException) {
90             log.error(correlationException.getMessage(), correlationException);
91             throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
92         } catch (RuntimeException e) {
93             log.error(e.getMessage(), e);
94             throw ExceptionUtil.buildExceptionResponse(e.getMessage());
95         }
96
97         return crResponse;
98     }
99
100     @DELETE
101     @Produces(MediaType.APPLICATION_JSON)
102     @Timed
103     @Path("/{packageName}")
104     public boolean undeployRule(@PathParam("packageName") String packageName,
105             @Context HttpServletRequest httpRequest) {
106
107         Locale locale = LanguageUtil.getLocale(httpRequest);
108
109         try {
110             droolsEngine.undeployRule(packageName);
111             closedLoopControlNameCache.remove(packageName);
112         } catch (CorrelationException correlationException) {
113             log.error(correlationException.getMessage(), correlationException);
114             throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
115         }
116
117         return true;
118     }
119
120
121     @POST
122     @ApiOperation(value = "Check the validity of a rule.")
123     @Produces(MediaType.APPLICATION_JSON)
124     @Timed
125     public boolean compileRule(CompileRuleRequest compileRuleRequest,
126             @Context HttpServletRequest httpRequest) {
127
128         Locale locale = LanguageUtil.getLocale(httpRequest);
129
130         try {
131             droolsEngine.compileRule(compileRuleRequest.getContent());
132         } catch (CorrelationException correlationException) {
133             log.error(correlationException.getMessage(), correlationException);
134             throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
135         }
136         return true;
137     }
138     
139     private String getPackageName(String contents){
140         Matcher m = packagePattern.matcher(contents);
141         
142         if (m.find( )) {
143            return m.group(1);
144         }else {
145            return null;
146         }
147     }
148 }