File: sfc_flow_classifier.py

package info (click to toggle)
python-neutronclient 1%3A11.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 2,068 kB
  • sloc: python: 21,778; makefile: 17; sh: 10
file content (346 lines) | stat: -rwxr-xr-x 13,897 bytes parent folder | download | duplicates (2)
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# Copyright (c) 2017 Huawei Technologies India Pvt.Limited.
# 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 argparse
import logging

from osc_lib.command import command
from osc_lib import exceptions
from osc_lib import utils
from osc_lib.utils import columns as column_util

from neutronclient._i18n import _
from neutronclient.common import exceptions as nc_exc

LOG = logging.getLogger(__name__)

resource = 'flow_classifier'

_attr_map = (
    ('id', 'ID', column_util.LIST_BOTH),
    ('name', 'Name', column_util.LIST_BOTH),
    ('summary', 'Summary', column_util.LIST_SHORT_ONLY),
    ('protocol', 'Protocol', column_util.LIST_LONG_ONLY),
    ('ethertype', 'Ethertype', column_util.LIST_LONG_ONLY),
    ('source_ip_prefix', 'Source IP',
     column_util.LIST_LONG_ONLY),
    ('destination_ip_prefix', 'Destination IP',
     column_util.LIST_LONG_ONLY),
    ('logical_source_port', 'Logical Source Port',
     column_util.LIST_LONG_ONLY),
    ('logical_destination_port', 'Logical Destination Port',
     column_util.LIST_LONG_ONLY),
    ('source_port_range_min', 'Source Port Range Min',
     column_util.LIST_LONG_ONLY),
    ('source_port_range_max', 'Source Port Range Max',
     column_util.LIST_LONG_ONLY),
    ('destination_port_range_min', 'Destination Port Range Min',
     column_util.LIST_LONG_ONLY),
    ('destination_port_range_max', 'Destination Port Range Max',
     column_util.LIST_LONG_ONLY),
    ('l7_parameters', 'L7 Parameters', column_util.LIST_LONG_ONLY),
    ('description', 'Description', column_util.LIST_LONG_ONLY),
    ('project_id', 'Project', column_util.LIST_LONG_ONLY),
)

_attr_map_dict = {
    'id': 'ID',
    'name': 'Name',
    'description': 'Description',
    'summary': 'Summary',
    'protocol': 'Protocol',
    'ethertype': 'Ethertype',
    'source_ip_prefix': 'Source IP',
    'destination_ip_prefix': 'Destination IP',
    'logical_source_port': 'Logical Source Port',
    'logical_destination_port': 'Logical Destination Port',
    'source_port_range_min': 'Source Port Range Min',
    'source_port_range_max': 'Source Port Range Max',
    'destination_port_range_min': 'Destination Port Range Min',
    'destination_port_range_max': 'Destination Port Range Max',
    'l7_parameters': 'L7 Parameters',
    'tenant_id': 'Project',
    'project_id': 'Project',
}


class CreateSfcFlowClassifier(command.ShowOne):
    _description = _("Create a flow classifier")

    def get_parser(self, prog_name):
        parser = super(CreateSfcFlowClassifier, self).get_parser(prog_name)
        parser.add_argument(
            'name',
            metavar='<name>',
            help=_('Name of the flow classifier'))
        parser.add_argument(
            '--description',
            metavar='<description>',
            help=_('Description for the flow classifier'))
        parser.add_argument(
            '--protocol',
            metavar='<protocol>',
            help=_('IP protocol name. Protocol name should be as per '
                   'IANA standard.'))
        parser.add_argument(
            '--ethertype',
            metavar='{IPv4,IPv6}',
            default='IPv4', choices=['IPv4', 'IPv6'],
            help=_('L2 ethertype, default is IPv4'))
        parser.add_argument(
            '--source-port',
            metavar='<min-port>:<max-port>',
            help=_('Source protocol port (allowed range [1,65535]. Must be '
                   'specified as a:b, where a=min-port and b=max-port) '
                   'in the allowed range.'))
        parser.add_argument(
            '--destination-port',
            metavar='<min-port>:<max-port>',
            help=_('Destination protocol port (allowed range [1,65535]. Must '
                   'be specified as a:b, where a=min-port and b=max-port) '
                   'in the allowed range.'))
        parser.add_argument(
            '--source-ip-prefix',
            metavar='<source-ip-prefix>',
            help=_('Source IP address in CIDR notation'))
        parser.add_argument(
            '--destination-ip-prefix',
            metavar='<destination-ip-prefix>',
            help=_('Destination IP address in CIDR notation'))
        parser.add_argument(
            '--logical-source-port',
            metavar='<logical-source-port>',
            help=_('Neutron source port (name or ID)'))
        parser.add_argument(
            '--logical-destination-port',
            metavar='<logical-destination-port>',
            help=_('Neutron destination port (name or ID)'))
        parser.add_argument(
            '--l7-parameters',
            help=_('Dictionary of L7 parameters. Currently, no value is '
                   'supported for this option.'))
        return parser

    def take_action(self, parsed_args):
        client = self.app.client_manager.network
        attrs = _get_common_attrs(self.app.client_manager, parsed_args)
        obj = client.create_sfc_flow_classifier(**attrs)
        display_columns, columns = utils.get_osc_show_columns_for_sdk_resource(
            obj, _attr_map_dict, ['location', 'tenant_id', 'summary'])
        data = utils.get_dict_properties(obj, columns)
        return display_columns, data


