Merge "Use """ to replace ''' in docstring"
[yardstick.git] / yardstick / benchmark / scenarios / parser / parser.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and other.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9 from __future__ import print_function
10 from __future__ import absolute_import
11 import pkg_resources
12 import logging
13 import subprocess
14 from yardstick.benchmark.scenarios import base
15
16 LOG = logging.getLogger(__name__)
17
18
19 class Parser(base.Scenario):
20     """running Parser Yang-to-Tosca module as a tool
21     validating output against expected outcome
22
23     more info https://wiki.opnfv.org/parser
24     """
25     __scenario_type__ = "Parser"
26
27     SETUP_SCRIPT = "parser_setup.sh"
28     TEARDOWN_SCRIPT = "parser_teardown.sh"
29     PARSER_SCRIPT = "parser.sh"
30
31     def __init__(self, scenario_cfg, context_cfg):
32         self.scenario_cfg = scenario_cfg
33         self.context_cfg = context_cfg
34         self.setup_done = False
35
36     def setup(self):
37         """scenario setup"""
38         self.setup_script = pkg_resources.resource_filename(
39             "yardstick.benchmark.scenarios.parser",
40             Parser.SETUP_SCRIPT)
41         cmd = "%s" % (self.setup_script)
42
43         subprocess.call(cmd, shell=True)
44
45         self.setup_done = True
46
47     def run(self, result):
48         """execute the translation"""
49         options = self.scenario_cfg['options']
50         yangfile = options.get("yangfile", '~/yardstick/samples/yang.yaml')
51         toscafile = options.get("toscafile", '~/yardstick/samples/tosca.yaml')
52
53         self.parser_script = pkg_resources.resource_filename(
54             "yardstick.benchmark.scenarios.parser",
55             Parser.PARSER_SCRIPT)
56
57         if not self.setup_done:
58             self.setup()
59
60         cmd1 = "%s %s %s" % (self.parser_script, yangfile, toscafile)
61         cmd2 = "chmod 777 %s" % (self.parser_script)
62         subprocess.call(cmd2, shell=True)
63         p = subprocess.Popen(cmd1, shell=True, stdout=subprocess.PIPE,
64                              stderr=subprocess.PIPE)
65         p.communicate()
66         print("yangtotosca finished")
67
68         result['yangtotosca'] = "success" if p.returncode == 0 else "fail"
69
70     def teardown(self):
71         """ for scenario teardown remove parser and pyang """
72         self.teardown_script = pkg_resources.resource_filename(
73             "yardstick.benchmark.scenarios.parser",
74             Parser.TEARDOWN_SCRIPT)
75         subprocess.call(self.teardown_script, shell=True)
76         self.teardown_done = True
77
78
79 def _test():
80     """internal test function"""
81     pass
82
83
84 if __name__ == '__main__':
85     _test()