blob: 0ccbed8c29adc15b798f6af442d2131305cfc78c [file] [log] [blame]
Girish Gowdru141ced82018-09-17 20:19:14 -07001#
2# Copyright 2018 the original author or authors.
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
17"""Resource KV store - interface between Resource Manager and backend store."""
18import structlog
19
20from voltha.core.config.config_backend import ConsulStore
21from voltha.core.config.config_backend import EtcdStore
22
23# KV store uses this prefix to store resource info
24PATH_PREFIX = 'resource_manager/{}/{}'
25
26
27class ResourceKvStore(object):
28 """Implements apis to store/get/remove resource in backend store."""
29
30 def __init__(self, technology, device_id, backend, host, port):
31 """
32 Create ResourceKvStore object.
33
34 Based on backend ('consul' and 'etcd' use the host and port
35 to create the respective object.
36
37 :param technology: PON technology
38 :param device_id: OLT device id
39 :param backend: Type of backend storage (etcd or consul)
40 :param host: host ip info for backend storage
41 :param port: port for the backend storage
42 :raises exception when invalid backend store passed as an argument
43 """
44 # logger
45 self._log = structlog.get_logger()
46
47 path = PATH_PREFIX.format(technology, device_id)
48 try:
49 if backend == 'consul':
50 self._kv_store = ConsulStore(host, port, path)
51 self._recurse = '?recurse'
52 elif backend == 'etcd':
53 self._kv_store = EtcdStore(host, port, path)
54 self._recurse = ''
55 else:
56 self._log.error('Invalid-backend')
57 raise Exception("Invalid-backend-for-kv-store")
58 except Exception as e:
59 self._log.exception("exception-in-init")
60 raise Exception(e)
61
62 def update_to_kv_store(self, path, resource):
63 """
64 Update resource.
65
66 :param path: path to update the resource
67 :param resource: updated resource
68 """
69 try:
70 self._kv_store[path] = str(resource)
71 self._log.debug("Resource-updated-in-kv-store", path=path)
72 return True
73 except BaseException:
74 self._log.exception("Resource-update-in-kv-store-failed",
75 path=path, resource=resource)
76 return False
77
78 def get_from_kv_store(self, path):
79 """
80 Get resource.
81
82 :param path: path to get the resource
83 """
84 resource = None
85 try:
86 resource = self._kv_store[path]
87 self._log.debug("Got-resource-from-kv-store", path=path)
88 except KeyError:
89 self._log.info("Resource-not-found-updating-resource",
90 path=path)
91 except BaseException:
92 self._log.exception("Getting-resource-from-kv-store-failed",
93 path=path)
94 return resource
95
96 def remove_from_kv_store(self, path):
97 """
98 Remove resource.
99
100 :param path: path to remove the resource
101 """
102 path = path + self._recurse
103 try:
104 del self._kv_store[path]
105 self._log.debug("Resource-deleted-in-kv-store", path=path)
106 return True
107 except BaseException:
108 self._log.exception("Resource-delete-in-kv-store-failed",
109 path=path)
110 return False