blob: b978b73532778f2030aff64e5a06ff690f83d9cb [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]+))?"
You Wang68568b12019-03-04 11:49:57 -0800103 if not cfgFile:
104 cfgFile = "%s%s.json" % ( main.configPath + main.forJson,
105 main.cfgName )
106 with open( cfgFile ) as cfg:
107 netcfg = json.load( cfg )
108
109 if 'ports' in netcfg.keys():
110 for port in netcfg[ 'ports' ].keys():
Jon Hall43060f62020-06-23 13:13:33 -0700111 searchObj = re.search( didRE, port )
You Wang68568b12019-03-04 11:49:57 -0800112 if searchObj:
Jon Hall214f88b2020-09-21 10:21:42 -0700113 # search for a match between keys of roleMap and device id and set role to value of key
114 role = "leaf"
115 for roleRE, roleValue in roleMap.items():
116 roleMatch = re.search( roleRE, searchObj.group( 'swNum' ) )
117 if roleMatch:
118 role = roleValue
119 break
120 new_port = 'device:' + switchPrefix + ':' + role + searchObj.group( 'swNum' ) + '/' + searchObj.group( 'portNum' )
You Wang68568b12019-03-04 11:49:57 -0800121 netcfg[ 'ports' ][ new_port ] = netcfg[ 'ports' ].pop( port )
122
123 if 'hosts' in netcfg.keys():
124 for ( host, hostCfg ) in netcfg[ 'hosts' ].items():
125 if type( hostCfg[ 'basic' ][ 'locations' ] ) is list:
126 new_locations = []
127 for location in hostCfg[ 'basic' ][ 'locations' ]:
Jon Hall43060f62020-06-23 13:13:33 -0700128 searchObj = re.search( didRE, location )
You Wang68568b12019-03-04 11:49:57 -0800129 if searchObj:
Jon Hall214f88b2020-09-21 10:21:42 -0700130 # search for a match between keys of roleMap and device id and set role to value of key
131 role = "leaf"
132 for roleRE, roleValue in roleMap.items():
133 roleMatch = re.search( roleRE, searchObj.group( 'swNum' ) )
134 if roleMatch:
135 role = roleValue
136 break
137 new_locations.append( 'device:' + switchPrefix + ':' + role + searchObj.group( 'swNum' ) + '/' + searchObj.group( 'portNum' ) )
You Wang68568b12019-03-04 11:49:57 -0800138 else:
139 new_locations.append( location )
140 netcfg[ 'hosts' ][ host ][ 'basic' ][ 'locations' ] = new_locations
141 else:
142 location = hostCfg[ 'basic' ][ 'locations' ]
Jon Hall43060f62020-06-23 13:13:33 -0700143 searchObj = re.search( didRE, location )
You Wang68568b12019-03-04 11:49:57 -0800144 if searchObj:
Jon Hall214f88b2020-09-21 10:21:42 -0700145 # search for a match between keys of roleMap and device id and set role to value of key
146 role = "leaf"
147 for roleRE, roleValue in roleMap.items():
148 roleMatch = re.search( roleRE, searchObj.group( 'swNum' ) )
149 if roleMatch:
150 role = roleValue
151 break
152 new_location = 'device:' + switchPrefix + ':' + role + searchObj.group( 'swNum' ) + '/' + searchObj.group( 'portNum' )
You Wang68568b12019-03-04 11:49:57 -0800153 netcfg[ 'hosts' ][ host ][ 'basic' ][ 'locations' ] = new_location
154
155 if 'devices' in netcfg.keys():
156 for device in netcfg[ 'devices' ].keys():
Jon Hall43060f62020-06-23 13:13:33 -0700157 searchObj = re.search( didRE, device )
You Wang68568b12019-03-04 11:49:57 -0800158 new_device = device
159 if searchObj:
Jon Hall214f88b2020-09-21 10:21:42 -0700160 #TODO This or roleMap? maybe use this to populate role Map?
You Wang68568b12019-03-04 11:49:57 -0800161 isLeaf = netcfg[ 'devices' ][ device ][ SR_APP ][ 'isEdgeRouter' ]
162 if isLeaf is True:
Jon Hall43060f62020-06-23 13:13:33 -0700163 new_device = 'device:' + switchPrefix + ':leaf' + searchObj.group( 'swNum' )
You Wang68568b12019-03-04 11:49:57 -0800164 else:
Jon Hall43060f62020-06-23 13:13:33 -0700165 new_device = 'device:' + switchPrefix + ':spine' + searchObj.group( 'swNum' )
You Wang68568b12019-03-04 11:49:57 -0800166 netcfg[ 'devices' ][ new_device ] = netcfg[ 'devices' ].pop( device )
167 if 'pairDeviceId' in netcfg[ 'devices' ][ new_device ][ SR_APP ].keys():
Jon Hall43060f62020-06-23 13:13:33 -0700168 searchObj = re.search( didRE,
You Wang68568b12019-03-04 11:49:57 -0800169 netcfg[ 'devices' ][ new_device ][ SR_APP ][ 'pairDeviceId' ])
170 if searchObj:
Jon Hall214f88b2020-09-21 10:21:42 -0700171 # search for a match between keys of roleMap and device id and set role to value of key
172 role = "leaf"
173 for roleRE, roleValue in roleMap.items():
174 roleMatch = re.search( roleRE, searchObj.group( 'swNum' ) )
175 if roleMatch:
176 role = roleValue
177 break
178 netcfg[ 'devices' ][ new_device ][ SR_APP ][ 'pairDeviceId' ] = 'device:' + switchPrefix + ':' + role + \
Jon Hall43060f62020-06-23 13:13:33 -0700179 searchObj.group( 'swNum' )
You Wang68568b12019-03-04 11:49:57 -0800180 if 'basic' in netcfg[ 'devices' ][ new_device ].keys():
181 if 'driver' in netcfg[ 'devices' ][ new_device ][ 'basic' ].keys():
182 del netcfg[ 'devices' ][ new_device ][ 'basic' ][ 'driver' ]
183
184 if 'apps' in netcfg.keys():
185 if DHCP_APP_ID in netcfg[ 'apps' ].keys():
186 for i, dhcpcfg in enumerate( netcfg[ 'apps' ][ DHCP_APP_ID ][ 'default' ] ):
187 if 'dhcpServerConnectPoint' in dhcpcfg.keys():
Jon Hall43060f62020-06-23 13:13:33 -0700188 searchObj = re.search( didRE,
You Wang68568b12019-03-04 11:49:57 -0800189 dhcpcfg[ 'dhcpServerConnectPoint' ] )
190 if searchObj:
Jon Hall214f88b2020-09-21 10:21:42 -0700191 # search for a match between keys of roleMap and device id and set role to value of key
192 role = "leaf"
193 for roleRE, roleValue in roleMap.items():
194 roleMatch = re.search( roleRE, searchObj.group( 'swNum' ) )
195 if roleMatch:
196 role = roleValue
197 break
You Wang68568b12019-03-04 11:49:57 -0800198 netcfg[ 'apps' ][ DHCP_APP_ID ][ 'default' ][ i ][ 'dhcpServerConnectPoint' ] = \
Jon Hall214f88b2020-09-21 10:21:42 -0700199 'device:' + switchPrefix + ':' + role + searchObj.group( 'swNum' ) + '/' + searchObj.group( 'portNum' )
200
201 if 'xconnects' in netcfg.keys():
202 new_xconnects = []
203 for xconnect in netcfg[ 'xconnects' ]:
204 searchObj = re.search( didRE, xconnect.get( "deviceId" ) )
205 if searchObj:
206 # search for a match between keys of roleMap and device id and set role to value of key
207 role = "leaf"
208 for roleRE, roleValue in roleMap.items():
209 roleMatch = re.search( roleRE, searchObj.group( 'swNum' ) )
210 if roleMatch:
211 role = roleValue
212 break
213 new_device = 'device:' + switchPrefix + ':' + role + searchObj.group( 'swNum' )
214 xconnect[ 'deviceId' ] = new_device
215 new_xconnects.append( xconnect )
216 netcfg[ 'xconnects' ] = new_xconnects
You Wang68568b12019-03-04 11:49:57 -0800217
218 with open( cfgFile, 'w' ) as cfg:
Jon Hall43060f62020-06-23 13:13:33 -0700219 cfg.write( json.dumps( netcfg, indent=4, separators=( ',', ':' ), sort_keys=True ) )