1 /*******************************************************************************
2 * Copyright 2016-2017 ZTE, Inc. and others.
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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
13 ******************************************************************************/
14 package org.onap.msb.apiroute.wrapper.dao.service;
16 import java.io.IOException;
17 import java.util.ArrayList;
18 import java.util.List;
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;
24 import com.fasterxml.jackson.core.JsonProcessingException;
26 public class ServiceDAOImpl implements IServiceDAO {
27 public void saveService(String key, ServiceInfo serviceInfo) throws Exception {
28 String serviceInfoStr = null;
30 serviceInfoStr = Jackson.MAPPER.writeValueAsString(serviceInfo);
31 } catch (JsonProcessingException e) {
32 throw new Exception("error occurred while parsing ServiceInfo to json data", e);
34 RedisAccessWrapper.save(key, serviceInfoStr);
37 public ServiceInfo queryService(String key) throws Exception {
38 ServiceInfo serviceInfo = null;
39 String serviceInfoStr = RedisAccessWrapper.query(key);
40 if (null == serviceInfoStr || "".equals(serviceInfoStr))
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);
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;
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);
65 public long deleteService(String key) throws Exception {
66 return RedisAccessWrapper.delete(key);
69 public long deleteMultiService(String keyPattern) throws Exception {
70 return RedisAccessWrapper.deleteMultiKeys(keyPattern);