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. |
| 30 | def bmv2ToOfdpa( main, cfgFile="" ): |
Jon Hall | 7640847 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 31 | didRE = r"device:(?P<swType>bmv2|tofino):(?P<swRole>leaf|spine)(?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 | 7640847 | 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 | 7640847 | 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 | 7640847 | 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 | 7640847 | 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 | 7640847 | 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 | 7640847 | 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 | 7640847 | 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 | 7640847 | 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 | 7640847 | 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 | 7640847 | 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 | 7640847 | 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 | 7640847 | 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 | |
| 87 | with open( cfgFile, 'w' ) as cfg: |
Jon Hall | 7640847 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 88 | cfg.write( json.dumps( netcfg, indent=4, separators=( ',', ':' ), sort_keys=True ) ) |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 89 | |
| 90 | # Translate configuration JSON file from OFDPA-OVS driver to BMv2 driver. |
Jon Hall | 7640847 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 91 | def 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 Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 94 | 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 Hall | 7640847 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 102 | searchObj = re.search( didRE, port ) |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 103 | if searchObj: |
Jon Hall | 7640847 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 104 | new_port = 'device:' + switchPrefix + ':leaf' + searchObj.group( 'swNum' ) + '/' + searchObj.group( 'portNum' ) |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 105 | 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 Hall | 7640847 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 112 | searchObj = re.search( didRE, location ) |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 113 | if searchObj: |
Jon Hall | 7640847 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 114 | new_locations.append( 'device:' + switchPrefix + ':leaf' + searchObj.group( 'swNum' ) + '/' + searchObj.group( 'portNum' ) ) |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 115 | else: |
| 116 | new_locations.append( location ) |
| 117 | netcfg[ 'hosts' ][ host ][ 'basic' ][ 'locations' ] = new_locations |
| 118 | else: |
| 119 | location = hostCfg[ 'basic' ][ 'locations' ] |
Jon Hall | 7640847 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 120 | searchObj = re.search( didRE, location ) |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 121 | if searchObj: |
Jon Hall | 7640847 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 122 | new_location = 'device:' + switchPrefix + ':leaf' + searchObj.group( 'swNum' ) + '/' + searchObj.group( 'portNum' ) |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 123 | netcfg[ 'hosts' ][ host ][ 'basic' ][ 'locations' ] = new_location |
| 124 | |
| 125 | if 'devices' in netcfg.keys(): |
| 126 | for device in netcfg[ 'devices' ].keys(): |
Jon Hall | 7640847 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 127 | searchObj = re.search( didRE, device ) |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 128 | new_device = device |
| 129 | if searchObj: |
| 130 | isLeaf = netcfg[ 'devices' ][ device ][ SR_APP ][ 'isEdgeRouter' ] |
| 131 | if isLeaf is True: |
Jon Hall | 7640847 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 132 | new_device = 'device:' + switchPrefix + ':leaf' + searchObj.group( 'swNum' ) |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 133 | else: |
Jon Hall | 7640847 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 134 | new_device = 'device:' + switchPrefix + ':spine' + searchObj.group( 'swNum' ) |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 135 | netcfg[ 'devices' ][ new_device ] = netcfg[ 'devices' ].pop( device ) |
| 136 | if 'pairDeviceId' in netcfg[ 'devices' ][ new_device ][ SR_APP ].keys(): |
Jon Hall | 7640847 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 137 | searchObj = re.search( didRE, |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 138 | netcfg[ 'devices' ][ new_device ][ SR_APP ][ 'pairDeviceId' ]) |
| 139 | if searchObj: |
Jon Hall | 7640847 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 140 | netcfg[ 'devices' ][ new_device ][ SR_APP ][ 'pairDeviceId' ] = 'device:' + switchPrefix + ':leaf' + \ |
| 141 | searchObj.group( 'swNum' ) |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 142 | 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 Hall | 7640847 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 150 | searchObj = re.search( didRE, |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 151 | dhcpcfg[ 'dhcpServerConnectPoint' ] ) |
| 152 | if searchObj: |
| 153 | netcfg[ 'apps' ][ DHCP_APP_ID ][ 'default' ][ i ][ 'dhcpServerConnectPoint' ] = \ |
Jon Hall | 7640847 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 154 | 'device:' + switchPrefix + ':leaf' + searchObj.group( 'swNum' ) + '/' + searchObj.group( 'portNum' ) |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 155 | |
| 156 | with open( cfgFile, 'w' ) as cfg: |
Jon Hall | 7640847 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 157 | cfg.write( json.dumps( netcfg, indent=4, separators=( ',', ':' ), sort_keys=True ) ) |