summaryrefslogtreecommitdiff
path: root/src/migration-scripts/flow-accounting/1-to-2
blob: 5ffb1eec891c7b9ff7b122844f6a8cb4b70fa05a (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
# Copyright 2021-2024 VyOS maintainers and contributors <maintainers@vyos.io>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library.  If not, see <http://www.gnu.org/licenses/>.

# migrate 'system flow-accounting sflow' to 'system sflow'

from vyos.configtree import ConfigTree

base = ['system', 'flow-accounting']
base_fa_sflow = base + ['sflow']
base_sflow = ['system', 'sflow']

def migrate(config: ConfigTree) -> None:
    if not config.exists(base_fa_sflow):
        # Nothing to do
        return

    if not config.exists(base_sflow):

        for iface in config.return_values(base + ['interface']):
            config.set(base_sflow + ['interface'], value=iface, replace=False)

        if config.exists(base + ['vrf']):
            vrf = config.return_value(base + ['vrf'])
            config.set(base_sflow + ['vrf'], value=vrf)

        if config.exists(base + ['enable-egress']):
            config.set(base_sflow + ['enable-egress'])

        if config.exists(base_fa_sflow + ['agent-address']):
            address = config.return_value(base_fa_sflow + ['agent-address'])
            config.set(base_sflow + ['agent-address'], value=address)

        if config.exists(base_fa_sflow + ['sampling-rate']):
            sr = config.return_value(base_fa_sflow + ['sampling-rate'])
            config.set(base_sflow + ['sampling-rate'], value=sr)

        for server in config.list_nodes(base_fa_sflow + ['server']):
            config.set(base_sflow + ['server'])
            config.set_tag(base_sflow + ['server'])
            config.set(base_sflow + ['server', server])
            tmp = base_fa_sflow + ['server', server]
            if config.exists(tmp + ['port']):
                port = config.return_value(tmp + ['port'])
                config.set(base_sflow + ['server', server, 'port'], value=port)

    if config.exists(base + ['netflow']):
        # delete only sflow from flow-accounting if netflow is set
        config.delete(base_fa_sflow)
    else:
        # delete all flow-accounting config otherwise
        config.delete(base)