blob: ce8620a9b1921a53871ef27c8538bf46f256c065 [file] [log] [blame]
Jeremyfc567ca2016-02-16 15:08:25 -08001"""
Jeremy Ronquillob27ce4c2017-07-17 12:41:28 -07002Copyright 2016 Open Networking Foundation (ONF)
3
4Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>,
5the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>,
6or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg>
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
11 (at your option) any later version.
12
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
19 along with TestON. If not, see <http://www.gnu.org/licenses/>.
20
21
Jeremyfc567ca2016-02-16 15:08:25 -080022 Wrapper functions for FUNCnetconf
23 This functions include Onosclidriver and Mininetclidriver driver functions
24 Author: Jeremy Songster, jeremy@onlab.us
25"""
26import time
27import json
Jon Hall53c5e662016-04-13 16:06:56 -070028import os
Jeremyfc567ca2016-02-16 15:08:25 -080029
Jon Hall0c3f46f2017-05-24 16:34:46 -070030
Jeremyfc567ca2016-02-16 15:08:25 -080031def __init__( self ):
32 self.default = ''
33
Jon Hall0c3f46f2017-05-24 16:34:46 -070034
Jeremyfc567ca2016-02-16 15:08:25 -080035def startApp( main ):
36 """
37 This function starts the netconf app in all onos nodes and ensures that
38 the OF-Config server is running on the node to be configured
39 """
Jeremyfc567ca2016-02-16 15:08:25 -080040 startResult = main.FALSE
Devin Lim142b5342017-07-20 15:22:39 -070041 startResult = main.Cluster.active( 0 ).REST.activateApp( appName="org.onosproject.netconf" )
Jeremyfc567ca2016-02-16 15:08:25 -080042 return startResult
43
Jon Hall0c3f46f2017-05-24 16:34:46 -070044
Jeremyfc567ca2016-02-16 15:08:25 -080045def startOFC( main ):
46 """
47 This function uses pexpect pxssh class to activate the ofc-server daemon on OC2
48 """
Jeremyfc567ca2016-02-16 15:08:25 -080049 startResult = main.FALSE
50 try:
51 main.ONOSbench.handle.sendline( "" )
52 main.ONOSbench.handle.expect( "\$" )
53 main.ONOSbench.handle.sendline( "ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1 }'" )
54 main.ONOSbench.handle.expect( "\$1 }'" )
55 main.ONOSbench.handle.expect( "\$" )
56 main.configDeviceIp = main.ONOSbench.handle.before
57 main.configDeviceIp = main.configDeviceIp.split()
58 main.configDeviceIp = main.configDeviceIp[ 0 ]
59 main.log.info( "Device to be configured: " + str( main.configDeviceIp ) )
60 main.ONOSbench.handle.sendline( "sudo ofc-server" )
61 main.ONOSbench.handle.expect( "\$" )
62 startResult = main.TRUE
63 return startResult
64 except pexpect.ExceptionPexpect as e:
65 main.log.exception( self.name + ": Pexpect exception found: " )
66 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -070067 main.cleanAndExit()
Jon Hall0c3f46f2017-05-24 16:34:46 -070068
Jeremyfc567ca2016-02-16 15:08:25 -080069def createConfig( main ):
70 """
71 This function writes a configuration file that can later be sent to the
72 REST API to configure a device.
73 The controller device is assumed to be OC1
74 The device to be configured is assumed to be OC2
75 """
76 createCfgResult = main.FALSE
77 # TODO, add ability to set Manufacturer, Hardware and Software versions
Jon Hall0c3f46f2017-05-24 16:34:46 -070078 main.cfgJson = '{ "devices":{ "netconf:' + main.configDeviceIp + ":" +\
79 main.configDevicePort + '":' + '{ "basic":{ "driver":"' +\
Jeremyfc567ca2016-02-16 15:08:25 -080080 main.configDriver + '" } } }, "apps": { "' +\
Devin Limc87f1892017-06-19 16:52:57 -070081 main.configApps + '":{ "netconf_devices":[ { "username":' +\
Jeremyfc567ca2016-02-16 15:08:25 -080082 main.configName + ', "password":' + main.configPass +\
83 ', "ip":"' + main.configDeviceIp + '", "port":' +\
84 main.configPort + '} ] } } }'
85 try:
Jon Hall53c5e662016-04-13 16:06:56 -070086 file = open( os.path.dirname( main.testFile ) + "/dependencies/netconfConfig.json", 'w' )
Jeremye7580f32016-04-11 12:41:21 -070087 # These lines can cause errors during the configuration process because
88 # they cause the json string to turn into an unordered dictionary before
89 # sorting it alphabetically which can cause the driver type to not be
90 # configured.
91 # main.cfgJson = json.loads( main.cfgJson )
92 # main.cfgJson = json.dumps( main.cfgJson, sort_keys=True,
Jon Hall0c3f46f2017-05-24 16:34:46 -070093 # indent=4, separators=( ',', ': ' ) )
Jeremyfc567ca2016-02-16 15:08:25 -080094 print main.cfgJson
95 file.write( main.cfgJson )
96 if file:
97 createCfgResult = main.TRUE
98 file.close()
99 return createCfgResult
100 else:
Jon Hall0c3f46f2017-05-24 16:34:46 -0700101 main.log.error( "There was an error opening the file" )
Jeremyfc567ca2016-02-16 15:08:25 -0800102 return createCfgResult
103 except:
Jon Hall0c3f46f2017-05-24 16:34:46 -0700104 main.log.exception( "There was an error opening the file" )
Jeremyfc567ca2016-02-16 15:08:25 -0800105 return createCfgResult
106
Jon Hall0c3f46f2017-05-24 16:34:46 -0700107
Jeremyfc567ca2016-02-16 15:08:25 -0800108def sendConfig( main ):
109 """
110 This function prepares the command needed to upload the configuration
111 file to the REST API
112 """
Jeremyfc567ca2016-02-16 15:08:25 -0800113 url = "/network/configuration"
114 method = "POST"
115 data = main.cfgJson
116 configResult = main.FALSE
Devin Lim142b5342017-07-20 15:22:39 -0700117 sendResult = main.Cluster.active( 0 ).REST.send( url=url, method=method, data=data )
Jeremyfc567ca2016-02-16 15:08:25 -0800118 main.log.info( "Device configuration request response code: " + str( sendResult[ 0 ] ) )
Jon Hall0c3f46f2017-05-24 16:34:46 -0700119 if ( 200 <= sendResult[ 0 ] <= 299 ):
Jeremyfc567ca2016-02-16 15:08:25 -0800120 configResult = main.TRUE
121 else:
122 configResult = main.FALSE
123
124 return configResult
125
Jon Hall0c3f46f2017-05-24 16:34:46 -0700126
Jeremyfc567ca2016-02-16 15:08:25 -0800127def devices( main ):
128 """
129 This function get the list of devices from the REST API, the ONOS CLI, and
130 the device-controllers command and check to see that each recognizes the
131 device is configured according to the configuration uploaded above.
132 """
133 availResult = main.FALSE
134 typeResult = main.FALSE
135 addressResult = main.FALSE
136 driverResult = main.FALSE
137 try:
Devin Lim142b5342017-07-20 15:22:39 -0700138 apiResult = main.Cluster.active( 0 ).REST.devices()
139 cliResult = main.Cluster.active( 0 ).CLI.devices()
Jeremyfc567ca2016-02-16 15:08:25 -0800140
141 apiDict = json.loads( apiResult )
142 cliDict = json.loads( cliResult )
143 apiAnnotations = apiDict[ 0 ].get( "annotations" )
144 cliAnnotations = cliDict[ 0 ].get( "annotations" )
145
146 main.log.info( "API device availability result: " + str( apiDict[ 0 ].get( "available" ) ) )
147 main.log.info( "CLI device availability result: " + str( cliDict[ 0 ].get( "available" ) ) )
Jon Hall0c3f46f2017-05-24 16:34:46 -0700148 if apiDict[ 0 ].get( "available" ) and cliDict[ 0 ].get( "available" ):
Jeremyfc567ca2016-02-16 15:08:25 -0800149 availResult = main.TRUE
150 main.log.info( "API device type result: " + apiDict[ 0 ].get( "type" ) )
151 main.log.info( "CLI device type result: " + cliDict[ 0 ].get( "type" ) )
152 if apiDict[ 0 ].get( "type" ) == "SWITCH" and cliDict[ 0 ].get( "type" ) == "SWITCH":
153 typeResult = main.TRUE
154 main.log.info( "API device ipaddress: " + apiAnnotations.get( "ipaddress" ) )
155 main.log.info( "CLI device ipaddress: " + apiAnnotations.get( "ipaddress" ) )
156 if str( apiAnnotations.get( "ipaddress" ) ) == main.configDeviceIp and str( cliAnnotations.get( "ipaddress" ) ) == main.configDeviceIp:
157 addressResult = main.TRUE
158 main.log.info( "API device driver: " + apiAnnotations.get( "driver" ) )
159 main.log.info( "CLI device driver: " + cliAnnotations.get( "driver" ) )
160 if apiAnnotations.get( "driver" ) == main.configDriver and cliAnnotations.get( "driver" ) == main.configDriver:
161 driverResult = main.TRUE
162
163 return availResult and typeResult and addressResult and driverResult
164 except TypeError:
165 main.log.error( "Device was not configured correctly" )
Jon Hall53c5e662016-04-13 16:06:56 -0700166 return main.FALSE