blob: b67a5f7aa1254b047d0f2bf5fbfe8fb5e7c091de [file] [log] [blame]
Jeremyfc567ca2016-02-16 15:08:25 -08001"""
2 Wrapper functions for FUNCnetconf
3 This functions include Onosclidriver and Mininetclidriver driver functions
4 Author: Jeremy Songster, jeremy@onlab.us
5"""
6import time
7import json
Jon Hall53c5e662016-04-13 16:06:56 -07008import os
Jeremyfc567ca2016-02-16 15:08:25 -08009
Jon Hall0c3f46f2017-05-24 16:34:46 -070010
Jeremyfc567ca2016-02-16 15:08:25 -080011def __init__( self ):
12 self.default = ''
13
Jon Hall0c3f46f2017-05-24 16:34:46 -070014
Jeremyfc567ca2016-02-16 15:08:25 -080015def 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 """
Jeremyfc567ca2016-02-16 15:08:25 -080020 startResult = main.FALSE
Devin Lim58046fa2017-07-05 16:55:00 -070021 startResult = main.RESTs[ 0 ].activateApp( appName="org.onosproject.netconf" )
Jeremyfc567ca2016-02-16 15:08:25 -080022 return startResult
23
Jon Hall0c3f46f2017-05-24 16:34:46 -070024
Jeremyfc567ca2016-02-16 15:08:25 -080025def startOFC( main ):
26 """
27 This function uses pexpect pxssh class to activate the ofc-server daemon on OC2
28 """
Jeremyfc567ca2016-02-16 15:08:25 -080029 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 Hall0c3f46f2017-05-24 16:34:46 -070050
Jeremyfc567ca2016-02-16 15:08:25 -080051def 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 Hall0c3f46f2017-05-24 16:34:46 -070060 main.cfgJson = '{ "devices":{ "netconf:' + main.configDeviceIp + ":" +\
61 main.configDevicePort + '":' + '{ "basic":{ "driver":"' +\
Jeremyfc567ca2016-02-16 15:08:25 -080062 main.configDriver + '" } } }, "apps": { "' +\
Devin Limc87f1892017-06-19 16:52:57 -070063 main.configApps + '":{ "netconf_devices":[ { "username":' +\
Jeremyfc567ca2016-02-16 15:08:25 -080064 main.configName + ', "password":' + main.configPass +\
65 ', "ip":"' + main.configDeviceIp + '", "port":' +\
66 main.configPort + '} ] } } }'
67 try:
Jon Hall53c5e662016-04-13 16:06:56 -070068 file = open( os.path.dirname( main.testFile ) + "/dependencies/netconfConfig.json", 'w' )
Jeremye7580f32016-04-11 12:41:21 -070069 # 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 Hall0c3f46f2017-05-24 16:34:46 -070075 # indent=4, separators=( ',', ': ' ) )
Jeremyfc567ca2016-02-16 15:08:25 -080076 print main.cfgJson
77 file.write( main.cfgJson )
78 if file:
79 createCfgResult = main.TRUE
80 file.close()
81 return createCfgResult
82 else:
Jon Hall0c3f46f2017-05-24 16:34:46 -070083 main.log.error( "There was an error opening the file" )
Jeremyfc567ca2016-02-16 15:08:25 -080084 return createCfgResult
85 except:
Jon Hall0c3f46f2017-05-24 16:34:46 -070086 main.log.exception( "There was an error opening the file" )
Jeremyfc567ca2016-02-16 15:08:25 -080087 return createCfgResult
88
Jon Hall0c3f46f2017-05-24 16:34:46 -070089
Jeremyfc567ca2016-02-16 15:08:25 -080090def sendConfig( main ):
91 """
92 This function prepares the command needed to upload the configuration
93 file to the REST API
94 """
Jeremyfc567ca2016-02-16 15:08:25 -080095 url = "/network/configuration"
96 method = "POST"
97 data = main.cfgJson
98 configResult = main.FALSE
Devin Lim58046fa2017-07-05 16:55:00 -070099 sendResult = main.RESTs[ 0 ].send( url=url, method=method, data=data )
Jeremyfc567ca2016-02-16 15:08:25 -0800100 main.log.info( "Device configuration request response code: " + str( sendResult[ 0 ] ) )
Jon Hall0c3f46f2017-05-24 16:34:46 -0700101 if ( 200 <= sendResult[ 0 ] <= 299 ):
Jeremyfc567ca2016-02-16 15:08:25 -0800102 configResult = main.TRUE
103 else:
104 configResult = main.FALSE
105
106 return configResult
107
Jon Hall0c3f46f2017-05-24 16:34:46 -0700108
Jeremyfc567ca2016-02-16 15:08:25 -0800109def 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:
Devin Lim58046fa2017-07-05 16:55:00 -0700120 apiResult = main.RESTs[ 0 ].devices()
121 cliResult = main.CLIs[ 0 ].devices()
Jeremyfc567ca2016-02-16 15:08:25 -0800122
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 Hall0c3f46f2017-05-24 16:34:46 -0700130 if apiDict[ 0 ].get( "available" ) and cliDict[ 0 ].get( "available" ):
Jeremyfc567ca2016-02-16 15:08:25 -0800131 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 Hall53c5e662016-04-13 16:06:56 -0700148 return main.FALSE