blob: 1e7015c9d9c17c803849e85b6c16ba06cf3be104 [file] [log] [blame]
timlindbergef8d55d2013-09-27 12:50:13 -07001'''
2Driver for blank dataplane VMs. Created for SDNIP test.
3'''
4
5import time
6import pexpect
7import struct, fcntl, os, sys, signal
8import sys
9import re
10import json
11sys.path.append("../")
12from drivers.common.clidriver import CLI
13
14class DPCliDriver(CLI):
15
16 def __init__(self):
17 super(CLI, self).__init__()
18
19 def connect(self,**connectargs):
20 for key in connectargs:
21 vars(self)[key] = connectargs[key]
22
23
24 self.name = self.options['name']
25 self.handle = super(DPCliDriver,self).connect(user_name = self.user_name, ip_address = self.ip_address,port = self.port, pwd = self.pwd)
26
27 if self.handle:
28 return self.handle
29 else :
30 main.log.info("NO HANDLE")
31 return main.FALSE
32
SeanCorcoranf580d102013-09-27 15:13:21 -070033 def create_interfaces(self, net, number, start):
34 '''
35 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,
36 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.
37 Every single ip address is placed in a file in /tmp titled 'ip_table{net}.txt'
38 The file is used by 'pingall_interfaces()' as a fping argument
39 This method returns true if all interfaces are created without a hitch, and false if a single interface has issues
40 '''
41
timlindbergef8d55d2013-09-27 12:50:13 -070042 self.handle.sendline("")
43 self.handle.expect("\$")
44
45 self.handle.sendline("rm /tmp/local_ip.txt")
46 self.handle.expect("\$")
47 self.handle.sendline("touch /tmp/local_ip.txt")
48 self.handle.expect("\$")
49
50 main.log.info("Creating interfaces")
51 k = 0
52 intf = 0
53 while number != 0:
54 k= k + 1
55 if k == 256:
56 k = 1
57 start = start + 1
58 number = number - 1
59 intf = intf + 1
60 ip = net+"."+str(start)+"."+str(k)+".1"
61 self.handle.sendline("sudo ifconfig eth0:"+str(intf)+" "+ip+" netmask 255.255.255.0")
62
63 i = self.handle.expect(["\$","password",pexpect.TIMEOUT,pexpect.EOF], timeout = 120)
64 if i == 0:
SeanCorcoranf580d102013-09-27 15:13:21 -070065 self.handle.sendline("echo "+str(ip)+" >> /tmp/local_ip.txt")
timlindbergef8d55d2013-09-27 12:50:13 -070066 self.handle.expect("\$")
67 elif i == 1:
68 main.log.info("Sending sudo password")
69 self.handle.sendline(self.pwd)
70 self.handle.expect("\$")
71 else:
72 main.log.error("INTERFACES NOT CREATED")
73 return main.FALSE
SeanCorcoranf580d102013-09-27 15:13:21 -070074
75
76 def pingall_interfaces(self, netsrc, netstrt, netdst, destlogin, destip):
77 '''
78 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.
79 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
80 If fping is not installed, this function will install fping then run the same command
81 '''
82
83 self.handle.sendline("")
84 self.handle.expect("\$")
85
86 self.handle.sendline("scp "+str(destlogin)+"@"+str(destip)+":/tmp/local_ip.txt /tmp/ip_table"+str(net)+".txt")
87 i = self.handle.expect(["100%","password",pexpect.TIMEOUT], timeout = 30)
88 if i == 0:
89 main.log.info("Copied ping file successfully")
90 elif i == 1:
91 self.handle.sendline(self.pwd)
timlindbergef8d55d2013-09-27 12:50:13 -070092 self.handle.expect("100%")
SeanCorcoranf580d102013-09-27 15:13:21 -070093 main.log.info("Copied ping file successfully")
94 elif i == 2:
95 main.log.error("COULD NOT COPY PING FILE FROM "+str(destip))
96 result = main.FALSE
97 return result
timlindbergef8d55d2013-09-27 12:50:13 -070098
SeanCorcoranf580d102013-09-27 15:13:21 -070099 self.handle.sendline("")
100 self.handle.expect("\$")
101
timlindbergef8d55d2013-09-27 12:50:13 -0700102 main.log.info("Pinging interfaces on the "+str(netdst)+" network from "+str(netsrc)+"."+str(netstrt)+".1.1")
103 self.handle.sendline("sudo fping -S "+str(netsrc)+"."+str(netstrt)+".1.1 -f /tmp/ip_table"+str(netdst)+".txt")
104 while 1:
105 i = self.handle.expect(["reachable","unreachable","\$","password",pexpect.TIMEOUT,"not installed"], timeout=45)
106 if i == 0:
107 result = main.TRUE
108 elif i == 1:
109 main.log.error("An interface was unreachable")
110 result = main.FALSE
111 return result
112 elif i == 2:
113 main.log.info("All interfaces reachable")
114 return result
115 elif i == 3:
116 self.handle.sendline(self.pwd)
117 elif i == 4:
118 main.log.error("Unable to fping")
119 result = main.FALSE
120 return result
121 elif i == 5:
122 main.log.info("fping not installed, installing fping")
123 self.handle.sendline("sudo apt-get install fping")
124 i = self.handle.expect(["password","\$",pexpect.TIMEOUT], timeout = 60)
125 if i == 0:
126 self.handle.sendline(self.pwd)
127 self.handle.expect("\$", timeout = 30)
SeanCorcoranf580d102013-09-27 15:13:21 -0700128 main.log.info("fping installed, now pinging interfaces")
timlindbergef8d55d2013-09-27 12:50:13 -0700129 self.handle.sendline("sudo fping -S "+str(netsrc)+"."+str(netstrt)+".1.1 -f /tmp/ip_table"+str(netdst)+".txt")
130 elif i == 1:
SeanCorcoranf580d102013-09-27 15:13:21 -0700131 main.log.info("fping installed, now pinging interfaces")
timlindbergef8d55d2013-09-27 12:50:13 -0700132 self.handle.sendline("sudo fping -S "+str(netsrc)+"."+str(netstrt)+".1.1 -f /tmp/ip_table"+str(netdst)+".txt")
133 elif i == 2:
134 main.log.error("Could not install fping")
135 result = main.FALSE
136 return result
137
138 def disconnect(self):
139 response = ''
140 try:
141 self.handle.sendline("exit")
142 self.handle.expect("closed")
143 except:
144 main.log.error("Connection failed to the host")
145 response = main.FALSE
146 return response
147