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