9ed0455c70083b8b49d330fff734d9e0c4ea6891
[msb/apigateway.git] /
1 /*******************************************************************************
2  * Copyright 2016-2017 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.dao.service;
17
18 import com.fasterxml.jackson.core.JsonProcessingException;
19
20 import java.io.IOException;
21 import java.util.ArrayList;
22 import java.util.List;
23
24 import org.onap.msb.apiroute.wrapper.dao.RedisAccessWrapper;
25 import org.onap.msb.apiroute.wrapper.dao.service.bean.ServiceInfo;
26 import org.onap.msb.apiroute.wrapper.util.Jackson;
27
28 public class ServiceDAOImpl implements  IServiceDAO{
29     public void saveService(String key, ServiceInfo serviceInfo) throws Exception {
30         String serviceInfoStr = null;
31         try {
32             serviceInfoStr = Jackson.MAPPER.writeValueAsString(serviceInfo);
33         } catch (JsonProcessingException e) {
34             throw new Exception("error occurred while parsing ServiceInfo to json data",e);
35         }
36         RedisAccessWrapper.save(key, serviceInfoStr);
37     }
38
39     public ServiceInfo queryService(String key) throws Exception {
40         ServiceInfo serviceInfo = null;
41         String serviceInfoStr = RedisAccessWrapper.query(key);
42         if (null == serviceInfoStr || "".equals(serviceInfoStr))
43             return null;
44         try {
45             serviceInfo = Jackson.MAPPER.readValue(serviceInfoStr, ServiceInfo.class);
46         } catch (IOException e) {
47             throw new Exception("error occurred while parsing the redis json data to ServiceInfo",e);
48         }
49         return serviceInfo;
50     }
51
52     public List<ServiceInfo> queryMultiService(String keyPattern) throws Exception {
53         List<String> serviceInfoStrList = RedisAccessWrapper.queryMultiKeys(keyPattern);
54         List<ServiceInfo> routeInfoList = new ArrayList<>();
55         for (String serviceInfoStr : serviceInfoStrList) {
56             ServiceInfo serviceInfo = null;
57             try {
58                 serviceInfo = Jackson.MAPPER.readValue(serviceInfoStr, ServiceInfo.class);
59                 routeInfoList.add(serviceInfo);
60             } catch (IOException e) {
61                 throw new Exception("error occurred while parsing the redis json data to ServiceInfo",e);
62             }
63         }
64         return routeInfoList;
65     }
66
67     public long deleteService(String key) throws Exception {
68         return RedisAccessWrapper.delete(key);
69     }
70
71     public long deleteMultiService(String keyPattern) throws Exception{
72         return RedisAccessWrapper.deleteMultiKeys(keyPattern);
73     }
74 }