4f592f5b4dfc984870a0fbb1443d9d58c67a7d0c
[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                 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;
100             }
101         } catch(ServiceException e) {
102                 LOGGER.error("service gateway create restful call result:", e);
103             throw e;
104         }
105
106         return id;
107     }
108     
109     /**
110      * get the parameters for restful<br/>
111      * 
112      * @author
113      * @param bodyData
114      *            Json Body
115      * @return the RestfulParametes Instance
116      * @since GSO 0.5, 2016-8-9
117      */
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);
122         return param;
123     }
124
125     /**
126      * Delete service instances.<br/>
127      * 
128      * @param serviceId service instance ID
129      * @param httpRequest http request
130      * @throws ServiceException operate DB or parameter is wrong.
131      * @since GSO 0.5
132      */
133     @Override
134     public Map<String, Object> deleteService(String serviceId, String reqContent, HttpServletRequest httpRequest) throws ServiceException {
135         if(httpRequest == null)
136         {               
137                 LOGGER.error("ServiceGatewayImpl.deleteService httpRequest is null");
138                 throw new ServiceException("ServiceGatewayImpl.deleteService httpRequest is null");
139         }
140         // Parse request
141         Map<String, Object> requestBody = JsonUtil.unMarshal(reqContent, Map.class);
142         ValidateUtil.assertObjectNotNull(requestBody);
143
144         // Validate data
145         String gatewayUri = (String)requestBody.get(Constant.SERVICE_GATEWAY_URI);
146         ValidateUtil.assertStringNotNull(gatewayUri);
147         requestBody.remove(Constant.SERVICE_GATEWAY_URI);
148
149         String operation = (String) requestBody.get(Constant.SERVICE_OPERATION);
150         ValidateUtil.assertStringNotNull(operation);
151         requestBody.remove(Constant.SERVICE_OPERATION);
152
153         // call the restful
154         try {
155             RestfulResponse restfulRsp = null;
156             Map<String, Object> result = new HashMap<String, Object>();
157             if(Constant.SERVICE_DELETE_OPERATION.equalsIgnoreCase(operation)) {
158                 restfulRsp = RestfulFactory.getRestInstance("http").delete(gatewayUri,
159                         getRestfulParameters(JsonUtil.marshal(requestBody)));
160                 result.put(Constant.RESPONSE_STATUS, "success");
161                 result.put(Constant.RESPONSE_STATUS_DESCRIPTION, "It is deleting.");
162                 result.put(Constant.RESPONSE_ERRORCODE, "202");
163             } else {
164                 restfulRsp = RestfulFactory.getRestInstance("http").post(gatewayUri,
165                         getRestfulParameters(JsonUtil.marshal(requestBody)));
166                 if (null != restfulRsp) {
167                     String jobId = restfulRsp.getRespHeaderStr(Constant.JOB_ID);
168                     result.put(Constant.JOB_ID, jobId);
169                 }
170             }
171             if (null != restfulRsp) {
172                 LOGGER.info("restful call result:", restfulRsp.getStatus());
173                 LOGGER.info("restful call content:", restfulRsp.getResponseContent());
174             }
175             return result;
176         } catch(ServiceException e) {
177             LOGGER.error("service gateway delete restful call result:", e);
178             throw e;
179         }
180     }
181
182 }