1 package org.onap.msb.apiroute.wrapper.dao.route;
3 import com.fasterxml.jackson.core.JsonProcessingException;
5 import java.io.IOException;
6 import java.util.ArrayList;
9 import org.onap.msb.apiroute.wrapper.dao.RedisAccessWrapper;
10 import org.onap.msb.apiroute.wrapper.dao.route.bean.RouteInfo;
11 import org.onap.msb.apiroute.wrapper.util.Jackson;
13 public class RouteDAOImpl implements IRouteDAO{
14 public void saveRoute(String key, RouteInfo routeInfo) throws Exception {
15 String routeInfoStr = null;
16 // change orginal url from “/” to empty string accord to the rewrite rule while forwarding
17 if("/".equals(routeInfo.getSpec().getUrl())){
18 routeInfo.getSpec().setUrl("");
21 routeInfoStr = Jackson.MAPPER.writeValueAsString(routeInfo);
22 } catch (JsonProcessingException e) {
23 throw new Exception("error occurred while parsing RouteInfo to json data",e);
25 RedisAccessWrapper.save(key, routeInfoStr);
28 public RouteInfo queryRoute(String key) throws Exception {
29 RouteInfo routeInfo = null;
30 String routeInfoStr = RedisAccessWrapper.query(key);
31 if (null == routeInfoStr || "".equals(routeInfoStr))
34 routeInfo = Jackson.MAPPER.readValue(routeInfoStr, RouteInfo.class);
35 } catch (IOException e) {
36 throw new Exception("error occurred while parsing the redis json data to RouteInfo",e);
41 public List<RouteInfo> queryMultiRoute(String keyPattern) throws Exception {
42 List<String> routeInfoStrList = RedisAccessWrapper.queryMultiKeys(keyPattern);
43 List<RouteInfo> routeInfoList = new ArrayList<>();
44 for (String routeInfoStr : routeInfoStrList) {
45 RouteInfo routeInfo = null;
47 routeInfo = Jackson.MAPPER.readValue(routeInfoStr, RouteInfo.class);
48 routeInfoList.add(routeInfo);
49 } catch (IOException e) {
50 throw new Exception("error occurred while parsing the redis json data to RouteInfo",e);
56 public long deleteRoute(String key) throws Exception {
57 return RedisAccessWrapper.delete(key);
60 public long deleteMultiRoute(String keyPattern) throws Exception{
61 return RedisAccessWrapper.deleteMultiKeys(keyPattern);