blob: 2d92139dc60282d4395a3a41ad7193510999ad17 [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-onlabd9e23de2015-08-06 10:34:44 -070060
kelvin-onlabedcff052015-01-16 12:53:55 -080061 self.handle = super(
62 PoxCliDriver,
63 self ).connect(
64 user_name=self.user_name,
65 ip_address=self.ip_address,
66 port=None,
67 pwd=self.pwd )
68
adminbae64d82013-08-01 10:50:15 -070069 if self.handle:
kelvin-onlabedcff052015-01-16 12:53:55 -080070 self.handle.expect( "openflow" )
71 command = self.getcmd( self.options )
72 # print command
73 main.log.info( "Entering into POX hierarchy" )
74 if self.options[ 'pox_lib_location' ] != 'default':
75 self.execute(
76 cmd="cd " +
77 self.options[ 'pox_lib_location' ],
78 prompt="/pox\$",
79 timeout=120 )
80 else:
81 self.execute(
82 cmd="cd ~/TestON/lib/pox/",
83 prompt="/pox\$",
84 timeout=120 )
85 # launching pox with components
86 main.log.info( "launching POX controller with given components" )
87 self.execute( cmd=command, prompt="DEBUG:", timeout=120 )
adminbae64d82013-08-01 10:50:15 -070088 return main.TRUE
kelvin-onlabedcff052015-01-16 12:53:55 -080089 else:
90 main.log.error(
91 "Connection failed to the host " +
92 self.user_name +
93 "@" +
94 self.ip_address )
95 main.log.error( "Failed to connect to the POX controller" )
adminbae64d82013-08-01 10:50:15 -070096 return main.FALSE
kelvin-onlabedcff052015-01-16 12:53:55 -080097
98 def disconnect( self, handle ):
adminbae64d82013-08-01 10:50:15 -070099 if self.handle:
kelvin-onlabedcff052015-01-16 12:53:55 -0800100 self.execute( cmd="exit()", prompt="/pox\$", timeout=120 )
101 else:
102 main.log.error( "Connection failed to the host" )
adminbae64d82013-08-01 10:50:15 -0700103
kelvin-onlabedcff052015-01-16 12:53:55 -0800104 def get_version( self ):
105 file_input = path + '/lib/pox/core.py'
106 version = super( PoxCliDriver, self ).get_version()
adminbae64d82013-08-01 10:50:15 -0700107 pattern = '\s*self\.version(.*)'
108 import re
kelvin-onlabedcff052015-01-16 12:53:55 -0800109 for line in open( file_input, 'r' ).readlines():
110 result = re.match( pattern, line )
adminbae64d82013-08-01 10:50:15 -0700111 if result:
kelvin-onlabedcff052015-01-16 12:53:55 -0800112 version = result.group( 0 )
113 version = re.sub(
114 "\s*self\.version\s*=\s*|\(|\)",
115 '',
116 version )
117 version = re.sub( ",", '.', version )
118 version = "POX " + version
adminbae64d82013-08-01 10:50:15 -0700119
kelvin-onlabedcff052015-01-16 12:53:55 -0800120 return version
121
122 def getcmd( self, options ):
123 command = "./pox.py "
adminbae64d82013-08-01 10:50:15 -0700124 for item in options.keys():
kelvin-onlabedcff052015-01-16 12:53:55 -0800125 if isinstance( options[ item ], dict ):
adminbae64d82013-08-01 10:50:15 -0700126 command = command + item
kelvin-onlabedcff052015-01-16 12:53:55 -0800127 for items in options[ item ].keys():
128 if options[ item ][ items ] == "None":
adminbae64d82013-08-01 10:50:15 -0700129 command = command + " --" + items + " "
kelvin-onlabedcff052015-01-16 12:53:55 -0800130 else:
131 command = command + " --" + items + \
132 "=" + options[ item ][ items ] + " "
adminbae64d82013-08-01 10:50:15 -0700133 else:
134 if item == 'pox_lib_location':
kelvin-onlabedcff052015-01-16 12:53:55 -0800135 poxLibPath = options[ item ]
adminbae64d82013-08-01 10:50:15 -0700136 elif item == 'type' or item == 'name':
137 pass
kelvin-onlabedcff052015-01-16 12:53:55 -0800138 else:
adminbae64d82013-08-01 10:50:15 -0700139 command = command + item
140
kelvin-onlabedcff052015-01-16 12:53:55 -0800141 return command
adminbae64d82013-08-01 10:50:15 -0700142
adminbae64d82013-08-01 10:50:15 -0700143
144if __name__ != "__main__":
145 import sys
146
kelvin-onlabedcff052015-01-16 12:53:55 -0800147 sys.modules[ __name__ ] = PoxCliDriver()