Merge "Fix packages for SLES15"
[vswitchperf.git] / vnfs / vnf / vnf.py
1 # Copyright 2015-2017 Intel Corporation.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #   http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 """
16 Interface for VNF.
17 """
18
19 import time
20 import pexpect
21 from tools import tasks
22
23 class IVnf(tasks.Process):
24
25     """
26     Interface for VNF.
27     """
28
29     _number_vnfs = 0
30
31     def __init__(self):
32         """
33         Initialization method.
34
35         Purpose of this method is to initialize all
36         common Vnf data, no services should be started by
37         this call (use ``start`` method instead).
38         """
39         self._number = IVnf._number_vnfs
40         self._logger.debug('Initializing %s. VM with index %s',
41                            self._number + 1, self._number)
42         IVnf._number_vnfs = IVnf._number_vnfs + 1
43         self._log_prefix = 'vnf_%d_cmd : ' % self._number
44         self._login_active = False
45
46     def stop(self):
47         """
48         Stops VNF instance.
49         """
50         if self.is_running():
51             self._logger.info('Killing VNF...')
52
53             # force termination of VNF and wait for it to terminate; It will avoid
54             # sporadic reboot of host. (caused by hugepages or DPDK ports)
55             super(IVnf, self).kill(signal='-9', sleep=10)
56
57     def login(self, dummy_timeout=120):
58         """
59         Login to GUEST instance.
60
61         This can be used after booting the machine
62
63         :param timeout: Timeout to wait for login to complete.
64
65         :returns: True if login is active
66         """
67         raise NotImplementedError()
68
69     def execute(self, cmd, delay=0):
70         """
71         execute ``cmd`` with given ``delay``.
72
73         This method makes asynchronous call to guest system
74         and waits given ``delay`` before returning. Can be
75         used with ``wait`` method to create synchronous call.
76
77         :param cmd: Command to execute on guest system.
78         :param delay: Delay (in seconds) to wait after sending
79                       command before returning. Please note that
80                       this value can be floating point which
81                       allows to pass milliseconds.
82
83         :returns: None.
84         """
85         self._logger.debug('%s%s', self._log_prefix, cmd)
86
87         # ensure that output from previous commands is flushed
88         try:
89             while not self._child.expect(r'.+', timeout=0.1):
90                 pass
91         except (pexpect.TIMEOUT, pexpect.EOF):
92             pass
93
94         self._child.sendline(cmd)
95         time.sleep(delay)
96
97     def wait(self, prompt='', timeout=30):
98         """
99         wait for ``prompt`` on guest system for given ``timeout``.
100
101         This method ends based on two conditions:
102         * ``prompt`` has been detected
103         * ``timeout`` has been reached.
104
105         :param prompt: method end condition. If ``prompt``
106                              won't be detected during given timeout,
107                              method will return False.
108         :param timeout: Time to wait for prompt (in seconds).
109                         Please note that this value can be floating
110                         point which allows to pass milliseconds.
111
112         :returns: output of executed command
113         """
114         self._child.expect(prompt, timeout=timeout)
115         return self._child.before
116
117     def execute_and_wait(self, cmd, timeout=30, prompt=''):
118         """
119         execute ``cmd`` with given ``timeout``.
120
121         This method makes synchronous call to guest system
122         and waits till ``cmd`` execution is finished
123         (based on ``prompt value) or ''timeout'' has
124         been reached.
125
126         :param cmd: Command to execute on guest system.
127         :param timeout: Timeout till the end of execution is not
128                         detected.
129         :param prompt: method end condition. If ``prompt``
130                              won't be detected during given timeout,
131                              method will return False. If no argument
132                              or None value will be passed, default
133                              ``prompt`` passed in __init__
134                              method will be used.
135
136         :returns: output of executed command
137         """
138         self.execute(cmd)
139         return self.wait(prompt=prompt, timeout=timeout)
140
141     def validate_start(self, _dummyresult):
142         """ Validate call of VNF start()
143         """
144         if self._child and self._child.isalive():
145             return True
146
147         return False
148
149     def validate_stop(self, result):
150         """ Validate call of VNF stop()
151         """
152         return not self.validate_start(result)
153
154     @staticmethod
155     def validate_execute_and_wait(result, _dummy_cmd, _dummy_timeout=30, _dummy_prompt=''):
156         """ Validate command execution within VNF
157         """
158         return len(result) > 0
159
160     @staticmethod
161     def validate_login(result):
162         """ Validate successful login into guest
163         """
164         return result
165
166     @staticmethod
167     def reset_vnf_counter():
168         """
169         Reset internal VNF counters
170
171         This method is static
172         """
173         IVnf._number_vnfs = 0