Jeremy | fc567ca | 2016-02-16 15:08:25 -0800 | [diff] [blame] | 1 | """ |
| 2 | Wrapper functions for FUNCnetconf |
| 3 | This functions include Onosclidriver and Mininetclidriver driver functions |
| 4 | Author: Jeremy Songster, jeremy@onlab.us |
| 5 | """ |
| 6 | import time |
| 7 | import json |
Jon Hall | 53c5e66 | 2016-04-13 16:06:56 -0700 | [diff] [blame] | 8 | import os |
Jeremy | fc567ca | 2016-02-16 15:08:25 -0800 | [diff] [blame] | 9 | |
Jon Hall | 0c3f46f | 2017-05-24 16:34:46 -0700 | [diff] [blame] | 10 | |
Jeremy | fc567ca | 2016-02-16 15:08:25 -0800 | [diff] [blame] | 11 | def __init__( self ): |
| 12 | self.default = '' |
| 13 | |
Jon Hall | 0c3f46f | 2017-05-24 16:34:46 -0700 | [diff] [blame] | 14 | |
Jeremy | fc567ca | 2016-02-16 15:08:25 -0800 | [diff] [blame] | 15 | def startApp( main ): |
| 16 | """ |
| 17 | This function starts the netconf app in all onos nodes and ensures that |
| 18 | the OF-Config server is running on the node to be configured |
| 19 | """ |
Jeremy | fc567ca | 2016-02-16 15:08:25 -0800 | [diff] [blame] | 20 | startResult = main.FALSE |
| 21 | startResult = main.CLIs[ 0 ].activateApp( appName="org.onosproject.netconf" ) |
| 22 | return startResult |
| 23 | |
Jon Hall | 0c3f46f | 2017-05-24 16:34:46 -0700 | [diff] [blame] | 24 | |
Jeremy | fc567ca | 2016-02-16 15:08:25 -0800 | [diff] [blame] | 25 | def startOFC( main ): |
| 26 | """ |
| 27 | This function uses pexpect pxssh class to activate the ofc-server daemon on OC2 |
| 28 | """ |
Jeremy | fc567ca | 2016-02-16 15:08:25 -0800 | [diff] [blame] | 29 | startResult = main.FALSE |
| 30 | try: |
| 31 | main.ONOSbench.handle.sendline( "" ) |
| 32 | main.ONOSbench.handle.expect( "\$" ) |
| 33 | main.ONOSbench.handle.sendline( "ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1 }'" ) |
| 34 | main.ONOSbench.handle.expect( "\$1 }'" ) |
| 35 | main.ONOSbench.handle.expect( "\$" ) |
| 36 | main.configDeviceIp = main.ONOSbench.handle.before |
| 37 | main.configDeviceIp = main.configDeviceIp.split() |
| 38 | main.configDeviceIp = main.configDeviceIp[ 0 ] |
| 39 | main.log.info( "Device to be configured: " + str( main.configDeviceIp ) ) |
| 40 | main.ONOSbench.handle.sendline( "sudo ofc-server" ) |
| 41 | main.ONOSbench.handle.expect( "\$" ) |
| 42 | startResult = main.TRUE |
| 43 | return startResult |
| 44 | except pexpect.ExceptionPexpect as e: |
| 45 | main.log.exception( self.name + ": Pexpect exception found: " ) |
| 46 | main.log.error( self.name + ": " + self.handle.before ) |
| 47 | main.cleanup() |
| 48 | main.exit() |
| 49 | |
Jon Hall | 0c3f46f | 2017-05-24 16:34:46 -0700 | [diff] [blame] | 50 | |
Jeremy | fc567ca | 2016-02-16 15:08:25 -0800 | [diff] [blame] | 51 | def createConfig( main ): |
| 52 | """ |
| 53 | This function writes a configuration file that can later be sent to the |
| 54 | REST API to configure a device. |
| 55 | The controller device is assumed to be OC1 |
| 56 | The device to be configured is assumed to be OC2 |
| 57 | """ |
| 58 | createCfgResult = main.FALSE |
| 59 | # TODO, add ability to set Manufacturer, Hardware and Software versions |
Jon Hall | 0c3f46f | 2017-05-24 16:34:46 -0700 | [diff] [blame] | 60 | main.cfgJson = '{ "devices":{ "netconf:' + main.configDeviceIp + ":" +\ |
| 61 | main.configDevicePort + '":' + '{ "basic":{ "driver":"' +\ |
Jeremy | fc567ca | 2016-02-16 15:08:25 -0800 | [diff] [blame] | 62 | main.configDriver + '" } } }, "apps": { "' +\ |
alison | 70b3f86 | 2016-12-02 15:42:33 -0800 | [diff] [blame] | 63 | main.configApps + '":{ "devices":[ { "username":' +\ |
Jeremy | fc567ca | 2016-02-16 15:08:25 -0800 | [diff] [blame] | 64 | main.configName + ', "password":' + main.configPass +\ |
| 65 | ', "ip":"' + main.configDeviceIp + '", "port":' +\ |
| 66 | main.configPort + '} ] } } }' |
| 67 | try: |
Jon Hall | 53c5e66 | 2016-04-13 16:06:56 -0700 | [diff] [blame] | 68 | file = open( os.path.dirname( main.testFile ) + "/dependencies/netconfConfig.json", 'w' ) |
Jeremy | e7580f3 | 2016-04-11 12:41:21 -0700 | [diff] [blame] | 69 | # These lines can cause errors during the configuration process because |
| 70 | # they cause the json string to turn into an unordered dictionary before |
| 71 | # sorting it alphabetically which can cause the driver type to not be |
| 72 | # configured. |
| 73 | # main.cfgJson = json.loads( main.cfgJson ) |
| 74 | # main.cfgJson = json.dumps( main.cfgJson, sort_keys=True, |
Jon Hall | 0c3f46f | 2017-05-24 16:34:46 -0700 | [diff] [blame] | 75 | # indent=4, separators=( ',', ': ' ) ) |
Jeremy | fc567ca | 2016-02-16 15:08:25 -0800 | [diff] [blame] | 76 | print main.cfgJson |
| 77 | file.write( main.cfgJson ) |
| 78 | if file: |
| 79 | createCfgResult = main.TRUE |
| 80 | file.close() |
| 81 | return createCfgResult |
| 82 | else: |
Jon Hall | 0c3f46f | 2017-05-24 16:34:46 -0700 | [diff] [blame] | 83 | main.log.error( "There was an error opening the file" ) |
Jeremy | fc567ca | 2016-02-16 15:08:25 -0800 | [diff] [blame] | 84 | return createCfgResult |
| 85 | except: |
Jon Hall | 0c3f46f | 2017-05-24 16:34:46 -0700 | [diff] [blame] | 86 | main.log.exception( "There was an error opening the file" ) |
Jeremy | fc567ca | 2016-02-16 15:08:25 -0800 | [diff] [blame] | 87 | return createCfgResult |
| 88 | |
Jon Hall | 0c3f46f | 2017-05-24 16:34:46 -0700 | [diff] [blame] | 89 | |
Jeremy | fc567ca | 2016-02-16 15:08:25 -0800 | [diff] [blame] | 90 | def sendConfig( main ): |
| 91 | """ |
| 92 | This function prepares the command needed to upload the configuration |
| 93 | file to the REST API |
| 94 | """ |
Jeremy | fc567ca | 2016-02-16 15:08:25 -0800 | [diff] [blame] | 95 | url = "/network/configuration" |
| 96 | method = "POST" |
| 97 | data = main.cfgJson |
| 98 | configResult = main.FALSE |
Jeremy Songster | eb5588a | 2016-05-11 11:15:37 -0700 | [diff] [blame] | 99 | sendResult = main.CLIs[ 0 ].send( url=url, method=method, data=data ) |
Jeremy | fc567ca | 2016-02-16 15:08:25 -0800 | [diff] [blame] | 100 | main.log.info( "Device configuration request response code: " + str( sendResult[ 0 ] ) ) |
Jon Hall | 0c3f46f | 2017-05-24 16:34:46 -0700 | [diff] [blame] | 101 | if ( 200 <= sendResult[ 0 ] <= 299 ): |
Jeremy | fc567ca | 2016-02-16 15:08:25 -0800 | [diff] [blame] | 102 | configResult = main.TRUE |
| 103 | else: |
| 104 | configResult = main.FALSE |
| 105 | |
| 106 | return configResult |
| 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 devices( main ): |
| 110 | """ |
| 111 | This function get the list of devices from the REST API, the ONOS CLI, and |
| 112 | the device-controllers command and check to see that each recognizes the |
| 113 | device is configured according to the configuration uploaded above. |
| 114 | """ |
| 115 | availResult = main.FALSE |
| 116 | typeResult = main.FALSE |
| 117 | addressResult = main.FALSE |
| 118 | driverResult = main.FALSE |
| 119 | try: |
| 120 | apiResult = main.CLIs[ 0 ].devices() |
| 121 | cliResult = main.CLIs2[ 0 ].devices() |
| 122 | |
| 123 | apiDict = json.loads( apiResult ) |
| 124 | cliDict = json.loads( cliResult ) |
| 125 | apiAnnotations = apiDict[ 0 ].get( "annotations" ) |
| 126 | cliAnnotations = cliDict[ 0 ].get( "annotations" ) |
| 127 | |
| 128 | main.log.info( "API device availability result: " + str( apiDict[ 0 ].get( "available" ) ) ) |
| 129 | main.log.info( "CLI device availability result: " + str( cliDict[ 0 ].get( "available" ) ) ) |
Jon Hall | 0c3f46f | 2017-05-24 16:34:46 -0700 | [diff] [blame] | 130 | if apiDict[ 0 ].get( "available" ) and cliDict[ 0 ].get( "available" ): |
Jeremy | fc567ca | 2016-02-16 15:08:25 -0800 | [diff] [blame] | 131 | availResult = main.TRUE |
| 132 | main.log.info( "API device type result: " + apiDict[ 0 ].get( "type" ) ) |
| 133 | main.log.info( "CLI device type result: " + cliDict[ 0 ].get( "type" ) ) |
| 134 | if apiDict[ 0 ].get( "type" ) == "SWITCH" and cliDict[ 0 ].get( "type" ) == "SWITCH": |
| 135 | typeResult = main.TRUE |
| 136 | main.log.info( "API device ipaddress: " + apiAnnotations.get( "ipaddress" ) ) |
| 137 | main.log.info( "CLI device ipaddress: " + apiAnnotations.get( "ipaddress" ) ) |
| 138 | if str( apiAnnotations.get( "ipaddress" ) ) == main.configDeviceIp and str( cliAnnotations.get( "ipaddress" ) ) == main.configDeviceIp: |
| 139 | addressResult = main.TRUE |
| 140 | main.log.info( "API device driver: " + apiAnnotations.get( "driver" ) ) |
| 141 | main.log.info( "CLI device driver: " + cliAnnotations.get( "driver" ) ) |
| 142 | if apiAnnotations.get( "driver" ) == main.configDriver and cliAnnotations.get( "driver" ) == main.configDriver: |
| 143 | driverResult = main.TRUE |
| 144 | |
| 145 | return availResult and typeResult and addressResult and driverResult |
| 146 | except TypeError: |
| 147 | main.log.error( "Device was not configured correctly" ) |
Jon Hall | 53c5e66 | 2016-04-13 16:06:56 -0700 | [diff] [blame] | 148 | return main.FALSE |