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;
19 import java.util.HashMap;
22 import javax.servlet.http.HttpServletRequest;
24 import org.apache.commons.lang.StringUtils;
25 import org.openo.baseservice.remoteservice.exception.ServiceException;
26 import org.openo.baseservice.roa.util.restclient.RestfulFactory;
27 import org.openo.baseservice.roa.util.restclient.RestfulParametes;
28 import org.openo.baseservice.roa.util.restclient.RestfulResponse;
29 import org.openo.baseservice.util.RestUtils;
30 import org.openo.gso.gui.servicegateway.constant.Constant;
31 import org.openo.gso.gui.servicegateway.service.inf.IServiceGateway;
32 import org.openo.gso.gui.servicegateway.util.json.JsonUtil;
33 import org.openo.gso.gui.servicegateway.util.validate.ValidateUtil;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * ServiceGateway service class.<br/>
43 * @version GSO 0.5 2016/8/4
45 public class ServiceGatewayImpl implements IServiceGateway {
50 private static final Logger LOGGER = LoggerFactory.getLogger(ServiceGatewayImpl.class);
54 * Create service instance.<br/>
56 * @param reqContent content of request
57 * @param httpRequest http request
58 * @throws ServiceException when operate DB or parameter is wrong.
61 @SuppressWarnings("unchecked")
63 public String createService(String reqContent, HttpServletRequest httpRequest) throws ServiceException {
65 if(StringUtils.isEmpty(reqContent))
67 LOGGER.error("ServiceGatewayImpl createService reqContent is null.");
68 throw new ServiceException("ServiceGatewayImpl createService reqContent is null.");
72 Map<String, Object> requestBody = JsonUtil.unMarshal(reqContent, Map.class);
73 Map<String, Object> service = (Map<String, Object>)requestBody.get(Constant.SERVICE_INDENTIFY);
76 service = requestBody;
78 ValidateUtil.assertObjectNotNull(requestBody);
81 String gatewayUri = (String)service.get(Constant.SERVICE_GATEWAY_URI);
82 ValidateUtil.assertStringNotNull(gatewayUri);
83 service.remove(Constant.SERVICE_GATEWAY_URI);
88 LOGGER.info("Send the cretation RESTful request to orchestrator.The Body is"+ requestBody.toString());
89 RestfulResponse restfulRsp = RestfulFactory.getRestInstance("http").post(gatewayUri,
90 getRestfulParameters(JsonUtil.marshal(requestBody)));
91 LOGGER.info("Receive the cretation RESTful response from orchestrator.The status is:"+restfulRsp.getStatus()+" the content is:"+
92 restfulRsp.getResponseContent());
93 if (null != restfulRsp) {
94 // Record the result of registration
95 // (201:success;415:Invalid Parameter;500:Internal Server Error)
96 LOGGER.info("restful call result:"+ restfulRsp.getStatus());
97 id = restfulRsp.getRespHeaderStr(Constant.SERVICE_ID);
98 id = (null == id) ? restfulRsp.getRespHeaderStr(Constant.NS_INSTANCE_ID) : id;
99 id = (null == id) ? restfulRsp.getRespHeaderStr(Constant.JOB_ID) : id;
101 } catch(ServiceException e) {
102 LOGGER.error("service gateway create restful call result:", e);
110 * get the parameters for restful<br/>
115 * @return the RestfulParametes Instance
116 * @since GSO 0.5, 2016-8-9
118 private static RestfulParametes getRestfulParameters(final String bodyData) {
119 RestfulParametes param = new RestfulParametes();
120 param.putHttpContextHeader(Constant.HEAD_ERMAP_TYPE, Constant.HEAD_ERMAP_VALUE);
121 param.setRawData(bodyData);
126 * Delete service instances.<br/>
128 * @param serviceId service instance ID
129 * @param httpRequest http request
130 * @throws ServiceException operate DB or parameter is wrong.
134 public Map<String, Object> deleteService(String serviceId, HttpServletRequest httpRequest) throws ServiceException {
135 if(httpRequest == null)
137 LOGGER.error("ServiceGatewayImpl.deleteService httpRequest is null");
138 throw new ServiceException("ServiceGatewayImpl.deleteService httpRequest is null");
141 String reqContent = RestUtils.getRequestBody(httpRequest);
142 Map<String, Object> requestBody = JsonUtil.unMarshal(reqContent, Map.class);
143 ValidateUtil.assertObjectNotNull(requestBody);
146 String gatewayUri = (String)requestBody.get(Constant.SERVICE_GATEWAY_URI);
147 ValidateUtil.assertStringNotNull(gatewayUri);
148 requestBody.remove(Constant.SERVICE_GATEWAY_URI);
150 String operation = (String) requestBody.get(Constant.SERVICE_OPERATION);
151 ValidateUtil.assertStringNotNull(operation);
152 requestBody.remove(Constant.SERVICE_OPERATION);
156 RestfulResponse restfulRsp = null;
157 Map<String, Object> result = new HashMap<String, Object>();
158 if(Constant.SERVICE_DELETE_OPERATION.equalsIgnoreCase(operation)) {
159 restfulRsp = RestfulFactory.getRestInstance("http").delete(gatewayUri,
160 getRestfulParameters(JsonUtil.marshal(requestBody)));
161 result.put(Constant.RESPONSE_STATUS, "success");
162 result.put(Constant.RESPONSE_STATUS_DESCRIPTION, "It is deleting.");
163 result.put(Constant.RESPONSE_ERRORCODE, "202");
165 restfulRsp = RestfulFactory.getRestInstance("http").post(gatewayUri,
166 getRestfulParameters(JsonUtil.marshal(requestBody)));
167 if (null != restfulRsp) {
168 String jobId = restfulRsp.getRespHeaderStr(Constant.JOB_ID);
169 result.put(Constant.JOB_ID, jobId);
172 if (null != restfulRsp) {
173 LOGGER.info("restful call result:", restfulRsp.getStatus());
174 LOGGER.info("restful call content:", restfulRsp.getResponseContent());
177 } catch(ServiceException e) {
178 LOGGER.error("service gateway delete restful call result:", e);