Merge "test_spec: Remove formatted text file verions of IETF Draft"
[vswitchperf.git] / vnfs / vnf / vnf.py
1 # Copyright 2015 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 from tools import tasks
21
22 class IVnf(tasks.Process):
23
24     """
25     Interface for VNF.
26     """
27
28     _number_vnfs = 0
29
30     def __init__(self):
31         """
32         Initialization method.
33
34         Purpose of this method is to initialize all
35         common Vnf data, no services should be started by
36         this call (use ``start`` method instead).
37         """
38         self._number = IVnf._number_vnfs
39         self._logger.debug('Initializing %s. VM with index %s',
40                            self._number + 1, self._number)
41         IVnf._number_vnfs = IVnf._number_vnfs + 1
42         self._log_prefix = 'vnf_%d_cmd : ' % self._number
43
44     def start(self):
45         """
46         Starts VNF instance.
47
48         This is a blocking function
49         """
50         super(IVnf, self).start()
51
52     def stop(self):
53         """
54         Stops VNF instance.
55         """
56         if self.is_running():
57             self._logger.info('Killing VNF...')
58
59             # force termination of VNF and wait for it to terminate; It will avoid
60             # sporadic reboot of host. (caused by hugepages or DPDK ports)
61             super(IVnf, self).kill(signal='-9', sleep=10)
62
63     def execute(self, cmd, delay=0):
64         """
65         execute ``cmd`` with given ``delay``.
66
67         This method makes asynchronous call to guest system
68         and waits given ``delay`` before returning. Can be
69         used with ``wait`` method to create synchronous call.
70
71         :param cmd: Command to execute on guest system.
72         :param delay: Delay (in seconds) to wait after sending
73                       command before returning. Please note that
74                       this value can be floating point which
75                       allows to pass milliseconds.
76
77         :returns: None.
78         """
79         self._logger.debug('%s%s', self._log_prefix, cmd)
80         self._child.sendline(cmd)
81         time.sleep(delay)
82
83     def wait(self, prompt='', timeout=30):
84         """
85         wait for ``prompt`` on guest system for given ``timeout``.
86
87         This method ends based on two conditions:
88         * ``prompt`` has been detected
89         * ``timeout`` has been reached.
90
91         :param prompt: method end condition. If ``prompt``
92                              won't be detected during given timeout,
93                              method will return False.
94         :param timeout: Time to wait for prompt (in seconds).
95                         Please note that this value can be floating
96                         point which allows to pass milliseconds.
97
98         :returns: True if result_cmd has been detected before
99                   timeout has been reached, False otherwise.
100         """
101         self._child.expect(prompt, timeout=timeout)
102
103     def execute_and_wait(self, cmd, timeout=30, prompt=''):
104         """
105         execute ``cmd`` with given ``timeout``.
106
107         This method makes synchronous call to guest system
108         and waits till ``cmd`` execution is finished
109         (based on ``prompt value) or ''timeout'' has
110         been reached.
111
112         :param cmd: Command to execute on guest system.
113         :param timeout: Timeout till the end of execution is not
114                         detected.
115         :param prompt: method end condition. If ``prompt``
116                              won't be detected during given timeout,
117                              method will return False. If no argument
118                              or None value will be passed, default
119                              ``prompt`` passed in __init__
120                              method will be used.
121
122         :returns: True if end of execution has been detected
123                   before timeout has been reached, False otherwise.
124         """
125         self.execute(cmd)
126         self.wait(prompt=prompt, timeout=timeout)
127
128     def validate_start(self, dummy_result):
129         """ Validate call of VNF start()
130         """
131         if self._child and self._child.isalive():
132             return True
133         else:
134             return False
135
136     def validate_stop(self, result):
137         """ Validate call of fVNF stop()
138         """
139         return not self.validate_start(result)
140
141     @staticmethod
142     def reset_vnf_counter():
143         """
144         Reset internal VNF counters
145
146         This method is static
147         """
148         IVnf._number_vnfs = 0
149