Fix java check style warning
[msb/apigateway.git] / apiroute / apiroute-service / src / main / java / org / onap / msb / apiroute / wrapper / dao / service / ServiceDAOImpl.java
1 /*******************************************************************************
2  * Copyright 2016-2017 ZTE, Inc. and others.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  * 
7  * http://www.apache.org/licenses/LICENSE-2.0
8  * 
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  ******************************************************************************/
14 package org.onap.msb.apiroute.wrapper.dao.service;
15
16 import java.io.IOException;
17 import java.util.ArrayList;
18 import java.util.List;
19
20 import org.onap.msb.apiroute.wrapper.dao.RedisAccessWrapper;
21 import org.onap.msb.apiroute.wrapper.dao.service.bean.ServiceInfo;
22 import org.onap.msb.apiroute.wrapper.util.Jackson;
23
24 import com.fasterxml.jackson.core.JsonProcessingException;
25
26 public class ServiceDAOImpl implements IServiceDAO {
27     public void saveService(String key, ServiceInfo serviceInfo) throws Exception {
28         String serviceInfoStr = null;
29         try {
30             serviceInfoStr = Jackson.MAPPER.writeValueAsString(serviceInfo);
31         } catch (JsonProcessingException e) {
32             throw new Exception("error occurred while parsing ServiceInfo to json data", e);
33         }
34         RedisAccessWrapper.save(key, serviceInfoStr);
35     }
36
37     public ServiceInfo queryService(String key) throws Exception {
38         ServiceInfo serviceInfo = null;
39         String serviceInfoStr = RedisAccessWrapper.query(key);
40         if (null == serviceInfoStr || "".equals(serviceInfoStr))
41             return null;
42         try {
43             serviceInfo = Jackson.MAPPER.readValue(serviceInfoStr, ServiceInfo.class);
44         } catch (IOException e) {
45             throw new Exception("error occurred while parsing the redis json data to ServiceInfo", e);
46         }
47         return serviceInfo;
48     }
49
50     public List<ServiceInfo> queryMultiService(String keyPattern) throws Exception {
51         List<String> serviceInfoStrList = RedisAccessWrapper.queryMultiKeys(keyPattern);
52         List<ServiceInfo> routeInfoList = new ArrayList<>();
53         for (String serviceInfoStr : serviceInfoStrList) {
54             ServiceInfo serviceInfo = null;
55             try {
56                 serviceInfo = Jackson.MAPPER.readValue(serviceInfoStr, ServiceInfo.class);
57                 routeInfoList.add(serviceInfo);
58             } catch (IOException e) {
59                 throw new Exception("error occurred while parsing the redis json data to ServiceInfo", e);
60             }
61         }
62         return routeInfoList;
63     }
64
65     public long deleteService(String key) throws Exception {
66         return RedisAccessWrapper.delete(key);
67     }
68
69     public long deleteMultiService(String keyPattern) throws Exception {
70         return RedisAccessWrapper.deleteMultiKeys(keyPattern);
71     }
72 }