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.service.impl;
21 import javax.servlet.http.HttpServletRequest;
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;
37 * ServiceGateway service class.<br/>
42 * @version GSO 0.5 2016/8/4
44 public class ServiceGatewayImpl implements IServiceGateway {
49 private static final Logger LOGGER = LoggerFactory.getLogger(ServiceGatewayImpl.class);
53 * Create service instance.<br/>
55 * @param reqContent content of request
56 * @param httpRequest http request
57 * @throws ServiceException when operate DB or parameter is wrong.
60 @SuppressWarnings("unchecked")
62 public String createService(String reqContent, HttpServletRequest httpRequest) throws ServiceException {
64 if(StringUtils.isEmpty(reqContent))
66 LOGGER.error("ServiceGatewayImpl createService reqContent is null.");
71 Map<String, Object> requestBody = JsonUtil.unMarshal(reqContent, Map.class);
72 Map<String, Object> service = (Map<String, Object>)requestBody.get(Constant.SERVICE_INDENTIFY);
75 service = requestBody;
77 ValidateUtil.assertObjectNotNull(requestBody);
80 String gatewayUri = (String)service.get(Constant.SERVICE_GATEWAY_URI);
81 ValidateUtil.assertStringNotNull(gatewayUri);
82 service.remove(Constant.SERVICE_GATEWAY_URI);
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;
97 } catch(ServiceException e) {
98 LOGGER.error("service gateway create restful call result:", e);
106 * get the parameters for restful<br/>
111 * @return the RestfulParametes Instance
112 * @since GSO 0.5, 2016-8-9
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);
122 * Delete service instances.<br/>
124 * @param serviceId service instance ID
125 * @param httpRequest http request
126 * @throws ServiceException operate DB or parameter is wrong.
130 public void deleteService(String serviceId, HttpServletRequest httpRequest) throws ServiceException {
132 String reqContent = RestUtils.getRequestBody(httpRequest);
133 Map<String, Object> requestBody = JsonUtil.unMarshal(reqContent, Map.class);
134 Map<String, Object> service = (Map<String, Object>)requestBody.get(Constant.SERVICE_INDENTIFY);
137 service = requestBody;
139 ValidateUtil.assertObjectNotNull(requestBody);
142 String gatewayUri = (String)service.get(Constant.SERVICE_GATEWAY_URI);
143 ValidateUtil.assertStringNotNull(gatewayUri);
144 service.remove(Constant.SERVICE_GATEWAY_URI);
146 String operation = (String) service.get(Constant.SERVICE_OPERATION);
147 ValidateUtil.assertStringNotNull(operation);
148 service.remove(Constant.SERVICE_OPERATION);
152 RestfulResponse restfulRsp = null;
153 if(Constant.SERVICE_DELETE_OPERATION.equalsIgnoreCase(operation)) {
154 restfulRsp = RestfulFactory.getRestInstance("http").delete(gatewayUri,
155 getRestfulParameters(JsonUtil.marshal(requestBody)));
157 restfulRsp = RestfulFactory.getRestInstance("http").post(gatewayUri,
158 getRestfulParameters(JsonUtil.marshal(requestBody)));
160 if (null != restfulRsp) {
161 LOGGER.info("restful call result:", restfulRsp.getStatus());
163 } catch(ServiceException e) {
164 LOGGER.error("service gateway delete restful call result:", e);