summaryrefslogtreecommitdiffstats
path: root/networking-odl/networking_odl/journal/dependency_validations.py
blob: a6f5f96b9b5ea90e1de6237004a53a82c5600cd2 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# 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.

from networking_odl.common import constants as odl_const
from networking_odl.db import db


def _is_valid_update_operation(session, row):
    # Check if there are older updates in the queue
    if db.check_for_older_ops(session, row):
        return False

    # Check for a pending or processing create operation on this uuid
    if db.check_for_pending_or_processing_ops(
            session, row.object_uuid, odl_const.ODL_CREATE):
        return False
    return True


def validate_network_operation(session, row):
    """Validate the network operation based on dependencies.

    Validate network operation depending on whether it's dependencies
    are still in 'pending' or 'processing' state. e.g.
    """
    if row.operation == odl_const.ODL_DELETE:
        # Check for any pending or processing create or update
        # ops on this uuid itself
        if db.check_for_pending_or_processing_ops(
            session, row.object_uuid, [odl_const.ODL_UPDATE,
                                       odl_const.ODL_CREATE]):
            return False
        # Check for dependent operations
        if db.check_for_pending_delete_ops_with_parent(
            session, odl_const.ODL_SUBNET, row.object_uuid):
            return False
        if db.check_for_pending_delete_ops_with_parent(
            session, odl_const.ODL_PORT, row.object_uuid):
            return False
        if db.check_for_pending_delete_ops_with_parent(
            session, odl_const.ODL_ROUTER, row.object_uuid):
            return False
    elif (row.operation == odl_const.ODL_UPDATE and
            not _is_valid_update_operation(session, row)):
        return False
    return True


def validate_subnet_operation(session, row):
    """Validate the subnet operation based on dependencies.

    Validate subnet operation depending on whether it's dependencies
    are still in 'pending' or 'processing' state. e.g.
    """
    if row.operation in (odl_const.ODL_CREATE, odl_const.ODL_UPDATE):
        network_id = row.data['network_id']
        # Check for pending or processing network operations
        if db.check_for_pending_or_processing_ops(session, network_id):
            return False
        if (row.operation == odl_const.ODL_UPDATE and
                not _is_valid_update_operation(session, row)):
            return False
    elif row.operation == odl_const.ODL_DELETE:
        # Check for any pending or processing create or update
        # ops on this uuid itself
        if db.check_for_pending_or_processing_ops(
            session, row.object_uuid, [odl_const.ODL_UPDATE,
                                       odl_const.ODL_CREATE]):
            return False
        # Check for dependent operations
        if db.check_for_pending_delete_ops_with_parent(
            session, odl_const.ODL_PORT, row.object_uuid):
            return False

    return True


def validate_port_operation(session, row):
    """Validate port operation based on dependencies.

    Validate port operation depending on whether it's dependencies
    are still in 'pending' or 'processing' state. e.g.
    """
    if row.operation in (odl_const.ODL_CREATE, odl_const.ODL_UPDATE):
        network_id = row.data['network_id']
        # Check for pending or processing network operations
        ops = db.check_for_pending_or_processing_ops(session, network_id)
        # Check for pending subnet operations.
        for fixed_ip in row.data['fixed_ips']:
            ip_ops = db.check_for_pending_or_processing_ops(
                session, fixed_ip['subnet_id'])
            ops = ops or ip_ops

        if ops:
            return False
        if (row.operation == odl_const.ODL_UPDATE and
                not _is_valid_update_operation(session, row)):
            return False
    elif row.operation == odl_const.ODL_DELETE:
        # Check for any pending or processing create or update
        # ops on this uuid itself
        if db.check_for_pending_or_processing_ops(
            session, row.object_uuid, [odl_const.ODL_UPDATE,
                                       odl_const.ODL_CREATE]):
            return False

    return True


