def execute_flow(self,
                     vlan_range,
                     port_name,
                     port_mode,
                     action_map=None,
                     error_map=None):
        port = AddRemoveVlanHelper.extract_port_name(port_name)
        with self._cli_handler.get_cli_service(
                self._cli_handler.config_mode) as cli_service:
            commit_rollback_actions = CommitRollbackActions(
                cli_service, self._logger)
            vlan_actions = AddRemoveVlanActions(cli_service, self._logger)
            try:
                existing_ranges = VlanRangeOperations.create_from_dict(
                    vlan_actions.get_vlans())
                range_instance = VlanRange(
                    VlanRange.range_from_string(vlan_range))
                range_intersection = VlanRangeOperations.find_intersection(
                    [range_instance], existing_ranges)

                vlan_actions.delete_member(port, vlan_range)
                commit_rollback_actions.commit()
                for _range in range_intersection:
                    vlan_actions.delete_vlan(_range.name)
                commit_rollback_actions.commit()
                return 'Success'
            except CommandExecutionException:
                commit_rollback_actions.rollback()
                raise
 def test_init(self):
     first_id = '20'
     last_id = '50'
     instance = VlanRange((first_id, last_id))
     self.assertEqual(instance.first_element, int(first_id))
     self.assertEqual(instance.last_element, int(last_id))
     self.assertEqual(instance.name,
                      'range-{0}-{1}'.format(first_id, last_id))
     with self.assertRaises(Exception):
         VlanRange((30, 10))
예제 #3
0
    def execute_flow(self, vlan_range, port_mode, port_name, qnq, c_tag):
        port = AddRemoveVlanHelper.extract_port_name(port_name)
        with self._cli_handler.config_mode_service() as cli_service:
            commit_rollback_actions = CommitRollbackActions(
                cli_service, self._logger)
            vlan_actions = AddRemoveVlanActions(cli_service, self._logger)
            try:
                existing_ranges = VlanRangeOperations.create_from_dict(
                    vlan_actions.get_vlans())
                new_range = VlanRange(VlanRange.range_from_string(vlan_range))
                range_intersection = VlanRangeOperations.find_intersection(
                    [new_range], existing_ranges)
                new_range_cutoff = VlanRangeOperations.cutoff_intersection(
                    [new_range], existing_ranges)

                if qnq:
                    for _range in range_intersection:
                        if not vlan_actions.check_vlan_qnq(_range.name):
                            raise Exception(
                                self.__class__.__name__,
                                'Not only QNQ vlans exist in vlan range intersection'
                            )
                    for _range in new_range_cutoff:
                        vlan_actions.create_qnq_vlan(_range.name,
                                                     _range.to_string())
                else:
                    for _range in range_intersection:
                        if vlan_actions.check_vlan_qnq(_range.name):
                            raise Exception(
                                self.__class__.__name__,
                                'QNQ vlans already exist in vlan range intersection'
                            )
                    for _range in new_range_cutoff:
                        vlan_actions.create_vlan(_range.name,
                                                 _range.to_string())

                vlan_actions.clean_port(port)
                vlan_actions.assign_member(port, vlan_range, port_mode)
                commit_rollback_actions.commit()
                return 'Success'
            except CommandExecutionException:
                commit_rollback_actions.rollback()
                raise
 def test_find_intersection(self):
     test_range = VlanRange((200, 300))
     range_list = [
         VlanRange((50, 100)),
         VlanRange((150, 200)),
         VlanRange((250, 270)),
         VlanRange((301, 400))
     ]
     self.assertEqual(
         VlanRangeOperations.find_intersection([test_range], range_list),
         [VlanRange(
             (150, 200)), VlanRange((250, 270))])
 def test_cutoff_intersection(self):
     test_range = VlanRange((100, 500))
     result = VlanRangeOperations.cutoff_intersection(
         [test_range], [VlanRange(
             (150, 200)), VlanRange((300, 350))])
     self.assertEqual(result, [
         VlanRange((100, 149)),
         VlanRange((201, 299)),
         VlanRange((351, 500))
     ])
 def test_create_from_dict(self):
     vlan_dict = {'test1': '10-20', 'test2': '40-50'}
     range_list = VlanRangeOperations.create_from_dict(vlan_dict)
     self.assertEqual(
         range_list, [VlanRange(
             (10, 20)), VlanRange((40, 50))])
 def test_to_string(self):
     self.assertEqual(VlanRange((30, 40)).to_string(), '30-40')
 def test_range_from_string(self):
     self.assertEqual(VlanRange(VlanRange.range_from_string('10-20')),
                      VlanRange((10, 20)))
     self.assertEqual(VlanRange(VlanRange.range_from_string('30')),
                      VlanRange((30, 30)))
 def test_cutoff(self):
     first_id = '50'
     last_id = '250'
     instance = VlanRange((first_id, last_id))
     self.assertEqual(instance.cutoff(VlanRange((20, 100))),
                      [VlanRange((101, 250))])
     self.assertEqual(instance.cutoff(VlanRange((200, 300))),
                      [VlanRange((50, 199))])
     self.assertEqual(instance.cutoff(VlanRange((50, 250))), [])
     self.assertEqual(instance.cutoff(VlanRange(
         (70, 200))), [VlanRange(
             (50, 69)), VlanRange((201, 250))])
     self.assertEqual(instance.cutoff(VlanRange((300, 400))),
                      [VlanRange((50, 250))])
 def test_intersect(self):
     first_id = '20'
     last_id = '50'
     instance = VlanRange((first_id, last_id))
     self.assertTrue(instance.intersect(VlanRange((10, 20))))
     self.assertTrue(instance.intersect(VlanRange((20, 50))))
     self.assertTrue(instance.intersect(VlanRange((50, 60))))
     self.assertTrue(instance.intersect(VlanRange((30, 40))))
     self.assertFalse(instance.intersect(VlanRange((5, 19))))
     self.assertFalse(instance.intersect(VlanRange((51, 60))))