2 * Copyright 2016 ZTE, Inc. and others.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.msb.apiroute.wrapper;
18 import java.io.FileNotFoundException;
19 import java.io.IOException;
21 import java.util.List;
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;
36 public class ApiRouteServiceWrapper {
38 private static final Logger LOGGER = LoggerFactory.getLogger(ApiRouteServiceWrapper.class);
41 private static ApiRouteServiceWrapper instance = new ApiRouteServiceWrapper();
43 private ApiRouteServiceWrapper() {}
45 public static ApiRouteServiceWrapper getInstance() {
50 public List<ApiRouteInfo> getAllApiRouteInstances(String routeWay) {
52 RouteUtil.checkRouteWay(routeWay);
55 String apiRedisKey=RouteUtil.getMutiRedisKey(RouteUtil.APIROUTE, routeWay);
57 return ApiRouteService.getInstance().getMultiApiRouteInstances(apiRedisKey);
59 } catch (Exception e) {
60 throw new ExtendedInternalServerErrorException(e.getMessage());
67 * @Title: getApiRouteInstance
68 * @Description: TODO(Through the name + version for a single service object information)
69 * @param: @param serviceName
70 * @param: @param version
72 * @return: ApiRouteInfo
74 public ApiRouteInfo getApiRouteInstance(String serviceName, String version, String host,String publish_port,String routeWay) {
76 RouteUtil.checkRouteWay(routeWay);
78 if ("null".equals(version)) {
82 RouteUtil.checkServiceNameAndVersion(serviceName,version);
84 String apiRedisPrefixedKey=RouteUtil.getAPIRedisPrefixedKey(serviceName, version, host, publish_port, routeWay);
86 ApiRouteInfo apiRouteInfo;
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());
96 if (null == apiRouteInfo) {
97 throw new ExtendedNotFoundException("Api RouteInfo not found");
107 * @Title updateApiRouteStatus
108 * @Description TODO(update ApiRoute Status)
113 * @return RouteResult
115 public synchronized ApiRouteInfo updateApiRouteStatus(String serviceName, String version,String host,String publish_port,
116 String status,String routeWay) {
118 RouteUtil.checkRouteWay(routeWay);
120 if ("null".equals(version)) {
124 RouteUtil.checkServiceNameAndVersion(serviceName,version);
126 RouteUtil.checkServiceStatus(status);
129 String apiRedisPrefixedKey=RouteUtil.getAPIRedisPrefixedKey(serviceName, version, host, publish_port, routeWay);
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());
138 ApiRouteInfo new_apiRouteInfo = getApiRouteInstance(serviceName, version,host,publish_port,routeWay);
139 return new_apiRouteInfo;
144 * @Title: saveApiRouteInstance
145 * @Description: TODO(save ApiRouteInstance)
146 * @param: @param apiRouteInfo
148 * @return: ApiRouteInfo
150 public synchronized ApiRouteInfo saveApiRouteInstance4Rest(ApiRouteInfo apiRouteInfo,String routeWay) {
152 RouteUtil.checkRouteWay(routeWay);
154 RouteUtil.checkRouterInfoFormat(apiRouteInfo);
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());
167 public synchronized void saveApiRouteInstance(ApiRouteInfo apiRouteInfo,String routeWay) throws Exception {
169 String apiRedisPrefixedKey=RouteUtil.getAPIRedisPrefixedKey(apiRouteInfo.getServiceName(), apiRouteInfo.getVersion(), apiRouteInfo.getHost(), apiRouteInfo.getPublish_port(), routeWay);
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);
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
193 public synchronized void deleteApiRoute(String serviceName, String version, String host,String publish_port,String routeWay) {
195 RouteUtil.checkRouteWay(routeWay);
197 if ("null".equals(version)) {
201 RouteUtil.checkServiceNameAndVersion(serviceName,version);
203 String apiRedisPrefixedKey=RouteUtil.getAPIRedisPrefixedKey(serviceName, version, host, publish_port, routeWay);
207 ApiRouteService.getInstance()
208 .deleteApiRouteService2Redis(apiRedisPrefixedKey);
209 LOGGER.info("delete apiRouteInfo [serviceName]"+serviceName+"[version]"+version+" [host]"+host +" [publish_port]"+publish_port+" [routeWay]"+routeWay+" success");
212 catch (ExtendedNotFoundException 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);
217 throw new ExtendedInternalServerErrorException("delete apiRouteInfo [serviceName]"+serviceName+"[version]"+version+e.getMessage());
225 * @Title: getAllApiDocs
226 * @Description: TODO(For local ext\initSwaggerJson directory of all the json file directory)
230 public String[] getAllApiDocs() {
231 URL apiDocsPath = ApiRouteServiceWrapper.class.getResource("/ext/initSwaggerJson");
232 if (apiDocsPath != null) {
233 String path = apiDocsPath.getPath();
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());
252 public String getAllrouteByJson(String routeWay){
254 Object[] apirouteArray= ApiRouteServiceWrapper.getInstance().getAllApiRouteInstances(routeWay).toArray();
255 Object[] iuirouteArray= IuiRouteServiceWrapper.getInstance().getAllIuiRouteInstances(routeWay).toArray();
256 Object[] customrouteArray= CustomRouteServiceWrapper.getInstance().getAllCustomRouteInstances(routeWay).toArray();
258 Object[] temprouteArray =CommonUtil.concat(apirouteArray, iuirouteArray);
259 Object[] allrouteArray=CommonUtil.concat(temprouteArray, customrouteArray);
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());