Initial commit
[ta/config-manager.git] / cmframework / src / cmframework / utils / cmdsshandler.py
1 # Copyright 2019 Nokia
2
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # 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
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 from __future__ import print_function
15 import logging
16 from urlparse import urlparse
17
18 from cmframework.apis import cmerror
19 from cmframework.apis import cmstate
20 from dss.client import dss_client
21 from dss.api import dss_error
22
23
24 class CMDSSHandler(cmstate.CMState):
25     def __init__(self, **kw):
26         uridata = urlparse(kw['uri'])
27         socket = uridata.path
28         self.client = dss_client.Client(socket)
29
30     def _domain_exists(self, domain):
31         return domain in self.get_domains()
32
33     def _name_exists_in_domain(self, domain, name):
34         return name in self.get_domain(domain)
35
36     def get(self, domain, name):
37         logging.debug('get called for %s %s', domain, name)
38
39         if self._domain_exists(domain):
40             try:
41                 domain_data = self.client.get_domain(domain)
42                 return domain_data.get(name, None)
43             except dss_error.Error as ex:
44                 raise cmerror.CMError(str(ex))
45
46         return None
47
48     def get_domain(self, domain):
49         logging.debug('get_domain called for %s', domain)
50
51         if self._domain_exists(domain):
52             try:
53                 return self.client.get_domain(domain)
54             except dss_error.Error as ex:
55                 raise cmerror.CMError(str(ex))
56
57         return None
58
59     def set(self, domain, name, value):
60         logging.debug('set called for setting %s %s=%s', domain, name, value)
61         try:
62             return self.client.set(domain, name, value)
63         except dss_error.Error as ex:
64             raise cmerror.CMError(str(ex))
65
66     def get_domains(self):
67         logging.debug('get_domains called')
68         try:
69             return self.client.get_domains()
70         except dss_error.Error as ex:
71             raise cmerror.CMError(str(ex))
72
73     def delete(self, domain, name):
74         logging.debug('delete called for %s %s', domain, name)
75
76         if self._domain_exists(domain):
77             if self._name_exists_in_domain(domain, name):
78                 try:
79                     self.client.delete(domain, name)
80                 except dss_error.Error as ex:
81                     raise cmerror.CMError(str(ex))
82
83     def delete_domain(self, domain):
84         logging.debug('delete_domain called for %s', domain)
85
86         if self._domain_exists(domain):
87             try:
88                 self.client.delete_domain(domain)
89             except dss_error.Error as ex:
90                 raise cmerror.CMError(str(ex))