NFVBENCH-153 Add support for python3
[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 """Configuration Plugin.
17
18 This module is used to override the configuration with platform specific constraints and extensions
19 """
20 import abc
21 from . import specs
22
23
24 class ConfigPluginBase(object, metaclass=abc.ABCMeta):
25     """Base class for config plugins."""
26
27     class InitializationFailure(Exception):
28         """Used in case of any init failure."""
29
30     def __init__(self, config):
31         """Save configuration."""
32         if not config:
33             raise ConfigPluginBase.InitializationFailure(
34                 'Initialization parameters need to be assigned.')
35         self.config = config
36
37     @abc.abstractmethod
38     def get_config(self):
39         """Return updated default configuration file."""
40
41     def set_config(self, config):
42         """Set a new configuration.
43
44         This method is called when the config has changed after this instance was initialized.
45         This is needed in the frequent case where the main config is changed in a copy and to
46         prevent this instance to keep pointing to the old copy of the config
47         """
48         self.config = config
49
50     @abc.abstractmethod
51     def get_openstack_spec(self):
52         """Return OpenStack specs for host."""
53
54     @abc.abstractmethod
55     def get_run_spec(self, config, openstack_spec):
56         """Return RunSpec for given platform."""
57
58     @abc.abstractmethod
59     def validate_config(self, cfg, openstack_spec):
60         """Validate config file."""
61
62     @abc.abstractmethod
63     def prepare_results_config(self, cfg):
64         """Insert any plugin specific information to the results.
65
66         This function is called before running configuration is copied.
67         Example usage is to remove sensitive information like switch credentials.
68         """
69
70     @abc.abstractmethod
71     def get_version(self):
72         """Return platform version."""
73
74
75 class ConfigPlugin(ConfigPluginBase):
76     """No-op config plugin class. Does not change anything."""
77
78     def __init__(self, config):
79         """Invoke the base class constructor."""
80         ConfigPluginBase.__init__(self, config)
81
82     def get_config(self):
83         """Public interface for updating config file. Just returns given config."""
84         return self.config
85
86     def get_openstack_spec(self):
87         """Return OpenStack specs for host."""
88         return specs.OpenStackSpec()
89
90     def get_run_spec(self, config, openstack_spec):
91         """Return RunSpec for given platform."""
92         return specs.RunSpec(config.no_vswitch_access, openstack_spec)
93
94     def validate_config(self, config, openstack_spec):
95         """Nothing to validate by default."""
96
97     def prepare_results_config(self, cfg):
98         """Nothing to add the results by default."""
99         return cfg
100
101     def get_version(self):
102         """Return an empty version."""
103         return {}