File: plugin.py

package info (click to toggle)
networking-sfc 21.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,544 kB
  • sloc: python: 26,957; sh: 76; makefile: 24
file content (300 lines) | stat: -rw-r--r-- 13,292 bytes parent folder | download | duplicates (3)
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# Copyright 2015 Futurewei. 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 oslo_log import helpers as log_helpers
from oslo_log import log as logging
from oslo_utils import excutils

from neutron_lib.db import api as db_api

from networking_sfc.db import sfc_db
from networking_sfc.extensions import servicegraph as sg_ext
from networking_sfc.extensions import sfc as sfc_ext
from networking_sfc.extensions import tap as tap_ext
from networking_sfc.services.sfc.common import context as sfc_ctx
from networking_sfc.services.sfc.common import exceptions as sfc_exc
from networking_sfc.services.sfc import driver_manager as sfc_driver


LOG = logging.getLogger(__name__)


class SfcPlugin(sfc_db.SfcDbPlugin):
    """SFC plugin implementation."""

    # REVISIT(vks1) This should be changed to string instead of importing
    # extensions explicitly. So that even if extensions increase in future,
    # imports do not.
    supported_extension_aliases = [sfc_ext.SFC_EXT, sg_ext.SG_EXT,
                                   tap_ext.TAP_EXT]
    path_prefix = sfc_ext.SFC_PREFIX

    def __init__(self):
        self.driver_manager = sfc_driver.SfcDriverManager()
        super().__init__()
        self.driver_manager.initialize()

    @log_helpers.log_method_call
    def create_port_chain(self, context, port_chain):
        with db_api.CONTEXT_WRITER.using(context):
            port_chain_db = super().create_port_chain(
                context, port_chain)
            portchain_db_context = sfc_ctx.PortChainContext(
                self, context, port_chain_db)
            self.driver_manager.create_port_chain_precommit(
                portchain_db_context)
        try:
            self.driver_manager.create_port_chain_postcommit(
                portchain_db_context)
        except sfc_exc.SfcDriverError as e:
            LOG.exception(e)
            with excutils.save_and_reraise_exception():
                LOG.error("Create port chain failed, "
                          "deleting port_chain '%s'",
                          port_chain_db['id'])
                self.delete_port_chain(context, port_chain_db['id'])

        return port_chain_db

    @log_helpers.log_method_call
    def update_port_chain(self, context, id, port_chain):
        with db_api.CONTEXT_WRITER.using(context):
            original_portchain = self.get_port_chain(context, id)
            updated_portchain = super().update_port_chain(
                context, id, port_chain)
            portchain_db_context = sfc_ctx.PortChainContext(
                self, context, updated_portchain,
                original_portchain=original_portchain)
            self.driver_manager.update_port_chain_precommit(
                portchain_db_context)

        try:
            self.driver_manager.update_port_chain_postcommit(
                portchain_db_context)
        except sfc_exc.SfcDriverError as e:
            LOG.exception(e)
            with excutils.save_and_reraise_exception():
                LOG.error("Update port chain failed, port_chain '%s'",
                          updated_portchain['id'])

        # TODO(qijing): should we rollback the database update here?
        return updated_portchain

    @log_helpers.log_method_call
    def delete_port_chain(self, context, id):
        pc = self.get_port_chain(context, id)
        pc_context = sfc_ctx.PortChainContext(self, context, pc)
        try:
            self.driver_manager.delete_port_chain(pc_context)
        except sfc_exc.SfcDriverError as e:
            LOG.exception(e)
            with excutils.save_and_reraise_exception():
                LOG.error("Delete port chain failed, portchain '%s'", id)

        # TODO(qijing): unsync in case deleted in driver but fail in database
        with db_api.CONTEXT_WRITER.using(context):
            pc = self.get_port_chain(context, id)
            pc_context = sfc_ctx.PortChainContext(self, context, pc)
            super().delete_port_chain(context, id)
            self.driver_manager.delete_port_chain_precommit(pc_context)
        self.driver_manager.delete_port_chain_postcommit(pc_context)

    @log_helpers.log_method_call
    def create_port_pair(self, context, port_pair):
        with db_api.CONTEXT_WRITER.using(context):
            portpair_db = super().create_port_pair(
                context, port_pair)
            portpair_context = sfc_ctx.PortPairContext(
                self, context, portpair_db)
            self.driver_manager.create_port_pair_precommit(portpair_context)

        try:
            self.driver_manager.create_port_pair_postcommit(portpair_context)
        except sfc_exc.SfcDriverError as e:
            LOG.exception(e)
            with excutils.save_and_reraise_exception():
                LOG.error("Create port pair failed, "
                          "deleting port_pair '%s'",
                          portpair_db['id'])
                self.delete_port_pair(context, portpair_db['id'])

        return portpair_db

    @log_helpers.log_method_call
    def update_port_pair(self, context, id, port_pair):
        with db_api.CONTEXT_WRITER.using(context):
            original_portpair = self.get_port_pair(context, id)
            updated_portpair = super().update_port_pair(
                context, id, port_pair)
            portpair_context = sfc_ctx.PortPairContext(
                self, context, updated_portpair,
                original_portpair=original_portpair)
            self.driver_manager.update_port_pair_precommit(portpair_context)
        try:
            self.driver_manager.update_port_pair_postcommit(portpair_context)
        except sfc_exc.SfcDriverError as e:
            LOG.exception(e)
            with excutils.save_and_reraise_exception():
                LOG.error("Update port pair failed, port_pair '%s'",
                          updated_portpair['id'])

        return updated_portpair

    @log_helpers.log_method_call
    def delete_port_pair(self, context, id):
        portpair = self.get_port_pair(context, id)
        portpair_context = sfc_ctx.PortPairContext(
            self, context, portpair)
        try:
            self.driver_manager.delete_port_pair(portpair_context)
        except sfc_exc.SfcDriverError as e:
            LOG.exception(e)
            with excutils.save_and_reraise_exception():
                LOG.error("Delete port pair failed, port_pair '%s'", id)

        with db_api.CONTEXT_WRITER.using(context):
            portpair = self.get_port_pair(context, id)
            portpair_context = sfc_ctx.PortPairContext(
                self, context, portpair)
            super().delete_port_pair(context, id)
            self.driver_manager.delete_port_pair_precommit(portpair_context)
        self.driver_manager.delete_port_pair_postcommit(portpair_context)

    @log_helpers.log_method_call
    def create_port_pair_group(self, context, port_pair_group):
        with db_api.CONTEXT_WRITER.using(context):
            portpairgroup_db = super().create_port_pair_group(
                context, port_pair_group)
            portpairgroup_context = sfc_ctx.PortPairGroupContext(
                self, context, portpairgroup_db)
            self.driver_manager.create_port_pair_group_precommit(
                portpairgroup_context)
        try:
            self.driver_manager.create_port_pair_group_postcommit(
                portpairgroup_context)
        except sfc_exc.SfcDriverError as e:
            LOG.exception(e)
            with excutils.save_and_reraise_exception():
                LOG.error("Create port pair group failed, "
                          "deleting port_pair_group '%s'",
                          portpairgroup_db['id'])
                self.delete_port_pair_group(context, portpairgroup_db['id'])

        return portpairgroup_db

    @log_helpers.log_method_call
    def update_port_pair_group(self, context, id, port_pair_group):
        with db_api.CONTEXT_WRITER.using(context):
            original_portpairgroup = self.get_port_pair_group(
                context, id)
            updated_portpairgroup = super().update_port_pair_group(
                context, id, port_pair_group)
            portpairgroup_context = sfc_ctx.PortPairGroupContext(
                self, context, updated_portpairgroup,
                original_portpairgroup=original_portpairgroup)
            self.driver_manager.update_port_pair_group_precommit(
                portpairgroup_context)
        try:
            self.driver_manager.update_port_pair_group_postcommit(
                portpairgroup_context)
        except sfc_exc.SfcDriverError as e:
            LOG.exception(e)
            with excutils.save_and_reraise_exception():
                LOG.error("Update port pair group failed, "
                          "port_pair_group '%s'",
                          updated_portpairgroup['id'])

        return updated_portpairgroup

    @log_helpers.log_method_call
    def delete_port_pair_group(self, context, id):
        portpairgroup = self.get_port_pair_group(context, id)
        portpairgroup_context = sfc_ctx.PortPairGroupContext(
            self, context, portpairgroup)
        try:
            self.driver_manager.delete_port_pair_group(portpairgroup_context)
        except sfc_exc.SfcDriverError as e:
            LOG.exception(e)
            with excutils.save_and_reraise_exception():
                LOG.error("Delete port pair group failed, "
                          "port_pair_group '%s'",
                          id)

        with db_api.CONTEXT_WRITER.using(context):
            portpairgroup = self.get_port_pair_group(context, id)
            portpairgroup_context = sfc_ctx.PortPairGroupContext(
                self, context, portpairgroup)
            super().delete_port_pair_group(context, id)
            self.driver_manager.delete_port_pair_group_precommit(
                portpairgroup_context)
        self.driver_manager.delete_port_pair_group_postcommit(
            portpairgroup_context)

    @log_helpers.log_method_call
    def create_service_graph(self, context, service_graph):
        with db_api.CONTEXT_WRITER.using(context):
            service_graph_db = super().create_service_graph(
                context, service_graph)
            service_graph_db_context = sfc_ctx.ServiceGraphContext(
                self, context, service_graph_db)
            self.driver_manager.create_service_graph_precommit(
                service_graph_db_context)
        try:
            self.driver_manager.create_service_graph_postcommit(
                service_graph_db_context)
        except sfc_exc.SfcDriverError as e:
            LOG.exception(e)
            with excutils.save_and_reraise_exception():
                LOG.error("Create Service Graph failed, "
                          "deleting Service Graph '%s'",
                          service_graph_db['id'])
                self.delete_service_graph(context, service_graph_db['id'])

        return service_graph_db

    @log_helpers.log_method_call
    def update_service_graph(self, context, id, service_graph):
        with db_api.CONTEXT_WRITER.using(context):
            original_graph = self.get_service_graph(context, id)
            updated_graph = super().update_service_graph(
                context, id, service_graph)
            service_graph_db_context = sfc_ctx.ServiceGraphContext(
                self, context, updated_graph,
                original_graph=original_graph)
            self.driver_manager.update_service_graph_precommit(
                service_graph_db_context)
        try:
            self.driver_manager.update_service_graph_postcommit(
                service_graph_db_context)
        except sfc_exc.SfcDriverError:
            with excutils.save_and_reraise_exception():
                LOG.error("Update failed, service_graph '%s'",
                          updated_graph['id'])
        return updated_graph

    @log_helpers.log_method_call
    def delete_service_graph(self, context, id):
        graph = self.get_service_graph(context, id)
        graph_context = sfc_ctx.ServiceGraphContext(self, context, graph)
        with db_api.CONTEXT_WRITER.using(context):
            graph = self.get_service_graph(context, id)
            graph_context = sfc_ctx.ServiceGraphContext(self, context, graph)
            super().delete_service_graph(context, id)
            self.driver_manager.delete_service_graph_precommit(graph_context)
        try:
            self.driver_manager.delete_service_graph_postcommit(graph_context)
        except sfc_exc.SfcDriverError as e:
            LOG.exception(e)
            with excutils.save_and_reraise_exception():
                LOG.error("Delete failed, service_graph '%s'", id)