5f23ad4e76d7e78a633cc3ac102e5469d46bb728
[vnfsdk/refrepo.git] /
1 /*
2  * Copyright (c) 2016, Huawei Technologies Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openo.gso.gui.servicegateway.service.impl;
18
19 import java.util.HashMap;
20 import java.util.Map;
21
22 import javax.servlet.http.HttpServletRequest;
23
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;
36
37 /**
38  * ServiceGateway service class.<br/>
39  * <p>
40  * </p>
41  * 
42  * @author
43  * @version GSO 0.5 2016/8/4
44  */
45 public class ServiceGatewayImpl implements IServiceGateway {
46
47     /**
48      * Log service.
49      */
50     private static final Logger LOGGER = LoggerFactory.getLogger(ServiceGatewayImpl.class);
51
52
53     /**
54      * Create service instance.<br/>
55      * 
56      * @param reqContent content of request
57      * @param httpRequest http request
58      * @throws ServiceException when operate DB or parameter is wrong.
59      * @since GSO 0.5
60      */
61     @SuppressWarnings("unchecked")
62     @Override
63     public String createService(String reqContent, HttpServletRequest httpRequest) throws ServiceException {
64         // check the value
65         if(StringUtils.isEmpty(reqContent))
66         {
67                 LOGGER.error("ServiceGatewayImpl createService reqContent is null.");
68                 throw new ServiceException("ServiceGatewayImpl createService reqContent is null.");             
69         }
70         
71         // Parse request
72         Map<String, Object> requestBody = JsonUtil.unMarshal(reqContent, Map.class);
73         Map<String, Object> service = (Map<String, Object>)requestBody.get(Constant.SERVICE_INDENTIFY);
74         if(null == service)
75         {
76                 service = requestBody;
77         }
78         ValidateUtil.assertObjectNotNull(requestBody);
79
80         // Validate data
81         String gatewayUri = (String)service.get(Constant.SERVICE_GATEWAY_URI);
82         ValidateUtil.assertStringNotNull(gatewayUri);
83         service.remove(Constant.SERVICE_GATEWAY_URI);
84
85         // call the restful 
86         String id = null;
87         try {
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                 Map<String,Object> rspBody = JsonUtil.unMarshal(restfulRsp.getResponseContent(),Map.class);
98                 id = (String)rspBody.get(Constant.SERVICE_ID);
99                 id = (null == id) ? (String)rspBody.get(Constant.NS_INSTANCE_ID) : id;
100                 id = (null == id) ? (String)rspBody.get(Constant.JOB_ID) : id;
101             }
102         } catch(ServiceException e) {
103                 LOGGER.error("service gateway create restful call result:", e);
104             throw e;
105         }
106
107         return id;
108     }
109     
110     /**
111      * get the parameters for restful<br/>
112      * 
113      * @author
114      * @param bodyData
115      *            Json Body
116      * @return the RestfulParametes Instance
117      * @since GSO 0.5, 2016-8-9
118      */
119     private static RestfulParametes getRestfulParameters(final String bodyData) {
120         RestfulParametes param = new RestfulParametes();
121         param.putHttpContextHeader(Constant.HEAD_ERMAP_TYPE, Constant.HEAD_ERMAP_VALUE);
122         param.setRawData(bodyData);
123         return param;
124     }
125
126     /**
127      * Delete service instances.<br/>
128      * 
129      * @param serviceId service instance ID
130      * @param httpRequest http request
131      * @throws ServiceException operate DB or parameter is wrong.
132      * @since GSO 0.5
133      */
134     @Override
135     public Map<String, Object> deleteService(String serviceId, String reqContent, HttpServletRequest httpRequest) throws ServiceException {
136         if(httpRequest == null)
137         {               
138                 LOGGER.error("ServiceGatewayImpl.deleteService httpRequest is null");
139                 throw new ServiceException("ServiceGatewayImpl.deleteService httpRequest is null");
140         }
141         // Parse request
142         Map<String, Object> requestBody = JsonUtil.unMarshal(reqContent, Map.class);
143         ValidateUtil.assertObjectNotNull(requestBody);
144
145         // Validate data
146         String gatewayUri = (String)requestBody.get(Constant.SERVICE_GATEWAY_URI);
147         ValidateUtil.assertStringNotNull(gatewayUri);
148         requestBody.remove(Constant.SERVICE_GATEWAY_URI);
149
150         String operation = (String) requestBody.get(Constant.SERVICE_OPERATION);
151         ValidateUtil.assertStringNotNull(operation);
152         requestBody.remove(Constant.SERVICE_OPERATION);
153
154         // call the restful
155         try {
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");
164             } else {
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);
170                 }
171             }
172             if (null != restfulRsp) {
173                 LOGGER.info("restful call result:", restfulRsp.getStatus());
174                 LOGGER.info("restful call content:", restfulRsp.getResponseContent());
175             }
176             return result;
177         } catch(ServiceException e) {
178             LOGGER.error("service gateway delete restful call result:", e);
179             throw e;
180         }
181     }
182
183 }