Initial commit
[ta/distributed-state-server.git] / src / dss / server / dss_plugin_loader.py
1 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
2 #
3 # Copyright 2019 Nokia
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #     http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17
18 import os
19 import imp
20 import sys
21 import logging
22 from dss.api import dss_error
23 from dss.server import dss_config
24
25 class DSSPluginLoader(object):
26     def __init__(self, config):
27         try:
28             plugin_file = config.get_plugin()
29             location = os.path.dirname(plugin_file)
30             pluginname = os.path.basename(plugin_file).replace(".py", "")
31             sys.path.append(location)
32             self.plugin = None
33             self._load_plugin(pluginname, config.get_plugin_config())
34         except Exception as exp:
35             raise dss_error.Error(str(exp))
36
37     def _load_plugin(self, pluginname, configfile):
38         logging.info('Loading plugin %s' % pluginname)
39         fp, pathname, description = imp.find_module(pluginname)
40         try:
41             pluginmodule = imp.load_module(pluginname, fp, pathname, description)
42             class_name = getattr(pluginmodule, pluginname)
43             self.plugin = class_name(configfile)
44         except Exception as exp:
45             logging.error('Failed to load plugin %s, got exp %s' % (pluginname, str(exp)))
46             raise
47         finally:
48             if fp:
49                 fp.close()
50
51     def set(self, domain, name, value):
52         logging.info("Setting %s/%s to %s" % (domain, name, value))
53         self.plugin.set(domain, name, value)
54
55     def get(self, domain, name):
56         logging.info("Getting attribute %s/%s" % (domain, name))
57         value = self.plugin.get(domain, name)
58         logging.info("Value of %s/%s is %s" % (domain, name, value))
59         return value
60
61     def get_domain(self, domain):
62         logging.info("Getting the attributes of domain %s" % domain)
63         attrs = self.plugin.get_domain(domain)
64         return attrs
65
66     def get_domains(self):
67         logging.info("Getting the domains")
68         domains = self.plugin.get_domains()
69         return domains
70
71     def delete(self, domain, name):
72         logging.info("Deleting %s/%s" % (domain, name))
73         self.plugin.delete(domain, name)
74
75     def delete_domain(self, domain):
76         logging.info("Delete domain %s" % domain)
77         self.plugin.delete_domain(domain)
78
79 if __name__ == '__main__':
80     pl = DSSPluginLoader("configfile")
81     pl.set("domain1", "name1", "value1")