diff --git a/gns3converter/converter.py b/gns3converter/converter.py index 9db23a3..4699bfc 100644 --- a/gns3converter/converter.py +++ b/gns3converter/converter.py @@ -43,6 +43,7 @@ def __init__(self, topology, debug=False): self.port_id = 1 self.links = [] self.configs = [] + self.datas = [] self.images = [] logging.getLogger(__name__) @@ -230,6 +231,12 @@ def generate_nodes(self, topology): and tmp_node.device_info['chassis'] == '3660': tmp_node.node['properties']['slot0'] = 'Leopard-2FE' + for name in ['rom', 'nvram', 'bootflash', 'disk0', 'disk1', 'slot0', 'slot1']: + self.datas.append({ + 'old': os.path.join('working', tmp_node.device_info['model'] + '_' + device + '_' + name), + 'new': tmp_node.device_info['model'] + '_i' + str(tmp_node.node['router_id']) + '_' + name + }) + # Calculate the router links tmp_node.calc_device_links() diff --git a/gns3converter/main.py b/gns3converter/main.py index 57567c7..f4d8854 100644 --- a/gns3converter/main.py +++ b/gns3converter/main.py @@ -252,6 +252,9 @@ def save(output_dir, converter, json_topology, snapshot, quiet): config_err = copy_configs(converter.configs, old_topology_dir, topology_files_dir) + copy_datas(converter.datas, old_topology_dir, + topology_files_dir) + # Copy any VPCS configurations to the the new topology copy_vpcs_configs(old_topology_dir, topology_files_dir) @@ -292,6 +295,30 @@ def save(output_dir, converter, json_topology, snapshot, quiet): logging.error(error) +def copy_datas(datas, source, target): + """ + Copy dynamips data to converted topology + + :param datas: Configs to copy + :param str source: Source topology directory + :param str target: Target topology files directory + :return: True when a data cannot be found, otherwise false + :rtype: bool + """ + data_err = False + if len(datas) > 0: + data_dir = os.path.join(target, 'dynamips') + os.makedirs(data_dir, exist_ok=True) + for data in datas: + old_data_file = os.path.join(source, data['old']) + new_data_file = os.path.join(data_dir, data['new']) + if os.path.isfile(old_data_file): + # Copy and rename the data + shutil.copy(old_data_file, new_data_file) + return data_err + + + def copy_configs(configs, source, target): """ Copy dynamips configs to converted topology diff --git a/tests/test_converter.py b/tests/test_converter.py index 8ab52e7..a364d7e 100644 --- a/tests/test_converter.py +++ b/tests/test_converter.py @@ -72,5 +72,47 @@ def test_generate_notes(self): res = self.app.generate_notes(notes) self.assertListEqual(res, exp_res) + def test_generate_nodes(self): + topology = {} + topology['conf'] = [ + {'sparsemem': True, + 'ghostios': True, + 'idlepc': '0x60bec828', + 'ram': 128, + 'model': 'c3725', + 'image': 'c3725-adventerprisek9-mz.124-15.T5.image' + } + ] + topology['devices'] = { + 'GooglISP': { + 'model': 'c7200', + 'aux': 2512, + 'hx': 19.5, + 'z': 1.0, + 'type': 'Router', + 'node_id': 11, + 'p1/0': 'VerISPon p1/0', + 'hv_id': 3, + 'x': -261.643648086, + 'cnfg': 'configs\\GooglISP.cfg', + 'f0/0': 'SW1 f0/0', + 'y': -419.773080371, + 'console': 2012, + 'from': 'ROUTER', + 'hy': -25.0, + 'slot0': 'C7200-IO-FE', + 'desc': 'Router', + 'slot1': 'PA-POS-OC3' + } + } + + config = self.app.generate_nodes(topology) + self.assertEqual(self.app.datas, [ + {'new': 'c7200_i11_rom', 'old': 'working/c7200_GooglISP_rom'}, + {'new': 'c7200_i11_nvram', 'old': 'working/c7200_GooglISP_nvram'}, + {'new': 'c7200_i11_bootflash', 'old': 'working/c7200_GooglISP_bootflash'}, + {'new': 'c7200_i11_disk0', 'old': 'working/c7200_GooglISP_disk0'} + ]) + if __name__ == '__main__': unittest.main()