e2200d4df58adacc9d9726668590bd7db90517ff
[vnfsdk/refrepo.git] /
1 /*\r
2  * Copyright (c) 2016, Huawei Technologies Co., Ltd.\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  * http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 package org.openo.gso.gui.servicegateway.service.impl;\r
18 \r
19 import java.util.Map;\r
20 \r
21 import javax.servlet.http.HttpServletRequest;\r
22 \r
23 import org.apache.commons.lang.StringUtils;\r
24 import org.openo.baseservice.remoteservice.exception.ServiceException;\r
25 import org.openo.baseservice.roa.util.restclient.RestfulFactory;\r
26 import org.openo.baseservice.roa.util.restclient.RestfulParametes;\r
27 import org.openo.baseservice.roa.util.restclient.RestfulResponse;\r
28 import org.openo.gso.gui.servicegateway.constant.Constant;\r
29 import org.openo.gso.gui.servicegateway.service.inf.IServiceGateway;\r
30 import org.openo.gso.gui.servicegateway.util.json.JsonUtil;\r
31 import org.openo.gso.gui.servicegateway.util.validate.ValidateUtil;\r
32 import org.slf4j.Logger;\r
33 import org.slf4j.LoggerFactory;\r
34 \r
35 /**\r
36  * ServiceGateway service class.<br/>\r
37  * <p>\r
38  * </p>\r
39  * \r
40  * @author\r
41  * @version GSO 0.5 2016/8/4\r
42  */\r
43 public class ServiceGatewayImpl implements IServiceGateway {\r
44 \r
45     /**\r
46      * Log service.\r
47      */\r
48     private static final Logger LOGGER = LoggerFactory.getLogger(ServiceGatewayImpl.class);\r
49 \r
50 \r
51     /**\r
52      * Create service instance.<br/>\r
53      * \r
54      * @param reqContent content of request\r
55      * @param httpRequest http request\r
56      * @throws ServiceException when operate DB or parameter is wrong.\r
57      * @since GSO 0.5\r
58      */\r
59     @SuppressWarnings("unchecked")\r
60     @Override\r
61     public String createService(String reqContent, HttpServletRequest httpRequest) throws ServiceException {\r
62         // check the value\r
63         if(StringUtils.isEmpty(reqContent))\r
64         {\r
65                 LOGGER.error("ServiceGatewayImpl createService reqContent is null.");\r
66                 return null;\r
67         }\r
68         \r
69         // Parse request\r
70         Map<String, Object> requestBody = JsonUtil.unMarshal(reqContent, Map.class);\r
71         Map<String, Object> service = (Map<String, Object>)requestBody.get(Constant.SERVICE_INDENTIFY);\r
72         if(null == service)\r
73         {\r
74                 service = requestBody;\r
75         }\r
76         ValidateUtil.assertObjectNotNull(requestBody);\r
77 \r
78         // Validate data\r
79         String gatewayUri = (String)service.get(Constant.SERVICE_GATEWAY_URI);\r
80         ValidateUtil.assertStringNotNull(gatewayUri);\r
81         service.remove(Constant.SERVICE_GATEWAY_URI);\r
82 \r
83         // call the restful \r
84         String id = null;\r
85         try {\r
86                 RestfulResponse restfulRsp = RestfulFactory.getRestInstance("http").post(gatewayUri,\r
87                     getRestfulParameters(JsonUtil.marshal(requestBody)));\r
88             if (null != restfulRsp) {\r
89                 // Record the result of registration\r
90                 // (201:success;415:Invalid Parameter;500:Internal Server Error)\r
91                 LOGGER.info("restful call result:", restfulRsp.getStatus());\r
92                 id = restfulRsp.getRespHeaderStr(Constant.SERVICE_ID);\r
93                 id = (null == id) ? restfulRsp.getRespHeaderStr(Constant.NS_INSTANCE_ID) : id;\r
94                 id = (null == id) ? restfulRsp.getRespHeaderStr(Constant.JOB_ID) : id;\r
95             }\r
96         } catch(ServiceException e) {\r
97                 LOGGER.error("service gateway create restful call result:", e);\r
98             throw e;\r
99         }\r
100 \r
101         return id;\r
102     }\r
103     \r
104     /**\r
105      * get the parameters for restful<br/>\r
106      * \r
107      * @author\r
108      * @param bodyData\r
109      *            Json Body\r
110      * @return the RestfulParametes Instance\r
111      * @since GSO 0.5, 2016-8-9\r
112      */\r
113     private static RestfulParametes getRestfulParameters(final String bodyData) {\r
114         RestfulParametes param = new RestfulParametes();\r
115         param.putHttpContextHeader(Constant.HEAD_ERMAP_TYPE, Constant.HEAD_ERMAP_VALUE);\r
116         param.setRawData(bodyData);\r
117         return param;\r
118     }\r
119 \r
120     /**\r
121      * Delete service instances.<br/>\r
122      * \r
123      * @param serviceId service instance ID\r
124      * @param httpRequest http request\r
125      * @throws ServiceException operate DB or parameter is wrong.\r
126      * @since GSO 0.5\r
127      */\r
128     @Override\r
129     public void deleteService(String serviceId, HttpServletRequest httpRequest) throws ServiceException {\r
130 \r
131     }\r
132 \r
133 }\r