creation of tests and configs subdirectories
[samplevnf.git] / VNFs / DPPD-PROX / helper-scripts / rapid / rapid_machine.py
1 #!/usr/bin/python
2
3 ##
4 ## Copyright (c) 2020 Intel Corporation
5 ##
6 ## Licensed under the Apache License, Version 2.0 (the "License");
7 ## you may not use this file except in compliance with the License.
8 ## You may obtain a copy of the License at
9 ##
10 ##     http://www.apache.org/licenses/LICENSE-2.0
11 ##
12 ## Unless required by applicable law or agreed to in writing, software
13 ## distributed under the License is distributed on an "AS IS" BASIS,
14 ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 ## See the License for the specific language governing permissions and
16 ## limitations under the License.
17 ##
18
19 from rapid_log import RapidLog 
20 from prox_ctrl import prox_ctrl
21 import os
22 import re
23
24 class RapidMachine(object):
25     """
26     Class to deal with a PROX instance (VM, bare metal, container)
27     """
28     def __init__(self, key, user, vim, rundir, resultsdir, machine_params,
29             configonly):
30         self.name = machine_params['name']
31         self.ip = machine_params['admin_ip']
32         self.key = key
33         self.user = user
34         self.rundir = rundir
35         self.resultsdir = resultsdir
36         self.dp_ports = []
37         self.dpdk_port_index = []
38         self.configonly = configonly
39         index = 1
40         while True:
41             ip_key = 'dp_ip{}'.format(index)
42             mac_key = 'dp_mac{}'.format(index)
43             if ip_key in machine_params.keys() and mac_key in machine_params.keys():
44                 dp_port = {'ip': machine_params[ip_key], 'mac' : machine_params[mac_key]}
45                 self.dp_ports.append(dict(dp_port))
46                 self.dpdk_port_index.append(index - 1)
47                 index += 1
48             else:
49                 break
50         self.machine_params = machine_params
51         self.vim = vim
52
53     def __del__(self):
54         if ((not self.configonly) and self.machine_params['prox_socket']):
55             self._client.scp_get('/prox.log', '{}/{}.prox.log'.format(
56                 self.resultsdir, self.name))
57
58     def get_cores(self):
59         return (self.machine_params['cores'])
60
61     def devbind(self):
62         # Script to bind the right network interface to the poll mode driver
63         for index, dp_port in enumerate(self.dp_ports, start = 1):
64             DevBindFileName = self.rundir + '/devbind-{}-port{}.sh'.format(self.ip, index)
65             self._client.scp_put('./devbind.sh', DevBindFileName)
66             cmd =  'sed -i \'s/MACADDRESS/' + dp_port['mac'] + '/\' ' + DevBindFileName 
67             result = self._client.run_cmd(cmd)
68             RapidLog.debug('devbind.sh MAC updated for port {} on {} {}'.format(index, self.name, result))
69             if ((not self.configonly) and self.machine_params['prox_launch_exit']):
70                 result = self._client.run_cmd(DevBindFileName)
71                 RapidLog.debug('devbind.sh running for port {} on {} {}'.format(index, self.name, result))
72
73     def generate_lua(self, vim, prox_config_file, appendix = ''):
74         PROXConfigfile =  open (prox_config_file, 'r')
75         PROXConfig = PROXConfigfile.read()
76         PROXConfigfile.close()
77         self.all_tasks_for_this_cfg = set(re.findall("task\s*=\s*(\d+)",PROXConfig))
78         self.LuaFileName = 'parameters-{}.lua'.format(self.ip)
79         with open(self.LuaFileName, "w") as LuaFile:
80             LuaFile.write('require "helper"\n')
81             LuaFile.write('name="%s"\n'% self.name)
82             for index, dp_port in enumerate(self.dp_ports, start = 1):
83                 LuaFile.write('local_ip{}="{}"\n'.format(index, dp_port['ip']))
84                 LuaFile.write('local_hex_ip{}=convertIPToHex(local_ip{})\n'.format(index, index))
85             if vim in ['kubernetes']:
86                 LuaFile.write("eal=\"--socket-mem=512,0 --file-prefix %s --pci-whitelist %s\"\n" % (self.name, self.machine_params['dp_pci_dev']))
87             else:
88                 LuaFile.write("eal=\"\"\n")
89             if 'cores' in self.machine_params.keys():
90                 LuaFile.write('cores="%s"\n'% ','.join(map(str, self.machine_params['cores'])))
91             if 'ports' in self.machine_params.keys():
92                 LuaFile.write('ports="%s"\n'% ','.join(map(str, self.machine_params['ports'])))
93             if 'dest_ports' in self.machine_params.keys():
94                 for index, dest_port in enumerate(self.machine_params['dest_ports'], start = 1):
95                     LuaFile.write('dest_ip{}="{}"\n'.format(index, dest_port['ip']))
96                     LuaFile.write('dest_hex_ip{}=convertIPToHex(dest_ip{})\n'.format(index, index))
97                     LuaFile.write('dest_hex_mac{}="{}"\n'.format(index , dest_port['mac'].replace(':',' ')))
98             LuaFile.write(appendix)
99         self._client.scp_put(self.LuaFileName, self.rundir + '/parameters.lua')
100         self._client.scp_put('helper.lua', self.rundir + '/helper.lua')
101
102     def start_prox(self, autostart=''):
103         if self.machine_params['prox_socket']:
104             self._client = prox_ctrl(self.ip, self.key, self.user)
105             self._client.connect()
106             if self.vim in ['OpenStack']:
107                 self.devbind()
108             _, prox_config_file_name = os.path.split(self.machine_params['config_file'])
109             self.generate_lua(self.vim, self.machine_params['config_file'])
110             self._client.scp_put(self.machine_params['config_file'], '{}/{}'.format(self.rundir, prox_config_file_name))
111             if ((not self.configonly) and self.machine_params['prox_launch_exit']):
112                 cmd = 'sudo {}/prox {} -t -o cli -f {}/{}'.format(self.rundir, autostart, self.rundir, prox_config_file_name)
113                 RapidLog.debug("Starting PROX on {}: {}".format(self.name, cmd))
114                 result = self._client.run_cmd(cmd, 'PROX Testing on {}'.format(self.name))
115                 RapidLog.debug("Finished PROX on {}: {}".format(self.name, cmd))
116
117     def close_prox(self):
118         if (not self.configonly) and self.machine_params['prox_socket'] and self.machine_params['prox_launch_exit']:
119             self.socket.quit_prox()
120
121     def connect_prox(self):
122         if self.machine_params['prox_socket']:
123            self.socket = self._client.connect_socket()
124
125     def start(self):
126         self.socket.start(self.get_cores())
127
128     def stop(self):
129         self.socket.stop(self.get_cores())
130
131     def reset_stats(self):
132         self.socket.reset_stats()
133
134     def core_stats(self):
135         return (self.socket.core_stats(self.get_cores(), self.all_tasks_for_this_cfg))
136
137     def multi_port_stats(self):
138         return (self.socket.multi_port_stats(self.dpdk_port_index))