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.ExceptionArgs;
26 import org.openo.baseservice.remoteservice.exception.ServiceException;
27 import org.openo.baseservice.roa.util.restclient.RestfulFactory;
28 import org.openo.baseservice.roa.util.restclient.RestfulParametes;
29 import org.openo.baseservice.roa.util.restclient.RestfulResponse;
30 import org.openo.baseservice.util.RestUtils;
31 import org.openo.gso.gui.servicegateway.constant.Constant;
32 import org.openo.gso.gui.servicegateway.exception.HttpCode;
33 import org.openo.gso.gui.servicegateway.service.inf.IServiceGateway;
34 import org.openo.gso.gui.servicegateway.util.json.JsonUtil;
35 import org.openo.gso.gui.servicegateway.util.validate.ValidateUtil;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * ServiceGateway service class.<br/>
45 * @version GSO 0.5 2016/8/4
47 public class ServiceGatewayImpl implements IServiceGateway {
52 private static final Logger LOGGER = LoggerFactory.getLogger(ServiceGatewayImpl.class);
56 * Create service instance.<br/>
58 * @param reqContent content of request
59 * @param httpRequest http request
60 * @throws ServiceException when operate DB or parameter is wrong.
63 @SuppressWarnings("unchecked")
65 public String createService(String reqContent, HttpServletRequest httpRequest) throws ServiceException {
67 if(StringUtils.isEmpty(reqContent))
69 LOGGER.error("ServiceGatewayImpl createService reqContent is null.");
70 throw new ServiceException("ServiceGatewayImpl createService reqContent is null.");
74 Map<String, Object> requestBody = JsonUtil.unMarshal(reqContent, Map.class);
75 Map<String, Object> service = (Map<String, Object>)requestBody.get(Constant.SERVICE_INDENTIFY);
78 service = requestBody;
80 ValidateUtil.assertObjectNotNull(requestBody);
83 String gatewayUri = (String)service.get(Constant.SERVICE_GATEWAY_URI);
84 ValidateUtil.assertStringNotNull(gatewayUri);
85 service.remove(Constant.SERVICE_GATEWAY_URI);
90 LOGGER.info("Send the cretation RESTful request to orchestrator.The Body is"+ requestBody.toString());
91 RestfulResponse restfulRsp = RestfulFactory.getRestInstance("http").post(gatewayUri,
92 getRestfulParameters(JsonUtil.marshal(requestBody)));
93 LOGGER.info("Receive the cretation RESTful response from orchestrator.The status is:"+restfulRsp.getStatus()+" the content is:"+
94 restfulRsp.getResponseContent());
95 if (null != restfulRsp)
97 // Record the result of registration
98 // (201:success;415:Invalid Parameter;500:Internal Server Error)
99 LOGGER.info("restful call result:"+ restfulRsp.getStatus());
100 if(restfulRsp.getStatus() == HttpCode.RESPOND_ACCEPTED || restfulRsp.getStatus() == HttpCode.RESPOND_OK || restfulRsp.getStatus() == HttpCode.CREATED_OK)
102 Map<String,Object> rspBody = JsonUtil.unMarshal(restfulRsp.getResponseContent(),Map.class);
103 id = (String)rspBody.get(Constant.SERVICE_ID);
104 id = (null == id) ? (String)rspBody.get(Constant.NS_INSTANCE_ID) : id;
105 id = (null == id) ? (String)rspBody.get(Constant.JOB_ID) : id;
109 ExceptionArgs args = new ExceptionArgs();
110 args.setDescArgs(new String[] {"Fail to create service:" + restfulRsp.getResponseContent()});
111 throw new ServiceException(ServiceException.DEFAULT_ID, restfulRsp.getStatus(), args);
114 } catch(ServiceException e) {
115 LOGGER.error("service gateway create restful call result:", e);
123 * get the parameters for restful<br/>
128 * @return the RestfulParametes Instance
129 * @since GSO 0.5, 2016-8-9
131 private static RestfulParametes getRestfulParameters(final String bodyData) {
132 RestfulParametes param = new RestfulParametes();
133 param.putHttpContextHeader(Constant.HEAD_ERMAP_TYPE, Constant.HEAD_ERMAP_VALUE);
134 param.setRawData(bodyData);
139 * Delete service instances.<br/>
141 * @param serviceId service instance ID
142 * @param httpRequest http request
143 * @throws ServiceException operate DB or parameter is wrong.
147 public Map<String, Object> deleteService(String serviceId, String reqContent, HttpServletRequest httpRequest) throws ServiceException {
148 if(httpRequest == null)
150 LOGGER.error("ServiceGatewayImpl.deleteService httpRequest is null");
151 throw new ServiceException("ServiceGatewayImpl.deleteService httpRequest is null");
154 Map<String, Object> requestBody = JsonUtil.unMarshal(reqContent, Map.class);
155 ValidateUtil.assertObjectNotNull(requestBody);
158 String gatewayUri = (String)requestBody.get(Constant.SERVICE_GATEWAY_URI);
159 ValidateUtil.assertStringNotNull(gatewayUri);
160 requestBody.remove(Constant.SERVICE_GATEWAY_URI);
162 String operation = (String) requestBody.get(Constant.SERVICE_OPERATION);
163 ValidateUtil.assertStringNotNull(operation);
164 requestBody.remove(Constant.SERVICE_OPERATION);
168 RestfulResponse restfulRsp = null;
169 Map<String, Object> result = new HashMap<String, Object>();
170 if(Constant.SERVICE_DELETE_OPERATION.equalsIgnoreCase(operation)) {
171 restfulRsp = RestfulFactory.getRestInstance("http").delete(gatewayUri,
172 getRestfulParameters(JsonUtil.marshal(requestBody)));
173 result.put(Constant.RESPONSE_STATUS, "success");
174 result.put(Constant.RESPONSE_STATUS_DESCRIPTION, "It is deleting.");
175 result.put(Constant.RESPONSE_ERRORCODE, "202");
177 restfulRsp = RestfulFactory.getRestInstance("http").post(gatewayUri,
178 getRestfulParameters(JsonUtil.marshal(requestBody)));
179 if (null != restfulRsp) {
180 Map<String,Object> rspBody = JsonUtil.unMarshal(restfulRsp.getResponseContent(),Map.class);
181 String jobId = (String)rspBody.get(Constant.JOB_ID);
182 result.put(Constant.JOB_ID, jobId);
185 if (null != restfulRsp) {
186 LOGGER.info("restful call result: {}", restfulRsp.getStatus());
187 LOGGER.info("restful call content:{}", restfulRsp.getResponseContent());
190 } catch(ServiceException e) {
191 LOGGER.error("service gateway delete restful call result:", e);