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