You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 1 | """ |
| 2 | Copyright 2018 Open Networking Foundation ( ONF ) |
| 3 | |
| 4 | Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>, |
| 5 | the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>, |
| 6 | or 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 | """ |
| 21 | import json |
| 22 | import re |
| 23 | |
| 24 | ONOS_GROUP_ID = 'org.onosproject' |
| 25 | SR_APP = 'segmentrouting' |
| 26 | DHCP_APP = 'dhcprelay' |
| 27 | DHCP_APP_ID = ONOS_GROUP_ID + '.' + DHCP_APP |
| 28 | |
| 29 | # Translate configuration JSON file from BMv2 driver to OFDPA-OVS driver. |
Jon Hall | 214f88b | 2020-09-21 10:21:42 -0700 | [diff] [blame] | 30 | def 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 Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 32 | 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 Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 40 | searchObj = re.search( didRE, port ) |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 41 | if searchObj: |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 42 | new_port = 'of:' + searchObj.group( 'swNum' ).zfill( 16 ) + '/' + searchObj.group( 'portNum' ) |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 43 | 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 Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 50 | searchObj = re.search( didRE, location ) |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 51 | if searchObj: |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 52 | new_locations.append( 'of:' + searchObj.group( 'swNum' ).zfill( 16 ) + '/' + searchObj.group( 'portNum' ) ) |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 53 | else: |
| 54 | new_locations.append( location ) |
| 55 | netcfg[ 'hosts' ][ host ][ 'basic' ][ 'locations' ] = new_locations |
| 56 | else: |
| 57 | location = hostCfg[ 'basic' ][ 'locations' ] |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 58 | searchObj = re.search( didRE, location ) |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 59 | if searchObj: |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 60 | new_location = 'of:' + searchObj.group( 'swNum' ).zfill( 16 ) + '/' + searchObj.group( 'portNum' ) |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 61 | netcfg[ 'hosts' ][ host ][ 'basic' ][ 'locations' ] = new_location |
| 62 | |
| 63 | if 'devices' in netcfg.keys(): |
| 64 | for device in netcfg[ 'devices' ].keys(): |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 65 | searchObj = re.search( didRE, device ) |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 66 | new_device = device |
| 67 | if searchObj: |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 68 | new_device = 'of:' + searchObj.group( 'swNum' ).zfill( 16 ) |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 69 | netcfg[ 'devices' ][ new_device ] = netcfg[ 'devices' ].pop( device ) |
| 70 | if 'pairDeviceId' in netcfg[ 'devices' ][ new_device ][ SR_APP ].keys(): |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 71 | searchObj = re.search( didRE, netcfg[ 'devices' ][ new_device ][ SR_APP ][ 'pairDeviceId' ]) |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 72 | if searchObj: |
| 73 | netcfg[ 'devices' ][ new_device ][ SR_APP ][ 'pairDeviceId' ] = 'of:' + \ |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 74 | searchObj.group( 'swNum' ).zfill( 16 ) |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 75 | 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 Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 82 | searchObj = re.search( didRE, dhcpcfg[ 'dhcpServerConnectPoint' ] ) |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 83 | if searchObj: |
| 84 | netcfg[ 'apps' ][ DHCP_APP_ID ][ 'default' ][ i ][ 'dhcpServerConnectPoint' ] = \ |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 85 | 'of:' + searchObj.group( 'swNum' ).zfill(16) + '/' + searchObj.group( 'portNum' ) |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 86 | |
Jon Hall | 214f88b | 2020-09-21 10:21:42 -0700 | [diff] [blame] | 87 | 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 Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 97 | with open( cfgFile, 'w' ) as cfg: |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 98 | cfg.write( json.dumps( netcfg, indent=4, separators=( ',', ':' ), sort_keys=True ) ) |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 99 | |
| 100 | # Translate configuration JSON file from OFDPA-OVS driver to BMv2 driver. |
Jon Hall | 214f88b | 2020-09-21 10:21:42 -0700 | [diff] [blame] | 101 | def ofdpaToBmv2( main, switchPrefix="bmv2", cfgFile="", roleMap={r'0*[1-9]([0-9]){2}': 'spine', r'0{15}[1-9]': "leaf"} ): |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 102 | didRE = r"of:0*(?P<swNum>[1-9][0-9]*)(/(?P<portNum>[0-9]+))?" |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 103 | if switchPrefix is None: |
| 104 | switchPrefix = '' |
| 105 | else: |
| 106 | switchPrefix += ':' |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 107 | 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 Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 115 | searchObj = re.search( didRE, port ) |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 116 | if searchObj: |
Jon Hall | 214f88b | 2020-09-21 10:21:42 -0700 | [diff] [blame] | 117 | # 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 Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 124 | new_port = 'device:' + switchPrefix + role + searchObj.group( 'swNum' ) + '/' + searchObj.group( 'portNum' ) |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 125 | 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 Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 132 | searchObj = re.search( didRE, location ) |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 133 | if searchObj: |
Jon Hall | 214f88b | 2020-09-21 10:21:42 -0700 | [diff] [blame] | 134 | # 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 Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 141 | new_locations.append( 'device:' + switchPrefix + role + searchObj.group( 'swNum' ) + '/' + searchObj.group( 'portNum' ) ) |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 142 | else: |
| 143 | new_locations.append( location ) |
| 144 | netcfg[ 'hosts' ][ host ][ 'basic' ][ 'locations' ] = new_locations |
| 145 | else: |
| 146 | location = hostCfg[ 'basic' ][ 'locations' ] |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 147 | searchObj = re.search( didRE, location ) |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 148 | if searchObj: |
Jon Hall | 214f88b | 2020-09-21 10:21:42 -0700 | [diff] [blame] | 149 | # 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 Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 156 | new_location = 'device:' + switchPrefix + role + searchObj.group( 'swNum' ) + '/' + searchObj.group( 'portNum' ) |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 157 | netcfg[ 'hosts' ][ host ][ 'basic' ][ 'locations' ] = new_location |
| 158 | |
| 159 | if 'devices' in netcfg.keys(): |
| 160 | for device in netcfg[ 'devices' ].keys(): |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 161 | searchObj = re.search( didRE, device ) |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 162 | new_device = device |
| 163 | if searchObj: |
Jon Hall | 214f88b | 2020-09-21 10:21:42 -0700 | [diff] [blame] | 164 | #TODO This or roleMap? maybe use this to populate role Map? |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 165 | isLeaf = netcfg[ 'devices' ][ device ][ SR_APP ][ 'isEdgeRouter' ] |
| 166 | if isLeaf is True: |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 167 | new_device = 'device:' + switchPrefix + 'leaf' + searchObj.group( 'swNum' ) |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 168 | else: |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 169 | new_device = 'device:' + switchPrefix + 'spine' + searchObj.group( 'swNum' ) |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 170 | netcfg[ 'devices' ][ new_device ] = netcfg[ 'devices' ].pop( device ) |
| 171 | if 'pairDeviceId' in netcfg[ 'devices' ][ new_device ][ SR_APP ].keys(): |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 172 | searchObj = re.search( didRE, |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 173 | netcfg[ 'devices' ][ new_device ][ SR_APP ][ 'pairDeviceId' ]) |
| 174 | if searchObj: |
Jon Hall | 214f88b | 2020-09-21 10:21:42 -0700 | [diff] [blame] | 175 | # 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 Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 182 | netcfg[ 'devices' ][ new_device ][ SR_APP ][ 'pairDeviceId' ] = 'device:' + switchPrefix + role + \ |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 183 | searchObj.group( 'swNum' ) |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 184 | 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 Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 192 | searchObj = re.search( didRE, |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 193 | dhcpcfg[ 'dhcpServerConnectPoint' ] ) |
| 194 | if searchObj: |
Jon Hall | 214f88b | 2020-09-21 10:21:42 -0700 | [diff] [blame] | 195 | # 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 Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 202 | netcfg[ 'apps' ][ DHCP_APP_ID ][ 'default' ][ i ][ 'dhcpServerConnectPoint' ] = \ |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 203 | 'device:' + switchPrefix + role + searchObj.group( 'swNum' ) + '/' + searchObj.group( 'portNum' ) |
Jon Hall | 214f88b | 2020-09-21 10:21:42 -0700 | [diff] [blame] | 204 | |
| 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 Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 217 | new_device = 'device:' + switchPrefix + role + searchObj.group( 'swNum' ) |
Jon Hall | 214f88b | 2020-09-21 10:21:42 -0700 | [diff] [blame] | 218 | xconnect[ 'deviceId' ] = new_device |
| 219 | new_xconnects.append( xconnect ) |
| 220 | netcfg[ 'xconnects' ] = new_xconnects |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 221 | |
| 222 | with open( cfgFile, 'w' ) as cfg: |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 223 | cfg.write( json.dumps( netcfg, indent=4, separators=( ',', ':' ), sort_keys=True ) ) |