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.");
67 throw new ServiceException("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 LOGGER.info("Send the cretation RESTful request to orchestrator.The Body is"+ requestBody.toString());
88 RestfulResponse restfulRsp = RestfulFactory.getRestInstance("http").post(gatewayUri,
89 getRestfulParameters(JsonUtil.marshal(requestBody)));
90 LOGGER.info("Receive the cretation RESTful response from orchestrator.The status is:"+restfulRsp.getStatus()+" the content is:"+
91 restfulRsp.getResponseContent());
92 if (null != restfulRsp) {
93 // Record the result of registration
94 // (201:success;415:Invalid Parameter;500:Internal Server Error)
95 LOGGER.info("restful call result:"+ restfulRsp.getStatus());
96 id = restfulRsp.getRespHeaderStr(Constant.SERVICE_ID);
97 id = (null == id) ? restfulRsp.getRespHeaderStr(Constant.NS_INSTANCE_ID) : id;
98 id = (null == id) ? restfulRsp.getRespHeaderStr(Constant.JOB_ID) : id;
100 } catch(ServiceException e) {
101 LOGGER.error("service gateway create restful call result:", e);
109 * get the parameters for restful<br/>
114 * @return the RestfulParametes Instance
115 * @since GSO 0.5, 2016-8-9
117 private static RestfulParametes getRestfulParameters(final String bodyData) {
118 RestfulParametes param = new RestfulParametes();
119 param.putHttpContextHeader(Constant.HEAD_ERMAP_TYPE, Constant.HEAD_ERMAP_VALUE);
120 param.setRawData(bodyData);
125 * Delete service instances.<br/>
127 * @param serviceId service instance ID
128 * @param httpRequest http request
129 * @throws ServiceException operate DB or parameter is wrong.
133 public void deleteService(String serviceId, HttpServletRequest httpRequest) throws ServiceException {
134 if(httpRequest == null)
136 LOGGER.error("ServiceGatewayImpl.deleteService httpRequest is null");
137 throw new ServiceException("ServiceGatewayImpl.deleteService httpRequest is null");
140 String reqContent = RestUtils.getRequestBody(httpRequest);
141 Map<String, Object> requestBody = JsonUtil.unMarshal(reqContent, Map.class);
142 ValidateUtil.assertObjectNotNull(requestBody);
145 String gatewayUri = (String)requestBody.get(Constant.SERVICE_GATEWAY_URI);
146 ValidateUtil.assertStringNotNull(gatewayUri);
147 requestBody.remove(Constant.SERVICE_GATEWAY_URI);
149 String operation = (String) requestBody.get(Constant.SERVICE_OPERATION);
150 ValidateUtil.assertStringNotNull(operation);
151 requestBody.remove(Constant.SERVICE_OPERATION);
155 RestfulResponse restfulRsp = null;
156 if(Constant.SERVICE_DELETE_OPERATION.equalsIgnoreCase(operation)) {
157 restfulRsp = RestfulFactory.getRestInstance("http").delete(gatewayUri,
158 getRestfulParameters(JsonUtil.marshal(requestBody)));
160 restfulRsp = RestfulFactory.getRestInstance("http").post(gatewayUri,
161 getRestfulParameters(JsonUtil.marshal(requestBody)));
163 if (null != restfulRsp) {
164 LOGGER.info("restful call result:", restfulRsp.getStatus());
166 } catch(ServiceException e) {
167 LOGGER.error("service gateway delete restful call result:", e);