Divide the MSB source codes into two repos
[msb/apigateway.git] / apiroute / apiroute-service / src / main / java / org / onap / msb / apiroute / wrapper / ApiRouteServiceWrapper.java
1 /**
2  * Copyright 2016 ZTE, Inc. and others.
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 package org.onap.msb.apiroute.wrapper;
17
18 import java.io.FileNotFoundException;
19 import java.io.IOException;
20 import java.net.URL;
21 import java.util.List;
22
23 import org.onap.msb.apiroute.api.ApiRouteInfo;
24 import org.onap.msb.apiroute.api.exception.ExtendedInternalServerErrorException;
25 import org.onap.msb.apiroute.api.exception.ExtendedNotFoundException;
26 import org.onap.msb.apiroute.wrapper.service.ApiRouteService;
27 import org.onap.msb.apiroute.wrapper.util.CommonUtil;
28 import org.onap.msb.apiroute.wrapper.util.FileUtil;
29 import org.onap.msb.apiroute.wrapper.util.JacksonJsonUtil;
30 import org.onap.msb.apiroute.wrapper.util.RouteUtil;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34
35
36 public class ApiRouteServiceWrapper  {
37
38   private static final Logger LOGGER = LoggerFactory.getLogger(ApiRouteServiceWrapper.class);
39
40
41   private static ApiRouteServiceWrapper instance = new ApiRouteServiceWrapper();
42
43   private ApiRouteServiceWrapper() {}
44
45   public static ApiRouteServiceWrapper getInstance() {
46     return instance;
47   }
48
49
50   public List<ApiRouteInfo> getAllApiRouteInstances(String routeWay) {
51    
52     RouteUtil.checkRouteWay(routeWay);
53     
54     try {
55       String apiRedisKey=RouteUtil.getMutiRedisKey(RouteUtil.APIROUTE, routeWay);
56
57      return ApiRouteService.getInstance().getMultiApiRouteInstances(apiRedisKey);
58       
59   } catch (Exception e) {
60       throw new ExtendedInternalServerErrorException(e.getMessage());
61   }
62   }
63
64
65
66   /**
67    * @Title: getApiRouteInstance
68    * @Description: TODO(Through the name + version for a single service object information)
69    * @param: @param serviceName
70    * @param: @param version
71    * @param: @return
72    * @return: ApiRouteInfo
73    */
74   public ApiRouteInfo getApiRouteInstance(String serviceName, String version, String host,String publish_port,String routeWay) {
75
76     RouteUtil.checkRouteWay(routeWay);
77     
78     if ("null".equals(version)) {
79       version = "";
80     }
81
82     RouteUtil.checkServiceNameAndVersion(serviceName,version);
83     
84     String apiRedisPrefixedKey=RouteUtil.getAPIRedisPrefixedKey(serviceName, version, host, publish_port, routeWay);
85       
86     ApiRouteInfo apiRouteInfo;
87     try {
88       apiRouteInfo = ApiRouteService.getInstance().getApiRouteInstance(apiRedisPrefixedKey);
89     } catch (Exception e) {
90       LOGGER.error("get ApiRouteInstance throw exception", e);
91       throw new ExtendedInternalServerErrorException("get ApiRouteInstance throw exception" + e.getMessage());
92     }
93
94     
95
96     if (null == apiRouteInfo) {
97       throw new ExtendedNotFoundException("Api RouteInfo not found");
98     }
99
100     return apiRouteInfo;
101
102   }
103
104  
105
106   /**
107    * @Title updateApiRouteStatus
108    * @Description TODO(update ApiRoute Status)
109    * @param serviceName
110    * @param version
111    * @param status
112    * @return
113    * @return RouteResult
114    */
115   public synchronized ApiRouteInfo updateApiRouteStatus(String serviceName, String version,String host,String publish_port,
116       String status,String routeWay) {
117
118     RouteUtil.checkRouteWay(routeWay);
119     
120     if ("null".equals(version)) {
121       version = "";
122     }
123     
124     RouteUtil.checkServiceNameAndVersion(serviceName,version);
125    
126     RouteUtil.checkServiceStatus(status);
127
128     
129     String apiRedisPrefixedKey=RouteUtil.getAPIRedisPrefixedKey(serviceName, version, host, publish_port, routeWay);
130   
131     try {
132       ApiRouteService.getInstance().updateApiRouteStatus2Redis(apiRedisPrefixedKey, status);
133     } catch (Exception e) {
134       LOGGER.error("update ApiRoute status  throw exception", e);
135       throw new ExtendedInternalServerErrorException(e.getMessage());
136     }
137
138     ApiRouteInfo new_apiRouteInfo = getApiRouteInstance(serviceName, version,host,publish_port,routeWay);
139     return new_apiRouteInfo;
140   }
141
142
143   /**
144    * @Title: saveApiRouteInstance
145    * @Description: TODO(save ApiRouteInstance)
146    * @param: @param apiRouteInfo
147    * @param: @return
148    * @return: ApiRouteInfo
149    */
150   public synchronized ApiRouteInfo saveApiRouteInstance4Rest(ApiRouteInfo apiRouteInfo,String routeWay) {
151
152     RouteUtil.checkRouteWay(routeWay);
153     
154     RouteUtil.checkRouterInfoFormat(apiRouteInfo);
155    
156     try {
157       saveApiRouteInstance(apiRouteInfo,routeWay);
158     } catch (Exception e) {     
159       throw new ExtendedInternalServerErrorException("save apiRouteInfo  fail:  [serviceName]"+apiRouteInfo.getServiceName()+"[version]"+apiRouteInfo.getVersion()+" [routeWay]"+routeWay+e.getMessage());
160     }
161    
162     return apiRouteInfo;
163   }
164   
165   
166
167   public synchronized void saveApiRouteInstance(ApiRouteInfo apiRouteInfo,String routeWay) throws Exception {
168     try {
169      String apiRedisPrefixedKey=RouteUtil.getAPIRedisPrefixedKey(apiRouteInfo.getServiceName(), apiRouteInfo.getVersion(), apiRouteInfo.getHost(), apiRouteInfo.getPublish_port(), routeWay);
170
171      ApiRouteService.getInstance().saveApiRouteService2Redis(apiRouteInfo, apiRedisPrefixedKey);
172      LOGGER.info("save apiRouteInfo [serviceName]"+apiRouteInfo.getServiceName()+"[version]"+apiRouteInfo.getVersion()+" [routeWay]"+routeWay+" success");
173     } catch (Exception e) {   
174       LOGGER.error("save apiRouteInfo [serviceName]"+apiRouteInfo.getServiceName()+"[version]"+apiRouteInfo.getVersion()+" [routeWay]"+routeWay+" throw exception", e);
175       throw e;
176     }
177     
178     
179   }
180
181
182
183   /**
184    * @Title: deleteApiRoute
185    * @Description: TODO(delete one ApiRoute)
186    * @param: @param type
187    * @param: @param serviceName
188    * @param: @param version
189    * @param: @param delKey
190    * @param: @return
191    * @return: void
192    */
193   public synchronized void deleteApiRoute(String serviceName, String version, String host,String publish_port,String routeWay) {
194
195     RouteUtil.checkRouteWay(routeWay);
196     
197     if ("null".equals(version)) {
198       version = "";
199     }
200
201     RouteUtil.checkServiceNameAndVersion(serviceName,version);
202
203     String apiRedisPrefixedKey=RouteUtil.getAPIRedisPrefixedKey(serviceName, version, host, publish_port, routeWay);
204
205     
206     try {
207       ApiRouteService.getInstance()
208           .deleteApiRouteService2Redis(apiRedisPrefixedKey);
209       LOGGER.info("delete apiRouteInfo [serviceName]"+serviceName+"[version]"+version+" [host]"+host +" [publish_port]"+publish_port+" [routeWay]"+routeWay+" success");
210
211     }
212     catch (ExtendedNotFoundException e) {
213       throw e;
214     }catch (Exception e) {
215       LOGGER.error("delete apiRouteInfo [serviceName]"+serviceName+"[version]"+version+" [host]"+host +" [publish_port]"+publish_port+" [routeWay]"+routeWay+" throw exception", e);
216
217       throw new ExtendedInternalServerErrorException("delete apiRouteInfo [serviceName]"+serviceName+"[version]"+version+e.getMessage());
218     }
219    
220
221   }
222   
223   
224   /**
225    * @Title: getAllApiDocs
226    * @Description: TODO(For local ext\initSwaggerJson directory of all the json file directory)
227    * @param: @return
228    * @return: String[]
229    */
230   public String[] getAllApiDocs() {
231     URL apiDocsPath = ApiRouteServiceWrapper.class.getResource("/ext/initSwaggerJson");
232     if (apiDocsPath != null) {
233       String path = apiDocsPath.getPath();
234
235       try {
236         return FileUtil.readfile(path);
237       } catch (FileNotFoundException e) {
238         // TODO Auto-generated catch block
239         LOGGER.error("read  ApiDocs Files throw FileNotFoundException", e);
240         throw new ExtendedInternalServerErrorException("read  ApiDocs Files throw FileNotFoundException:" + e.getMessage());
241       } catch (IOException e) {
242         // TODO Auto-generated catch block
243         LOGGER.error("read  ApiDocs Files throw IOexception", e);
244         throw new ExtendedInternalServerErrorException("read  ApiDocs Files throw IOexception:" + e.getMessage());
245       }
246
247     }
248
249     return null;
250   }
251   
252   public String getAllrouteByJson(String routeWay){
253
254     Object[] apirouteArray= ApiRouteServiceWrapper.getInstance().getAllApiRouteInstances(routeWay).toArray();
255     Object[] iuirouteArray= IuiRouteServiceWrapper.getInstance().getAllIuiRouteInstances(routeWay).toArray();
256     Object[] customrouteArray= CustomRouteServiceWrapper.getInstance().getAllCustomRouteInstances(routeWay).toArray();
257     
258     Object[] temprouteArray =CommonUtil.concat(apirouteArray, iuirouteArray);
259     Object[] allrouteArray=CommonUtil.concat(temprouteArray, customrouteArray);
260     
261    
262     String allrouteJson;
263     try {
264       allrouteJson = JacksonJsonUtil.beanToJson(allrouteArray);
265     } catch (Exception e) {
266       LOGGER.error("exportService beanToJson throw Exception", e);
267       throw new ExtendedInternalServerErrorException("exportService beanToJson throw Exception:"+ e.getMessage());
268     }
269     return allrouteJson;
270   }
271
272
273 }