Shad Ansari | e9c2db8 | 2019-01-22 15:05:36 -0800 | [diff] [blame^] | 1 | # |
| 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.""" |
| 18 | import structlog |
| 19 | |
| 20 | from voltha.core.config.config_backend import ConsulStore |
| 21 | from voltha.core.config.config_backend import EtcdStore |
| 22 | |
| 23 | # KV store uses this prefix to store resource info |
| 24 | PATH_PREFIX = 'service/voltha/resource_manager/{}' |
| 25 | |
| 26 | |
| 27 | class 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) |
| 48 | try: |
| 49 | if backend == 'consul': |
| 50 | self._kv_store = ConsulStore(host, port, path) |
| 51 | elif backend == 'etcd': |
| 52 | self._kv_store = EtcdStore(host, port, path) |
| 53 | else: |
| 54 | self._log.error('Invalid-backend') |
| 55 | raise Exception("Invalid-backend-for-kv-store") |
| 56 | except Exception as e: |
| 57 | self._log.exception("exception-in-init") |
| 58 | raise Exception(e) |
| 59 | |
| 60 | def update_to_kv_store(self, path, resource): |
| 61 | """ |
| 62 | Update resource. |
| 63 | |
| 64 | :param path: path to update the resource |
| 65 | :param resource: updated resource |
| 66 | """ |
| 67 | try: |
| 68 | self._kv_store[path] = str(resource) |
| 69 | self._log.debug("Resource-updated-in-kv-store", path=path) |
| 70 | return True |
| 71 | except BaseException: |
| 72 | self._log.exception("Resource-update-in-kv-store-failed", |
| 73 | path=path, resource=resource) |
| 74 | return False |
| 75 | |
| 76 | def get_from_kv_store(self, path): |
| 77 | """ |
| 78 | Get resource. |
| 79 | |
| 80 | :param path: path to get the resource |
| 81 | """ |
| 82 | resource = None |
| 83 | try: |
| 84 | resource = self._kv_store[path] |
| 85 | self._log.debug("Got-resource-from-kv-store", path=path) |
| 86 | except KeyError: |
| 87 | self._log.info("Resource-not-found-updating-resource", |
| 88 | path=path) |
| 89 | except BaseException: |
| 90 | self._log.exception("Getting-resource-from-kv-store-failed", |
| 91 | path=path) |
| 92 | return resource |
| 93 | |
| 94 | def remove_from_kv_store(self, path): |
| 95 | """ |
| 96 | Remove resource. |
| 97 | |
| 98 | :param path: path to remove the resource |
| 99 | """ |
| 100 | try: |
| 101 | del self._kv_store[path] |
| 102 | self._log.debug("Resource-deleted-in-kv-store", path=path) |
| 103 | return True |
| 104 | except BaseException: |
| 105 | self._log.exception("Resource-delete-in-kv-store-failed", |
| 106 | path=path) |
| 107 | return False |