class DeleteSfcFlowClassifier(command.Command):
    _description = _("Delete a given flow classifier")

    def get_parser(self, prog_name):
        parser = super(DeleteSfcFlowClassifier, self).get_parser(prog_name)
        parser.add_argument(
            'flow_classifier',
            metavar='<flow-classifier>',
            nargs='+',
            help=_("Flow classifier(s) to delete (name or ID)")
        )
        return parser

    def take_action(self, parsed_args):
        client = self.app.client_manager.network
        result = 0
        for fcl in parsed_args.flow_classifier:
            try:
                fc_id = client.find_sfc_flow_classifier(
                    fcl, ignore_missing=False)['id']
                client.delete_sfc_flow_classifier(fc_id)
            except Exception as e:
                result += 1
                LOG.error(_("Failed to delete flow classifier with name "
                            "or ID '%(fc)s': %(e)s"), {'fc': fcl, 'e': e})
        if result > 0:
            total = len(parsed_args.flow_classifier)
            msg = (_("%(result)s of %(total)s flow classifier(s) "
                     "failed to delete.") % {'result': result, 'total': total})
            raise exceptions.CommandError(msg)


class ListSfcFlowClassifier(command.Lister):
    _description = _("List flow classifiers")

    def get_parser(self, prog_name):
        parser = super(ListSfcFlowClassifier, self).get_parser(prog_name)
        parser.add_argument(
            '--long',
            action='store_true',
            help=_("List additional fields in output")
        )
        return parser

    def extend_list(self, data, parsed_args):
        ext_data = []
        for d in data:
            val = []
            protocol = d['protocol'].upper() if d['protocol'] else 'any'
            val.append('protocol: ' + protocol)
            val.append(self._get_protocol_port_details(d, 'source'))
            val.append(self._get_protocol_port_details(d, 'destination'))
            if 'logical_source_port' in d:
                val.append('neutron_source_port: ' +
                           str(d['logical_source_port']))

            if 'logical_destination_port' in d:
                val.append('neutron_destination_port: ' +
                           str(d['logical_destination_port']))

            if 'l7_parameters' in d:
                l7_param = 'l7_parameters: {%s}' % ','.join(d['l7_parameters'])
                val.append(l7_param)
            d['summary'] = ',\n'.join(val)
            ext_data.append(d)
        return ext_data

    def _get_protocol_port_details(self, data, val):
        type_ip_prefix = val + '_ip_prefix'
        ip_prefix = data.get(type_ip_prefix)
        if not ip_prefix:
            ip_prefix = 'any'
        min_port = data.get(val + '_port_range_min')
        if min_port is None:
            min_port = 'any'
        max_port = data.get(val + '_port_range_max')
        if max_port is None:
            max_port = 'any'
        return '%s[port]: %s[%s:%s]' % (
            val, ip_prefix, min_port, max_port)

    def take_action(self, parsed_args):
        client = self.app.client_manager.network
        obj = client.sfc_flow_classifiers()
        obj_extend = self.extend_list(obj, parsed_args)
        headers, columns = column_util.get_column_definitions(
            _attr_map, long_listing=parsed_args.long)
        return (headers, (utils.get_dict_properties(
            s, columns) for s in obj_extend))


