3480dbbcbe82b4bd5aaa1dc92359e0e149388f05
[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 java.util.Locale;
23 import javax.inject.Inject;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.ws.rs.DELETE;
26 import javax.ws.rs.POST;
27 import javax.ws.rs.PUT;
28 import javax.ws.rs.Path;
29 import javax.ws.rs.PathParam;
30 import javax.ws.rs.Produces;
31 import javax.ws.rs.core.Context;
32 import javax.ws.rs.core.MediaType;
33 import lombok.extern.slf4j.Slf4j;
34 import org.jvnet.hk2.annotations.Service;
35 import org.onap.holmes.common.exception.CorrelationException;
36 import org.onap.holmes.common.utils.ExceptionUtil;
37 import org.onap.holmes.common.utils.LanguageUtil;
38 import org.onap.holmes.engine.manager.DroolsEngine;
39 import org.onap.holmes.engine.request.CompileRuleRequest;
40 import org.onap.holmes.engine.request.DeployRuleRequest;
41 import org.onap.holmes.engine.response.CorrelationRuleResponse;
42
43 @Service
44 @Path("/rule")
45 @Api(tags = {"Engine Manager"})
46 @Produces(MediaType.APPLICATION_JSON)
47 @Slf4j
48 public class EngineResources {
49
50     @Inject
51     DroolsEngine droolsEngine;
52
53     @PUT
54     @ApiOperation(value = "Add rule to Engine and Cache", response = CorrelationRuleResponse.class)
55     @Produces(MediaType.APPLICATION_JSON)
56     @Timed
57     public CorrelationRuleResponse deployRule(DeployRuleRequest deployRuleRequest,
58             @Context HttpServletRequest httpRequest) {
59
60         CorrelationRuleResponse crResponse = new CorrelationRuleResponse();
61         Locale locale = LanguageUtil.getLocale(httpRequest);
62         try {
63
64             String packageName = droolsEngine.deployRule(deployRuleRequest, locale);
65             log.info("Rule deployed. Package name: " + packageName);
66             crResponse.setPackageName(packageName);
67
68         } catch (CorrelationException correlationException) {
69             log.error(correlationException.getMessage(), correlationException);
70             throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
71         }
72
73         return crResponse;
74     }
75
76     @DELETE
77     @ApiOperation(value = "delete rule")
78     @Produces(MediaType.APPLICATION_JSON)
79     @Timed
80     @Path("/{packageName}")
81     public boolean undeployRule(@PathParam("packageName") String packageName,
82             @Context HttpServletRequest httpRequest) {
83
84         Locale locale = LanguageUtil.getLocale(httpRequest);
85
86         try {
87
88             droolsEngine.undeployRule(packageName, locale);
89
90         } catch (CorrelationException correlationException) {
91             log.error(correlationException.getMessage(), correlationException);
92             throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
93         }
94         return true;
95     }
96
97
98     @POST
99     @ApiOperation(value = "compile rule")
100     @Produces(MediaType.APPLICATION_JSON)
101     @Timed
102     public boolean compileRule(CompileRuleRequest compileRuleRequest,
103             @Context HttpServletRequest httpRequest) {
104
105         Locale locale = LanguageUtil.getLocale(httpRequest);
106
107         try {
108             droolsEngine.compileRule(compileRuleRequest.getContent(), locale);
109         } catch (CorrelationException correlationException) {
110             log.error(correlationException.getMessage(), correlationException);
111             throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
112         }
113         return true;
114     }
115 }