1 # Copyright 2015 Intel Corporation.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
20 from tools import tasks
22 class IVnf(tasks.Process):
32 Initialization method.
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).
38 self._number = IVnf._number_vnfs
39 IVnf._number_vnfs = IVnf._number_vnfs + 1
40 self._log_prefix = 'vnf_%d_cmd : ' % self._number
46 This is a blocking function
48 super(IVnf, self).start()
55 self._logger.info('Killing VNF...')
57 # force termination of VNF and wait for it to terminate; It will avoid
58 # sporadic reboot of host. (caused by hugepages or DPDK ports)
59 super(IVnf, self).kill(signal='-9', sleep=10)
61 def execute(self, cmd, delay=0):
63 execute ``cmd`` with given ``delay``.
65 This method makes asynchronous call to guest system
66 and waits given ``delay`` before returning. Can be
67 used with ``wait`` method to create synchronous call.
69 :param cmd: Command to execute on guest system.
70 :param delay: Delay (in seconds) to wait after sending
71 command before returning. Please note that
72 this value can be floating point which
73 allows to pass milliseconds.
77 self._logger.debug('%s%s', self._log_prefix, cmd)
78 self._child.sendline(cmd)
81 def wait(self, prompt='', timeout=30):
83 wait for ``prompt`` on guest system for given ``timeout``.
85 This method ends based on two conditions:
86 * ``prompt`` has been detected
87 * ``timeout`` has been reached.
89 :param prompt: method end condition. If ``prompt``
90 won't be detected during given timeout,
91 method will return False.
92 :param timeout: Time to wait for prompt (in seconds).
93 Please note that this value can be floating
94 point which allows to pass milliseconds.
96 :returns: True if result_cmd has been detected before
97 timeout has been reached, False otherwise.
99 self._child.expect(prompt, timeout=timeout)
101 def execute_and_wait(self, cmd, timeout=30, prompt=''):
103 execute ``cmd`` with given ``timeout``.
105 This method makes synchronous call to guest system
106 and waits till ``cmd`` execution is finished
107 (based on ``prompt value) or ''timeout'' has
110 :param cmd: Command to execute on guest system.
111 :param timeout: Timeout till the end of execution is not
113 :param prompt: method end condition. If ``prompt``
114 won't be detected during given timeout,
115 method will return False. If no argument
116 or None value will be passed, default
117 ``prompt`` passed in __init__
120 :returns: True if end of execution has been detected
121 before timeout has been reached, False otherwise.
124 self.wait(prompt=prompt, timeout=timeout)
126 def validate_start(self, dummy_result):
127 """ Validate call of VNF start()
129 if self._child and self._child.isalive():
134 def validate_stop(self, result):
135 """ Validate call of fVNF stop()
137 return not self.validate_start(result)
140 def reset_vnf_counter():
142 Reset internal VNF counters
144 This method is static
146 IVnf._number_vnfs = 0