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