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.gso.gui.servicegateway.constant.Constant;
29 import org.openo.gso.gui.servicegateway.service.inf.IServiceGateway;
30 import org.openo.gso.gui.servicegateway.util.json.JsonUtil;
31 import org.openo.gso.gui.servicegateway.util.validate.ValidateUtil;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * ServiceGateway service class.<br/>
41 * @version GSO 0.5 2016/8/4
43 public class ServiceGatewayImpl implements IServiceGateway {
48 private static final Logger LOGGER = LoggerFactory.getLogger(ServiceGatewayImpl.class);
52 * Create service instance.<br/>
54 * @param reqContent content of request
55 * @param httpRequest http request
56 * @throws ServiceException when operate DB or parameter is wrong.
59 @SuppressWarnings("unchecked")
61 public String createService(String reqContent, HttpServletRequest httpRequest) throws ServiceException {
63 if(StringUtils.isEmpty(reqContent))
65 LOGGER.error("ServiceGatewayImpl createService reqContent is null.");
70 Map<String, Object> requestBody = JsonUtil.unMarshal(reqContent, Map.class);
71 Map<String, Object> service = (Map<String, Object>)requestBody.get(Constant.SERVICE_INDENTIFY);
74 service = requestBody;
76 ValidateUtil.assertObjectNotNull(requestBody);
79 String gatewayUri = (String)service.get(Constant.SERVICE_GATEWAY_URI);
80 ValidateUtil.assertStringNotNull(gatewayUri);
81 service.remove(Constant.SERVICE_GATEWAY_URI);
86 RestfulResponse restfulRsp = RestfulFactory.getRestInstance("http").post(gatewayUri,
87 getRestfulParameters(JsonUtil.marshal(requestBody)));
88 if (null != restfulRsp) {
89 // Record the result of registration
90 // (201:success;415:Invalid Parameter;500:Internal Server Error)
91 LOGGER.info("restful call result:", restfulRsp.getStatus());
92 id = restfulRsp.getRespHeaderStr(Constant.SERVICE_ID);
93 id = (null == id) ? restfulRsp.getRespHeaderStr(Constant.NS_INSTANCE_ID) : id;
94 id = (null == id) ? restfulRsp.getRespHeaderStr(Constant.JOB_ID) : id;
96 } catch(ServiceException e) {
97 LOGGER.error("service gateway create restful call result:", e);
105 * get the parameters for restful<br/>
110 * @return the RestfulParametes Instance
111 * @since GSO 0.5, 2016-8-9
113 private static RestfulParametes getRestfulParameters(final String bodyData) {
114 RestfulParametes param = new RestfulParametes();
115 param.putHttpContextHeader(Constant.HEAD_ERMAP_TYPE, Constant.HEAD_ERMAP_VALUE);
116 param.setRawData(bodyData);
121 * Delete service instances.<br/>
123 * @param serviceId service instance ID
124 * @param httpRequest http request
125 * @throws ServiceException operate DB or parameter is wrong.
129 public void deleteService(String serviceId, HttpServletRequest httpRequest) throws ServiceException {