summaryrefslogtreecommitdiff
path: root/src/conf_mode/vpp_interfaces_xconnect.py
blob: 29f2da5209b6fbdd5063c32827bfb480f7eb5af5 (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
#!/usr/bin/env python3
#
# Copyright (C) VyOS Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

from vyos.config import Config
from vyos import ConfigError
from vyos.configdict import get_interface_dict
from vyos.utils.process import is_systemd_service_active

from vyos.ifconfig.vpp import VPPXconnectInterface
from vyos.vpp.config_deps import deps_bond_dict
from vyos.vpp.config_deps import deps_bridge_dict
from vyos.vpp.config_deps import deps_xconnect_dict
from vyos.vpp.config_verify import verify_member_conflicts
from vyos.vpp.config_verify import verify_vpp_interface_not_in_feature
from vyos.vpp.utils import cli_ifaces_list


def get_config(config=None) -> dict:
    """Get Xconnect interface configuration

    Args:
        config (vyos.config.Config, optional): The VyOS configuration dictionary
    Returns:
        dict: Bridge interface configuration
    """
    if config:
        conf = config
    else:
        conf = Config()

    base = ['interfaces', 'vpp', 'xconnect']

    ifname, config = get_interface_dict(conf, base)

    if not conf.exists(['vpp']) and not conf.exists(base):
        config['remove_vpp'] = True
        return config

    # Get effective config as we need full dictionary per interface delete
    effective_config = conf.get_config_dict(
        base + [ifname],
        key_mangling=('-', '_'),
        effective=True,
        get_first_key=True,
        no_tag_node_value_mangle=True,
    )

    if effective_config:
        config.update({'effective': effective_config})

    config['bond_members'] = deps_bond_dict(conf)
    config['bridge_members'] = deps_bridge_dict(conf)
    config['xconn_members'] = deps_xconnect_dict(conf)
    config['vpp_ifaces'] = cli_ifaces_list(conf, 'candidate')

    # VPP config for member-in-feature checks
    config['vpp'] = conf.get_config_dict(
        ['vpp'],
        key_mangling=('-', '_'),
        get_first_key=True,
        no_tag_node_value_mangle=True,
    )

    return config


def verify(config):
    if 'deleted' in config or 'remove_vpp' in config:
        return None

    if not is_systemd_service_active('vpp.service'):
        raise ConfigError(
            'Cannot configure layer 2 cross-connect: vpp.service is not running'
        )

    # Xconnect requires 2 members
    if len(config.get('member', {}).get('interface')) != 2:
        raise ConfigError('Cross connect requires 2 members')

    not_allowed_prefixes = ('vppbond', 'vppbr', 'vpplo')
    for iface in config.get('member', {}).get('interface', []):
        # Ensure the interface is allowed as xconnect member
        if iface.startswith(not_allowed_prefixes):
            raise ConfigError(f'{iface} cannot be configured as xconnect member')
        # Member must belong to VPP
        if iface not in config['vpp_ifaces']:
            raise ConfigError(f'{iface} must be a VPP interface for xconnect')

        # Each interface can belong only to one xconnect
        xconn_members = config['xconn_members'][iface]
        if len(xconn_members) > 1:
            raise ConfigError(
                f'Interface {iface} added to more than one xconnect: {", ".join(xconn_members)}'
            )

        verify_member_conflicts(iface, config, 'xconn')
        verify_vpp_interface_not_in_feature(iface, config.get('vpp'))


def generate(config):
    pass


def apply(config):
    if 'remove_vpp' in config:
        return None

    ifname = config.get('ifname')
    xconnect = VPPXconnectInterface(ifname)

    # Delete xconnect
    if 'effective' in config:
        remove_config = config.get('effective')
        members = remove_config['member']['interface']
        xconnect.remove(members)

    if 'deleted' in config:
        return None

    # Add xconnect
    xconnect.update(config)

    return None


if __name__ == '__main__':
    try:
        c = get_config()
        verify(c)
        generate(c)
        apply(c)
    except ConfigError as e:
        print(e)
        exit(1)