blob: 9523e81cb9ba90e006344fa32b898380ec4287c8 [file] [log] [blame]
Jon Hall66e001c2015-11-12 09:45:10 -08001"""
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07002Copyright 2015 Open Networking Foundation ( ONF )
Jeremy Ronquillob27ce4c2017-07-17 12:41:28 -07003
4Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>,
5the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>,
6or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg>
7
8 TestON is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070011 ( at your option ) any later version.
Jeremy Ronquillob27ce4c2017-07-17 12:41:28 -070012
13 TestON is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with TestON. If not, see <http://www.gnu.org/licenses/>
20
21
22 These functions are for use with the Network config system
Jon Hall66e001c2015-11-12 09:45:10 -080023"""
24import time
25
Jon Hallbc080f92017-05-24 16:29:55 -070026
Jon Hall66e001c2015-11-12 09:45:10 -080027def compareCfg( main, gossipTime=None ):
28 """
29 Compare the network configurations across all nodes in the network
30 gossipTime is the number of seconds each gossip round take for the netCfg maps
31 """
32 main.step( "Check net config" )
33 if gossipTime:
Devin Lim142b5342017-07-20 15:22:39 -070034 time.sleep( gossipTime * main.Cluster.numCtrls )
Jon Hall66e001c2015-11-12 09:45:10 -080035 responses = []
Jeremy Ronquillo4a30ffe2017-06-07 11:36:35 -070036 result = utilities.retry( f=checkNodeResponses,
37 retValue=False,
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070038 kwargs={ 'main': main, 'responses': responses },
39 sleep=main.retrysleep,
40 attempts=main.retrytimes )
Jeremy Ronquillo4a30ffe2017-06-07 11:36:35 -070041 utilities.assert_equals( expect=True,
42 actual=result,
43 onpass="Net Cfg is the same on all nodes",
44 onfail="Check Net Cfg failed. Check above messages." )
45
46
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070047def checkNodeResponses( main, responses ):
Jeremy Ronquillo4a30ffe2017-06-07 11:36:35 -070048 numberOfFailedNodes = 0 # Tracks the number of nodes that failed to get net configuration
Devin Lim142b5342017-07-20 15:22:39 -070049 for ctrl in main.Cluster.active():
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070050 response = ctrl.REST.getNetCfg()
Devin Lim142b5342017-07-20 15:22:39 -070051 responses.append( ctrl.REST.pprint( response ) )
Jon Hall66e001c2015-11-12 09:45:10 -080052 if response == main.FALSE:
Jeremy Ronquillo4a30ffe2017-06-07 11:36:35 -070053 numberOfFailedNodes += 1
54
Jon Hallbc080f92017-05-24 16:29:55 -070055 compare = [ i == responses[ 0 ] for i in responses ]
Jeremy Ronquillo4a30ffe2017-06-07 11:36:35 -070056 if numberOfFailedNodes == 0 and all( compare ):
57 return True
58
59 # Failed, providing feedback on cli
60 if numberOfFailedNodes > 0:
61 main.log.warn( numberOfFailedNodes + " node(s) failed to GET Net Config" )
Jon Hall66e001c2015-11-12 09:45:10 -080062 if not all( compare ):
Jeremy Ronquillo4a30ffe2017-06-07 11:36:35 -070063 main.log.debug( "Net Cfg is different on some nodes. Net Config results:" )
Jon Hall66e001c2015-11-12 09:45:10 -080064 for i in responses:
65 main.log.debug( i )
Jeremy Ronquillo4a30ffe2017-06-07 11:36:35 -070066 return False
Jeremy Ronquillo4cf537d2017-06-26 11:20:52 -070067
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070068
Jeremy Ronquillo4cf537d2017-06-26 11:20:52 -070069def checkDeviceAnnotations( main, jsonObj, sw ):
70 id = str( sw.get( 'id' ) )
71 keys = [ 'name', 'owner', 'rackAddress' ]
72 correct = True
73 for k in keys:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070074 if str( sw.get( 'annotations', {} ).get( k ) ) != str( jsonObj[ k ] ):
Jeremy Ronquillo4cf537d2017-06-26 11:20:52 -070075 correct = False
76 main.log.debug( "{} is wrong on switch: ".format( k ) + id )
77 if not correct:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070078 main.log.error( "Annotations for switch " + id + " are incorrect: {}".format( sw ) )
Jeremy Ronquillo4cf537d2017-06-26 11:20:52 -070079 return correct
80
81
82def checkAllDeviceAnnotations( main, json ):
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070083 devices = main.Cluster.active( 0 ).REST.devices()
Jeremy Ronquillo4cf537d2017-06-26 11:20:52 -070084 id = "of:0000000000000001"
85 i = 1
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070086 result = []
Jeremy Ronquillo4cf537d2017-06-26 11:20:52 -070087 try:
88 for sw in json.loads( devices ):
89 if id in sw.get( 'id' ):
90 jsonObj = getattr( main, "s" + str( i ) + "Json" )
91 isDeviceAnnotationCorrect = checkDeviceAnnotations( main, jsonObj, sw )
92 result.append( isDeviceAnnotationCorrect )
93 i += 1
94 id = "of:000000000000000" + str( i )
95 except( TypeError, ValueError ):
96 main.log.error( "Problem loading device" )
97 return False
98 return all( result )