Change the signature for config validation function
[nfvbench.git] / nfvbench / nfvbench.py
index 0dcf2f1..d3f7f02 100644 (file)
@@ -21,6 +21,8 @@ from chain_runner import ChainRunner
 from collections import defaultdict
 from config import config_load
 from config import config_loads
+from config import get_err_config
+import copy
 import credentials
 import datetime
 from factory import BasicFactory
@@ -32,6 +34,7 @@ from nfvbenchd import WebSocketIoServer
 import os
 import pbr.version
 from pkg_resources import resource_string
+from specs import ChainType
 from specs import Specs
 from summarizer import NFVBenchSummarizer
 import sys
@@ -86,7 +89,7 @@ class NFVBench(object):
                     "vswitch": self.specs.openstack.vswitch,
                     "encaps": self.specs.openstack.encaps
                 },
-                "config": self.config_plugin.prepare_results_config(dict(self.config)),
+                "config": self.config_plugin.prepare_results_config(copy.deepcopy(self.config)),
                 "benchmarks": {
                     "network": {
                         "service_chain": self.chain_runner.run(),
@@ -119,8 +122,8 @@ class NFVBench(object):
 
     def print_summary(self, result):
         """Print summary of the result"""
-        print NFVBenchSummarizer(result)
-        sys.stdout.flush()
+        summary = NFVBenchSummarizer(result)
+        LOG.info(str(summary))
 
     def save(self, result):
         """Save results in json format file."""
@@ -203,7 +206,7 @@ class NFVBench(object):
                 raise Exception('Please provide existing path for storing results in JSON file. '
                                 'Path used: {path}'.format(path=self.config.std_json_path))
 
-        self.config_plugin.validate_config(self.config)
+        self.config_plugin.validate_config(self.config, self.specs.openstack)
 
 
 def parse_opts_from_cli():
@@ -363,6 +366,11 @@ def parse_opts_from_cli():
                         default=None,
                         help='Override traffic profile direction (requires -fs)')
 
+    parser.add_argument('--log-file', '--logfile', dest='log_file',
+                        action='store',
+                        help='Filename for saving logs',
+                        metavar='<log_file>')
+
     opts, unknown_opts = parser.parse_known_args()
     return opts, unknown_opts
 
@@ -394,10 +402,17 @@ def override_custom_traffic(config, frame_sizes, unidir):
         "profile": traffic_profile_name
     }
 
+def check_physnet(name, netattrs):
+    if not netattrs.physical_network:
+        raise Exception("SRIOV requires physical_network to be specified for the {n} network"
+                        .format(n=name))
+    if not netattrs.segmentation_id:
+        raise Exception("SRIOV requires segmentation_id to be specified for the {n} network"
+                        .format(n=name))
 
 def main():
     try:
-        log.setup('nfvbench')
+        log.setup()
         # load default config file
         config, default_cfg = load_default_config()
         # create factory for platform specific classes
@@ -413,7 +428,7 @@ def main():
         openstack_spec = config_plugin.get_openstack_spec()
 
         opts, unknown_opts = parse_opts_from_cli()
-        log.set_level('nfvbench', debug=opts.debug)
+        log.set_level(debug=opts.debug)
 
         if opts.version:
             print pbr.version.VersionInfo('nfvbench').version_string_with_vcs()
@@ -434,23 +449,51 @@ def main():
             # override default config options with start config at path parsed from CLI
             # check if it is an inline yaml/json config or a file name
             if os.path.isfile(opts.config):
-                print opts.config
+                LOG.info('Loading configuration file: ' + opts.config)
                 config = config_load(opts.config, config)
                 config.name = os.path.basename(opts.config)
             else:
+                LOG.info('Loading configuration string: ' + opts.config)
                 config = config_loads(opts.config, config)
 
+        # Making sure no unknown option is given
+        err_config = get_err_config(config, default_cfg)
+        if err_config:
+            err_msg = 'Unknown options found in config file/string: ' + err_config
+            LOG.error(err_msg)
+            raise Exception(err_msg)
+
         # traffic profile override options
         override_custom_traffic(config, opts.frame_sizes, opts.unidir)
 
         # copy over cli options that are used in config
         config.generator_profile = opts.generator_profile
+        if opts.sriov:
+            config.sriov = True
+        if opts.log_file:
+            config.log_file = opts.log_file
 
         # show running config in json format
         if opts.show_config:
             print json.dumps(config, sort_keys=True, indent=4)
             sys.exit(0)
 
+        if config.sriov and config.service_chain != ChainType.EXT:
+            # if sriov is requested (does not apply to ext chains)
+            # make sure the physnet names are specified
+            check_physnet("left", config.internal_networks.left)
+            check_physnet("right", config.internal_networks.right)
+            if config.service_chain == ChainType.PVVP:
+                check_physnet("middle", config.internal_networks.middle)
+
+        # update the config in the config plugin as it might have changed
+        # in a copy of the dict (config plugin still holds the original dict)
+        config_plugin.set_config(config)
+
+        # add file log if requested
+        if config.log_file:
+            log.add_file_logger(config.log_file)
+
         nfvbench = NFVBench(config, openstack_spec, config_plugin, factory)
 
         if opts.server:
@@ -469,7 +512,9 @@ def main():
         else:
             with utils.RunLock():
                 if unknown_opts:
-                    LOG.warning('Unknown options: ' + ' '.join(unknown_opts))
+                    err_msg = 'Unknown options: ' + ' '.join(unknown_opts)
+                    LOG.error(err_msg)
+                    raise Exception(err_msg)
 
                 # remove unfilled values
                 opts = {k: v for k, v in vars(opts).iteritems() if v is not None}
@@ -480,11 +525,12 @@ def main():
                 if 'result' in result and result['status']:
                     nfvbench.save(result['result'])
                     nfvbench.print_summary(result['result'])
-    except Exception:
+    except Exception as exc:
         LOG.error({
             'status': NFVBench.STATUS_ERROR,
             'error_message': traceback.format_exc()
         })
+        print str(exc)
         sys.exit(1)
 
 if __name__ == '__main__':