Jeremy | fc567ca | 2016-02-16 15:08:25 -0800 | [diff] [blame] | 1 | """ |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame^] | 2 | Copyright 2016 Open Networking Foundation ( ONF ) |
Jeremy Ronquillo | b27ce4c | 2017-07-17 12:41:28 -0700 | [diff] [blame] | 3 | |
| 4 | Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>, |
| 5 | the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>, |
| 6 | or 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 |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame^] | 11 | ( at your option ) any later version. |
Jeremy Ronquillo | b27ce4c | 2017-07-17 12:41:28 -0700 | [diff] [blame] | 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 | |
Jeremy | fc567ca | 2016-02-16 15:08:25 -0800 | [diff] [blame] | 22 | Wrapper functions for FUNCnetconf |
| 23 | This functions include Onosclidriver and Mininetclidriver driver functions |
| 24 | Author: Jeremy Songster, jeremy@onlab.us |
| 25 | """ |
| 26 | import time |
| 27 | import json |
Jon Hall | 53c5e66 | 2016-04-13 16:06:56 -0700 | [diff] [blame] | 28 | import os |
Jeremy | fc567ca | 2016-02-16 15:08:25 -0800 | [diff] [blame] | 29 | |
Jon Hall | 0c3f46f | 2017-05-24 16:34:46 -0700 | [diff] [blame] | 30 | |
Jeremy | fc567ca | 2016-02-16 15:08:25 -0800 | [diff] [blame] | 31 | def __init__( self ): |
| 32 | self.default = '' |
| 33 | |
Jon Hall | 0c3f46f | 2017-05-24 16:34:46 -0700 | [diff] [blame] | 34 | |
Jeremy | fc567ca | 2016-02-16 15:08:25 -0800 | [diff] [blame] | 35 | def 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 | """ |
Jeremy | fc567ca | 2016-02-16 15:08:25 -0800 | [diff] [blame] | 40 | startResult = main.FALSE |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 41 | startResult = main.Cluster.active( 0 ).REST.activateApp( appName="org.onosproject.netconf" ) |
Jeremy | fc567ca | 2016-02-16 15:08:25 -0800 | [diff] [blame] | 42 | return startResult |
| 43 | |
Jon Hall | 0c3f46f | 2017-05-24 16:34:46 -0700 | [diff] [blame] | 44 | |
Jeremy | fc567ca | 2016-02-16 15:08:25 -0800 | [diff] [blame] | 45 | def startOFC( main ): |
| 46 | """ |
| 47 | This function uses pexpect pxssh class to activate the ofc-server daemon on OC2 |
| 48 | """ |
Jeremy | fc567ca | 2016-02-16 15:08:25 -0800 | [diff] [blame] | 49 | 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 Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 67 | main.cleanAndExit() |
Jon Hall | 0c3f46f | 2017-05-24 16:34:46 -0700 | [diff] [blame] | 68 | |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame^] | 69 | |
Jeremy | fc567ca | 2016-02-16 15:08:25 -0800 | [diff] [blame] | 70 | def createConfig( main ): |
| 71 | """ |
| 72 | This function writes a configuration file that can later be sent to the |
| 73 | REST API to configure a device. |
| 74 | The controller device is assumed to be OC1 |
| 75 | The device to be configured is assumed to be OC2 |
| 76 | """ |
| 77 | createCfgResult = main.FALSE |
| 78 | # TODO, add ability to set Manufacturer, Hardware and Software versions |
Jon Hall | 0c3f46f | 2017-05-24 16:34:46 -0700 | [diff] [blame] | 79 | main.cfgJson = '{ "devices":{ "netconf:' + main.configDeviceIp + ":" +\ |
| 80 | main.configDevicePort + '":' + '{ "basic":{ "driver":"' +\ |
Jeremy | fc567ca | 2016-02-16 15:08:25 -0800 | [diff] [blame] | 81 | main.configDriver + '" } } }, "apps": { "' +\ |
Devin Lim | c87f189 | 2017-06-19 16:52:57 -0700 | [diff] [blame] | 82 | main.configApps + '":{ "netconf_devices":[ { "username":' +\ |
Jeremy | fc567ca | 2016-02-16 15:08:25 -0800 | [diff] [blame] | 83 | main.configName + ', "password":' + main.configPass +\ |
| 84 | ', "ip":"' + main.configDeviceIp + '", "port":' +\ |
| 85 | main.configPort + '} ] } } }' |
| 86 | try: |
Jon Hall | 53c5e66 | 2016-04-13 16:06:56 -0700 | [diff] [blame] | 87 | file = open( os.path.dirname( main.testFile ) + "/dependencies/netconfConfig.json", 'w' ) |
Jeremy | e7580f3 | 2016-04-11 12:41:21 -0700 | [diff] [blame] | 88 | # These lines can cause errors during the configuration process because |
| 89 | # they cause the json string to turn into an unordered dictionary before |
| 90 | # sorting it alphabetically which can cause the driver type to not be |
| 91 | # configured. |
| 92 | # main.cfgJson = json.loads( main.cfgJson ) |
| 93 | # main.cfgJson = json.dumps( main.cfgJson, sort_keys=True, |
Jon Hall | 0c3f46f | 2017-05-24 16:34:46 -0700 | [diff] [blame] | 94 | # indent=4, separators=( ',', ': ' ) ) |
Jeremy | fc567ca | 2016-02-16 15:08:25 -0800 | [diff] [blame] | 95 | print main.cfgJson |
| 96 | file.write( main.cfgJson ) |
| 97 | if file: |
| 98 | createCfgResult = main.TRUE |
| 99 | file.close() |
| 100 | return createCfgResult |
| 101 | else: |
Jon Hall | 0c3f46f | 2017-05-24 16:34:46 -0700 | [diff] [blame] | 102 | main.log.error( "There was an error opening the file" ) |
Jeremy | fc567ca | 2016-02-16 15:08:25 -0800 | [diff] [blame] | 103 | return createCfgResult |
| 104 | except: |
Jon Hall | 0c3f46f | 2017-05-24 16:34:46 -0700 | [diff] [blame] | 105 | main.log.exception( "There was an error opening the file" ) |
Jeremy | fc567ca | 2016-02-16 15:08:25 -0800 | [diff] [blame] | 106 | return createCfgResult |
| 107 | |
Jon Hall | 0c3f46f | 2017-05-24 16:34:46 -0700 | [diff] [blame] | 108 | |
Jeremy | fc567ca | 2016-02-16 15:08:25 -0800 | [diff] [blame] | 109 | def sendConfig( main ): |
| 110 | """ |
| 111 | This function prepares the command needed to upload the configuration |
| 112 | file to the REST API |
| 113 | """ |
Jeremy | fc567ca | 2016-02-16 15:08:25 -0800 | [diff] [blame] | 114 | url = "/network/configuration" |
| 115 | method = "POST" |
| 116 | data = main.cfgJson |
| 117 | configResult = main.FALSE |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 118 | sendResult = main.Cluster.active( 0 ).REST.send( url=url, method=method, data=data ) |
Jeremy | fc567ca | 2016-02-16 15:08:25 -0800 | [diff] [blame] | 119 | main.log.info( "Device configuration request response code: " + str( sendResult[ 0 ] ) ) |
Jon Hall | 0c3f46f | 2017-05-24 16:34:46 -0700 | [diff] [blame] | 120 | if ( 200 <= sendResult[ 0 ] <= 299 ): |
Jeremy | fc567ca | 2016-02-16 15:08:25 -0800 | [diff] [blame] | 121 | configResult = main.TRUE |
| 122 | else: |
| 123 | configResult = main.FALSE |
| 124 | |
| 125 | return configResult |
| 126 | |
Jon Hall | 0c3f46f | 2017-05-24 16:34:46 -0700 | [diff] [blame] | 127 | |
Jeremy | fc567ca | 2016-02-16 15:08:25 -0800 | [diff] [blame] | 128 | def devices( main ): |
| 129 | """ |
| 130 | This function get the list of devices from the REST API, the ONOS CLI, and |
| 131 | the device-controllers command and check to see that each recognizes the |
| 132 | device is configured according to the configuration uploaded above. |
| 133 | """ |
| 134 | availResult = main.FALSE |
| 135 | typeResult = main.FALSE |
| 136 | addressResult = main.FALSE |
| 137 | driverResult = main.FALSE |
| 138 | try: |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 139 | apiResult = main.Cluster.active( 0 ).REST.devices() |
| 140 | cliResult = main.Cluster.active( 0 ).CLI.devices() |
Jeremy | fc567ca | 2016-02-16 15:08:25 -0800 | [diff] [blame] | 141 | |
| 142 | apiDict = json.loads( apiResult ) |
| 143 | cliDict = json.loads( cliResult ) |
| 144 | apiAnnotations = apiDict[ 0 ].get( "annotations" ) |
| 145 | cliAnnotations = cliDict[ 0 ].get( "annotations" ) |
| 146 | |
| 147 | main.log.info( "API device availability result: " + str( apiDict[ 0 ].get( "available" ) ) ) |
| 148 | main.log.info( "CLI device availability result: " + str( cliDict[ 0 ].get( "available" ) ) ) |
Jon Hall | 0c3f46f | 2017-05-24 16:34:46 -0700 | [diff] [blame] | 149 | if apiDict[ 0 ].get( "available" ) and cliDict[ 0 ].get( "available" ): |
Jeremy | fc567ca | 2016-02-16 15:08:25 -0800 | [diff] [blame] | 150 | availResult = main.TRUE |
| 151 | main.log.info( "API device type result: " + apiDict[ 0 ].get( "type" ) ) |
| 152 | main.log.info( "CLI device type result: " + cliDict[ 0 ].get( "type" ) ) |
| 153 | if apiDict[ 0 ].get( "type" ) == "SWITCH" and cliDict[ 0 ].get( "type" ) == "SWITCH": |
| 154 | typeResult = main.TRUE |
| 155 | main.log.info( "API device ipaddress: " + apiAnnotations.get( "ipaddress" ) ) |
| 156 | main.log.info( "CLI device ipaddress: " + apiAnnotations.get( "ipaddress" ) ) |
| 157 | if str( apiAnnotations.get( "ipaddress" ) ) == main.configDeviceIp and str( cliAnnotations.get( "ipaddress" ) ) == main.configDeviceIp: |
| 158 | addressResult = main.TRUE |
| 159 | main.log.info( "API device driver: " + apiAnnotations.get( "driver" ) ) |
| 160 | main.log.info( "CLI device driver: " + cliAnnotations.get( "driver" ) ) |
| 161 | if apiAnnotations.get( "driver" ) == main.configDriver and cliAnnotations.get( "driver" ) == main.configDriver: |
| 162 | driverResult = main.TRUE |
| 163 | |
| 164 | return availResult and typeResult and addressResult and driverResult |
| 165 | except TypeError: |
| 166 | main.log.error( "Device was not configured correctly" ) |
Jon Hall | 53c5e66 | 2016-04-13 16:06:56 -0700 | [diff] [blame] | 167 | return main.FALSE |