blob: 057a3f165ef8443aa8f32cb910b7bec7a4e349b9 [file] [log] [blame]
kelvin8ec71442015-01-15 16:57:00 -08001"""
2Driver for blank dataplane VMs. Created for SDNIP test.
3"""
timlindbergef8d55d2013-09-27 12:50:13 -07004import time
5import pexpect
kelvin8ec71442015-01-15 16:57:00 -08006import struct
7import fcntl
8import os
9import sys
10import signal
timlindbergef8d55d2013-09-27 12:50:13 -070011import sys
12import re
13import json
kelvin8ec71442015-01-15 16:57:00 -080014sys.path.append( "../" )
timlindbergef8d55d2013-09-27 12:50:13 -070015from drivers.common.clidriver import CLI
16
timlindbergef8d55d2013-09-27 12:50:13 -070017
kelvin8ec71442015-01-15 16:57:00 -080018class DPCliDriver( CLI ):
timlindbergef8d55d2013-09-27 12:50:13 -070019
kelvin8ec71442015-01-15 16:57:00 -080020 def __init__( self ):
21 super( CLI, self ).__init__()
22
23 def connect( self, **connectargs ):
timlindbergef8d55d2013-09-27 12:50:13 -070024 for key in connectargs:
kelvin8ec71442015-01-15 16:57:00 -080025 vars( self )[ key ] = connectargs[ key ]
timlindbergef8d55d2013-09-27 12:50:13 -070026
kelvin8ec71442015-01-15 16:57:00 -080027 self.name = self.options[ 'name' ]
28 self.handle = super( DPCliDriver, self ).connect( user_name=self.user_name,
29 ip_address=self.ip_address,
30 port=self.port,
31 pwd=self.pwd )
timlindbergef8d55d2013-09-27 12:50:13 -070032
33 if self.handle:
34 return self.handle
kelvin8ec71442015-01-15 16:57:00 -080035 else:
36 main.log.info( "NO HANDLE" )
timlindbergef8d55d2013-09-27 12:50:13 -070037 return main.FALSE
38
kelvin8ec71442015-01-15 16:57:00 -080039 def create_interfaces( self, net, number, start ):
40 """
SeanCorcoranf580d102013-09-27 15:13:21 -070041 Creates a number,specified by 'number,' of subinterfaces on eth0. Ip addresses start at 'net'.'start'.1.1 with a 24 bit netmask. Addresses increment sequentially in the third quad,
42 therefore all interfaces are in different subnets on the same machine. When the third quad reaches 255, it is reset to 1 and the second quad is incremented.
43 Every single ip address is placed in a file in /tmp titled 'ip_table{net}.txt'
44 The file is used by 'pingall_interfaces()' as a fping argument
45 This method returns true if all interfaces are created without a hitch, and false if a single interface has issues
kelvin8ec71442015-01-15 16:57:00 -080046 """
47 self.handle.sendline( "" )
48 self.handle.expect( "\$" )
SeanCorcoranf580d102013-09-27 15:13:21 -070049
kelvin8ec71442015-01-15 16:57:00 -080050 self.handle.sendline( "rm /tmp/local_ip.txt" )
51 self.handle.expect( "\$" )
52 self.handle.sendline( "touch /tmp/local_ip.txt" )
53 self.handle.expect( "\$" )
timlindbergef8d55d2013-09-27 12:50:13 -070054
kelvin8ec71442015-01-15 16:57:00 -080055 main.log.info( "Creating interfaces" )
timlindbergef8d55d2013-09-27 12:50:13 -070056 k = 0
57 intf = 0
58 while number != 0:
kelvin8ec71442015-01-15 16:57:00 -080059 k = k + 1
timlindbergef8d55d2013-09-27 12:50:13 -070060 if k == 256:
61 k = 1
62 start = start + 1
63 number = number - 1
64 intf = intf + 1
kelvin8ec71442015-01-15 16:57:00 -080065 ip = net + "." + str( start ) + "." + str( k ) + ".1"
66 self.handle.sendline(
67 "sudo ifconfig eth0:" + str(
68 intf ) + " " + ip + " netmask 255.255.255.0" )
timlindbergef8d55d2013-09-27 12:50:13 -070069
kelvin8ec71442015-01-15 16:57:00 -080070 i = self.handle.expect( [
71 "\$",
72 "password",
73 pexpect.TIMEOUT,
74 pexpect.EOF ],
75 timeout=120 )
SeanCorcoranf580d102013-09-27 15:13:21 -070076
kelvin8ec71442015-01-15 16:57:00 -080077 if i == 0:
78 self.handle.sendline(
79 "echo " + str( ip ) + " >> /tmp/local_ip.txt" )
80 self.handle.expect( "\$" )
81 elif i == 1:
82 main.log.info( "Sending sudo password" )
83 self.handle.sendline( self.pwd )
84 self.handle.expect( "\$" )
85 else:
86 main.log.error( "INTERFACES NOT CREATED" )
87 return main.FALSE
SeanCorcoranf580d102013-09-27 15:13:21 -070088
kelvin8ec71442015-01-15 16:57:00 -080089 def pingall_interfaces( self, netsrc, netstrt, netdst, destlogin, destip ):
90 """
91 Copies the /tmp/ip_table{ net }.txt file from the machine you wish to ping, then runs fping with a source address of { netsrc }.{ netstrt }.1.1 on the copied file.
SeanCorcoranf580d102013-09-27 15:13:21 -070092 Check every single response for reachable or unreachable. If all are reachable, function returns true. If a SINGLE host is unreachable, then the function stops and returns false
93 If fping is not installed, this function will install fping then run the same command
kelvin8ec71442015-01-15 16:57:00 -080094 """
95 self.handle.sendline( "" )
96 self.handle.expect( "\$" )
SeanCorcoranf580d102013-09-27 15:13:21 -070097
kelvin8ec71442015-01-15 16:57:00 -080098 self.handle.sendline( "scp " + str( destlogin ) + "@" +
99 str( destip ) + ":/tmp/local_ip.txt /tmp/ip_table" +
100 str( net ) + ".txt" )
101
102 i = self.handle.expect( [
103 "100%",
104 "password",
105 pexpect.TIMEOUT ],
106 timeout=30 )
107
SeanCorcoranf580d102013-09-27 15:13:21 -0700108 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800109 main.log.info( "Copied ping file successfully" )
SeanCorcoranf580d102013-09-27 15:13:21 -0700110 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800111 self.handle.sendline( self.pwd )
112 self.handle.expect( "100%" )
113 main.log.info( "Copied ping file successfully" )
SeanCorcoranf580d102013-09-27 15:13:21 -0700114 elif i == 2:
kelvin8ec71442015-01-15 16:57:00 -0800115 main.log.error( "COULD NOT COPY PING FILE FROM " + str( destip ) )
SeanCorcoranf580d102013-09-27 15:13:21 -0700116 result = main.FALSE
117 return result
SeanCorcoranf580d102013-09-27 15:13:21 -0700118
kelvin8ec71442015-01-15 16:57:00 -0800119 self.handle.sendline( "" )
120 self.handle.expect( "\$" )
121
122 main.log.info( "Pinging interfaces on the " + str( netdst ) +
123 " network from " + str( netsrc ) + "." +
124 str( netstrt ) + ".1.1" )
125 self.handle.sendline( "sudo fping -S " + str( netsrc ) + "." +
126 str( netstrt ) + ".1.1 -f /tmp/ip_table" +
127 str( netdst ) + ".txt" )
timlindbergef8d55d2013-09-27 12:50:13 -0700128 while 1:
kelvin8ec71442015-01-15 16:57:00 -0800129 i = self.handle.expect( [
130 "reachable",
131 "unreachable",
132 "\$",
133 "password",
134 pexpect.TIMEOUT,
135 "not installed" ],
136 timeout=45 )
timlindbergef8d55d2013-09-27 12:50:13 -0700137 if i == 0:
138 result = main.TRUE
139 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800140 main.log.error( "An interface was unreachable" )
timlindbergef8d55d2013-09-27 12:50:13 -0700141 result = main.FALSE
142 return result
143 elif i == 2:
kelvin8ec71442015-01-15 16:57:00 -0800144 main.log.info( "All interfaces reachable" )
timlindbergef8d55d2013-09-27 12:50:13 -0700145 return result
146 elif i == 3:
kelvin8ec71442015-01-15 16:57:00 -0800147 self.handle.sendline( self.pwd )
timlindbergef8d55d2013-09-27 12:50:13 -0700148 elif i == 4:
kelvin8ec71442015-01-15 16:57:00 -0800149 main.log.error( "Unable to fping" )
timlindbergef8d55d2013-09-27 12:50:13 -0700150 result = main.FALSE
151 return result
152 elif i == 5:
kelvin8ec71442015-01-15 16:57:00 -0800153 main.log.info( "fping not installed, installing fping" )
154 self.handle.sendline( "sudo apt-get install fping" )
155 i = self.handle.expect(
156 [ "password",
157 "\$",
158 pexpect.TIMEOUT ],
159 timeout=60 )
timlindbergef8d55d2013-09-27 12:50:13 -0700160 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800161 self.handle.sendline( self.pwd )
162 self.handle.expect( "\$", timeout=30 )
163 main.log.info( "fping installed, now pinging interfaces" )
164 self.handle.sendline(
165 "sudo fping -S " + str(
166 netsrc ) + "." + str(
167 netstrt ) + ".1.1 -f /tmp/ip_table" + str(
168 netdst ) + ".txt" )
timlindbergef8d55d2013-09-27 12:50:13 -0700169 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800170 main.log.info( "fping installed, now pinging interfaces" )
171 self.handle.sendline(
172 "sudo fping -S " + str(
173 netsrc ) + "." + str(
174 netstrt ) + ".1.1 -f /tmp/ip_table" + str(
175 netdst ) + ".txt" )
timlindbergef8d55d2013-09-27 12:50:13 -0700176 elif i == 2:
kelvin8ec71442015-01-15 16:57:00 -0800177 main.log.error( "Could not install fping" )
timlindbergef8d55d2013-09-27 12:50:13 -0700178 result = main.FALSE
179 return result
180
kelvin8ec71442015-01-15 16:57:00 -0800181 def disconnect( self ):
timlindbergef8d55d2013-09-27 12:50:13 -0700182 response = ''
183 try:
kelvin8ec71442015-01-15 16:57:00 -0800184 self.handle.sendline( "exit" )
185 self.handle.expect( "closed" )
timlindbergef8d55d2013-09-27 12:50:13 -0700186 except:
kelvin8ec71442015-01-15 16:57:00 -0800187 main.log.error( "Connection failed to the host" )
timlindbergef8d55d2013-09-27 12:50:13 -0700188 response = main.FALSE
189 return response
190