blob: 39effc75ded1110a71899f9776d5b6bf4dd7578e [file] [log] [blame]
adminbae64d82013-08-01 10:50:15 -07001#!/usr/bin/env python
kelvin-onlabedcff052015-01-16 12:53:55 -08002"""
adminbae64d82013-08-01 10:50:15 -07003Created on 26-Oct-2012
kelvin-onlabedcff052015-01-16 12:53:55 -08004
5author:: Raghav Kashyap( raghavkashyap@paxterrasolutions.com )
adminbae64d82013-08-01 10:50:15 -07006
7
8 TestON is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
kelvin-onlabedcff052015-01-16 12:53:55 -080011 ( at your option ) any later version.
adminbae64d82013-08-01 10:50:15 -070012
13 TestON is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
kelvin-onlabedcff052015-01-16 12:53:55 -080019 along with TestON. If not, see <http://www.gnu.org/licenses/>.
adminbae64d82013-08-01 10:50:15 -070020
21
22pox driver provides the basic functions of POX controller
kelvin-onlabedcff052015-01-16 12:53:55 -080023"""
adminbae64d82013-08-01 10:50:15 -070024import pexpect
kelvin-onlabedcff052015-01-16 12:53:55 -080025import struct
26import fcntl
27import os
kelvin-onlabedcff052015-01-16 12:53:55 -080028import signal
adminbae64d82013-08-01 10:50:15 -070029import sys
30from drivers.common.cli.emulatordriver import Emulator
31
adminbae64d82013-08-01 10:50:15 -070032
kelvin-onlabedcff052015-01-16 12:53:55 -080033class PoxCliDriver( Emulator ):
34
35 """
36 PoxCliDriver driver provides the basic functions of POX controller
37 """
38 def __init__( self ):
39 super( Emulator, self ).__init__()
40 self.handle = self
41 self.wrapped = sys.modules[ __name__ ]
42
43 def connect( self, **connectargs ):
44 #,user_name, ip_address, pwd,options ):
45 """
46 this subroutine is to launch pox controller . It must have arguments as :
adminbae64d82013-08-01 10:50:15 -070047 user_name = host name ,
48 ip_address = ip address of the host ,
49 pwd = password of host ,
50 options = it is a topology hash which will consists the component's details for the test run
51
52 *** host is here a virtual mahine or system where pox framework hierarchy exists
kelvin-onlabedcff052015-01-16 12:53:55 -080053 """
adminbae64d82013-08-01 10:50:15 -070054 for key in connectargs:
kelvin-onlabedcff052015-01-16 12:53:55 -080055 vars( self )[ key ] = connectargs[ key ]
56
57 self.name = self.options[ 'name' ]
58
adminbae64d82013-08-01 10:50:15 -070059 poxLibPath = 'default'
kelvin-onlabedcff052015-01-16 12:53:55 -080060 copy = super(
61 PoxCliDriver,
62 self ).secureCopy(
63 self.user_name,
64 self.ip_address,
65 '/home/openflow/pox/pox/core.py',
66 self.pwd,
67 path +
68 '/lib/pox/' )
69 self.handle = super(
70 PoxCliDriver,
71 self ).connect(
72 user_name=self.user_name,
73 ip_address=self.ip_address,
74 port=None,
75 pwd=self.pwd )
76
adminbae64d82013-08-01 10:50:15 -070077 if self.handle:
kelvin-onlabedcff052015-01-16 12:53:55 -080078 self.handle.expect( "openflow" )
79 command = self.getcmd( self.options )
80 # print command
81 main.log.info( "Entering into POX hierarchy" )
82 if self.options[ 'pox_lib_location' ] != 'default':
83 self.execute(
84 cmd="cd " +
85 self.options[ 'pox_lib_location' ],
86 prompt="/pox\$",
87 timeout=120 )
88 else:
89 self.execute(
90 cmd="cd ~/TestON/lib/pox/",
91 prompt="/pox\$",
92 timeout=120 )
93 # launching pox with components
94 main.log.info( "launching POX controller with given components" )
95 self.execute( cmd=command, prompt="DEBUG:", timeout=120 )
adminbae64d82013-08-01 10:50:15 -070096 return main.TRUE
kelvin-onlabedcff052015-01-16 12:53:55 -080097 else:
98 main.log.error(
99 "Connection failed to the host " +
100 self.user_name +
101 "@" +
102 self.ip_address )
103 main.log.error( "Failed to connect to the POX controller" )
adminbae64d82013-08-01 10:50:15 -0700104 return main.FALSE
kelvin-onlabedcff052015-01-16 12:53:55 -0800105
106 def disconnect( self, handle ):
adminbae64d82013-08-01 10:50:15 -0700107 if self.handle:
kelvin-onlabedcff052015-01-16 12:53:55 -0800108 self.execute( cmd="exit()", prompt="/pox\$", timeout=120 )
109 else:
110 main.log.error( "Connection failed to the host" )
adminbae64d82013-08-01 10:50:15 -0700111
kelvin-onlabedcff052015-01-16 12:53:55 -0800112 def get_version( self ):
113 file_input = path + '/lib/pox/core.py'
114 version = super( PoxCliDriver, self ).get_version()
adminbae64d82013-08-01 10:50:15 -0700115 pattern = '\s*self\.version(.*)'
116 import re
kelvin-onlabedcff052015-01-16 12:53:55 -0800117 for line in open( file_input, 'r' ).readlines():
118 result = re.match( pattern, line )
adminbae64d82013-08-01 10:50:15 -0700119 if result:
kelvin-onlabedcff052015-01-16 12:53:55 -0800120 version = result.group( 0 )
121 version = re.sub(
122 "\s*self\.version\s*=\s*|\(|\)",
123 '',
124 version )
125 version = re.sub( ",", '.', version )
126 version = "POX " + version
adminbae64d82013-08-01 10:50:15 -0700127
kelvin-onlabedcff052015-01-16 12:53:55 -0800128 return version
129
130 def getcmd( self, options ):
131 command = "./pox.py "
adminbae64d82013-08-01 10:50:15 -0700132 for item in options.keys():
kelvin-onlabedcff052015-01-16 12:53:55 -0800133 if isinstance( options[ item ], dict ):
adminbae64d82013-08-01 10:50:15 -0700134 command = command + item
kelvin-onlabedcff052015-01-16 12:53:55 -0800135 for items in options[ item ].keys():
136 if options[ item ][ items ] == "None":
adminbae64d82013-08-01 10:50:15 -0700137 command = command + " --" + items + " "
kelvin-onlabedcff052015-01-16 12:53:55 -0800138 else:
139 command = command + " --" + items + \
140 "=" + options[ item ][ items ] + " "
adminbae64d82013-08-01 10:50:15 -0700141 else:
142 if item == 'pox_lib_location':
kelvin-onlabedcff052015-01-16 12:53:55 -0800143 poxLibPath = options[ item ]
adminbae64d82013-08-01 10:50:15 -0700144 elif item == 'type' or item == 'name':
145 pass
kelvin-onlabedcff052015-01-16 12:53:55 -0800146 else:
adminbae64d82013-08-01 10:50:15 -0700147 command = command + item
148
kelvin-onlabedcff052015-01-16 12:53:55 -0800149 return command
adminbae64d82013-08-01 10:50:15 -0700150
adminbae64d82013-08-01 10:50:15 -0700151
152if __name__ != "__main__":
153 import sys
154
kelvin-onlabedcff052015-01-16 12:53:55 -0800155 sys.modules[ __name__ ] = PoxCliDriver()