def validate_router_operation(session, row):
    """Validate router operation based on dependencies.

    Validate router operation depending on whether it's dependencies
    are still in 'pending' or 'processing' state.
    """
    if row.operation in (odl_const.ODL_CREATE, odl_const.ODL_UPDATE):
        if row.data['gw_port_id'] is not None:
            if db.check_for_pending_or_processing_ops(session,
                                                      row.data['gw_port_id']):
                return False
        if (row.operation == odl_const.ODL_UPDATE and
                not _is_valid_update_operation(session, row)):
            return False
    elif row.operation == odl_const.ODL_DELETE:
        # Check for any pending or processing create or update
        # operations on this uuid.
        if db.check_for_pending_or_processing_ops(session, row.object_uuid,
                                                  [odl_const.ODL_UPDATE,
                                                   odl_const.ODL_CREATE]):
            return False

        # Check that dependent port delete operation has completed.
        if db.check_for_pending_delete_ops_with_parent(
            session, odl_const.ODL_PORT, row.object_uuid):
            return False

        # Check that dependent floatingip delete operation has completed.
        if db.check_for_pending_delete_ops_with_parent(
                session, odl_const.ODL_FLOATINGIP, row.object_uuid):
            return False

        # Check that dependent router interface remove operation has completed.
        if db.check_for_pending_remove_ops_with_parent(
                session, row.object_uuid):
            return False

    return True


def validate_floatingip_operation(session, row):
    """Validate floatingip operation based on dependencies.

    Validate floating IP operation depending on whether it's dependencies
    are still in 'pending' or 'processing' state.
    """
    if row.operation in (odl_const.ODL_CREATE, odl_const.ODL_UPDATE):
        network_id = row.data.get('floating_network_id')
        if network_id is not None:
            if not db.check_for_pending_or_processing_ops(session, network_id):
                port_id = row.data.get('port_id')
                if port_id is not None:
                    if db.check_for_pending_or_processing_ops(session,
                                                              port_id):
                        return False
            else:
                return False

        router_id = row.data.get('router_id')
        if router_id is not None:
            if db.check_for_pending_or_processing_ops(session, router_id):
                return False
        if (row.operation == odl_const.ODL_UPDATE and
                not _is_valid_update_operation(session, row)):
            return False
    elif row.operation == odl_const.ODL_DELETE:
        # Check for any pending or processing create or update
        # ops on this uuid itself
        if db.check_for_pending_or_processing_ops(session, row.object_uuid,
                                                  [odl_const.ODL_UPDATE,
                                                   odl_const.ODL_CREATE]):
            return False

    return True


def validate_router_interface_operation(session, row):
    """Validate router_interface operation based on dependencies.

    Validate router_interface operation depending on whether it's dependencies
    are still in 'pending' or 'processing' state.
    """
    if row.operation == odl_const.ODL_ADD:
        # Verify that router event has been completed.
        if db.check_for_pending_or_processing_ops(session, row.data['id']):
            return False

        # TODO(rcurran): Check for port_id?
        if db.check_for_pending_or_processing_ops(session,
                                                  row.data['subnet_id']):
            return False
    elif row.operation == odl_const.ODL_REMOVE:
        if db.check_for_pending_or_processing_add(session, row.data['id'],
                                                  row.data['subnet_id']):
            return False

    return True


def validate_security_group_operation(session, row):
    """Validate security_group operation based on dependencies.

    Validate security_group operation depending on whether it's dependencies
    are still in 'pending' or 'processing' state. e.g.
    """
    return True


def validate_security_group_rule_operation(session, row):
    """Validate security_group_rule operation based on dependencies.

    Validate security_group_rule operation depending on whether it's
    dependencies are still in 'pending' or 'processing' state. e.g.
    """
    return True

_VALIDATION_MAP = {
    odl_const.ODL_NETWORK: validate_network_operation,
    odl_const.ODL_SUBNET: validate_subnet_operation,
    odl_const.ODL_PORT: validate_port_operation,
    odl_const.ODL_ROUTER: validate_router_operation,
    odl_const.ODL_ROUTER_INTF: validate_router_interface_operation,
    odl_const.ODL_FLOATINGIP: validate_floatingip_operation,
    odl_const.ODL_SG: validate_security_group_operation,
    odl_const.ODL_SG_RULE: validate_security_group_rule_operation,
}


def validate(session, row):
    """Validate resource dependency in journaled operations.

    :param session: db session
    :param row: entry in journal entry to be validated
    """
    return _VALIDATION_MAP[row.object_type](session, row)


def register_validator(object_type, validator):
    """Register validator function for given resource.

    :param object_type: neutron resource type
    :param validator: function to be registered which validates resource
         dependencies
    """
    assert object_type not in _VALIDATION_MAP
    _VALIDATION_MAP[object_type] = validator