Advertisement
Guest User

Untitled

a guest
Jul 29th, 2014
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.55 KB | None | 0 0
  1. import abc
  2.  
  3. from oslo.config import cfg
  4. import six
  5.  
  6. from neutron.api import extensions
  7. from neutron.api.v2 import attributes as attr
  8. from neutron.api.v2 import resource_helper
  9. from neutron.common import exceptions as qexception
  10. from neutron.openstack.common import log as logging
  11. from neutron.plugins.common import constants
  12. from neutron.services import service_base
  13.  
  14.  
  15. LOG = logging.getLogger(__name__)
  16.  
  17.  
  18. # Exceptions
  19. class RedirectionNotFound(qexception.NotFound):
  20.     message = _("Redirection could not be found.")
  21.  
  22. class RedirectionInvalidProtocol(qexception.NotFound):
  23.     message = _("Protocol is not valid.")
  24.  
  25.  
  26.  
  27. fw_valid_protocol_values = [None, constants.TCP, constants.UDP, constants.ICMP]
  28. fw_valid_action_values = [constants.FWAAS_ALLOW, constants.FWAAS_DENY]
  29.  
  30.  
  31. def convert_protocol(value):
  32.     if value is None:
  33.         return
  34.     if value.isdigit():
  35.         val = int(value)
  36.         if 0 <= val <= 255:
  37.             return val
  38.         else:
  39.             raise RedirectionInvalidProtocol(protocol=value,
  40.                                               values=
  41.                                               fw_valid_protocol_values)
  42.     elif value.lower() in fw_valid_protocol_values:
  43.         return value.lower()
  44.     else:
  45.         raise RedirectionInvalidProtocol(protocol=value,
  46.                                           values=
  47.                                           fw_valid_protocol_values)
  48.  
  49.  
  50. def convert_action_to_case_insensitive(value):
  51.     if value is None:
  52.         return
  53.     else:
  54.         return value.lower()
  55.  
  56.  
  57. def convert_port_to_string(value):
  58.     if value is None:
  59.         return
  60.     else:
  61.         return str(value)
  62.  
  63.  
  64. def _validate_port_range(data, key_specs=None):
  65.     if data is None:
  66.         return
  67.     data = str(data)
  68.     ports = data.split(':')
  69.     for p in ports:
  70.         try:
  71.             val = int(p)
  72.         except (ValueError, TypeError):
  73.             msg = _("Port '%s' is not a valid number") % p
  74.             LOG.debug(msg)
  75.             return msg
  76.         if val <= 0 or val > 65535:
  77.             msg = _("Invalid port '%s'") % p
  78.             LOG.debug(msg)
  79.             return msg
  80.  
  81.  
  82. attr.validators['type:port_range'] = _validate_port_range
  83.  
  84.  
  85. RESOURCE_ATTRIBUTE_MAP = {
  86.     'redirections': {
  87.         'name': {'allow_post': True, 'allow_put': True,
  88.                  'default': None, 'is_visible': True},
  89.         'fixed_port': {'allow_post': False, 'allow_put': False,
  90.                        'is_visible': True},
  91.         'fixed_ip_address': {'allow_post': False, 'allow_put': False,
  92.                      'is_visible': True, 'default': None},
  93.         'floating_port': {'allow_post': True, 'allow_put': True,
  94.                      'is_visible': True, 'default': None,
  95.                      'convert_to': convert_protocol},
  96.         'protocol': {'allow_post': True, 'allow_put': True,
  97.                      'is_visible': True, 'default': None,
  98.                      'convert_to': convert_protocol,
  99.                      'validate': {'type:values': fw_valid_protocol_values}},
  100.         'floating_ip_address': {'allow_post': True, 'allow_put': True,
  101.                                 'validate': {'type:ip_address_or_none': None},
  102.                                 'is_visible': True},
  103.         'floating_network_id': {'allow_post': True, 'allow_put': False,
  104.                                 'validate': {'type:uuid': None},
  105.                                 'is_visible': True},
  106.         'router_id': {'allow_post': False, 'allow_put': False,
  107.                       'validate': {'type:uuid_or_none': None},
  108.                       'is_visible': True, 'default': None},
  109.         'port_id': {'allow_post': True, 'allow_put': True,
  110.                     'validate': {'type:uuid_or_none': None},
  111.                     'is_visible': True, 'default': None,
  112.                     'required_by_policy': True},
  113.     },
  114. }
  115.  
  116. #port_quota_opts = [
  117. #    cfg.IntOpt('quota_redirection',
  118. #               default=1,
  119. #               help=_('Number of ports redirected by ???. '
  120. #                      'A negative value means unlimited.')),
  121. #]
  122. #cfg.CONF.register_opts(port_quota_opts, 'QUOTAS')
  123.  
  124.  
  125. class Floatingport(extensions.ExtensionDescriptor):
  126.  
  127.     @classmethod
  128.     def get_name(cls):
  129.         return "Floating port service"
  130.  
  131.     @classmethod
  132.     def get_alias(cls):
  133.         return "floatingport"
  134.  
  135.     @classmethod
  136.     def get_description(cls):
  137.         return "Extension for port forwarding"
  138.  
  139.     @classmethod
  140.     def get_namespace(cls):
  141.         return "http://wiki.openstack.org/Neutron/FWaaS/API_1.0"
  142.  
  143.     @classmethod
  144.     def get_updated(cls):
  145.         return "2013-02-25T10:00:00-00:00"
  146.  
  147.     @classmethod
  148.     def get_resources(cls):
  149.         special_mappings = {'port_policies': 'port_policy'}
  150.         plural_mappings = resource_helper.build_plural_mappings(
  151.             special_mappings, RESOURCE_ATTRIBUTE_MAP)
  152.         attr.PLURALS.update(plural_mappings)
  153.         action_map = {'port_policy': {'insert_rule': 'PUT',
  154.                                           'remove_rule': 'PUT'}}
  155.         return resource_helper.build_resource_info(plural_mappings,
  156.                                                    RESOURCE_ATTRIBUTE_MAP,
  157.                                                    constants.FIREWALL,
  158.                                                    action_map=action_map)
  159.  
  160.     @classmethod
  161.     def get_plugin_interface(cls):
  162.         return FloatingPortPluginBase
  163.  
  164.     def update_attributes_map(self, attributes):
  165.         super(Floatingport, self).update_attributes_map(
  166.             attributes, extension_attrs_map=RESOURCE_ATTRIBUTE_MAP)
  167.  
  168.     def get_extended_resources(self, version):
  169.         if version == "2.0":
  170.             return RESOURCE_ATTRIBUTE_MAP
  171.         else:
  172.             return {}
  173.  
  174.  
  175. @six.add_metaclass(abc.ABCMeta)
  176. class FloatingPortPluginBase(service_base.ServicePluginBase):
  177.     supported_extension_aliases = ['floatingport']
  178.  
  179.     def get_plugin_name(self):
  180.         return 'floatingport'
  181.  
  182.     def get_plugin_type(self):
  183.         return 'floatingport'
  184.  
  185.     def get_plugin_description(self):
  186.         return 'Floating port service plugin'
  187.  
  188.     @abc.abstractmethod
  189.     def create_floatingport(self, context, floatingport):
  190.         pass
  191.  
  192.     @abc.abstractmethod
  193.     def update_floatingport(self, context, id, floatingport):
  194.         pass
  195.  
  196.     @abc.abstractmethod
  197.     def get_floatingport(self, context, id, fields=None):
  198.         pass
  199.  
  200.     @abc.abstractmethod
  201.     def get_floatingports(self, context, id, fields=None):
  202.         pass
  203.  
  204.     @abc.abstractmethod
  205.     def delete_floatingport(self, context, id):
  206.         pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement