Blame libnmstate/validator.py

Packit Service 0535c1
#
Packit Service 0535c1
# Copyright (c) 2018-2020 Red Hat, Inc.
Packit Service 0535c1
#
Packit Service 0535c1
# This file is part of nmstate
Packit Service 0535c1
#
Packit Service 0535c1
# This program is free software: you can redistribute it and/or modify
Packit Service 0535c1
# it under the terms of the GNU Lesser General Public License as published by
Packit Service 0535c1
# the Free Software Foundation, either version 2.1 of the License, or
Packit Service 0535c1
# (at your option) any later version.
Packit Service 0535c1
#
Packit Service 0535c1
# This program is distributed in the hope that it will be useful,
Packit Service 0535c1
# but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 0535c1
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service 0535c1
# GNU Lesser General Public License for more details.
Packit Service 0535c1
#
Packit Service 0535c1
# You should have received a copy of the GNU Lesser General Public License
Packit Service 0535c1
# along with this program. If not, see <https://www.gnu.org/licenses/>.
Packit Service 0535c1
#
Packit Service 0535c1
Packit Service 0535c1
import copy
Packit Service 0535c1
import logging
Packit Service 0535c1
Packit Service 0535c1
import jsonschema as js
Packit Service 0535c1
Packit Service 0535c1
from libnmstate.ifaces.ovs import is_ovs_running
Packit Service 0535c1
from libnmstate.schema import Interface
Packit Service 0535c1
from libnmstate.schema import InterfaceType
Packit Service 0535c1
from libnmstate.error import NmstateDependencyError
Packit Service 0535c1
Packit Service 0535c1
from . import schema
Packit Service 0535c1
from .plugin import NmstatePlugin
Packit Service 0535c1
Packit Service 0535c1
MAX_SUPPORTED_INTERFACES = 1000
Packit Service 0535c1
Packit Service 0535c1
Packit Service 0535c1
def schema_validate(data, validation_schema=schema.ifaces_schema):
Packit Service 0535c1
    data = copy.deepcopy(data)
Packit Service 0535c1
    _validate_max_supported_intface_count(data)
Packit Service 0535c1
    for ifstate in data.get(schema.Interface.KEY, ()):
Packit Service 0535c1
        if not ifstate.get(schema.Interface.TYPE):
Packit Service 0535c1
            ifstate[schema.Interface.TYPE] = schema.InterfaceType.UNKNOWN
Packit Service 0535c1
    js.validate(data, validation_schema)
Packit Service 0535c1
Packit Service 0535c1
Packit Service 0535c1
def validate_capabilities(state, capabilities):
Packit Service 0535c1
    validate_interface_capabilities(state.get(Interface.KEY, []), capabilities)
Packit Service 0535c1
Packit Service 0535c1
Packit Service 0535c1
def validate_interface_capabilities(ifaces_state, capabilities):
Packit Service 0535c1
    ifaces_types = {iface_state.get("type") for iface_state in ifaces_state}
Packit Service 0535c1
    has_ovs_capability = NmstatePlugin.OVS_CAPABILITY in capabilities
Packit Service 0535c1
    has_team_capability = NmstatePlugin.TEAM_CAPABILITY in capabilities
Packit Service 0535c1
    ovs_is_running = is_ovs_running()
Packit Service 0535c1
    for iface_type in ifaces_types:
Packit Service 0535c1
        is_ovs_type = iface_type in (
Packit Service 0535c1
            InterfaceType.OVS_BRIDGE,
Packit Service 0535c1
            InterfaceType.OVS_INTERFACE,
Packit Service 0535c1
            InterfaceType.OVS_PORT,
Packit Service 0535c1
        )
Packit Service 0535c1
        if is_ovs_type and not has_ovs_capability:
Packit Service 0535c1
            if not ovs_is_running:
Packit Service 0535c1
                raise NmstateDependencyError(
Packit Service 0535c1
                    "openvswitch service is not started."
Packit Service 0535c1
                )
Packit Service 0535c1
            else:
Packit Service 0535c1
                raise NmstateDependencyError(
Packit Service 0535c1
                    "Open vSwitch NetworkManager support not installed "
Packit Service 0535c1
                    "and started"
Packit Service 0535c1
                )
Packit Service 0535c1
        elif iface_type == InterfaceType.TEAM and not has_team_capability:
Packit Service 0535c1
            raise NmstateDependencyError(
Packit Service 0535c1
                "NetworkManager-team plugin not installed and started"
Packit Service 0535c1
            )
Packit Service 0535c1
Packit Service 0535c1
Packit Service 0535c1
def _validate_max_supported_intface_count(data):
Packit Service 0535c1
    """
Packit Service 0535c1
    Raises warning if the interfaces count in the single desired state
Packit Service 0535c1
    exceeds the limit specified in MAX_SUPPORTED_INTERFACES
Packit Service 0535c1
    """
Packit Service 0535c1
    num_of_interfaces = len(
Packit Service 0535c1
        [intface for intface in data.get(schema.Interface.KEY, ())]
Packit Service 0535c1
    )
Packit Service 0535c1
    if num_of_interfaces > MAX_SUPPORTED_INTERFACES:
Packit Service 0535c1
        logging.warning(
Packit Service 0535c1
            "Interfaces count exceeds the limit %s in desired state",
Packit Service 0535c1
            MAX_SUPPORTED_INTERFACES,
Packit Service 0535c1
        )