# Copyright 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/>.

# T6989:
#   - remove syslog arbitrary file logging
#   - remove syslog user console logging
#   - move "global preserve-fqdn" one CLI level up
#   - rename "host" to "remote"
#
# T8059:
#   - if a syslog remote contains a port (like 192.0.2.1:9000),
#     migrate the port part to the new dedicated "port" option
#
# XXX: this script leads to data loss when a config
# has multiple remotes with the same host address but different ports.
# That issue needs to be addressed separately (T8058)

import re

from vyos.configtree import ConfigTree

base = ['system', 'syslog']

def migrate(config: ConfigTree) -> None:
    if not config.exists(base):
        return
    # Drop support for custom file logging
    if config.exists(base + ['file']):
        config.delete(base + ['file'])

    # Drop support for logging to a user tty
    # This should be dynamically added via an op-mode command like "terminal monitor"
    if config.exists(base + ['user']):
        config.delete(base + ['user'])

    # Move "global preserve-fqdn" one CLI level up, as it relates to all
    # logging targets (console, global and remote)
    preserve_fqdn_base = base + ['global', 'preserve-fqdn']
    if config.exists(preserve_fqdn_base):
        config.delete(preserve_fqdn_base)
        config.set(base + ['preserve-fqdn'])

    # Move "global marker" one CLI level up, as it relates to all
    # logging targets (console, global and remote)
    marker_base = base + ['global', 'marker']
    if config.exists(marker_base):
        config.copy(marker_base, base + ['marker'])
        config.delete(marker_base)

    # Rename "global" -> "local" as this describes what is logged locally
    # on the router to a file on the filesystem
    if config.exists(base + ['global']):
        config.rename(base + ['global'], 'local')

    vrf = ''
    if config.exists(base + ['vrf']):
        vrf = config.return_value(base + ['vrf'])
        config.delete(base + ['vrf'])

    # Rename host x.x.x.x -> remote x.x.x.x
    if config.exists(base + ['host']):
        config.set(base + ['remote'])
        config.set_tag(base + ['remote'])
        for remote in config.list_nodes(base + ['host']):
            # Check if the host address has a port
            # to migrate the port part to the new dedicated "port" option
            res = re.match(r'(?P<host>[^:]+):(?P<port>.*)', remote)
            if res:
                remote_host = res.group('host')
                remote_port = res.group('port')
            else:
                remote_host = remote
                remote_port = None

            # XXX: the fact that it was possible to use node names like "192.0.2.1:9000"
            # made it possible to create configurations that would send different messages
            # to different ports on the same server.
            # At the moment, such configurations are unsupported
            # so the script only keeps one of such remotes.
            if config.exists(base + ['remote', remote_host]):
                # Skip the remote if it already exists
                # XXX: This tacitly implies that if multiple remotes
                # with the same address but different ports are used,
                # only the first one of those makes it into the migrated config.
                continue
            else:
                config.copy(base + ['host', remote], base + ['remote', remote_host])

                if remote_port:
                    config.set(base + ['remote', remote_host, 'port'], value=remote_port)

                config.set_tag(base + ['remote'])
                if vrf:
                    config.set(base + ['remote', remote_host, 'vrf'], value=vrf)

        config.delete(base + ['host'])
