Add DMaaP Configuration APIs for Testing
[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 javax.inject.Inject;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.ws.rs.DELETE;
27 import javax.ws.rs.POST;
28 import javax.ws.rs.PUT;
29 import javax.ws.rs.Path;
30 import javax.ws.rs.PathParam;
31 import javax.ws.rs.Produces;
32 import javax.ws.rs.core.Context;
33 import javax.ws.rs.core.MediaType;
34 import lombok.extern.slf4j.Slf4j;
35 import org.jvnet.hk2.annotations.Service;
36 import org.onap.holmes.common.dmaap.DmaapService;
37 import org.onap.holmes.common.exception.CorrelationException;
38 import org.onap.holmes.common.utils.ExceptionUtil;
39 import org.onap.holmes.common.utils.LanguageUtil;
40 import org.onap.holmes.engine.manager.DroolsEngine;
41 import org.onap.holmes.engine.request.CompileRuleRequest;
42 import org.onap.holmes.engine.request.DeployRuleRequest;
43 import org.onap.holmes.engine.response.CorrelationRuleResponse;
44
45 @Service
46 @Path("/rule")
47 @Api(tags = {"Holmes Engine Management"})
48 @Produces(MediaType.APPLICATION_JSON)
49 @Slf4j
50 public class EngineResources {
51
52     @Inject
53     DroolsEngine droolsEngine;
54
55     @PUT
56     @ApiOperation(value = "Deploy a rule into the Drools engine.", response = CorrelationRuleResponse.class)
57     @Produces(MediaType.APPLICATION_JSON)
58     @Timed
59     public CorrelationRuleResponse deployRule(
60             @ApiParam(value = "The request entity of the HTTP call, which comprises three "
61                     + "fields: \"content\" , \"loopcontrolname\" and \"engineid\". "
62                     + "The \"content\" should be a valid Drools rule string and the \"engineid\" "
63                     + "has to be \"engine-d\" in the Amsterdam release.", required = true) DeployRuleRequest deployRuleRequest,
64             @Context HttpServletRequest httpRequest) {
65
66         CorrelationRuleResponse crResponse = new CorrelationRuleResponse();
67         Locale locale = LanguageUtil.getLocale(httpRequest);
68         try {
69             String packageName = droolsEngine.deployRule(deployRuleRequest, locale);
70             DmaapService.loopControlNames
71                     .put(packageName, deployRuleRequest.getLoopControlName());
72             log.info("Rule deployed. Package name: " + packageName);
73             crResponse.setPackageName(packageName);
74
75         } catch (CorrelationException correlationException) {
76             log.error(correlationException.getMessage(), correlationException);
77             throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
78         }
79
80         return crResponse;
81     }
82
83     @DELETE
84     @ApiOperation(value = "Undeploy a rule from the Drools engine.")
85     @Produces(MediaType.APPLICATION_JSON)
86     @Timed
87     @Path("/{packageName}")
88     public boolean undeployRule(@PathParam("packageName") String packageName,
89             @Context HttpServletRequest httpRequest) {
90
91         Locale locale = LanguageUtil.getLocale(httpRequest);
92
93         try {
94
95             droolsEngine.undeployRule(packageName, locale);
96
97         } catch (CorrelationException correlationException) {
98             log.error(correlationException.getMessage(), correlationException);
99             throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
100         }
101         return true;
102     }
103
104
105     @POST
106     @ApiOperation(value = "Check the validity of a rule.")
107     @Produces(MediaType.APPLICATION_JSON)
108     @Timed
109     public boolean compileRule(CompileRuleRequest compileRuleRequest,
110             @Context HttpServletRequest httpRequest) {
111
112         Locale locale = LanguageUtil.getLocale(httpRequest);
113
114         try {
115             droolsEngine.compileRule(compileRuleRequest.getContent(), locale);
116         } catch (CorrelationException correlationException) {
117             log.error(correlationException.getMessage(), correlationException);
118             throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
119         }
120         return true;
121     }
122 }