Divide the MSB source codes into two repos
[msb/apigateway.git] / apiroute / apiroute-service / src / main / java / org / onap / msb / apiroute / wrapper / IuiRouteServiceWrapper.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.IuiRouteInfo;
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.IuiRouteService;
24 import org.onap.msb.apiroute.wrapper.util.RouteUtil;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 public class IuiRouteServiceWrapper {
29
30
31   private static final Logger LOGGER = LoggerFactory.getLogger(IuiRouteServiceWrapper.class);
32
33   private static IuiRouteServiceWrapper instance = new IuiRouteServiceWrapper();
34
35   private IuiRouteServiceWrapper() {}
36
37   public static IuiRouteServiceWrapper getInstance() {
38     return instance;
39   }
40
41
42
43   /**
44    * @Title: getAllIuiRouteService
45    * @Description: TODO(get All IuiRouteServices)
46    * @param: @return
47    * @return: IuiRouteInfo[]
48    */
49   public List<IuiRouteInfo> getAllIuiRouteInstances(String routeWay) {
50
51     RouteUtil.checkRouteWay(routeWay);
52     
53     try {
54       String iuiRedisKey = RouteUtil.getMutiRedisKey(RouteUtil.IUIROUTE, routeWay);
55
56       return IuiRouteService.getInstance().getMultiIuiRouteInstances(iuiRedisKey);
57
58     } catch (Exception e) {
59       throw new ExtendedInternalServerErrorException(e.getMessage());
60     }
61
62
63   }
64
65
66
67   /**
68    * @Title: getIuiRouteInstance
69    * @Description: TODO(get one IuiRouteInstance by serviceName)
70    * @param: @param serviceName
71    * @param: @return
72    * @return: IuiRouteInfo
73    */
74   public IuiRouteInfo getIuiRouteInstance(String serviceName, String host, String publish_port,
75       String routeWay) {
76
77     RouteUtil.checkRouteWay(routeWay);
78
79     String iuiRedisPrefixedKey =
80         RouteUtil
81             .getRedisPrefixedKey(RouteUtil.IUIROUTE, serviceName, host, publish_port, routeWay);
82
83     IuiRouteInfo iuiRouteInfo;
84     try {
85       iuiRouteInfo = IuiRouteService.getInstance().getIuiRouteInstance(iuiRedisPrefixedKey);
86     } catch (Exception e) {
87       LOGGER.error("get IuiRouteInstance throw exception", e);
88       throw new ExtendedInternalServerErrorException("get IuiRouteInstance throw exception"
89           + e.getMessage());
90     }
91
92
93
94     if (null == iuiRouteInfo) {
95       throw new ExtendedNotFoundException("iui RouteInfo not found");
96     }
97
98     return iuiRouteInfo;
99   }
100
101
102
103   /**
104    * @Title updateIuiRouteStatus
105    * @Description TODO(update one IuiRoute Status)
106    * @param serviceName
107    * @param status
108    * @return
109    * @return RouteResult
110    */
111   public synchronized IuiRouteInfo updateIuiRouteStatus(String serviceName, String host,
112       String publish_port, String status, String routeWay) {
113
114     RouteUtil.checkRouteWay(routeWay);
115
116     RouteUtil.checkServiceStatus(status);
117
118     try {
119       String iuiRedisPrefixedKey =
120           RouteUtil.getRedisPrefixedKey(RouteUtil.IUIROUTE, serviceName, host, publish_port,
121               routeWay);
122
123       IuiRouteService.getInstance().updateIuiRouteStatus2Redis(iuiRedisPrefixedKey, status);
124
125
126     } catch (Exception e) {
127       LOGGER.error("update IuiRoute status  throw exception", e);
128       throw new ExtendedInternalServerErrorException(e.getMessage());
129     }
130
131     IuiRouteInfo new_iuiRouteInfo = getIuiRouteInstance(serviceName, host, publish_port, routeWay);
132
133     return new_iuiRouteInfo;
134   }
135
136   /**
137    * @Title: saveIuiRouteInstance
138    * @Description: TODO(save one IuiRouteInstance)
139    * @param: @param IuiRouteInfo
140    * @param: @return
141    * @return: IuiRouteInfo
142    */
143   public synchronized IuiRouteInfo saveIuiRouteInstance4Rest(IuiRouteInfo iuiRouteInfo,
144       String routeWay) {
145
146     RouteUtil.checkRouteWay(routeWay);
147
148     RouteUtil.checkRouterInfoFormat(iuiRouteInfo);
149
150
151     try {
152       saveIuiRouteInstance(iuiRouteInfo, routeWay);
153     } catch (Exception e) {
154       throw new ExtendedInternalServerErrorException("save iuiRouteInfo  fail: [serviceName]"
155           + iuiRouteInfo.getServiceName() + e.getMessage());
156     }
157
158     return iuiRouteInfo;
159   }
160
161
162   public synchronized void saveIuiRouteInstance(IuiRouteInfo iuiRouteInfo, String routeWay)
163       throws Exception {
164     try {
165       String iuiRedisPrefixedKey =
166           RouteUtil.getRedisPrefixedKey(RouteUtil.IUIROUTE, iuiRouteInfo.getServiceName(),
167               iuiRouteInfo.getHost(), iuiRouteInfo.getPublish_port(), routeWay);
168
169       IuiRouteService.getInstance().saveIuiRouteService2Redis(iuiRouteInfo, iuiRedisPrefixedKey);
170       LOGGER.info("save iuiRouteInfo [serviceName]" + iuiRouteInfo.getServiceName() + " [host]"
171           + iuiRouteInfo.getHost() + " [publish_port]" + iuiRouteInfo.getPublish_port()
172           + " [routeWay]" + routeWay + " success");
173
174     } catch (Exception e) {
175       LOGGER.error("save iuiRouteInfo [serviceName]" + iuiRouteInfo.getServiceName() + " [host]"
176           + iuiRouteInfo.getHost() + " [publish_port]" + iuiRouteInfo.getPublish_port()
177           + " [routeWay]" + routeWay + " throw exception", e);
178       throw e;
179     }
180   }
181
182
183
184   /**
185    * @Title: deleteIuiRoute
186    * @Description: TODO(delete one IuiRoute)
187    * @param: @param type
188    * @param: @param serviceName
189    * @param: @param delKey
190    * @param: @return
191    * @return: void
192    */
193   public synchronized void deleteIuiRoute(String serviceName, String host, String publish_port,
194       String routeWay) {
195
196     RouteUtil.checkRouteWay(routeWay);
197
198     String iuiRedisPrefixedKey =
199         RouteUtil
200             .getRedisPrefixedKey(RouteUtil.IUIROUTE, serviceName, host, publish_port, routeWay);
201
202     try {
203       IuiRouteService.getInstance().deleteIuiRouteService2Redis(iuiRedisPrefixedKey);
204       LOGGER.info("delete iuiRouteInfo [serviceName]" + serviceName + " [host]" + host
205           + " [publish_port]" + publish_port + " [routeWay]" + routeWay + " success");
206
207     } catch (ExtendedNotFoundException e) {
208       throw e;
209     } catch (Exception e) {
210       LOGGER.error("delete iuiRouteInfo [serviceName]" + serviceName + " [host]" + host
211           + " [publish_port]" + publish_port + " [routeWay]" + routeWay + " throw exception", e);
212       throw new ExtendedInternalServerErrorException("delete iuiRouteInfo [serviceName]" + serviceName +e.getMessage());
213     }
214
215
216
217   }
218
219
220
221 }