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="" ): |
| 31 | if not cfgFile: |
| 32 | cfgFile = "%s%s.json" % ( main.configPath + main.forJson, |
| 33 | main.cfgName ) |
| 34 | with open( cfgFile ) as cfg: |
| 35 | netcfg = json.load( cfg ) |
| 36 | |
| 37 | if 'ports' in netcfg.keys(): |
| 38 | for port in netcfg[ 'ports' ].keys(): |
| 39 | searchObj = re.search( "device:bmv2:leaf([1-9][0-9]*)/([0-9]+)", port ) |
| 40 | if searchObj: |
| 41 | new_port = 'of:' + searchObj.group( 1 ).zfill( 16 ) + '/' + searchObj.group( 2 ) |
| 42 | netcfg[ 'ports' ][ new_port ] = netcfg[ 'ports' ].pop( port ) |
| 43 | |
| 44 | if 'hosts' in netcfg.keys(): |
| 45 | for ( host, hostCfg ) in netcfg[ 'hosts' ].items(): |
| 46 | if type( hostCfg[ 'basic' ][ 'locations' ] ) is list: |
| 47 | new_locations = [] |
| 48 | for location in hostCfg[ 'basic' ][ 'locations' ]: |
| 49 | searchObj = re.search( "device:bmv2:leaf([1-9][0-9]*)/([0-9]+)", location ) |
| 50 | if searchObj: |
| 51 | new_locations.append( 'of:' + searchObj.group( 1 ).zfill( 16 ) + '/' + searchObj.group( 2 ) ) |
| 52 | else: |
| 53 | new_locations.append( location ) |
| 54 | netcfg[ 'hosts' ][ host ][ 'basic' ][ 'locations' ] = new_locations |
| 55 | else: |
| 56 | location = hostCfg[ 'basic' ][ 'locations' ] |
| 57 | searchObj = re.search( "device:bmv2:leaf([1-9][0-9]*)/([0-9]+)", location ) |
| 58 | if searchObj: |
| 59 | new_location = 'of:' + searchObj.group( 1 ).zfill( 16 ) + '/' + searchObj.group( 2 ) |
| 60 | netcfg[ 'hosts' ][ host ][ 'basic' ][ 'locations' ] = new_location |
| 61 | |
| 62 | if 'devices' in netcfg.keys(): |
| 63 | for device in netcfg[ 'devices' ].keys(): |
| 64 | searchObj = re.search( "device:bmv2:(leaf|spine)([1-9][0-9]*)", device ) |
| 65 | new_device = device |
| 66 | if searchObj: |
| 67 | new_device = 'of:' + searchObj.group( 2 ).zfill( 16 ) |
| 68 | netcfg[ 'devices' ][ new_device ] = netcfg[ 'devices' ].pop( device ) |
| 69 | if 'pairDeviceId' in netcfg[ 'devices' ][ new_device ][ SR_APP ].keys(): |
| 70 | searchObj = re.search( "device:bmv2:leaf([1-9][0-9]*)", |
| 71 | netcfg[ 'devices' ][ new_device ][ SR_APP ][ 'pairDeviceId' ]) |
| 72 | if searchObj: |
| 73 | netcfg[ 'devices' ][ new_device ][ SR_APP ][ 'pairDeviceId' ] = 'of:' + \ |
| 74 | searchObj.group( 1 ).zfill( 16 ) |
| 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(): |
| 82 | searchObj = re.search( "device:bmv2:leaf([1-9][0-9]*)/([0-9]+)", |
| 83 | dhcpcfg[ 'dhcpServerConnectPoint' ] ) |
| 84 | if searchObj: |
| 85 | netcfg[ 'apps' ][ DHCP_APP_ID ][ 'default' ][ i ][ 'dhcpServerConnectPoint' ] = \ |
| 86 | 'of:' + searchObj.group( 1 ).zfill(16) + '/' + searchObj.group( 2 ) |
| 87 | |
| 88 | with open( cfgFile, 'w' ) as cfg: |
| 89 | cfg.write( json.dumps( netcfg, indent=4, separators=( ',', ':' ) ) ) |
| 90 | |
| 91 | # Translate configuration JSON file from OFDPA-OVS driver to BMv2 driver. |
| 92 | def ofdpaToBmv2( main, cfgFile="" ): |
| 93 | if not cfgFile: |
| 94 | cfgFile = "%s%s.json" % ( main.configPath + main.forJson, |
| 95 | main.cfgName ) |
| 96 | with open( cfgFile ) as cfg: |
| 97 | netcfg = json.load( cfg ) |
| 98 | |
| 99 | if 'ports' in netcfg.keys(): |
| 100 | for port in netcfg[ 'ports' ].keys(): |
| 101 | searchObj = re.search( "of:0*([1-9][0-9]*)/([0-9]+)", port ) |
| 102 | if searchObj: |
| 103 | new_port = 'device:bmv2:leaf' + searchObj.group( 1 ) + '/' + searchObj.group( 2 ) |
| 104 | netcfg[ 'ports' ][ new_port ] = netcfg[ 'ports' ].pop( port ) |
| 105 | |
| 106 | if 'hosts' in netcfg.keys(): |
| 107 | for ( host, hostCfg ) in netcfg[ 'hosts' ].items(): |
| 108 | if type( hostCfg[ 'basic' ][ 'locations' ] ) is list: |
| 109 | new_locations = [] |
| 110 | for location in hostCfg[ 'basic' ][ 'locations' ]: |
| 111 | searchObj = re.search( "of:0*([1-9][0-9]*)/([0-9]+)", location ) |
| 112 | if searchObj: |
| 113 | new_locations.append( 'device:bmv2:leaf' + searchObj.group( 1 ) + '/' + searchObj.group( 2 ) ) |
| 114 | else: |
| 115 | new_locations.append( location ) |
| 116 | netcfg[ 'hosts' ][ host ][ 'basic' ][ 'locations' ] = new_locations |
| 117 | else: |
| 118 | location = hostCfg[ 'basic' ][ 'locations' ] |
| 119 | searchObj = re.search( "of:0*([1-9][0-9]*)/([0-9]+)", location ) |
| 120 | if searchObj: |
| 121 | new_location = 'device:bmv2:leaf' + searchObj.group( 1 ) + '/' + searchObj.group( 2 ) |
| 122 | netcfg[ 'hosts' ][ host ][ 'basic' ][ 'locations' ] = new_location |
| 123 | |
| 124 | if 'devices' in netcfg.keys(): |
| 125 | for device in netcfg[ 'devices' ].keys(): |
| 126 | searchObj = re.search( "of:0*([1-9][0-9]*)", device ) |
| 127 | new_device = device |
| 128 | if searchObj: |
| 129 | isLeaf = netcfg[ 'devices' ][ device ][ SR_APP ][ 'isEdgeRouter' ] |
| 130 | if isLeaf is True: |
| 131 | new_device = 'device:bmv2:leaf' + searchObj.group( 1 ) |
| 132 | else: |
| 133 | new_device = 'device:bmv2:spine' + searchObj.group( 1 ) |
| 134 | netcfg[ 'devices' ][ new_device ] = netcfg[ 'devices' ].pop( device ) |
| 135 | if 'pairDeviceId' in netcfg[ 'devices' ][ new_device ][ SR_APP ].keys(): |
| 136 | searchObj = re.search( "of:0*([1-9][0-9]*)", |
| 137 | netcfg[ 'devices' ][ new_device ][ SR_APP ][ 'pairDeviceId' ]) |
| 138 | if searchObj: |
| 139 | netcfg[ 'devices' ][ new_device ][ SR_APP ][ 'pairDeviceId' ] = 'device:bmv2:leaf' + \ |
| 140 | searchObj.group( 1 ) |
| 141 | if 'basic' in netcfg[ 'devices' ][ new_device ].keys(): |
| 142 | if 'driver' in netcfg[ 'devices' ][ new_device ][ 'basic' ].keys(): |
| 143 | del netcfg[ 'devices' ][ new_device ][ 'basic' ][ 'driver' ] |
| 144 | |
| 145 | if 'apps' in netcfg.keys(): |
| 146 | if DHCP_APP_ID in netcfg[ 'apps' ].keys(): |
| 147 | for i, dhcpcfg in enumerate( netcfg[ 'apps' ][ DHCP_APP_ID ][ 'default' ] ): |
| 148 | if 'dhcpServerConnectPoint' in dhcpcfg.keys(): |
| 149 | searchObj = re.search( "of:0*([1-9][0-9]*)/([0-9]+)", |
| 150 | dhcpcfg[ 'dhcpServerConnectPoint' ] ) |
| 151 | if searchObj: |
| 152 | netcfg[ 'apps' ][ DHCP_APP_ID ][ 'default' ][ i ][ 'dhcpServerConnectPoint' ] = \ |
| 153 | 'device:bmv2:leaf' + searchObj.group( 1 ) + '/' + searchObj.group( 2 ) |
| 154 | |
| 155 | with open( cfgFile, 'w' ) as cfg: |
| 156 | cfg.write( json.dumps( netcfg, indent=4, separators=( ',', ':' ) ) ) |