1 /*******************************************************************************
2 * Copyright 2016-2017 ZTE, Inc. and others.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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;
18 import com.fasterxml.jackson.core.JsonProcessingException;
20 import java.io.IOException;
21 import java.util.ArrayList;
22 import java.util.List;
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;
28 public class ServiceDAOImpl implements IServiceDAO{
29 public void saveService(String key, ServiceInfo serviceInfo) throws Exception {
30 String serviceInfoStr = null;
32 serviceInfoStr = Jackson.MAPPER.writeValueAsString(serviceInfo);
33 } catch (JsonProcessingException e) {
34 throw new Exception("error occurred while parsing ServiceInfo to json data",e);
36 RedisAccessWrapper.save(key, serviceInfoStr);
39 public ServiceInfo queryService(String key) throws Exception {
40 ServiceInfo serviceInfo = null;
41 String serviceInfoStr = RedisAccessWrapper.query(key);
42 if (null == serviceInfoStr || "".equals(serviceInfoStr))
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);
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;
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);
67 public long deleteService(String key) throws Exception {
68 return RedisAccessWrapper.delete(key);
71 public long deleteMultiService(String keyPattern) throws Exception{
72 return RedisAccessWrapper.deleteMultiKeys(keyPattern);