Initial commit
[ta/config-manager.git] / cmframework / src / cmframework / server / cmvalidator.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 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
15 import logging
16
17 from cmframework.utils.cmpluginloader import CMPluginLoader
18 from cmframework.utils.cmpluginmanager import CMPluginManager
19
20
21 class CMValidator(CMPluginManager):
22     def __init__(self, plugins_path, plugin_client):
23         logging.info('Validator constructor, plugins_path is %s', plugins_path)
24         CMPluginManager.__init__(self, plugins_path)
25         self.load_plugin()
26         self.plugin_client = plugin_client
27
28     def load_plugin(self):
29         pl = CMPluginLoader(self.plugins_path)
30         self.pluginlist, self.filterdict = pl.load()
31         logging.info('Plugin(s): %r', self.pluginlist)
32         logging.info('Subscription(s): %r', self.filterdict)
33
34     def validate_delete(self, indata):
35         self.validate_plugins(indata, 'validate_delete')
36
37     def validate_set(self, indata):
38         self.validate_plugins(indata, 'validate_set')
39
40     def validate_plugins(self, indata, operation):
41         # import pdb; pdb.set_trace()
42         logging.debug('validate_plugins called with data %s', indata)
43         for plugin, objectname in self.pluginlist.iteritems():
44             filtername = self.filterdict[plugin]
45             inputdata = self.build_input(indata, filtername)
46             if inputdata:
47                 logging.debug('Calling validation plugin %s with %s', plugin, inputdata)
48                 class_name = getattr(objectname, plugin)
49                 instance = class_name()
50                 instance.plugin_client = self.plugin_client
51                 try:
52                     func = getattr(instance, operation)
53                     func(inputdata)
54                 except AttributeError:
55                     logging.info('Plugin %s does have function %s implemented', plugin, operation)