7016252902d6b985ce66f36fe40cc3e61a668d43
[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.Map;
20
21 import javax.servlet.http.HttpServletRequest;
22
23 import org.apache.commons.lang.StringUtils;
24 import org.openo.baseservice.remoteservice.exception.ServiceException;
25 import org.openo.baseservice.roa.util.restclient.RestfulFactory;
26 import org.openo.baseservice.roa.util.restclient.RestfulParametes;
27 import org.openo.baseservice.roa.util.restclient.RestfulResponse;
28 import org.openo.gso.gui.servicegateway.constant.Constant;
29 import org.openo.gso.gui.servicegateway.service.inf.IServiceGateway;
30 import org.openo.gso.gui.servicegateway.util.json.JsonUtil;
31 import org.openo.gso.gui.servicegateway.util.validate.ValidateUtil;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * ServiceGateway service class.<br/>
37  * <p>
38  * </p>
39  * 
40  * @author
41  * @version GSO 0.5 2016/8/4
42  */
43 public class ServiceGatewayImpl implements IServiceGateway {
44
45     /**
46      * Log service.
47      */
48     private static final Logger LOGGER = LoggerFactory.getLogger(ServiceGatewayImpl.class);
49
50
51     /**
52      * Create service instance.<br/>
53      * 
54      * @param reqContent content of request
55      * @param httpRequest http request
56      * @throws ServiceException when operate DB or parameter is wrong.
57      * @since GSO 0.5
58      */
59     @SuppressWarnings("unchecked")
60     @Override
61     public String createService(String reqContent, HttpServletRequest httpRequest) throws ServiceException {
62         // check the value
63         if(StringUtils.isEmpty(reqContent))
64         {
65                 LOGGER.error("ServiceGatewayImpl createService reqContent is null.");
66                 return null;
67         }
68         
69         // Parse request
70         Map<String, Object> requestBody = JsonUtil.unMarshal(reqContent, Map.class);
71         Map<String, Object> service = (Map<String, Object>)requestBody.get(Constant.SERVICE_INDENTIFY);
72         if(null == service)
73         {
74                 service = requestBody;
75         }
76         ValidateUtil.assertObjectNotNull(requestBody);
77
78         // Validate data
79         String gatewayUri = (String)service.get(Constant.SERVICE_GATEWAY_URI);
80         ValidateUtil.assertStringNotNull(gatewayUri);
81         service.remove(Constant.SERVICE_GATEWAY_URI);
82
83         // call the restful 
84         String id = null;
85         try {
86                 RestfulResponse restfulRsp = RestfulFactory.getRestInstance("http").post(gatewayUri,
87                     getRestfulParameters(JsonUtil.marshal(requestBody)));
88             if (null != restfulRsp) {
89                 // Record the result of registration
90                 // (201:success;415:Invalid Parameter;500:Internal Server Error)
91                 LOGGER.info("restful call result:", restfulRsp.getStatus());
92                 id = restfulRsp.getRespHeaderStr(Constant.SERVICE_ID);
93                 id = (null == id) ? restfulRsp.getRespHeaderStr(Constant.NS_INSTANCE_ID) : id;
94                 id = (null == id) ? restfulRsp.getRespHeaderStr(Constant.JOB_ID) : id;
95             }
96         } catch(ServiceException e) {
97                 LOGGER.error("service gateway create restful call result:", e);
98             throw e;
99         }
100
101         return id;
102     }
103     
104     /**
105      * get the parameters for restful<br/>
106      * 
107      * @author
108      * @param bodyData
109      *            Json Body
110      * @return the RestfulParametes Instance
111      * @since GSO 0.5, 2016-8-9
112      */
113     private static RestfulParametes getRestfulParameters(final String bodyData) {
114         RestfulParametes param = new RestfulParametes();
115         param.putHttpContextHeader(Constant.HEAD_ERMAP_TYPE, Constant.HEAD_ERMAP_VALUE);
116         param.setRawData(bodyData);
117         return param;
118     }
119
120     /**
121      * Delete service instances.<br/>
122      * 
123      * @param serviceId service instance ID
124      * @param httpRequest http request
125      * @throws ServiceException operate DB or parameter is wrong.
126      * @since GSO 0.5
127      */
128     @Override
129     public void deleteService(String serviceId, HttpServletRequest httpRequest) throws ServiceException {
130
131     }
132
133 }