blob: 37df23a192858cbd8477c554cdf39d5265027783 [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
8
9def __init__( self ):
10 self.default = ''
11
12def 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
22def 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
48def 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' )
Jeremye7580f32016-04-11 12:41:21 -070066 # These lines can cause errors during the configuration process because
67 # they cause the json string to turn into an unordered dictionary before
68 # sorting it alphabetically which can cause the driver type to not be
69 # configured.
70 # main.cfgJson = json.loads( main.cfgJson )
71 # main.cfgJson = json.dumps( main.cfgJson, sort_keys=True,
72 # indent=4, separators=(',', ': '))
Jeremyfc567ca2016-02-16 15:08:25 -080073 print main.cfgJson
74 file.write( main.cfgJson )
75 if file:
76 createCfgResult = main.TRUE
77 file.close()
78 return createCfgResult
79 else:
80 main.log.error( "There was an error opening the file")
81 return createCfgResult
82 except:
83 main.log.exception( "There was an error opening the file")
84 return createCfgResult
85
86def sendConfig( main ):
87 """
88 This function prepares the command needed to upload the configuration
89 file to the REST API
90 """
91 ip = main.ONOSip[0]
92 port = 8181
93 url = "/network/configuration"
94 method = "POST"
95 data = main.cfgJson
96 configResult = main.FALSE
97 sendResult = main.CLIs[ 0 ].send( ip=ip, port=port, url=url, method=method, data=data )
98 main.log.info( "Device configuration request response code: " + str( sendResult[ 0 ] ) )
99 if ( 200 <= sendResult[ 0 ] <= 299):
100 configResult = main.TRUE
101 else:
102 configResult = main.FALSE
103
104 return configResult
105
106def devices( main ):
107 """
108 This function get the list of devices from the REST API, the ONOS CLI, and
109 the device-controllers command and check to see that each recognizes the
110 device is configured according to the configuration uploaded above.
111 """
112 availResult = main.FALSE
113 typeResult = main.FALSE
114 addressResult = main.FALSE
115 driverResult = main.FALSE
116 try:
117 apiResult = main.CLIs[ 0 ].devices()
118 cliResult = main.CLIs2[ 0 ].devices()
119
120 apiDict = json.loads( apiResult )
121 cliDict = json.loads( cliResult )
122 apiAnnotations = apiDict[ 0 ].get( "annotations" )
123 cliAnnotations = cliDict[ 0 ].get( "annotations" )
124
125 main.log.info( "API device availability result: " + str( apiDict[ 0 ].get( "available" ) ) )
126 main.log.info( "CLI device availability result: " + str( cliDict[ 0 ].get( "available" ) ) )
127 if apiDict[ 0 ].get( "available" ) == True and cliDict[ 0 ].get( "available" ) == True:
128 availResult = main.TRUE
129 main.log.info( "API device type result: " + apiDict[ 0 ].get( "type" ) )
130 main.log.info( "CLI device type result: " + cliDict[ 0 ].get( "type" ) )
131 if apiDict[ 0 ].get( "type" ) == "SWITCH" and cliDict[ 0 ].get( "type" ) == "SWITCH":
132 typeResult = main.TRUE
133 main.log.info( "API device ipaddress: " + apiAnnotations.get( "ipaddress" ) )
134 main.log.info( "CLI device ipaddress: " + apiAnnotations.get( "ipaddress" ) )
135 if str( apiAnnotations.get( "ipaddress" ) ) == main.configDeviceIp and str( cliAnnotations.get( "ipaddress" ) ) == main.configDeviceIp:
136 addressResult = main.TRUE
137 main.log.info( "API device driver: " + apiAnnotations.get( "driver" ) )
138 main.log.info( "CLI device driver: " + cliAnnotations.get( "driver" ) )
139 if apiAnnotations.get( "driver" ) == main.configDriver and cliAnnotations.get( "driver" ) == main.configDriver:
140 driverResult = main.TRUE
141
142 return availResult and typeResult and addressResult and driverResult
143 except TypeError:
144 main.log.error( "Device was not configured correctly" )
145 return main.FALSE