78c2ebb69c4d77ed3fdef80d8e3c786012c5a1af
[nfvbench.git] / nfvbench / config_plugin.py
1 #!/usr/bin/env python
2 # Copyright 2016 Cisco Systems, Inc.  All rights reserved.
3 #
4 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
5 #    not use this file except in compliance with the License. You may obtain
6 #    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, WITHOUT
12 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 #    License for the specific language governing permissions and limitations
14 #    under the License.
15 #
16
17 import abc
18 import specs
19
20
21 class ConfigPluginBase(object):
22     """Base class for config plugins. Need to implement public interfaces."""
23     __metaclass__ = abc.ABCMeta
24
25     class InitializationFailure(Exception):
26         pass
27
28     def __init__(self, config):
29         if not config:
30             raise ConfigPluginBase.InitializationFailure(
31                 'Initialization parameters need to be assigned.')
32
33         self.config = config
34
35     @abc.abstractmethod
36     def get_config(self):
37         """Returns updated default configuration file."""
38
39     def set_config(self, config):
40         """This method is called when the config has changed after this instance was initialized.
41
42         This is needed in teh frequent case where the main config is changed in a copy and to
43         prevent this instance to keep pointing to the old copy of the config
44         """
45         self.config = config
46
47     @abc.abstractmethod
48     def get_openstack_spec(self):
49         """Returns OpenStack specs for host."""
50
51     @abc.abstractmethod
52     def get_run_spec(self, openstack_spec):
53         """Returns RunSpec for given platform."""
54
55     @abc.abstractmethod
56     def validate_config(self, cfg, openstack_spec):
57         """Validate config file."""
58
59     @abc.abstractmethod
60     def prepare_results_config(self, cfg):
61         """This function is called before running configuration is copied.
62         Example usage is to remove sensitive information like switch credentials.
63         """
64
65     @abc.abstractmethod
66     def get_version(self):
67         """Returns platform version."""
68
69
70 class ConfigPlugin(ConfigPluginBase):
71     """No-op config plugin class. Does not change anything."""
72
73     def __init__(self, config):
74         ConfigPluginBase.__init__(self, config)
75
76     def get_config(self):
77         """Public interface for updating config file. Just returns given config."""
78         return self.config
79
80     def get_openstack_spec(self):
81         """Returns OpenStack specs for host."""
82         return specs.OpenStackSpec()
83
84     def get_run_spec(self, openstack_spec):
85         """Returns RunSpec for given platform."""
86         return specs.RunSpec(self.config.no_vswitch_access, openstack_spec)
87
88     def validate_config(self, config, openstack_spec):
89         pass
90
91     def prepare_results_config(self, cfg):
92         return cfg
93
94     def get_version(self):
95         return {}