blob: 6b538a982fc752b5471432d26aa1e11b8c520cee [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.
Jon Hall214f88b2020-09-21 10:21:42 -070030def bmv2ToOfdpa( main, cfgFile="", rolesRE=r'spine|leaf' ):
31 didRE = r"device:(?P<swType>bmv2|tofino):(?P<swRole>" + rolesRE + ")(?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
Jon Hall214f88b2020-09-21 10:21:42 -070087 if 'xconnects' in netcfg.keys():
88 new_xconnects = []
89 for xconnect in netcfg[ 'xconnects' ]:
90 searchObj = re.search( didRE, xconnect.get( "deviceId" ) )
91 if searchObj:
92 new_device = 'of:' + searchObj.group( 'swNum' ).zfill( 16 )
93 xconnect[ 'deviceId' ] = new_device
94 new_xconnects.append( xconnect )
95 netcfg[ 'xconnects' ] = new_xconnects
96
You Wang68568b12019-03-04 11:49:57 -080097 with open( cfgFile, 'w' ) as cfg:
Jon Hall43060f62020-06-23 13:13:33 -070098 cfg.write( json.dumps( netcfg, indent=4, separators=( ',', ':' ), sort_keys=True ) )
You Wang68568b12019-03-04 11:49:57 -080099
100# Translate configuration JSON file from OFDPA-OVS driver to BMv2 driver.
Jon Hall214f88b2020-09-21 10:21:42 -0700101def ofdpaToBmv2( main, switchPrefix="bmv2", cfgFile="", roleMap={r'0*[1-9]([0-9]){2}': 'spine', r'0{15}[1-9]': "leaf"} ):
Jon Hall43060f62020-06-23 13:13:33 -0700102 didRE = r"of:0*(?P<swNum>[1-9][0-9]*)(/(?P<portNum>[0-9]+))?"
Jon Hall06fd0df2021-01-25 15:50:06 -0800103 if switchPrefix is None:
104 switchPrefix = ''
105 else:
106 switchPrefix += ':'
You Wang68568b12019-03-04 11:49:57 -0800107 if not cfgFile:
108 cfgFile = "%s%s.json" % ( main.configPath + main.forJson,
109 main.cfgName )
110 with open( cfgFile ) as cfg:
111 netcfg = json.load( cfg )
112
113 if 'ports' in netcfg.keys():
114 for port in netcfg[ 'ports' ].keys():
Jon Hall43060f62020-06-23 13:13:33 -0700115 searchObj = re.search( didRE, port )
You Wang68568b12019-03-04 11:49:57 -0800116 if searchObj:
Jon Hall214f88b2020-09-21 10:21:42 -0700117 # search for a match between keys of roleMap and device id and set role to value of key
118 role = "leaf"
119 for roleRE, roleValue in roleMap.items():
120 roleMatch = re.search( roleRE, searchObj.group( 'swNum' ) )
121 if roleMatch:
122 role = roleValue
123 break
Jon Hall06fd0df2021-01-25 15:50:06 -0800124 new_port = 'device:' + switchPrefix + role + searchObj.group( 'swNum' ) + '/' + searchObj.group( 'portNum' )
You Wang68568b12019-03-04 11:49:57 -0800125 netcfg[ 'ports' ][ new_port ] = netcfg[ 'ports' ].pop( port )
126
127 if 'hosts' in netcfg.keys():
128 for ( host, hostCfg ) in netcfg[ 'hosts' ].items():
129 if type( hostCfg[ 'basic' ][ 'locations' ] ) is list:
130 new_locations = []
131 for location in hostCfg[ 'basic' ][ 'locations' ]:
Jon Hall43060f62020-06-23 13:13:33 -0700132 searchObj = re.search( didRE, location )
You Wang68568b12019-03-04 11:49:57 -0800133 if searchObj:
Jon Hall214f88b2020-09-21 10:21:42 -0700134 # search for a match between keys of roleMap and device id and set role to value of key
135 role = "leaf"
136 for roleRE, roleValue in roleMap.items():
137 roleMatch = re.search( roleRE, searchObj.group( 'swNum' ) )
138 if roleMatch:
139 role = roleValue
140 break
Jon Hall06fd0df2021-01-25 15:50:06 -0800141 new_locations.append( 'device:' + switchPrefix + role + searchObj.group( 'swNum' ) + '/' + searchObj.group( 'portNum' ) )
You Wang68568b12019-03-04 11:49:57 -0800142 else:
143 new_locations.append( location )
144 netcfg[ 'hosts' ][ host ][ 'basic' ][ 'locations' ] = new_locations
145 else:
146 location = hostCfg[ 'basic' ][ 'locations' ]
Jon Hall43060f62020-06-23 13:13:33 -0700147 searchObj = re.search( didRE, location )
You Wang68568b12019-03-04 11:49:57 -0800148 if searchObj:
Jon Hall214f88b2020-09-21 10:21:42 -0700149 # search for a match between keys of roleMap and device id and set role to value of key
150 role = "leaf"
151 for roleRE, roleValue in roleMap.items():
152 roleMatch = re.search( roleRE, searchObj.group( 'swNum' ) )
153 if roleMatch:
154 role = roleValue
155 break
Jon Hall06fd0df2021-01-25 15:50:06 -0800156 new_location = 'device:' + switchPrefix + role + searchObj.group( 'swNum' ) + '/' + searchObj.group( 'portNum' )
You Wang68568b12019-03-04 11:49:57 -0800157 netcfg[ 'hosts' ][ host ][ 'basic' ][ 'locations' ] = new_location
158
159 if 'devices' in netcfg.keys():
160 for device in netcfg[ 'devices' ].keys():
Jon Hall43060f62020-06-23 13:13:33 -0700161 searchObj = re.search( didRE, device )
You Wang68568b12019-03-04 11:49:57 -0800162 new_device = device
163 if searchObj:
Jon Hall214f88b2020-09-21 10:21:42 -0700164 #TODO This or roleMap? maybe use this to populate role Map?
You Wang68568b12019-03-04 11:49:57 -0800165 isLeaf = netcfg[ 'devices' ][ device ][ SR_APP ][ 'isEdgeRouter' ]
166 if isLeaf is True:
Jon Hall06fd0df2021-01-25 15:50:06 -0800167 new_device = 'device:' + switchPrefix + 'leaf' + searchObj.group( 'swNum' )
You Wang68568b12019-03-04 11:49:57 -0800168 else:
Jon Hall06fd0df2021-01-25 15:50:06 -0800169 new_device = 'device:' + switchPrefix + 'spine' + searchObj.group( 'swNum' )
You Wang68568b12019-03-04 11:49:57 -0800170 netcfg[ 'devices' ][ new_device ] = netcfg[ 'devices' ].pop( device )
171 if 'pairDeviceId' in netcfg[ 'devices' ][ new_device ][ SR_APP ].keys():
Jon Hall43060f62020-06-23 13:13:33 -0700172 searchObj = re.search( didRE,
You Wang68568b12019-03-04 11:49:57 -0800173 netcfg[ 'devices' ][ new_device ][ SR_APP ][ 'pairDeviceId' ])
174 if searchObj:
Jon Hall214f88b2020-09-21 10:21:42 -0700175 # search for a match between keys of roleMap and device id and set role to value of key
176 role = "leaf"
177 for roleRE, roleValue in roleMap.items():
178 roleMatch = re.search( roleRE, searchObj.group( 'swNum' ) )
179 if roleMatch:
180 role = roleValue
181 break
Jon Hall06fd0df2021-01-25 15:50:06 -0800182 netcfg[ 'devices' ][ new_device ][ SR_APP ][ 'pairDeviceId' ] = 'device:' + switchPrefix + role + \
Jon Hall43060f62020-06-23 13:13:33 -0700183 searchObj.group( 'swNum' )
You Wang68568b12019-03-04 11:49:57 -0800184 if 'basic' in netcfg[ 'devices' ][ new_device ].keys():
185 if 'driver' in netcfg[ 'devices' ][ new_device ][ 'basic' ].keys():
186 del netcfg[ 'devices' ][ new_device ][ 'basic' ][ 'driver' ]
187
188 if 'apps' in netcfg.keys():
189 if DHCP_APP_ID in netcfg[ 'apps' ].keys():
190 for i, dhcpcfg in enumerate( netcfg[ 'apps' ][ DHCP_APP_ID ][ 'default' ] ):
191 if 'dhcpServerConnectPoint' in dhcpcfg.keys():
Jon Hall43060f62020-06-23 13:13:33 -0700192 searchObj = re.search( didRE,
You Wang68568b12019-03-04 11:49:57 -0800193 dhcpcfg[ 'dhcpServerConnectPoint' ] )
194 if searchObj:
Jon Hall214f88b2020-09-21 10:21:42 -0700195 # search for a match between keys of roleMap and device id and set role to value of key
196 role = "leaf"
197 for roleRE, roleValue in roleMap.items():
198 roleMatch = re.search( roleRE, searchObj.group( 'swNum' ) )
199 if roleMatch:
200 role = roleValue
201 break
You Wang68568b12019-03-04 11:49:57 -0800202 netcfg[ 'apps' ][ DHCP_APP_ID ][ 'default' ][ i ][ 'dhcpServerConnectPoint' ] = \
Jon Hall06fd0df2021-01-25 15:50:06 -0800203 'device:' + switchPrefix + role + searchObj.group( 'swNum' ) + '/' + searchObj.group( 'portNum' )
Jon Hall214f88b2020-09-21 10:21:42 -0700204
205 if 'xconnects' in netcfg.keys():
206 new_xconnects = []
207 for xconnect in netcfg[ 'xconnects' ]:
208 searchObj = re.search( didRE, xconnect.get( "deviceId" ) )
209 if searchObj:
210 # search for a match between keys of roleMap and device id and set role to value of key
211 role = "leaf"
212 for roleRE, roleValue in roleMap.items():
213 roleMatch = re.search( roleRE, searchObj.group( 'swNum' ) )
214 if roleMatch:
215 role = roleValue
216 break
Jon Hall06fd0df2021-01-25 15:50:06 -0800217 new_device = 'device:' + switchPrefix + role + searchObj.group( 'swNum' )
Jon Hall214f88b2020-09-21 10:21:42 -0700218 xconnect[ 'deviceId' ] = new_device
219 new_xconnects.append( xconnect )
220 netcfg[ 'xconnects' ] = new_xconnects
You Wang68568b12019-03-04 11:49:57 -0800221
222 with open( cfgFile, 'w' ) as cfg:
Jon Hall43060f62020-06-23 13:13:33 -0700223 cfg.write( json.dumps( netcfg, indent=4, separators=( ',', ':' ), sort_keys=True ) )