Change the method returns the result
[holmes/engine-management.git] / engine-d / src / main / java / org / openo / 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.openo.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.io.IOException;
23 import java.util.Locale;
24 import javax.annotation.PostConstruct;
25 import javax.inject.Inject;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.ws.rs.DELETE;
28 import javax.ws.rs.POST;
29 import javax.ws.rs.PUT;
30 import javax.ws.rs.Path;
31 import javax.ws.rs.PathParam;
32 import javax.ws.rs.Produces;
33 import javax.ws.rs.core.Context;
34 import javax.ws.rs.core.MediaType;
35 import lombok.extern.slf4j.Slf4j;
36 import org.jvnet.hk2.annotations.Service;
37 import org.openo.holmes.common.api.entity.ServiceRegisterEntity;
38 import org.openo.holmes.common.config.MicroServiceConfig;
39 import org.openo.holmes.common.exception.CorrelationException;
40 import org.openo.holmes.common.utils.ExceptionUtil;
41 import org.openo.holmes.common.utils.LanguageUtil;
42 import org.openo.holmes.common.utils.MSBRegisterUtil;
43 import org.openo.holmes.engine.manager.DroolsEngine;
44 import org.openo.holmes.engine.request.CompileRuleRequest;
45 import org.openo.holmes.engine.request.DeployRuleRequest;
46 import org.openo.holmes.engine.response.CorrelationRuleResponse;
47
48 @Service
49 @Path("/rule")
50 @Api(tags = {"Engine Manager"})
51 @Produces(MediaType.APPLICATION_JSON)
52 @Slf4j
53 public class EngineResources {
54
55     @Inject
56     DroolsEngine droolsEngine;
57
58     @Inject
59     MSBRegisterUtil msbRegisterUtil;
60
61     @PostConstruct
62     private void initMsbService() {
63         try {
64             msbRegisterUtil.register(initServiceEntity());
65         } catch (IOException e) {
66             log.warn("Failed register msb" + e.getMessage(), e);
67         }
68     }
69
70     @PUT
71     @ApiOperation(value = "Add rule to Engine and Cache", response = CorrelationRuleResponse.class)
72     @Produces(MediaType.APPLICATION_JSON)
73     @Timed
74     public CorrelationRuleResponse deployRule(DeployRuleRequest deployRuleRequest,
75             @Context HttpServletRequest httpRequest) {
76
77         CorrelationRuleResponse crResponse = new CorrelationRuleResponse();
78         Locale locale = LanguageUtil.getLocale(httpRequest);
79         try {
80
81             String packageName = droolsEngine.deployRule(deployRuleRequest, locale);
82             log.info("Rule deployed. Package name: " + packageName);
83             crResponse.setPackageName(packageName);
84
85         } catch (CorrelationException correlationException) {
86             log.error(correlationException.getMessage(), correlationException);
87             throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
88         }
89
90         return crResponse;
91     }
92
93     @DELETE
94     @ApiOperation(value = "delete rule")
95     @Produces(MediaType.APPLICATION_JSON)
96     @Timed
97     @Path("/{packageName}")
98     public boolean undeployRule(@PathParam("packageName") String packageName,
99             @Context HttpServletRequest httpRequest) {
100
101         Locale locale = LanguageUtil.getLocale(httpRequest);
102
103         try {
104
105             droolsEngine.undeployRule(packageName, locale);
106
107         } catch (CorrelationException correlationException) {
108             log.error(correlationException.getMessage(), correlationException);
109             throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
110         }
111         return true;
112     }
113
114
115     @POST
116     @ApiOperation(value = "compile rule")
117     @Produces(MediaType.APPLICATION_JSON)
118     @Timed
119     public boolean compileRule(CompileRuleRequest compileRuleRequest,
120             @Context HttpServletRequest httpRequest) {
121
122         Locale locale = LanguageUtil.getLocale(httpRequest);
123
124         try {
125             droolsEngine.compileRule(compileRuleRequest.getContent(), locale);
126         } catch (CorrelationException correlationException) {
127             log.error(correlationException.getMessage(), correlationException);
128             throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
129         }
130         return true;
131     }
132
133     private ServiceRegisterEntity initServiceEntity() {
134         ServiceRegisterEntity serviceRegisterEntity = new ServiceRegisterEntity();
135         serviceRegisterEntity.setServiceName("holmes-engine");
136         serviceRegisterEntity.setProtocol("REST");
137         serviceRegisterEntity.setVersion("v1");
138         serviceRegisterEntity.setUrl("/api/holmes-engine/v1");
139         serviceRegisterEntity.setSingleNode(MicroServiceConfig.getServiceIp(), "9102", 0);
140         serviceRegisterEntity.setVisualRange("1|0");
141         return serviceRegisterEntity;
142     }
143 }