blob: 349e54e357e41cbac72f6895de25ef7b0748ff2b [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
Jeremy Songsterae01bba2016-07-11 15:39:17 -07004Modified 2015 by ON.Lab
5
6Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>,
7the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>,
8or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg>
kelvin-onlabedcff052015-01-16 12:53:55 -08009
10author:: Raghav Kashyap( raghavkashyap@paxterrasolutions.com )
adminbae64d82013-08-01 10:50:15 -070011
12
13 TestON is free software: you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation, either version 2 of the License, or
kelvin-onlabedcff052015-01-16 12:53:55 -080016 ( at your option ) any later version.
adminbae64d82013-08-01 10:50:15 -070017
18 TestON is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
kelvin-onlabedcff052015-01-16 12:53:55 -080024 along with TestON. If not, see <http://www.gnu.org/licenses/>.
adminbae64d82013-08-01 10:50:15 -070025
26
27pox driver provides the basic functions of POX controller
kelvin-onlabedcff052015-01-16 12:53:55 -080028"""
adminbae64d82013-08-01 10:50:15 -070029import pexpect
kelvin-onlabedcff052015-01-16 12:53:55 -080030import struct
31import fcntl
32import os
kelvin-onlabedcff052015-01-16 12:53:55 -080033import signal
adminbae64d82013-08-01 10:50:15 -070034import sys
35from drivers.common.cli.emulatordriver import Emulator
36
adminbae64d82013-08-01 10:50:15 -070037
kelvin-onlabedcff052015-01-16 12:53:55 -080038class PoxCliDriver( Emulator ):
39
40 """
41 PoxCliDriver driver provides the basic functions of POX controller
42 """
43 def __init__( self ):
Devin Limdc78e202017-06-09 18:30:07 -070044 super( PoxCliDriver, self ).__init__()
kelvin-onlabedcff052015-01-16 12:53:55 -080045 self.handle = self
46 self.wrapped = sys.modules[ __name__ ]
47
48 def connect( self, **connectargs ):
49 #,user_name, ip_address, pwd,options ):
50 """
51 this subroutine is to launch pox controller . It must have arguments as :
adminbae64d82013-08-01 10:50:15 -070052 user_name = host name ,
53 ip_address = ip address of the host ,
54 pwd = password of host ,
55 options = it is a topology hash which will consists the component's details for the test run
56
57 *** host is here a virtual mahine or system where pox framework hierarchy exists
kelvin-onlabedcff052015-01-16 12:53:55 -080058 """
adminbae64d82013-08-01 10:50:15 -070059 for key in connectargs:
kelvin-onlabedcff052015-01-16 12:53:55 -080060 vars( self )[ key ] = connectargs[ key ]
61
62 self.name = self.options[ 'name' ]
63
adminbae64d82013-08-01 10:50:15 -070064 poxLibPath = 'default'
kelvin-onlabd9e23de2015-08-06 10:34:44 -070065
kelvin-onlabedcff052015-01-16 12:53:55 -080066 self.handle = super(
67 PoxCliDriver,
68 self ).connect(
69 user_name=self.user_name,
70 ip_address=self.ip_address,
71 port=None,
72 pwd=self.pwd )
73
adminbae64d82013-08-01 10:50:15 -070074 if self.handle:
kelvin-onlabedcff052015-01-16 12:53:55 -080075 self.handle.expect( "openflow" )
76 command = self.getcmd( self.options )
77 # print command
78 main.log.info( "Entering into POX hierarchy" )
79 if self.options[ 'pox_lib_location' ] != 'default':
80 self.execute(
81 cmd="cd " +
82 self.options[ 'pox_lib_location' ],
Devin Limdc78e202017-06-09 18:30:07 -070083 prompt="/pox" + self.prompt,
kelvin-onlabedcff052015-01-16 12:53:55 -080084 timeout=120 )
85 else:
86 self.execute(
87 cmd="cd ~/TestON/lib/pox/",
Devin Limdc78e202017-06-09 18:30:07 -070088 prompt="/pox" + self.prompt,
kelvin-onlabedcff052015-01-16 12:53:55 -080089 timeout=120 )
90 # launching pox with components
91 main.log.info( "launching POX controller with given components" )
92 self.execute( cmd=command, prompt="DEBUG:", timeout=120 )
adminbae64d82013-08-01 10:50:15 -070093 return main.TRUE
kelvin-onlabedcff052015-01-16 12:53:55 -080094 else:
95 main.log.error(
96 "Connection failed to the host " +
97 self.user_name +
98 "@" +
99 self.ip_address )
100 main.log.error( "Failed to connect to the POX controller" )
adminbae64d82013-08-01 10:50:15 -0700101 return main.FALSE
kelvin-onlabedcff052015-01-16 12:53:55 -0800102
103 def disconnect( self, handle ):
adminbae64d82013-08-01 10:50:15 -0700104 if self.handle:
Devin Limdc78e202017-06-09 18:30:07 -0700105 self.execute( cmd="exit()", prompt="/pox" + self.prompt, timeout=120 )
kelvin-onlabedcff052015-01-16 12:53:55 -0800106 else:
107 main.log.error( "Connection failed to the host" )
adminbae64d82013-08-01 10:50:15 -0700108
kelvin-onlabedcff052015-01-16 12:53:55 -0800109 def get_version( self ):
110 file_input = path + '/lib/pox/core.py'
111 version = super( PoxCliDriver, self ).get_version()
adminbae64d82013-08-01 10:50:15 -0700112 pattern = '\s*self\.version(.*)'
113 import re
kelvin-onlabedcff052015-01-16 12:53:55 -0800114 for line in open( file_input, 'r' ).readlines():
115 result = re.match( pattern, line )
adminbae64d82013-08-01 10:50:15 -0700116 if result:
kelvin-onlabedcff052015-01-16 12:53:55 -0800117 version = result.group( 0 )
118 version = re.sub(
119 "\s*self\.version\s*=\s*|\(|\)",
120 '',
121 version )
122 version = re.sub( ",", '.', version )
123 version = "POX " + version
adminbae64d82013-08-01 10:50:15 -0700124
kelvin-onlabedcff052015-01-16 12:53:55 -0800125 return version
126
127 def getcmd( self, options ):
128 command = "./pox.py "
adminbae64d82013-08-01 10:50:15 -0700129 for item in options.keys():
kelvin-onlabedcff052015-01-16 12:53:55 -0800130 if isinstance( options[ item ], dict ):
adminbae64d82013-08-01 10:50:15 -0700131 command = command + item
kelvin-onlabedcff052015-01-16 12:53:55 -0800132 for items in options[ item ].keys():
133 if options[ item ][ items ] == "None":
adminbae64d82013-08-01 10:50:15 -0700134 command = command + " --" + items + " "
kelvin-onlabedcff052015-01-16 12:53:55 -0800135 else:
136 command = command + " --" + items + \
137 "=" + options[ item ][ items ] + " "
adminbae64d82013-08-01 10:50:15 -0700138 else:
139 if item == 'pox_lib_location':
kelvin-onlabedcff052015-01-16 12:53:55 -0800140 poxLibPath = options[ item ]
adminbae64d82013-08-01 10:50:15 -0700141 elif item == 'type' or item == 'name':
142 pass
kelvin-onlabedcff052015-01-16 12:53:55 -0800143 else:
adminbae64d82013-08-01 10:50:15 -0700144 command = command + item
145
kelvin-onlabedcff052015-01-16 12:53:55 -0800146 return command
adminbae64d82013-08-01 10:50:15 -0700147
adminbae64d82013-08-01 10:50:15 -0700148
149if __name__ != "__main__":
150 import sys
151
kelvin-onlabedcff052015-01-16 12:53:55 -0800152 sys.modules[ __name__ ] = PoxCliDriver()