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