class SetSfcFlowClassifier(command.Command):
    _description = _("Set flow classifier properties")

    def get_parser(self, prog_name):
        parser = super(SetSfcFlowClassifier, self).get_parser(prog_name)
        parser.add_argument(
            '--name',
            metavar='<name>',
            help=_('Name of the flow classifier'))
        parser.add_argument(
            '--description',
            metavar='<description>',
            help=_('Description for the flow classifier'))
        parser.add_argument(
            'flow_classifier',
            metavar='<flow-classifier>',
            help=_("Flow classifier to modify (name or ID)")
        )
        return parser

    def take_action(self, parsed_args):
        client = self.app.client_manager.network
        fc_id = client.find_sfc_flow_classifier(parsed_args.flow_classifier,
                                                ignore_missing=False)['id']
        attrs = _get_common_attrs(self.app.client_manager, parsed_args,
                                  is_create=False)
        try:
            client.update_sfc_flow_classifier(fc_id, **attrs)
        except Exception as e:
            msg = (_("Failed to update flow classifier '%(fc)s': %(e)s")
                   % {'fc': parsed_args.flow_classifier, 'e': e})
            raise exceptions.CommandError(msg)


class ShowSfcFlowClassifier(command.ShowOne):
    _description = _("Display flow classifier details")

    def get_parser(self, prog_name):
        parser = super(ShowSfcFlowClassifier, self).get_parser(prog_name)
        parser.add_argument(
            'flow_classifier',
            metavar='<flow-classifier>',
            help=_("Flow classifier to display (name or ID)")
        )
        return parser

    def take_action(self, parsed_args):
        client = self.app.client_manager.network
        fc_id = client.find_sfc_flow_classifier(parsed_args.flow_classifier,
                                                ignore_missing=False)['id']
        obj = client.get_sfc_flow_classifier(fc_id)
        display_columns, columns = utils.get_osc_show_columns_for_sdk_resource(
            obj, _attr_map_dict, ['location', 'tenant_id', 'summary'])
        data = utils.get_dict_properties(obj, columns)
        return display_columns, data


def _get_common_attrs(client_manager, parsed_args, is_create=True):
    attrs = {}
    if parsed_args.name is not None:
        attrs['name'] = parsed_args.name
    if parsed_args.description is not None:
        attrs['description'] = parsed_args.description
    if is_create:
        _get_attrs(client_manager, attrs, parsed_args)
    return attrs


def _get_attrs(client_manager, attrs, parsed_args):
    if parsed_args.protocol is not None:
        attrs['protocol'] = parsed_args.protocol
    if parsed_args.ethertype:
        attrs['ethertype'] = parsed_args.ethertype
    if parsed_args.source_ip_prefix is not None:
        attrs['source_ip_prefix'] = parsed_args.source_ip_prefix
    if parsed_args.destination_ip_prefix is not None:
        attrs['destination_ip_prefix'] = parsed_args.destination_ip_prefix
    if parsed_args.logical_source_port is not None:
        attrs['logical_source_port'] = client_manager.network.find_port(
            parsed_args.logical_source_port, ignore_missing=False
        )['id']
    if parsed_args.logical_destination_port is not None:
        attrs['logical_destination_port'] = client_manager.network.find_port(
            parsed_args.logical_destination_port, ignore_missing=False
        )['id']
    if parsed_args.source_port is not None:
        _fill_protocol_port_info(attrs, 'source',
                                        parsed_args.source_port)
    if parsed_args.destination_port is not None:
        _fill_protocol_port_info(attrs, 'destination',
                                        parsed_args.destination_port)
    if parsed_args.l7_parameters is not None:
        attrs['l7_parameters'] = parsed_args.l7_parameters


def _fill_protocol_port_info(attrs, port_type, port_val):
    min_port, sep, max_port = port_val.partition(":")
    if not min_port:
        msg = ("Invalid port value '%s', expected format is "
               "min-port:max-port or min-port.")
        raise argparse.ArgumentTypeError(msg % port_val)
    if not max_port:
        max_port = min_port
    try:
        attrs[port_type + '_port_range_min'] = int(min_port)
        attrs[port_type + '_port_range_max'] = int(max_port)
    except ValueError:
        message = (_("Protocol port value %s must be an integer "
                     "or integer:integer.") % port_val)
        raise nc_exc.CommandError(message=message)