File: _l2_gateway.py

package info (click to toggle)
networking-l2gw 1%3A13.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,304 kB
  • sloc: python: 12,230; sh: 316; makefile: 31
file content (160 lines) | stat: -rw-r--r-- 5,383 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
# Copyright 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 neutronclient.common import extension
from neutronclient.common import utils
from oslo_serialization import jsonutils

from networking_l2gw._i18n import _

INTERFACE_DELIMITER = ";"
SEGMENTATION_ID_DELIMITER = "#"
INTERFACE_SEG_ID_DELIMITER = "|"


def _format_devices(l2_gateway):
    try:
        return '\n'.join([jsonutils.dumps(gateway) for gateway in
                          l2_gateway['devices']])
    except (TypeError, KeyError):
        return ''


class L2Gateway(extension.NeutronClientExtension):
    resource = 'l2_gateway'
    resource_plural = 'l2_gateways'
    path = 'l2-gateways'
    object_path = '/%s' % path
    resource_path = '/%s/%%s' % path
    versions = ['2.0']


def get_interface(interfaces):
    interface_dict = []
    for interface in interfaces:
        if INTERFACE_SEG_ID_DELIMITER in interface:
            int_name = interface.split(INTERFACE_SEG_ID_DELIMITER)[0]
            segid = interface.split(INTERFACE_SEG_ID_DELIMITER)[1]
            if SEGMENTATION_ID_DELIMITER in segid:
                segid = segid.split(SEGMENTATION_ID_DELIMITER)
            else:
                segid = [segid]
            interface_detail = {'name': int_name, 'segmentation_id': segid}
        else:
            interface_detail = {'name': interface}
        interface_dict.append(interface_detail)
    return interface_dict


def add_known_arguments(self, parser):
    parser.add_argument(
        '--device',
        metavar='name=name,interface_names=INTERFACE-DETAILS',
        action='append', dest='devices', type=utils.str2dict,
        help=_('Device name and Interface-names of l2gateway. '
               'INTERFACE-DETAILS is of form '
               '\"<interface_name1>;[<interface_name2>]'
               '[|<seg_id1>[#<seg_id2>]]\" '
               '(--device option can be repeated)'))


def args2body(self, parsed_args):
        if parsed_args.devices:
            devices = parsed_args.devices
            interfaces = []
        else:
            devices = []
        device_dict = []
        for device in devices:
            if 'interface_names' in device.keys():
                interface = device['interface_names']
                if INTERFACE_DELIMITER in interface:
                    interface_dict = interface.split(INTERFACE_DELIMITER)
                    interfaces = get_interface(interface_dict)
                else:
                    interfaces = get_interface([interface])
            if 'name' in device.keys():
                device = {'device_name': device['name'],
                          'interfaces': interfaces}
            else:
                device = {'interfaces': interfaces}
            device_dict.append(device)
        if parsed_args.name:
            l2gw_name = parsed_args.name
            body = {'l2_gateway': {'name': l2gw_name,
                                   'devices': device_dict}, }
        else:
            body = {'l2_gateway': {'devices': device_dict}, }
        return body


class L2GatewayCreate(extension.ClientExtensionCreate, L2Gateway):
    """Create l2gateway information."""

    shell_command = 'l2-gateway-create'

    def add_known_arguments(self, parser):
        parser.add_argument(
            'name', metavar='<GATEWAY-NAME>',
            help=_('Descriptive name for logical gateway.'))
        add_known_arguments(self, parser)

    def args2body(self, parsed_args):
        body = args2body(self, parsed_args)
        if parsed_args.tenant_id:
            body['l2_gateway']['tenant_id'] = parsed_args.tenant_id
        return body


class L2GatewayList(extension.ClientExtensionList, L2Gateway):
    """List l2gateway that belongs to a given tenant."""

    shell_command = 'l2-gateway-list'
    _formatters = {'devices': _format_devices, }
    list_columns = ['id', 'name', 'devices']
    pagination_support = True
    sorting_support = True


class L2GatewayShow(extension.ClientExtensionShow, L2Gateway):
    """Show information of a given l2gateway."""

    shell_command = 'l2-gateway-show'


class L2GatewayDelete(extension.ClientExtensionDelete, L2Gateway):
    """Delete a given l2gateway."""

    shell_command = 'l2-gateway-delete'


class L2GatewayUpdate(extension.ClientExtensionUpdate, L2Gateway):
    """Update a given l2gateway."""

    shell_command = 'l2-gateway-update'

    def add_known_arguments(self, parser):
        parser.add_argument(
            '--name', metavar='name',
            help=_('Descriptive name for logical gateway.'))
        add_known_arguments(self, parser)

    def args2body(self, parsed_args):
        if parsed_args.devices:
            body = args2body(self, parsed_args)
        else:
            body = {'l2_gateway': {'name': parsed_args.name}}
        return body