3c47c6579d2481028e7a0a7bf62e1fcdf3c0a2ed
[vnfsdk/refrepo.git] /
1 /*
2  * Copyright (c) 2016, Huawei Technologies Co., Ltd.
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
17 package org.openo.gso.gui.servicegateway.roa.impl;
18
19 import java.util.Map;
20
21 import javax.servlet.http.HttpServletRequest;
22 import javax.ws.rs.core.Response;
23
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;
35
36 /**
37  * Implement class for restful interface.<br/>
38  * <p>
39  * </p>
40  * 
41  * @author
42  * @version GSO 0.5 2016/8/4
43  */
44 public class ServiceGatewayRoaModuleImpl implements IServiceGatewayRoaModule {
45
46     /**
47      * Log server.
48      */
49     private static final Logger LOGGER = LoggerFactory.getLogger(ServiceGatewayRoaModuleImpl.class);
50
51     /**
52      * Service manager.
53      */
54     private IServiceGateway serviceGateway = new ServiceGatewayImpl();
55
56     /**
57      * Create service instance.<br/>
58      * 
59      * @param servletReq http request
60      * @return response
61      * @throws ServiceException when operate database or parameter is wrong.
62      * @since GSO 0.5
63      */
64     @Override
65     public Response createService(HttpServletRequest servletReq) {
66         Map<String, Object> operateStatus = null;
67         Map<String, Object> result = null;
68         String serviceId = null;
69         try {
70             // 1. Check validation
71             String reqContent = RestUtils.getRequestBody(servletReq);
72             ValidateUtil.assertStringNotNull(reqContent);
73
74             // 2. Create service
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);
81
82             return Response.accepted().entity(result).build();
83         }
84
85         operateStatus = ResponseUtils.setOperateStatus(Constant.RESPONSE_STATUS_SUCCESS, null,
86                 String.valueOf(HttpCode.RESPOND_OK));
87         result = ResponseUtils.setResult(serviceId, operateStatus);
88
89         return Response.accepted().entity(result).build();
90     }
91
92     /**
93      * Delete service instance.<br/>
94      * 
95      * @param serviceId service instance id
96      * @param servletReq http request
97      * @return response
98      * @throws ServiceException when operate database or parameter is wrong.
99      * @since GSO 0.5
100      */
101     @Override
102     public Response deleteService(String serviceId, HttpServletRequest servletReq) {
103         Map<String, Object> operateStatus = null;
104         Map<String, Object> result = null;
105         try {
106             // 1. Check validation
107             String reqContent = RestUtils.getRequestBody(servletReq);
108             ValidateUtil.assertStringNotNull(reqContent);
109
110             // 2. Delete service
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);
117
118             return Response.accepted().entity(result).build();
119         }
120
121         operateStatus = ResponseUtils.setOperateStatus(Constant.RESPONSE_STATUS_SUCCESS, null,
122                 String.valueOf(HttpCode.RESPOND_OK));
123         result = ResponseUtils.setResult(serviceId, operateStatus);
124
125         return Response.accepted().entity(result).build();
126     }
127
128         public IServiceGateway getServiceGateway() 
129         {
130                 return serviceGateway;
131         }
132
133         public void setServiceGateway(IServiceGateway serviceGateway) 
134         {
135                 this.serviceGateway = serviceGateway;
136         }
137     
138 }