Divide the MSB source codes into two repos
[msb/apigateway.git] / apiroute / apiroute-service / src / main / java / org / onap / msb / apiroute / wrapper / CustomRouteServiceWrapper.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.util.List;
19
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;
27
28 public class CustomRouteServiceWrapper {
29
30
31   private static final Logger LOGGER = LoggerFactory.getLogger(CustomRouteServiceWrapper.class);
32
33   private static CustomRouteServiceWrapper instance = new CustomRouteServiceWrapper();
34
35   private CustomRouteServiceWrapper() {}
36
37   public static CustomRouteServiceWrapper getInstance() {
38     return instance;
39   }
40
41
42   /**
43    * @Title: getAllCustomRouteService
44    * @Description: TODO(get AllCustomRoute Service)
45    * @param: @return
46    * @return: CustomRouteInfo[]
47    */
48   public List<CustomRouteInfo> getAllCustomRouteInstances(String routeWay) {
49
50     RouteUtil.checkRouteWay(routeWay);
51     
52     try {
53       String customRedisKey = RouteUtil.getMutiRedisKey(RouteUtil.CUSTOMROUTE, routeWay);
54       return CustomRouteService.getInstance().getMultiCustomRouteInstances(customRedisKey);
55
56     } catch (Exception e) {
57       throw new ExtendedInternalServerErrorException(e.getMessage());
58     }
59
60
61   }
62
63
64
65   /**
66    * @Title: getCustomRouteInstance
67    * @Description: TODO(get CustomRouteInstance by serviceName)
68    * @param: @param serviceName
69    * @param: @return
70    * @return: CustomRouteInfo
71    */
72   public CustomRouteInfo getCustomRouteInstance(String serviceName, String host,
73       String publish_port, String routeWay) {
74
75     RouteUtil.checkRouteWay(routeWay);
76
77     String customRedisPrefixedKey =
78         RouteUtil.getRedisPrefixedKey(RouteUtil.CUSTOMROUTE, serviceName, host, publish_port,
79             routeWay);
80
81     CustomRouteInfo customRouteInfo;
82     try {
83       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());
88     }
89
90
91     if (null == customRouteInfo) {
92       throw new ExtendedNotFoundException("customRoute Info not found");
93
94     }
95
96     return customRouteInfo;
97
98   }
99
100
101   /**
102    * @Title updateCustomRouteStatus
103    * @Description TODO(update one CustomRoute Status)
104    * @param serviceName
105    * @param status
106    * @return
107    * @return RouteResult
108    */
109   public synchronized CustomRouteInfo updateCustomRouteStatus(String serviceName, String host,
110       String publish_port, String status, String routeWay) {
111
112     RouteUtil.checkRouteWay(routeWay);
113
114     RouteUtil.checkServiceStatus(status);
115
116     String customRedisPrefixedKey =
117         RouteUtil.getRedisPrefixedKey(RouteUtil.CUSTOMROUTE, serviceName, host, publish_port,
118             routeWay);
119
120
121     try {
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());
127     }
128
129     CustomRouteInfo new_customRouteInfo =
130         getCustomRouteInstance(serviceName, host, publish_port, routeWay);
131
132     return new_customRouteInfo;
133   }
134
135   /**
136    * @Title: saveCustomRouteInstance
137    * @Description: TODO(save one CustomRouteInstance)
138    * @param: @param CustomRouteInfo
139    * @param: @return
140    * @return: CustomRouteInfo
141    */
142   public synchronized CustomRouteInfo saveCustomRouteInstance4Rest(CustomRouteInfo customRouteInfo,
143       String routeWay) {
144
145     RouteUtil.checkRouteWay(routeWay);
146
147     RouteUtil.checkRouterInfoFormat(customRouteInfo);
148
149     try {
150       saveCustomRouteInstance(customRouteInfo, routeWay);
151
152     } catch (Exception e) {
153
154       throw new ExtendedInternalServerErrorException("save CustomRouteInfo  fail: [serviceName]"+customRouteInfo.getServiceName()+e.getMessage());
155     }
156
157     return customRouteInfo;
158
159   }
160
161
162   public synchronized CustomRouteInfo saveCustomRouteInstance(CustomRouteInfo customRouteInfo,
163       String routeWay) throws Exception {
164     try {
165     String customRedisPrefixedKey =
166         RouteUtil.getRedisPrefixedKey(RouteUtil.CUSTOMROUTE, customRouteInfo.getServiceName(),
167             customRouteInfo.getHost(), customRouteInfo.getPublish_port(), routeWay);;
168
169   
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");
173
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);
176
177       throw e;
178     }
179    
180     return customRouteInfo;
181
182   }
183
184
185   /**
186    * @Title: deleteCustomRoute
187    * @Description: TODO(delete one CustomRoute)
188    * @param: @param type
189    * @param: @param serviceName
190    * @param: @param delKey
191    * @param: @return
192    * @return: void
193    */
194   public synchronized void deleteCustomRoute(String serviceName, String host, String publish_port,
195       String routeWay) {
196
197     RouteUtil.checkRouteWay(routeWay);
198
199     String customRedisPrefixedKey =
200         RouteUtil.getRedisPrefixedKey(RouteUtil.CUSTOMROUTE, serviceName, host, publish_port,
201             routeWay);
202
203     try {
204       CustomRouteService.getInstance().deleteCustomRouteService2Redis(customRedisPrefixedKey);
205       LOGGER.info("delete CustomRouteInfo [serviceName]"+serviceName+" [host]"+host +" [publish_port]"+publish_port+" [routeWay]"+routeWay+" success");
206
207     } catch (ExtendedNotFoundException e) {
208       throw 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());
212     }
213
214
215
216   }
217 }