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