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