2 * Copyright (c) 2016, Huawei Technologies Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openo.gso.gui.servicegateway.roa.impl;
21 import javax.servlet.http.HttpServletRequest;
22 import javax.ws.rs.core.Response;
24 import org.openo.baseservice.remoteservice.exception.ServiceException;
25 import org.openo.baseservice.util.RestUtils;
26 import org.openo.gso.gui.servicegateway.constant.Constant;
27 import org.openo.gso.gui.servicegateway.exception.HttpCode;
28 import org.openo.gso.gui.servicegateway.roa.inf.IServiceGatewayRoaModule;
29 import org.openo.gso.gui.servicegateway.service.impl.ServiceGatewayImpl;
30 import org.openo.gso.gui.servicegateway.service.inf.IServiceGateway;
31 import org.openo.gso.gui.servicegateway.util.http.ResponseUtils;
32 import org.openo.gso.gui.servicegateway.util.validate.ValidateUtil;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * Implement class for restful interface.<br/>
42 * @version GSO 0.5 2016/8/4
44 public class ServiceGatewayRoaModuleImpl implements IServiceGatewayRoaModule {
49 private static final Logger LOGGER = LoggerFactory.getLogger(ServiceGatewayRoaModuleImpl.class);
54 private IServiceGateway serviceGateway = new ServiceGatewayImpl();
57 * Create service instance.<br/>
59 * @param servletReq http request
61 * @throws ServiceException when operate database or parameter is wrong.
65 public Response createService(HttpServletRequest servletReq) {
66 Map<String, Object> operateStatus = null;
67 Map<String, Object> result = null;
68 String serviceId = null;
70 // 1. Check validation
71 String reqContent = RestUtils.getRequestBody(servletReq);
72 ValidateUtil.assertStringNotNull(reqContent);
75 serviceId = serviceGateway.createService(reqContent, servletReq);
76 } catch(ServiceException exception) {
77 LOGGER.error("Fail to create service instance.");
78 operateStatus = ResponseUtils.setOperateStatus(Constant.RESPONSE_STATUS_FAIL, exception,
79 String.valueOf(exception.getHttpCode()));
80 result = ResponseUtils.setResult(serviceId, operateStatus);
82 return Response.accepted().entity(result).build();
85 operateStatus = ResponseUtils.setOperateStatus(Constant.RESPONSE_STATUS_SUCCESS, null,
86 String.valueOf(HttpCode.RESPOND_OK));
87 result = ResponseUtils.setResult(serviceId, operateStatus);
89 return Response.accepted().entity(result).build();
93 * Delete service instance.<br/>
95 * @param serviceId service instance id
96 * @param servletReq http request
98 * @throws ServiceException when operate database or parameter is wrong.
102 public Response deleteService(String serviceId, HttpServletRequest servletReq) {
103 Map<String, Object> operateStatus = null;
104 Map<String, Object> result = null;
106 // 1. Check validation
107 String reqContent = RestUtils.getRequestBody(servletReq);
108 ValidateUtil.assertStringNotNull(reqContent);
111 serviceGateway.deleteService(serviceId, servletReq);
112 } catch(ServiceException exception) {
113 LOGGER.error("Fail to delete service instance.");
114 operateStatus = ResponseUtils.setOperateStatus(Constant.RESPONSE_STATUS_FAIL, exception,
115 String.valueOf(exception.getHttpCode()));
116 result = ResponseUtils.setResult(serviceId, operateStatus);
118 return Response.accepted().entity(result).build();
121 operateStatus = ResponseUtils.setOperateStatus(Constant.RESPONSE_STATUS_SUCCESS, null,
122 String.valueOf(HttpCode.RESPOND_OK));
123 result = ResponseUtils.setResult(serviceId, operateStatus);
125 return Response.accepted().entity(result).build();