Divide the MSB source codes into two repos
[msb/apigateway.git] / apiroute / apiroute-service / src / main / java / org / onap / msb / apiroute / wrapper / dao / route / RouteDAOImpl.java
1 package org.onap.msb.apiroute.wrapper.dao.route;
2
3 import com.fasterxml.jackson.core.JsonProcessingException;
4
5 import java.io.IOException;
6 import java.util.ArrayList;
7 import java.util.List;
8
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;
12
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("");
19         }
20         try {
21             routeInfoStr = Jackson.MAPPER.writeValueAsString(routeInfo);
22         } catch (JsonProcessingException e) {
23             throw new Exception("error occurred while parsing RouteInfo to json data",e);
24         }
25         RedisAccessWrapper.save(key, routeInfoStr);
26     }
27
28     public RouteInfo queryRoute(String key) throws Exception {
29         RouteInfo routeInfo = null;
30         String routeInfoStr = RedisAccessWrapper.query(key);
31         if (null == routeInfoStr || "".equals(routeInfoStr))
32             return null;
33         try {
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);
37         }
38         return routeInfo;
39     }
40
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;
46             try {
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);
51             }
52         }
53         return routeInfoList;
54     }
55
56     public long deleteRoute(String key) throws Exception {
57         return RedisAccessWrapper.delete(key);
58     }
59
60     public long deleteMultiRoute(String keyPattern) throws Exception{
61         return RedisAccessWrapper.deleteMultiKeys(keyPattern);
62     }
63 }