77a9ec7dc61db685e31889782a63e1810e7988b1
[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             LOGGER.info("Received a request form the NBI the reqContent is :"+ reqContent);
74
75             // 2. Create service
76             serviceId = serviceGateway.createService(reqContent, servletReq);
77         } catch(ServiceException exception) {
78             LOGGER.error("Fail to create service instance.");
79             operateStatus = ResponseUtils.setOperateStatus(Constant.RESPONSE_STATUS_FAIL, exception,
80                     String.valueOf(exception.getHttpCode()));            
81             result = ResponseUtils.setResult(serviceId, operateStatus);
82
83             return Response.accepted().entity(result).build();
84         }
85
86         operateStatus = ResponseUtils.setOperateStatus(Constant.RESPONSE_STATUS_SUCCESS, null,
87                 String.valueOf(HttpCode.RESPOND_OK));
88         result = ResponseUtils.setResult(serviceId, operateStatus);
89
90         return Response.accepted().entity(result).build();
91     }
92
93     /**
94      * Delete service instance.<br/>
95      * 
96      * @param serviceId service instance id
97      * @param servletReq http request
98      * @return response
99      * @throws ServiceException when operate database or parameter is wrong.
100      * @since GSO 0.5
101      */
102     @Override
103     public Response deleteService(String serviceId, HttpServletRequest servletReq) {
104         Map<String, Object> operateStatus = null;
105         Map<String, Object> result = null;
106         try {
107             // 1. Check validation
108             String reqContent = RestUtils.getRequestBody(servletReq);
109             ValidateUtil.assertStringNotNull(reqContent);
110
111             // 2. Delete service
112             serviceGateway.deleteService(serviceId, servletReq);
113         } catch(ServiceException exception) {
114             LOGGER.error("Fail to delete service instance.");
115             operateStatus = ResponseUtils.setOperateStatus(Constant.RESPONSE_STATUS_FAIL, exception,
116                     String.valueOf(exception.getHttpCode()));
117             result = ResponseUtils.setResult(serviceId, operateStatus);
118
119             return Response.accepted().entity(result).build();
120         }
121
122         operateStatus = ResponseUtils.setOperateStatus(Constant.RESPONSE_STATUS_SUCCESS, null,
123                 String.valueOf(HttpCode.RESPOND_OK));
124         result = ResponseUtils.setResult(serviceId, operateStatus);
125
126         return Response.accepted().entity(result).build();
127     }
128
129         public IServiceGateway getServiceGateway() 
130         {
131                 return serviceGateway;
132         }
133
134         public void setServiceGateway(IServiceGateway serviceGateway) 
135         {
136                 this.serviceGateway = serviceGateway;
137         }
138     
139 }