blob: 5e95efe6e6f288b49af0bf29e9db51afde9359ce [file] [log] [blame]
You Wang68568b12019-03-04 11:49:57 -08001"""
2Copyright 2018 Open Networking Foundation ( ONF )
3
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
11 ( at your option ) any later version.
12
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"""
21import json
22import re
23
24ONOS_GROUP_ID = 'org.onosproject'
25SR_APP = 'segmentrouting'
26DHCP_APP = 'dhcprelay'
27DHCP_APP_ID = ONOS_GROUP_ID + '.' + DHCP_APP
28
29# Translate configuration JSON file from BMv2 driver to OFDPA-OVS driver.
30def bmv2ToOfdpa( main, cfgFile="" ):
Jon Hall43060f62020-06-23 13:13:33 -070031 didRE = r"device:(?P<swType>bmv2|tofino):(?P<swRole>leaf|spine)(?P<swNum>[1-9][0-9]*)(/(?P<portNum>[0-9]+))?"
You Wang68568b12019-03-04 11:49:57 -080032 if not cfgFile:
33 cfgFile = "%s%s.json" % ( main.configPath + main.forJson,
34 main.cfgName )
35 with open( cfgFile ) as cfg:
36 netcfg = json.load( cfg )
37
38 if 'ports' in netcfg.keys():
39 for port in netcfg[ 'ports' ].keys():
Jon Hall43060f62020-06-23 13:13:33 -070040 searchObj = re.search( didRE, port )
You Wang68568b12019-03-04 11:49:57 -080041 if searchObj:
Jon Hall43060f62020-06-23 13:13:33 -070042 new_port = 'of:' + searchObj.group( 'swNum' ).zfill( 16 ) + '/' + searchObj.group( 'portNum' )
You Wang68568b12019-03-04 11:49:57 -080043 netcfg[ 'ports' ][ new_port ] = netcfg[ 'ports' ].pop( port )
44
45 if 'hosts' in netcfg.keys():
46 for ( host, hostCfg ) in netcfg[ 'hosts' ].items():
47 if type( hostCfg[ 'basic' ][ 'locations' ] ) is list:
48 new_locations = []
49 for location in hostCfg[ 'basic' ][ 'locations' ]:
Jon Hall43060f62020-06-23 13:13:33 -070050 searchObj = re.search( didRE, location )
You Wang68568b12019-03-04 11:49:57 -080051 if searchObj:
Jon Hall43060f62020-06-23 13:13:33 -070052 new_locations.append( 'of:' + searchObj.group( 'swNum' ).zfill( 16 ) + '/' + searchObj.group( 'portNum' ) )
You Wang68568b12019-03-04 11:49:57 -080053 else:
54 new_locations.append( location )
55 netcfg[ 'hosts' ][ host ][ 'basic' ][ 'locations' ] = new_locations
56 else:
57 location = hostCfg[ 'basic' ][ 'locations' ]
Jon Hall43060f62020-06-23 13:13:33 -070058 searchObj = re.search( didRE, location )
You Wang68568b12019-03-04 11:49:57 -080059 if searchObj:
Jon Hall43060f62020-06-23 13:13:33 -070060 new_location = 'of:' + searchObj.group( 'swNum' ).zfill( 16 ) + '/' + searchObj.group( 'portNum' )
You Wang68568b12019-03-04 11:49:57 -080061 netcfg[ 'hosts' ][ host ][ 'basic' ][ 'locations' ] = new_location
62
63 if 'devices' in netcfg.keys():
64 for device in netcfg[ 'devices' ].keys():
Jon Hall43060f62020-06-23 13:13:33 -070065 searchObj = re.search( didRE, device )
You Wang68568b12019-03-04 11:49:57 -080066 new_device = device
67 if searchObj:
Jon Hall43060f62020-06-23 13:13:33 -070068 new_device = 'of:' + searchObj.group( 'swNum' ).zfill( 16 )
You Wang68568b12019-03-04 11:49:57 -080069 netcfg[ 'devices' ][ new_device ] = netcfg[ 'devices' ].pop( device )
70 if 'pairDeviceId' in netcfg[ 'devices' ][ new_device ][ SR_APP ].keys():
Jon Hall43060f62020-06-23 13:13:33 -070071 searchObj = re.search( didRE, netcfg[ 'devices' ][ new_device ][ SR_APP ][ 'pairDeviceId' ])
You Wang68568b12019-03-04 11:49:57 -080072 if searchObj:
73 netcfg[ 'devices' ][ new_device ][ SR_APP ][ 'pairDeviceId' ] = 'of:' + \
Jon Hall43060f62020-06-23 13:13:33 -070074 searchObj.group( 'swNum' ).zfill( 16 )
You Wang68568b12019-03-04 11:49:57 -080075 if 'basic' in netcfg[ 'devices' ][ new_device ].keys():
76 netcfg[ 'devices' ][ new_device ][ 'basic' ].update( { 'driver': 'ofdpa-ovs' } )
77
78 if 'apps' in netcfg.keys():
79 if DHCP_APP_ID in netcfg[ 'apps' ].keys():
80 for i, dhcpcfg in enumerate( netcfg[ 'apps' ][ DHCP_APP_ID ][ 'default' ] ):
81 if 'dhcpServerConnectPoint' in dhcpcfg.keys():
Jon Hall43060f62020-06-23 13:13:33 -070082 searchObj = re.search( didRE, dhcpcfg[ 'dhcpServerConnectPoint' ] )
You Wang68568b12019-03-04 11:49:57 -080083 if searchObj:
84 netcfg[ 'apps' ][ DHCP_APP_ID ][ 'default' ][ i ][ 'dhcpServerConnectPoint' ] = \
Jon Hall43060f62020-06-23 13:13:33 -070085 'of:' + searchObj.group( 'swNum' ).zfill(16) + '/' + searchObj.group( 'portNum' )
You Wang68568b12019-03-04 11:49:57 -080086
87 with open( cfgFile, 'w' ) as cfg:
Jon Hall43060f62020-06-23 13:13:33 -070088 cfg.write( json.dumps( netcfg, indent=4, separators=( ',', ':' ), sort_keys=True ) )
You Wang68568b12019-03-04 11:49:57 -080089
90# Translate configuration JSON file from OFDPA-OVS driver to BMv2 driver.
Jon Hall43060f62020-06-23 13:13:33 -070091def ofdpaToBmv2( main, switchPrefix="bmv2", cfgFile="" ):
92 didRE= r"device:(?P<swType>bmv2|tofino):(?P<swRole>leaf|spine)(?P<swNum>[1-9][0-9]*)(/(?P<portNum>[0-9]+))?"
93 didRE = r"of:0*(?P<swNum>[1-9][0-9]*)(/(?P<portNum>[0-9]+))?"
You Wang68568b12019-03-04 11:49:57 -080094 if not cfgFile:
95 cfgFile = "%s%s.json" % ( main.configPath + main.forJson,
96 main.cfgName )
97 with open( cfgFile ) as cfg:
98 netcfg = json.load( cfg )
99
100 if 'ports' in netcfg.keys():
101 for port in netcfg[ 'ports' ].keys():
Jon Hall43060f62020-06-23 13:13:33 -0700102 searchObj = re.search( didRE, port )
You Wang68568b12019-03-04 11:49:57 -0800103 if searchObj:
Jon Hall43060f62020-06-23 13:13:33 -0700104 new_port = 'device:' + switchPrefix + ':leaf' + searchObj.group( 'swNum' ) + '/' + searchObj.group( 'portNum' )
You Wang68568b12019-03-04 11:49:57 -0800105 netcfg[ 'ports' ][ new_port ] = netcfg[ 'ports' ].pop( port )
106
107 if 'hosts' in netcfg.keys():
108 for ( host, hostCfg ) in netcfg[ 'hosts' ].items():
109 if type( hostCfg[ 'basic' ][ 'locations' ] ) is list:
110 new_locations = []
111 for location in hostCfg[ 'basic' ][ 'locations' ]:
Jon Hall43060f62020-06-23 13:13:33 -0700112 searchObj = re.search( didRE, location )
You Wang68568b12019-03-04 11:49:57 -0800113 if searchObj:
Jon Hall43060f62020-06-23 13:13:33 -0700114 new_locations.append( 'device:' + switchPrefix + ':leaf' + searchObj.group( 'swNum' ) + '/' + searchObj.group( 'portNum' ) )
You Wang68568b12019-03-04 11:49:57 -0800115 else:
116 new_locations.append( location )
117 netcfg[ 'hosts' ][ host ][ 'basic' ][ 'locations' ] = new_locations
118 else:
119 location = hostCfg[ 'basic' ][ 'locations' ]
Jon Hall43060f62020-06-23 13:13:33 -0700120 searchObj = re.search( didRE, location )
You Wang68568b12019-03-04 11:49:57 -0800121 if searchObj:
Jon Hall43060f62020-06-23 13:13:33 -0700122 new_location = 'device:' + switchPrefix + ':leaf' + searchObj.group( 'swNum' ) + '/' + searchObj.group( 'portNum' )
You Wang68568b12019-03-04 11:49:57 -0800123 netcfg[ 'hosts' ][ host ][ 'basic' ][ 'locations' ] = new_location
124
125 if 'devices' in netcfg.keys():
126 for device in netcfg[ 'devices' ].keys():
Jon Hall43060f62020-06-23 13:13:33 -0700127 searchObj = re.search( didRE, device )
You Wang68568b12019-03-04 11:49:57 -0800128 new_device = device
129 if searchObj:
130 isLeaf = netcfg[ 'devices' ][ device ][ SR_APP ][ 'isEdgeRouter' ]
131 if isLeaf is True:
Jon Hall43060f62020-06-23 13:13:33 -0700132 new_device = 'device:' + switchPrefix + ':leaf' + searchObj.group( 'swNum' )
You Wang68568b12019-03-04 11:49:57 -0800133 else:
Jon Hall43060f62020-06-23 13:13:33 -0700134 new_device = 'device:' + switchPrefix + ':spine' + searchObj.group( 'swNum' )
You Wang68568b12019-03-04 11:49:57 -0800135 netcfg[ 'devices' ][ new_device ] = netcfg[ 'devices' ].pop( device )
136 if 'pairDeviceId' in netcfg[ 'devices' ][ new_device ][ SR_APP ].keys():
Jon Hall43060f62020-06-23 13:13:33 -0700137 searchObj = re.search( didRE,
You Wang68568b12019-03-04 11:49:57 -0800138 netcfg[ 'devices' ][ new_device ][ SR_APP ][ 'pairDeviceId' ])
139 if searchObj:
Jon Hall43060f62020-06-23 13:13:33 -0700140 netcfg[ 'devices' ][ new_device ][ SR_APP ][ 'pairDeviceId' ] = 'device:' + switchPrefix + ':leaf' + \
141 searchObj.group( 'swNum' )
You Wang68568b12019-03-04 11:49:57 -0800142 if 'basic' in netcfg[ 'devices' ][ new_device ].keys():
143 if 'driver' in netcfg[ 'devices' ][ new_device ][ 'basic' ].keys():
144 del netcfg[ 'devices' ][ new_device ][ 'basic' ][ 'driver' ]
145
146 if 'apps' in netcfg.keys():
147 if DHCP_APP_ID in netcfg[ 'apps' ].keys():
148 for i, dhcpcfg in enumerate( netcfg[ 'apps' ][ DHCP_APP_ID ][ 'default' ] ):
149 if 'dhcpServerConnectPoint' in dhcpcfg.keys():
Jon Hall43060f62020-06-23 13:13:33 -0700150 searchObj = re.search( didRE,
You Wang68568b12019-03-04 11:49:57 -0800151 dhcpcfg[ 'dhcpServerConnectPoint' ] )
152 if searchObj:
153 netcfg[ 'apps' ][ DHCP_APP_ID ][ 'default' ][ i ][ 'dhcpServerConnectPoint' ] = \
Jon Hall43060f62020-06-23 13:13:33 -0700154 'device:' + switchPrefix + ':leaf' + searchObj.group( 'swNum' ) + '/' + searchObj.group( 'portNum' )
You Wang68568b12019-03-04 11:49:57 -0800155
156 with open( cfgFile, 'w' ) as cfg:
Jon Hall43060f62020-06-23 13:13:33 -0700157 cfg.write( json.dumps( netcfg, indent=4, separators=( ',', ':' ), sort_keys=True ) )