Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import abc
- from oslo.config import cfg
- import six
- from neutron.api import extensions
- from neutron.api.v2 import attributes as attr
- from neutron.api.v2 import resource_helper
- from neutron.common import exceptions as qexception
- from neutron.openstack.common import log as logging
- from neutron.plugins.common import constants
- from neutron.services import service_base
- LOG = logging.getLogger(__name__)
- # Exceptions
- class RedirectionNotFound(qexception.NotFound):
- message = _("Redirection could not be found.")
- class RedirectionInvalidProtocol(qexception.NotFound):
- message = _("Protocol is not valid.")
- fw_valid_protocol_values = [None, constants.TCP, constants.UDP, constants.ICMP]
- fw_valid_action_values = [constants.FWAAS_ALLOW, constants.FWAAS_DENY]
- def convert_protocol(value):
- if value is None:
- return
- if value.isdigit():
- val = int(value)
- if 0 <= val <= 255:
- return val
- else:
- raise RedirectionInvalidProtocol(protocol=value,
- values=
- fw_valid_protocol_values)
- elif value.lower() in fw_valid_protocol_values:
- return value.lower()
- else:
- raise RedirectionInvalidProtocol(protocol=value,
- values=
- fw_valid_protocol_values)
- def convert_action_to_case_insensitive(value):
- if value is None:
- return
- else:
- return value.lower()
- def convert_port_to_string(value):
- if value is None:
- return
- else:
- return str(value)
- def _validate_port_range(data, key_specs=None):
- if data is None:
- return
- data = str(data)
- ports = data.split(':')
- for p in ports:
- try:
- val = int(p)
- except (ValueError, TypeError):
- msg = _("Port '%s' is not a valid number") % p
- LOG.debug(msg)
- return msg
- if val <= 0 or val > 65535:
- msg = _("Invalid port '%s'") % p
- LOG.debug(msg)
- return msg
- attr.validators['type:port_range'] = _validate_port_range
- RESOURCE_ATTRIBUTE_MAP = {
- 'redirections': {
- 'name': {'allow_post': True, 'allow_put': True,
- 'default': None, 'is_visible': True},
- 'fixed_port': {'allow_post': False, 'allow_put': False,
- 'is_visible': True},
- 'fixed_ip_address': {'allow_post': False, 'allow_put': False,
- 'is_visible': True, 'default': None},
- 'floating_port': {'allow_post': True, 'allow_put': True,
- 'is_visible': True, 'default': None,
- 'convert_to': convert_protocol},
- 'protocol': {'allow_post': True, 'allow_put': True,
- 'is_visible': True, 'default': None,
- 'convert_to': convert_protocol,
- 'validate': {'type:values': fw_valid_protocol_values}},
- 'floating_ip_address': {'allow_post': True, 'allow_put': True,
- 'validate': {'type:ip_address_or_none': None},
- 'is_visible': True},
- 'floating_network_id': {'allow_post': True, 'allow_put': False,
- 'validate': {'type:uuid': None},
- 'is_visible': True},
- 'router_id': {'allow_post': False, 'allow_put': False,
- 'validate': {'type:uuid_or_none': None},
- 'is_visible': True, 'default': None},
- 'port_id': {'allow_post': True, 'allow_put': True,
- 'validate': {'type:uuid_or_none': None},
- 'is_visible': True, 'default': None,
- 'required_by_policy': True},
- },
- }
- #port_quota_opts = [
- # cfg.IntOpt('quota_redirection',
- # default=1,
- # help=_('Number of ports redirected by ???. '
- # 'A negative value means unlimited.')),
- #]
- #cfg.CONF.register_opts(port_quota_opts, 'QUOTAS')
- class Floatingport(extensions.ExtensionDescriptor):
- @classmethod
- def get_name(cls):
- return "Floating port service"
- @classmethod
- def get_alias(cls):
- return "floatingport"
- @classmethod
- def get_description(cls):
- return "Extension for port forwarding"
- @classmethod
- def get_namespace(cls):
- return "http://wiki.openstack.org/Neutron/FWaaS/API_1.0"
- @classmethod
- def get_updated(cls):
- return "2013-02-25T10:00:00-00:00"
- @classmethod
- def get_resources(cls):
- special_mappings = {'port_policies': 'port_policy'}
- plural_mappings = resource_helper.build_plural_mappings(
- special_mappings, RESOURCE_ATTRIBUTE_MAP)
- attr.PLURALS.update(plural_mappings)
- action_map = {'port_policy': {'insert_rule': 'PUT',
- 'remove_rule': 'PUT'}}
- return resource_helper.build_resource_info(plural_mappings,
- RESOURCE_ATTRIBUTE_MAP,
- constants.FIREWALL,
- action_map=action_map)
- @classmethod
- def get_plugin_interface(cls):
- return FloatingPortPluginBase
- def update_attributes_map(self, attributes):
- super(Floatingport, self).update_attributes_map(
- attributes, extension_attrs_map=RESOURCE_ATTRIBUTE_MAP)
- def get_extended_resources(self, version):
- if version == "2.0":
- return RESOURCE_ATTRIBUTE_MAP
- else:
- return {}
- @six.add_metaclass(abc.ABCMeta)
- class FloatingPortPluginBase(service_base.ServicePluginBase):
- supported_extension_aliases = ['floatingport']
- def get_plugin_name(self):
- return 'floatingport'
- def get_plugin_type(self):
- return 'floatingport'
- def get_plugin_description(self):
- return 'Floating port service plugin'
- @abc.abstractmethod
- def create_floatingport(self, context, floatingport):
- pass
- @abc.abstractmethod
- def update_floatingport(self, context, id, floatingport):
- pass
- @abc.abstractmethod
- def get_floatingport(self, context, id, fields=None):
- pass
- @abc.abstractmethod
- def get_floatingports(self, context, id, fields=None):
- pass
- @abc.abstractmethod
- def delete_floatingport(self, context, id):
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement