0a70850c91a7e4b3a00da83e3dfe4594375b6b56
[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.service.impl;
18
19 import java.util.Map;
20
21 import javax.servlet.http.HttpServletRequest;
22
23 import org.apache.commons.lang.StringUtils;
24 import org.openo.baseservice.remoteservice.exception.ServiceException;
25 import org.openo.baseservice.roa.util.restclient.RestfulFactory;
26 import org.openo.baseservice.roa.util.restclient.RestfulParametes;
27 import org.openo.baseservice.roa.util.restclient.RestfulResponse;
28 import org.openo.baseservice.util.RestUtils;
29 import org.openo.gso.gui.servicegateway.constant.Constant;
30 import org.openo.gso.gui.servicegateway.service.inf.IServiceGateway;
31 import org.openo.gso.gui.servicegateway.util.json.JsonUtil;
32 import org.openo.gso.gui.servicegateway.util.validate.ValidateUtil;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * ServiceGateway service class.<br/>
38  * <p>
39  * </p>
40  * 
41  * @author
42  * @version GSO 0.5 2016/8/4
43  */
44 public class ServiceGatewayImpl implements IServiceGateway {
45
46     /**
47      * Log service.
48      */
49     private static final Logger LOGGER = LoggerFactory.getLogger(ServiceGatewayImpl.class);
50
51
52     /**
53      * Create service instance.<br/>
54      * 
55      * @param reqContent content of request
56      * @param httpRequest http request
57      * @throws ServiceException when operate DB or parameter is wrong.
58      * @since GSO 0.5
59      */
60     @SuppressWarnings("unchecked")
61     @Override
62     public String createService(String reqContent, HttpServletRequest httpRequest) throws ServiceException {
63         // check the value
64         if(StringUtils.isEmpty(reqContent))
65         {
66                 LOGGER.error("ServiceGatewayImpl createService reqContent is null.");
67                 throw new ServiceException("ServiceGatewayImpl createService reqContent is null.");             
68         }
69         
70         // Parse request
71         Map<String, Object> requestBody = JsonUtil.unMarshal(reqContent, Map.class);
72         Map<String, Object> service = (Map<String, Object>)requestBody.get(Constant.SERVICE_INDENTIFY);
73         if(null == service)
74         {
75                 service = requestBody;
76         }
77         ValidateUtil.assertObjectNotNull(requestBody);
78
79         // Validate data
80         String gatewayUri = (String)service.get(Constant.SERVICE_GATEWAY_URI);
81         ValidateUtil.assertStringNotNull(gatewayUri);
82         service.remove(Constant.SERVICE_GATEWAY_URI);
83
84         // call the restful 
85         String id = null;
86         try {
87                 RestfulResponse restfulRsp = RestfulFactory.getRestInstance("http").post(gatewayUri,
88                     getRestfulParameters(JsonUtil.marshal(requestBody)));
89             if (null != restfulRsp) {
90                 // Record the result of registration
91                 // (201:success;415:Invalid Parameter;500:Internal Server Error)
92                 LOGGER.info("restful call result:", restfulRsp.getStatus());
93                 id = restfulRsp.getRespHeaderStr(Constant.SERVICE_ID);
94                 id = (null == id) ? restfulRsp.getRespHeaderStr(Constant.NS_INSTANCE_ID) : id;
95                 id = (null == id) ? restfulRsp.getRespHeaderStr(Constant.JOB_ID) : id;
96             }
97         } catch(ServiceException e) {
98                 LOGGER.error("service gateway create restful call result:", e);
99             throw e;
100         }
101
102         return id;
103     }
104     
105     /**
106      * get the parameters for restful<br/>
107      * 
108      * @author
109      * @param bodyData
110      *            Json Body
111      * @return the RestfulParametes Instance
112      * @since GSO 0.5, 2016-8-9
113      */
114     private static RestfulParametes getRestfulParameters(final String bodyData) {
115         RestfulParametes param = new RestfulParametes();
116         param.putHttpContextHeader(Constant.HEAD_ERMAP_TYPE, Constant.HEAD_ERMAP_VALUE);
117         param.setRawData(bodyData);
118         return param;
119     }
120
121     /**
122      * Delete service instances.<br/>
123      * 
124      * @param serviceId service instance ID
125      * @param httpRequest http request
126      * @throws ServiceException operate DB or parameter is wrong.
127      * @since GSO 0.5
128      */
129     @Override
130     public void deleteService(String serviceId, HttpServletRequest httpRequest) throws ServiceException {
131         if(httpRequest == null)
132         {               
133                 LOGGER.error("ServiceGatewayImpl.deleteService httpRequest is null");
134                 throw new ServiceException("ServiceGatewayImpl.deleteService httpRequest is null");
135         }
136         // Parse request
137         String reqContent = RestUtils.getRequestBody(httpRequest);
138         Map<String, Object> requestBody = JsonUtil.unMarshal(reqContent, Map.class);
139         Map<String, Object> service = (Map<String, Object>)requestBody.get(Constant.SERVICE_INDENTIFY);
140         if(null == service)
141         {
142             service = requestBody;
143         }
144         ValidateUtil.assertObjectNotNull(requestBody);
145
146         // Validate data
147         String gatewayUri = (String)service.get(Constant.SERVICE_GATEWAY_URI);
148         ValidateUtil.assertStringNotNull(gatewayUri);
149         service.remove(Constant.SERVICE_GATEWAY_URI);
150
151         String operation = (String) service.get(Constant.SERVICE_OPERATION);
152         ValidateUtil.assertStringNotNull(operation);
153         service.remove(Constant.SERVICE_OPERATION);
154
155         // call the restful
156         try {
157             RestfulResponse restfulRsp = null;
158             if(Constant.SERVICE_DELETE_OPERATION.equalsIgnoreCase(operation)) {
159                 restfulRsp = RestfulFactory.getRestInstance("http").delete(gatewayUri,
160                         getRestfulParameters(JsonUtil.marshal(requestBody)));
161             } else {
162                 restfulRsp = RestfulFactory.getRestInstance("http").post(gatewayUri,
163                         getRestfulParameters(JsonUtil.marshal(requestBody)));
164             }
165             if (null != restfulRsp) {
166                 LOGGER.info("restful call result:", restfulRsp.getStatus());
167             }
168         } catch(ServiceException e) {
169             LOGGER.error("service gateway delete restful call result:", e);
170             throw e;
171         }
172     }
173
174 }