From 4979a23b8b2c0094ced98cf05eebb692d6609937 Mon Sep 17 00:00:00 2001 From: Wojciech Dec Date: Wed, 17 Aug 2016 13:14:23 +0200 Subject: Correcting networking-odl to mitaka/stable + app topology patch Change-Id: Iddcd8dda2d49fcdd8e0f37a1d052a6fa8a24b035 Signed-off-by: Wojciech Dec --- networking-odl/networking_odl/common/filters.py | 227 ++++++++++++++++-------- 1 file changed, 150 insertions(+), 77 deletions(-) (limited to 'networking-odl/networking_odl/common/filters.py') diff --git a/networking-odl/networking_odl/common/filters.py b/networking-odl/networking_odl/common/filters.py index fb42a0e..340fa4d 100644 --- a/networking-odl/networking_odl/common/filters.py +++ b/networking-odl/networking_odl/common/filters.py @@ -12,85 +12,158 @@ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. +import abc +import six + from networking_odl.common import constants as odl_const from networking_odl.common import utils as odl_utils -def _filter_unmapped_null(resource_dict, unmapped_keys): - # NOTE(yamahata): bug work around - # https://bugs.eclipse.org/bugs/show_bug.cgi?id=475475 - # Null-value for an unmapped element causes next mapped - # collection to contain a null value - # JSON: { "unmappedField": null, "mappedCollection": [ "a" ] } - # - # Java Object: - # class Root { - # Collection mappedCollection = new ArrayList; - # } - # - # Result: - # Field B contains one element; null - # - # TODO(yamahata): update along side with neutron and ODL - # add when neutron adds more extensions - # delete when ODL neutron northbound supports it - # TODO(yamahata): do same thing for other resources - keys_to_del = [key for key in unmapped_keys - if resource_dict.get(key) is None] - if keys_to_del: - odl_utils.try_del(resource_dict, keys_to_del) - - -_NETWORK_UNMAPPED_KEYS = ['qos_policy_id'] -_PORT_UNMAPPED_KEYS = ['binding:profile', 'dns_name', - 'port_security_enabled', 'qos_policy_id'] - - -def _filter_network_create(network): - odl_utils.try_del(network, ['status', 'subnets']) - _filter_unmapped_null(network, _NETWORK_UNMAPPED_KEYS) - - -def _filter_network_update(network): - odl_utils.try_del(network, ['id', 'status', 'subnets', 'tenant_id']) - _filter_unmapped_null(network, _NETWORK_UNMAPPED_KEYS) - - -def _filter_subnet_update(subnet): - odl_utils.try_del(subnet, ['id', 'network_id', 'ip_version', 'cidr', - 'allocation_pools', 'tenant_id']) - - -def _filter_port_create(port): - """Filter out port attributes not required for a create.""" - odl_utils.try_del(port, ['status']) - _filter_unmapped_null(port, _PORT_UNMAPPED_KEYS) - - -def _filter_port_update(port): - """Filter out port attributes for an update operation.""" - odl_utils.try_del(port, ['network_id', 'id', 'status', 'mac_address', - 'tenant_id', 'fixed_ips']) - _filter_unmapped_null(port, _PORT_UNMAPPED_KEYS) - - -def _filter_router_update(router): - """Filter out attributes for an update operation.""" - odl_utils.try_del(router, ['id', 'tenant_id', 'status']) - - -_FILTER_MAP = { - (odl_const.ODL_NETWORK, odl_const.ODL_CREATE): _filter_network_create, - (odl_const.ODL_NETWORK, odl_const.ODL_UPDATE): _filter_network_update, - (odl_const.ODL_SUBNET, odl_const.ODL_UPDATE): _filter_subnet_update, - (odl_const.ODL_PORT, odl_const.ODL_CREATE): _filter_port_create, - (odl_const.ODL_PORT, odl_const.ODL_UPDATE): _filter_port_update, - (odl_const.ODL_ROUTER, odl_const.ODL_UPDATE): _filter_router_update, +@six.add_metaclass(abc.ABCMeta) +class ResourceFilterBase(object): + @staticmethod + @abc.abstractmethod + def filter_create_attributes(resource): + pass + + @staticmethod + @abc.abstractmethod + def filter_update_attributes(resource): + pass + + +class NetworkFilter(ResourceFilterBase): + @staticmethod + def filter_create_attributes(network): + """Filter out network attributes not required for a create.""" + odl_utils.try_del(network, ['status', 'subnets']) + + @staticmethod + def filter_update_attributes(network): + """Filter out network attributes for an update operation.""" + odl_utils.try_del(network, ['id', 'status', 'subnets', 'tenant_id']) + + +class SubnetFilter(ResourceFilterBase): + @staticmethod + def filter_create_attributes(subnet): + """Filter out subnet attributes not required for a create.""" + pass + + @staticmethod + def filter_update_attributes(subnet): + """Filter out subnet attributes for an update operation.""" + odl_utils.try_del(subnet, ['id', 'network_id', 'ip_version', 'cidr', + 'allocation_pools', 'tenant_id']) + + +class PortFilter(ResourceFilterBase): + @staticmethod + def _filter_unmapped_null(port): + # NOTE(yamahata): bug work around + # https://bugs.eclipse.org/bugs/show_bug.cgi?id=475475 + # Null-value for an unmapped element causes next mapped + # collection to contain a null value + # JSON: { "unmappedField": null, "mappedCollection": [ "a" ] } + # + # Java Object: + # class Root { + # Collection mappedCollection = new ArrayList; + # } + # + # Result: + # Field B contains one element; null + # + # TODO(yamahata): update along side with neutron and ODL + # add when neutron adds more extensions + # delete when ODL neutron northbound supports it + # TODO(yamahata): do same thing for other resources + unmapped_keys = ['dns_name', 'port_security_enabled', + 'binding:profile'] + keys_to_del = [key for key in unmapped_keys if port.get(key) is None] + if keys_to_del: + odl_utils.try_del(port, keys_to_del) + + @classmethod + def filter_create_attributes(cls, port): + """Filter out port attributes not required for a create.""" + cls._filter_unmapped_null(port) + odl_utils.try_del(port, ['status']) + + @classmethod + def filter_update_attributes(cls, port): + """Filter out port attributes for an update operation.""" + cls._filter_unmapped_null(port) + odl_utils.try_del(port, ['network_id', 'id', 'status', 'mac_address', + 'tenant_id', 'fixed_ips']) + + +class SecurityGroupFilter(ResourceFilterBase): + @staticmethod + def filter_create_attributes(sg): + """Filter out security-group attributes not required for a create.""" + pass + + @staticmethod + def filter_update_attributes(sg): + """Filter out security-group attributes for an update operation.""" + pass + + +class SecurityGroupRuleFilter(ResourceFilterBase): + @staticmethod + def filter_create_attributes(sg_rule): + """Filter out sg-rule attributes not required for a create.""" + pass + + @staticmethod + def filter_update_attributes(sg_rule): + """Filter out sg-rule attributes for an update operation.""" + pass + + +class RouterFilter(ResourceFilterBase): + @staticmethod + def filter_create_attributes(router): + """Filter out attributes not required for a create.""" + pass + + @staticmethod + def filter_update_attributes(router): + """Filter out attributes for an update operation.""" + odl_utils.try_del(router, ['id', 'tenant_id', 'status']) + + +class FloatingIPFilter(ResourceFilterBase): + @staticmethod + def filter_create_attributes(floatingip): + """Filter out attributes not required for a create.""" + pass + + @staticmethod + def filter_update_attributes(floatingip): + """Filter out attributes for an update operation.""" + pass + + +class RouterIntfFilter(ResourceFilterBase): + @staticmethod + def filter_add_attributes(routerintf): + """Filter out attributes not required for a create.""" + pass + + @staticmethod + def filter_remove_attributes(routerintf): + """Filter out attributes for an update operation.""" + pass + +FILTER_MAP = { + odl_const.ODL_NETWORK: NetworkFilter, + odl_const.ODL_SUBNET: SubnetFilter, + odl_const.ODL_PORT: PortFilter, + odl_const.ODL_ROUTER: RouterFilter, + odl_const.ODL_ROUTER_INTF: RouterIntfFilter, + odl_const.ODL_FLOATINGIP: FloatingIPFilter, + odl_const.ODL_SG: SecurityGroupFilter, + odl_const.ODL_SG_RULE: SecurityGroupRuleFilter, } - - -def filter_for_odl(object_type, operation, data): - """Filter out the attributed before sending the data to ODL""" - filter_key = (object_type, operation) - if filter_key in _FILTER_MAP: - _FILTER_MAP[filter_key](data) -- cgit 1.2.3-korg