Exemplo n.º 1
0
    def process_topology(self, old_top):
        """
        Processes the sections returned by get_instances

        :param ConfigObj old_top: old topology as processed by
                                  :py:meth:`read_topology`
        :returns: tuple of dicts containing hypervisors, devices and artwork
        :rtype: tuple
        """
        sections = self.get_sections(old_top)
        topo = LegacyTopology(sections, old_top)

        for instance in sorted(sections):

            if instance.startswith('vbox') \
                    and isinstance(old_top[instance]['VBoxDevice'], dict):
                topo.add_conf_item(instance, 'VBoxDevice')
                old_top[instance].pop('VBoxDevice')

            for item in sorted(old_top[instance]):
                if isinstance(old_top[instance][item], dict):
                    if item in MODEL_TRANSFORM:
                        # A configuration item (topo.conf)
                        topo.add_conf_item(instance, item)
                    elif instance == 'GNS3-DATA' and \
                            (item.startswith('SHAPE')
                             or item.startswith('NOTE')
                             or item.startswith('PIXMAP')):
                        # Item is an artwork item e.g. shapes and notes from
                        # GNS3-DATA
                        topo.add_artwork_item(instance, item)
                    else:
                        # It must be a physical item (topo.devices)
                        topo.add_physical_item(instance, item)
        return topo.topology
Exemplo n.º 2
0
class TestLegacyTopology(unittest.TestCase):
    def setUp(self):
        conf = ConfigObj()
        conf['127.0.0.1:7200'] = {'3725': {'image': 'c3725.image',
                                           'ram': 128,
                                           'x': None,
                                           'y': None},
                                  'ROUTER R1': {'cnfg': 'configs/R1.cfg',
                                                'console': 2101,
                                                'aux': 2501,
                                                'model': None}}
        self.app = LegacyTopology([], conf)

    def add_hv_details(self):
        instance = '127.0.0.1:7200'
        item = '3725'

        self.app.add_conf_item(instance, item)

    def test_add_artwork_item(self):
        self.app.old_top['GNS3-DATA'] = {
            'NOTE 1': {'text': 'SomeText', 'x': 20, 'y': 25,
                       'color': '#1a1a1a'},
            'NOTE 2': {'text': 'f0/0', 'x': 20, 'y': 25,
                       'color': '#1a1a1a', 'interface': 'f0/0'},
            'SHAPE 1': {'type': 'ellipse', 'x': 20, 'y': 25, 'width': 500,
                        'height': 250, 'border_style': 2}
        }

        exp_res = {'SHAPE': {'1': {'type': 'ellipse',
                                   'x': 20, 'y': 25,
                                   'color': '#ffffff',
                                   'transparency': 0,
                                   'width': 500,
                                   'height': 250,
                                   'border_style': 2}},
                   'PIXMAP': {},
                   'NOTE': {'1': {'text': 'SomeText',
                                  'x': 20, 'y': 25,
                                  'color': '#1a1a1a'}}
                   }

        self.app.add_artwork_item('GNS3-DATA', 'SHAPE 1')
        self.app.add_artwork_item('GNS3-DATA', 'NOTE 1')
        self.app.add_artwork_item('GNS3-DATA', 'NOTE 2')

        self.assertDictEqual(self.app.topology['artwork'], exp_res)

    def test_add_conf_item(self):
        instance = '127.0.0.1:7200'
        item = '3725'

        exp_res = [{'image': 'c3725.image', 'model': 'c3725', 'ram': 128}]

        self.app.add_conf_item(instance, item)
        self.assertListEqual(self.app.topology['conf'], exp_res)

    def test_add_physical_item_no_model(self):
        self.add_hv_details()

        instance = '127.0.0.1:7200'
        item = 'ROUTER R1'

        exp_res = {'R1': {'hv_id': 0,
                          'node_id': 1,
                          'type': 'Router',
                          'desc': 'Router',
                          'from': 'ROUTER',
                          'cnfg': 'configs/R1.cfg',
                          'console': 2101,
                          'aux': 2501,
                          'model': 'c3725',
                          'hx': 19.5, 'hy': -25}}

        self.app.add_physical_item(instance, item)
        self.assertDictEqual(self.app.topology['devices'], exp_res)

    def test_add_physical_item_with_model(self):
        self.add_hv_details()

        instance = '127.0.0.1:7200'
        item = 'ROUTER R1'

        exp_res = {'R1': {'hv_id': 0,
                          'node_id': 1,
                          'type': 'Router',
                          'desc': 'Router',
                          'from': 'ROUTER',
                          'cnfg': 'configs/R1.cfg',
                          'console': 2101,
                          'aux': 2501,
                          'model': 'c7200',
                          'hx': 19.5, 'hy': -25}}

        self.app.old_top['127.0.0.1:7200']['ROUTER R1']['model'] = '7200'

        self.app.add_physical_item(instance, item)
        self.assertDictEqual(self.app.topology['devices'], exp_res)

    def test_device_typename(self):
        exp_result = {'ROUTER R1': {'name': 'R1', 'type': 'Router'},
                      'QEMU Q1': {'name': 'Q1', 'type': 'QemuVM'},
                      'ASA ASA1': {'name': 'ASA1', 'type': 'QemuVM'},
                      'PIX PIX1': {'name': 'PIX1', 'type': 'QemuVM'},
                      'JUNOS JUNOS1': {'name': 'JUNOS1', 'type': 'QemuVM'},
                      'IDS IDS1': {'name': 'IDS1', 'type': 'QemuVM'},
                      'VBOX V1': {'name': 'V1', 'type': 'VirtualBoxVM'},
                      'FRSW FR1': {'name': 'FR1', 'type': 'FrameRelaySwitch'},
                      'ETHSW SW1': {'name': 'SW1', 'type': 'EthernetSwitch'},
                      'Hub Hub1': {'name': 'Hub1', 'type': 'EthernetHub'},
                      'ATMSW SW1': {'name': 'SW1', 'type': 'ATMSwitch'},
                      'ATMBR BR1': {'name': 'BR1', 'type': 'ATMBR'},
                      'Cloud C1': {'name': 'C1', 'type': 'Cloud'}}

        for device in exp_result:
                (name, dev_type) = self.app.device_typename(device)
                self.assertEqual(exp_result[device]['name'], name)
                self.assertEqual(exp_result[device]['type'], dev_type['type'])

    def test_vbox_id(self):
        self.assertEqual(self.app.vbox_id, 1)
        self.app.vbox_id = 5
        self.assertEqual(self.app.vbox_id, 5)

    def test_qemu_id(self):
        self.assertEqual(self.app.qemu_id, 1)
        self.app.qemu_id = 5
        self.assertEqual(self.app.qemu_id, 5)
Exemplo n.º 3
0
class TestLegacyTopology(unittest.TestCase):
    def setUp(self):
        conf = ConfigObj()
        conf['127.0.0.1:7200'] = {
            '3725': {
                'image': 'c3725.image',
                'ram': 128,
                'x': None,
                'y': None
            },
            'ROUTER R1': {
                'cnfg': 'configs/R1.cfg',
                'console': 2101,
                'aux': 2501,
                'model': None
            }
        }
        self.app = LegacyTopology([], conf)

    def add_hv_details(self):
        instance = '127.0.0.1:7200'
        item = '3725'

        self.app.add_conf_item(instance, item)

    def test_add_artwork_item(self):
        self.app.old_top['GNS3-DATA'] = {
            'NOTE 1': {
                'text': 'SomeText',
                'x': 20,
                'y': 25,
                'color': '#1a1a1a'
            },
            'NOTE 2': {
                'text': 'f0/0',
                'x': 20,
                'y': 25,
                'color': '#1a1a1a',
                'interface': 'f0/0'
            },
            'SHAPE 1': {
                'type': 'ellipse',
                'x': 20,
                'y': 25,
                'width': 500,
                'border_width': 4,
                'height': 250,
                'border_style': 2,
                'rotate': "45"
            }
        }

        exp_res = {
            'SHAPE': {
                '1': {
                    'type': 'ellipse',
                    'x': 20,
                    'y': 25,
                    'color': '#ffffff',
                    'transparency': 0,
                    'width': 500,
                    'height': 250,
                    'border_style': 2,
                    'border_width': 4,
                    'rotation': 45.0
                }
            },
            'PIXMAP': {},
            'NOTE': {
                '1': {
                    'text': 'SomeText',
                    'x': 20,
                    'y': 25,
                    'color': '#1a1a1a'
                }
            }
        }

        self.app.add_artwork_item('GNS3-DATA', 'SHAPE 1')
        self.app.add_artwork_item('GNS3-DATA', 'NOTE 1')
        self.app.add_artwork_item('GNS3-DATA', 'NOTE 2')

        self.assertDictEqual(self.app.topology['artwork'], exp_res)

    def test_add_conf_item(self):
        instance = '127.0.0.1:7200'
        item = '3725'

        exp_res = [{'image': 'c3725.image', 'model': 'c3725', 'ram': 128}]

        self.app.add_conf_item(instance, item)
        self.assertListEqual(self.app.topology['conf'], exp_res)

    def test_add_physical_item_no_model(self):
        self.add_hv_details()

        instance = '127.0.0.1:7200'
        item = 'ROUTER R1'

        exp_res = {
            'R1': {
                'hv_id': 0,
                'node_id': 1,
                'type': 'Router',
                'desc': 'Router',
                'from': 'ROUTER',
                'cnfg': 'configs/R1.cfg',
                'console': 2101,
                'aux': 2501,
                'model': 'c3725',
                'hx': 19.5,
                'hy': -25
            }
        }

        self.app.add_physical_item(instance, item)
        self.assertDictEqual(self.app.topology['devices'], exp_res)

    def test_add_physical_item_with_model(self):
        self.add_hv_details()

        instance = '127.0.0.1:7200'
        item = 'ROUTER R1'

        exp_res = {
            'R1': {
                'hv_id': 0,
                'node_id': 1,
                'type': 'Router',
                'desc': 'Router',
                'from': 'ROUTER',
                'cnfg': 'configs/R1.cfg',
                'console': 2101,
                'aux': 2501,
                'model': 'c7200',
                'hx': 19.5,
                'hy': -25
            }
        }

        self.app.old_top['127.0.0.1:7200']['ROUTER R1']['model'] = '7200'

        self.app.add_physical_item(instance, item)
        self.assertDictEqual(self.app.topology['devices'], exp_res)

    def test_device_typename(self):
        exp_result = {
            'ROUTER R1': {
                'name': 'R1',
                'type': 'Router'
            },
            'QEMU Q1': {
                'name': 'Q1',
                'type': 'QemuVM'
            },
            'ASA ASA1': {
                'name': 'ASA1',
                'type': 'QemuVM'
            },
            'PIX PIX1': {
                'name': 'PIX1',
                'type': 'QemuVM'
            },
            'JUNOS JUNOS1': {
                'name': 'JUNOS1',
                'type': 'QemuVM'
            },
            'IDS IDS1': {
                'name': 'IDS1',
                'type': 'QemuVM'
            },
            'VBOX V1': {
                'name': 'V1',
                'type': 'VirtualBoxVM'
            },
            'FRSW FR1': {
                'name': 'FR1',
                'type': 'FrameRelaySwitch'
            },
            'ETHSW SW1': {
                'name': 'SW1',
                'type': 'EthernetSwitch'
            },
            'Hub Hub1': {
                'name': 'Hub1',
                'type': 'EthernetHub'
            },
            'ATMSW SW1': {
                'name': 'SW1',
                'type': 'ATMSwitch'
            },
            'ATMBR BR1': {
                'name': 'BR1',
                'type': 'ATMBR'
            },
            'Cloud C1': {
                'name': 'C1',
                'type': 'Cloud'
            }
        }

        for device in exp_result:
            (name, dev_type) = self.app.device_typename(device)
            self.assertEqual(exp_result[device]['name'], name)
            self.assertEqual(exp_result[device]['type'], dev_type['type'])

    def test_vbox_id(self):
        self.assertEqual(self.app.vbox_id, 1)
        self.app.vbox_id = 5
        self.assertEqual(self.app.vbox_id, 5)

    def test_qemu_id(self):
        self.assertEqual(self.app.qemu_id, 1)
        self.app.qemu_id = 5
        self.assertEqual(self.app.qemu_id, 5)