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 | """ |
| 92 | ip = main.ONOSip[0] |
| 93 | port = 8181 |
| 94 | url = "/network/configuration" |
| 95 | method = "POST" |
| 96 | data = main.cfgJson |
| 97 | configResult = main.FALSE |
| 98 | sendResult = main.CLIs[ 0 ].send( ip=ip, port=port, url=url, method=method, data=data ) |
| 99 | main.log.info( "Device configuration request response code: " + str( sendResult[ 0 ] ) ) |
| 100 | if ( 200 <= sendResult[ 0 ] <= 299): |
| 101 | configResult = main.TRUE |
| 102 | else: |
| 103 | configResult = main.FALSE |
| 104 | |
| 105 | return configResult |
| 106 | |
| 107 | def devices( main ): |
| 108 | """ |
| 109 | This function get the list of devices from the REST API, the ONOS CLI, and |
| 110 | the device-controllers command and check to see that each recognizes the |
| 111 | device is configured according to the configuration uploaded above. |
| 112 | """ |
| 113 | availResult = main.FALSE |
| 114 | typeResult = main.FALSE |
| 115 | addressResult = main.FALSE |
| 116 | driverResult = main.FALSE |
| 117 | try: |
| 118 | apiResult = main.CLIs[ 0 ].devices() |
| 119 | cliResult = main.CLIs2[ 0 ].devices() |
| 120 | |
| 121 | apiDict = json.loads( apiResult ) |
| 122 | cliDict = json.loads( cliResult ) |
| 123 | apiAnnotations = apiDict[ 0 ].get( "annotations" ) |
| 124 | cliAnnotations = cliDict[ 0 ].get( "annotations" ) |
| 125 | |
| 126 | main.log.info( "API device availability result: " + str( apiDict[ 0 ].get( "available" ) ) ) |
| 127 | main.log.info( "CLI device availability result: " + str( cliDict[ 0 ].get( "available" ) ) ) |
| 128 | if apiDict[ 0 ].get( "available" ) == True and cliDict[ 0 ].get( "available" ) == True: |
| 129 | availResult = main.TRUE |
| 130 | main.log.info( "API device type result: " + apiDict[ 0 ].get( "type" ) ) |
| 131 | main.log.info( "CLI device type result: " + cliDict[ 0 ].get( "type" ) ) |
| 132 | if apiDict[ 0 ].get( "type" ) == "SWITCH" and cliDict[ 0 ].get( "type" ) == "SWITCH": |
| 133 | typeResult = main.TRUE |
| 134 | main.log.info( "API device ipaddress: " + apiAnnotations.get( "ipaddress" ) ) |
| 135 | main.log.info( "CLI device ipaddress: " + apiAnnotations.get( "ipaddress" ) ) |
| 136 | if str( apiAnnotations.get( "ipaddress" ) ) == main.configDeviceIp and str( cliAnnotations.get( "ipaddress" ) ) == main.configDeviceIp: |
| 137 | addressResult = main.TRUE |
| 138 | main.log.info( "API device driver: " + apiAnnotations.get( "driver" ) ) |
| 139 | main.log.info( "CLI device driver: " + cliAnnotations.get( "driver" ) ) |
| 140 | if apiAnnotations.get( "driver" ) == main.configDriver and cliAnnotations.get( "driver" ) == main.configDriver: |
| 141 | driverResult = main.TRUE |
| 142 | |
| 143 | return availResult and typeResult and addressResult and driverResult |
| 144 | except TypeError: |
| 145 | main.log.error( "Device was not configured correctly" ) |
Jon Hall | 53c5e66 | 2016-04-13 16:06:56 -0700 | [diff] [blame] | 146 | return main.FALSE |