summaryrefslogtreecommitdiffstats
path: root/networking-odl/networking_odl/common/filters.py
blob: 340fa4da64c9fecc8de5c047eb0d635c37b25c33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# Copyright (c) 2015 OpenStack Foundation
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    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


@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<String> mappedCollection = new ArrayList<String>;
        #   }
        #
        #   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,
}