Divide the MSB source codes into two repos
[msb/apigateway.git] / apiroute / apiroute-service / src / main / java / org / onap / msb / apiroute / wrapper / dao / service / ServiceDAOImpl.java
1 package org.onap.msb.apiroute.wrapper.dao.service;
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.service.bean.ServiceInfo;
11 import org.onap.msb.apiroute.wrapper.util.Jackson;
12
13 public class ServiceDAOImpl implements  IServiceDAO{
14     public void saveService(String key, ServiceInfo serviceInfo) throws Exception {
15         String serviceInfoStr = null;
16         try {
17             serviceInfoStr = Jackson.MAPPER.writeValueAsString(serviceInfo);
18         } catch (JsonProcessingException e) {
19             throw new Exception("error occurred while parsing ServiceInfo to json data",e);
20         }
21         RedisAccessWrapper.save(key, serviceInfoStr);
22     }
23
24     public ServiceInfo queryService(String key) throws Exception {
25         ServiceInfo serviceInfo = null;
26         String serviceInfoStr = RedisAccessWrapper.query(key);
27         if (null == serviceInfoStr || "".equals(serviceInfoStr))
28             return null;
29         try {
30             serviceInfo = Jackson.MAPPER.readValue(serviceInfoStr, ServiceInfo.class);
31         } catch (IOException e) {
32             throw new Exception("error occurred while parsing the redis json data to ServiceInfo",e);
33         }
34         return serviceInfo;
35     }
36
37     public List<ServiceInfo> queryMultiService(String keyPattern) throws Exception {
38         List<String> serviceInfoStrList = RedisAccessWrapper.queryMultiKeys(keyPattern);
39         List<ServiceInfo> routeInfoList = new ArrayList<>();
40         for (String serviceInfoStr : serviceInfoStrList) {
41             ServiceInfo serviceInfo = null;
42             try {
43                 serviceInfo = Jackson.MAPPER.readValue(serviceInfoStr, ServiceInfo.class);
44                 routeInfoList.add(serviceInfo);
45             } catch (IOException e) {
46                 throw new Exception("error occurred while parsing the redis json data to ServiceInfo",e);
47             }
48         }
49         return routeInfoList;
50     }
51
52     public long deleteService(String key) throws Exception {
53         return RedisAccessWrapper.delete(key);
54     }
55
56     public long deleteMultiService(String keyPattern) throws Exception{
57         return RedisAccessWrapper.deleteMultiKeys(keyPattern);
58     }
59 }