summaryrefslogtreecommitdiffstats
path: root/networking_sfc/services/flowclassifier/plugin.py
blob: 692e1d8ad975dcc9823fe195c9b8c248ab84b958 (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
# 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.i18n import _LE
from neutron import manager

from networking_sfc.db import flowclassifier_db as fc_db
from networking_sfc.extensions import flowclassifier as fc_ext
from networking_sfc.services.flowclassifier.common import context as fc_ctx
from networking_sfc.services.flowclassifier.common import exceptions as fc_exc
from networking_sfc.services.flowclassifier import driver_manager as fc_driver


LOG = logging.getLogger(__name__)


class FlowClassifierPlugin(fc_db.FlowClassifierDbPlugin):

    """Implementation of the Plugin."""
    supported_extension_aliases = [fc_ext.FLOW_CLASSIFIER_EXT]
    path_prefix = fc_ext.FLOW_CLASSIFIER_PREFIX

    def __init__(self):
        self.driver_manager = fc_driver.FlowClassifierDriverManager()
        super(FlowClassifierPlugin, self).__init__()
        self.driver_manager.initialize()

    def _get_port(self, context, id):
        port = super(FlowClassifierPlugin, self)._get_port(context, id)
        core_plugin = manager.NeutronManager.get_plugin()
        return core_plugin.get_port(context, port['id'])

    def _get_fixed_ip_from_port(self, context, logical_port, ip_prefix):
        if logical_port is not None:
            port = self._get_port(context, logical_port)
            if (
                ip_prefix is None and
                'fixed_ips' in port and
                port['fixed_ips']
            ):
                for fixed_ip in port['fixed_ips']:
                    ip_prefix = (
                        '%s/32' % fixed_ip['ip_address']
                    )
                    break
        return ip_prefix

    @log_helpers.log_method_call
    def create_flow_classifier(self, context, flow_classifier):
        fc_db = super(FlowClassifierPlugin, self).create_flow_classifier(
            context, flow_classifier)
        fc_db_context = fc_ctx.FlowClassifierContext(self, context, fc_db)
        try:
            self.driver_manager.create_flow_classifier(fc_db_context)
        except fc_exc.FlowClassifierDriverError as e:
            LOG.exception(e)
            with excutils.save_and_reraise_exception():
                LOG.error(_LE("Create flow classifier failed, "
                              "deleting flow_classifier '%s'"),
                          fc_db['id'])
                self.delete_flow_classifier(context, fc_db['id'])
        return fc_db

    @log_helpers.log_method_call
    def update_flow_classifier(self, context, id, flow_classifier):
        original_flowclassifier = self.get_flow_classifier(context, id)
        updated_fc = super(FlowClassifierPlugin, self).update_flow_classifier(
            context, id, flow_classifier)
        fc_db_context = fc_ctx.FlowClassifierContext(
            self, context, updated_fc,
            original_flowclassifier=original_flowclassifier)

        try:
            self.driver_manager.update_flow_classifier(fc_db_context)
        except fc_exc.FlowClassifierDriverError as e:
            LOG.exception(e)
            with excutils.save_and_reraise_exception():
                LOG.error(_LE("Update flow classifier failed, "
                              "flow_classifier '%s'"),
                          updated_fc['id'])

        return updated_fc

    @log_helpers.log_method_call
    def delete_flow_classifier(self, context, fc_id):
        fc = self.get_flow_classifier(context, fc_id)
        fc_context = fc_ctx.FlowClassifierContext(self, context, fc)
        try:
            self.driver_manager.delete_flow_classifier(fc_context)
        except fc_exc.FlowClassfierDriverError as e:
            LOG.exception(e)
            with excutils.save_and_reraise_exception():
                LOG.error(_LE("Delete port pair group failed, "
                              "flow_classifier '%s'"),
                          fc_id)

        super(FlowClassifierPlugin, self).delete_flow_classifier(
            context, fc_id)