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.util.List;
20 import org.onap.msb.apiroute.api.CustomRouteInfo;
21 import org.onap.msb.apiroute.api.exception.ExtendedInternalServerErrorException;
22 import org.onap.msb.apiroute.api.exception.ExtendedNotFoundException;
23 import org.onap.msb.apiroute.wrapper.service.CustomRouteService;
24 import org.onap.msb.apiroute.wrapper.util.RouteUtil;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 public class CustomRouteServiceWrapper {
31 private static final Logger LOGGER = LoggerFactory.getLogger(CustomRouteServiceWrapper.class);
33 private static CustomRouteServiceWrapper instance = new CustomRouteServiceWrapper();
35 private CustomRouteServiceWrapper() {}
37 public static CustomRouteServiceWrapper getInstance() {
43 * @Title: getAllCustomRouteService
44 * @Description: TODO(get AllCustomRoute Service)
46 * @return: CustomRouteInfo[]
48 public List<CustomRouteInfo> getAllCustomRouteInstances(String routeWay) {
50 RouteUtil.checkRouteWay(routeWay);
53 String customRedisKey = RouteUtil.getMutiRedisKey(RouteUtil.CUSTOMROUTE, routeWay);
54 return CustomRouteService.getInstance().getMultiCustomRouteInstances(customRedisKey);
56 } catch (Exception e) {
57 throw new ExtendedInternalServerErrorException(e.getMessage());
66 * @Title: getCustomRouteInstance
67 * @Description: TODO(get CustomRouteInstance by serviceName)
68 * @param: @param serviceName
70 * @return: CustomRouteInfo
72 public CustomRouteInfo getCustomRouteInstance(String serviceName, String host,
73 String publish_port, String routeWay) {
75 RouteUtil.checkRouteWay(routeWay);
77 String customRedisPrefixedKey =
78 RouteUtil.getRedisPrefixedKey(RouteUtil.CUSTOMROUTE, serviceName, host, publish_port,
81 CustomRouteInfo customRouteInfo;
84 CustomRouteService.getInstance().getCustomRouteInstance(customRedisPrefixedKey);
85 } catch (Exception e) {
86 LOGGER.error("get customRouteInstance throw exception", e);
87 throw new ExtendedInternalServerErrorException("get customRouteInstance throw exception"+ e.getMessage());
91 if (null == customRouteInfo) {
92 throw new ExtendedNotFoundException("customRoute Info not found");
96 return customRouteInfo;
102 * @Title updateCustomRouteStatus
103 * @Description TODO(update one CustomRoute Status)
107 * @return RouteResult
109 public synchronized CustomRouteInfo updateCustomRouteStatus(String serviceName, String host,
110 String publish_port, String status, String routeWay) {
112 RouteUtil.checkRouteWay(routeWay);
114 RouteUtil.checkServiceStatus(status);
116 String customRedisPrefixedKey =
117 RouteUtil.getRedisPrefixedKey(RouteUtil.CUSTOMROUTE, serviceName, host, publish_port,
122 CustomRouteService.getInstance()
123 .updateCustomRouteStatus2Redis(customRedisPrefixedKey, status);
124 } catch (Exception e) {
125 LOGGER.error("update CustomRoute status throw exception", e);
126 throw new ExtendedInternalServerErrorException(e.getMessage());
129 CustomRouteInfo new_customRouteInfo =
130 getCustomRouteInstance(serviceName, host, publish_port, routeWay);
132 return new_customRouteInfo;
136 * @Title: saveCustomRouteInstance
137 * @Description: TODO(save one CustomRouteInstance)
138 * @param: @param CustomRouteInfo
140 * @return: CustomRouteInfo
142 public synchronized CustomRouteInfo saveCustomRouteInstance4Rest(CustomRouteInfo customRouteInfo,
145 RouteUtil.checkRouteWay(routeWay);
147 RouteUtil.checkRouterInfoFormat(customRouteInfo);
150 saveCustomRouteInstance(customRouteInfo, routeWay);
152 } catch (Exception e) {
154 throw new ExtendedInternalServerErrorException("save CustomRouteInfo fail: [serviceName]"+customRouteInfo.getServiceName()+e.getMessage());
157 return customRouteInfo;
162 public synchronized CustomRouteInfo saveCustomRouteInstance(CustomRouteInfo customRouteInfo,
163 String routeWay) throws Exception {
165 String customRedisPrefixedKey =
166 RouteUtil.getRedisPrefixedKey(RouteUtil.CUSTOMROUTE, customRouteInfo.getServiceName(),
167 customRouteInfo.getHost(), customRouteInfo.getPublish_port(), routeWay);;
170 CustomRouteService.getInstance().saveCustomRouteService2Redis(customRouteInfo,
171 customRedisPrefixedKey);
172 LOGGER.info("save CustomRouteInfo [serviceName]"+customRouteInfo.getServiceName()+" [host]"+customRouteInfo.getHost() +" [publish_port]"+customRouteInfo.getPublish_port()+" [routeWay]"+routeWay+" success");
174 } catch (Exception e) {
175 LOGGER.error("save CustomRouteInfo [serviceName]"+customRouteInfo.getServiceName()+" [host]"+customRouteInfo.getHost() +" [publish_port]"+customRouteInfo.getPublish_port()+" [routeWay]"+routeWay+" throw exception", e);
180 return customRouteInfo;
186 * @Title: deleteCustomRoute
187 * @Description: TODO(delete one CustomRoute)
188 * @param: @param type
189 * @param: @param serviceName
190 * @param: @param delKey
194 public synchronized void deleteCustomRoute(String serviceName, String host, String publish_port,
197 RouteUtil.checkRouteWay(routeWay);
199 String customRedisPrefixedKey =
200 RouteUtil.getRedisPrefixedKey(RouteUtil.CUSTOMROUTE, serviceName, host, publish_port,
204 CustomRouteService.getInstance().deleteCustomRouteService2Redis(customRedisPrefixedKey);
205 LOGGER.info("delete CustomRouteInfo [serviceName]"+serviceName+" [host]"+host +" [publish_port]"+publish_port+" [routeWay]"+routeWay+" success");
207 } catch (ExtendedNotFoundException e) {
209 } catch (Exception e) {
210 LOGGER.error("delete CustomRouteInfo [serviceName]"+serviceName+" [host]"+host +" [publish_port]"+publish_port+" [routeWay]"+routeWay+" throw exception", e);
211 throw new ExtendedInternalServerErrorException("delete CustomRouteInfo [serviceName]"+serviceName+e.getMessage());