blob: 291e916ad21f44db86d1c5800da0dce6a7ce41cd [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
28import sys
29import signal
adminbae64d82013-08-01 10:50:15 -070030import sys
31from drivers.common.cli.emulatordriver import Emulator
32
adminbae64d82013-08-01 10:50:15 -070033
kelvin-onlabedcff052015-01-16 12:53:55 -080034class PoxCliDriver( Emulator ):
35
36 """
37 PoxCliDriver driver provides the basic functions of POX controller
38 """
39 def __init__( self ):
40 super( Emulator, self ).__init__()
41 self.handle = self
42 self.wrapped = sys.modules[ __name__ ]
43
44 def connect( self, **connectargs ):
45 #,user_name, ip_address, pwd,options ):
46 """
47 this subroutine is to launch pox controller . It must have arguments as :
adminbae64d82013-08-01 10:50:15 -070048 user_name = host name ,
49 ip_address = ip address of the host ,
50 pwd = password of host ,
51 options = it is a topology hash which will consists the component's details for the test run
52
53 *** host is here a virtual mahine or system where pox framework hierarchy exists
kelvin-onlabedcff052015-01-16 12:53:55 -080054 """
adminbae64d82013-08-01 10:50:15 -070055 for key in connectargs:
kelvin-onlabedcff052015-01-16 12:53:55 -080056 vars( self )[ key ] = connectargs[ key ]
57
58 self.name = self.options[ 'name' ]
59
adminbae64d82013-08-01 10:50:15 -070060 poxLibPath = 'default'
kelvin-onlabedcff052015-01-16 12:53:55 -080061 copy = super(
62 PoxCliDriver,
63 self ).secureCopy(
64 self.user_name,
65 self.ip_address,
66 '/home/openflow/pox/pox/core.py',
67 self.pwd,
68 path +
69 '/lib/pox/' )
70 self.handle = super(
71 PoxCliDriver,
72 self ).connect(
73 user_name=self.user_name,
74 ip_address=self.ip_address,
75 port=None,
76 pwd=self.pwd )
77
adminbae64d82013-08-01 10:50:15 -070078 if self.handle:
kelvin-onlabedcff052015-01-16 12:53:55 -080079 self.handle.expect( "openflow" )
80 command = self.getcmd( self.options )
81 # print command
82 main.log.info( "Entering into POX hierarchy" )
83 if self.options[ 'pox_lib_location' ] != 'default':
84 self.execute(
85 cmd="cd " +
86 self.options[ 'pox_lib_location' ],
87 prompt="/pox\$",
88 timeout=120 )
89 else:
90 self.execute(
91 cmd="cd ~/TestON/lib/pox/",
92 prompt="/pox\$",
93 timeout=120 )
94 # launching pox with components
95 main.log.info( "launching POX controller with given components" )
96 self.execute( cmd=command, prompt="DEBUG:", timeout=120 )
adminbae64d82013-08-01 10:50:15 -070097 return main.TRUE
kelvin-onlabedcff052015-01-16 12:53:55 -080098 else:
99 main.log.error(
100 "Connection failed to the host " +
101 self.user_name +
102 "@" +
103 self.ip_address )
104 main.log.error( "Failed to connect to the POX controller" )
adminbae64d82013-08-01 10:50:15 -0700105 return main.FALSE
kelvin-onlabedcff052015-01-16 12:53:55 -0800106
107 def disconnect( self, handle ):
adminbae64d82013-08-01 10:50:15 -0700108 if self.handle:
kelvin-onlabedcff052015-01-16 12:53:55 -0800109 self.execute( cmd="exit()", prompt="/pox\$", timeout=120 )
110 else:
111 main.log.error( "Connection failed to the host" )
adminbae64d82013-08-01 10:50:15 -0700112
kelvin-onlabedcff052015-01-16 12:53:55 -0800113 def get_version( self ):
114 file_input = path + '/lib/pox/core.py'
115 version = super( PoxCliDriver, self ).get_version()
adminbae64d82013-08-01 10:50:15 -0700116 pattern = '\s*self\.version(.*)'
117 import re
kelvin-onlabedcff052015-01-16 12:53:55 -0800118 for line in open( file_input, 'r' ).readlines():
119 result = re.match( pattern, line )
adminbae64d82013-08-01 10:50:15 -0700120 if result:
kelvin-onlabedcff052015-01-16 12:53:55 -0800121 version = result.group( 0 )
122 version = re.sub(
123 "\s*self\.version\s*=\s*|\(|\)",
124 '',
125 version )
126 version = re.sub( ",", '.', version )
127 version = "POX " + version
adminbae64d82013-08-01 10:50:15 -0700128
kelvin-onlabedcff052015-01-16 12:53:55 -0800129 return version
130
131 def getcmd( self, options ):
132 command = "./pox.py "
adminbae64d82013-08-01 10:50:15 -0700133 for item in options.keys():
kelvin-onlabedcff052015-01-16 12:53:55 -0800134 if isinstance( options[ item ], dict ):
adminbae64d82013-08-01 10:50:15 -0700135 command = command + item
kelvin-onlabedcff052015-01-16 12:53:55 -0800136 for items in options[ item ].keys():
137 if options[ item ][ items ] == "None":
adminbae64d82013-08-01 10:50:15 -0700138 command = command + " --" + items + " "
kelvin-onlabedcff052015-01-16 12:53:55 -0800139 else:
140 command = command + " --" + items + \
141 "=" + options[ item ][ items ] + " "
adminbae64d82013-08-01 10:50:15 -0700142 else:
143 if item == 'pox_lib_location':
kelvin-onlabedcff052015-01-16 12:53:55 -0800144 poxLibPath = options[ item ]
adminbae64d82013-08-01 10:50:15 -0700145 elif item == 'type' or item == 'name':
146 pass
kelvin-onlabedcff052015-01-16 12:53:55 -0800147 else:
adminbae64d82013-08-01 10:50:15 -0700148 command = command + item
149
kelvin-onlabedcff052015-01-16 12:53:55 -0800150 return command
adminbae64d82013-08-01 10:50:15 -0700151
adminbae64d82013-08-01 10:50:15 -0700152
153if __name__ != "__main__":
154 import sys
155
kelvin-onlabedcff052015-01-16 12:53:55 -0800156 sys.modules[ __name__ ] = PoxCliDriver()