|
Packit Service |
0535c1 |
#
|
|
Packit Service |
0535c1 |
# Copyright (c) 2018-2019 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 |
|
|
Packit Service |
0535c1 |
from .common import NM
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
IFACE_TYPE_UNKNOWN = "unknown"
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
class ApiIfaceAdminState:
|
|
Packit Service |
0535c1 |
DOWN = "down"
|
|
Packit Service |
0535c1 |
UP = "up"
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
class Api2Nm:
|
|
Packit Service |
0535c1 |
_iface_types_map = None
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
@staticmethod
|
|
Packit Service |
0535c1 |
def get_iface_type(name):
|
|
Packit Service |
0535c1 |
return Api2Nm.get_iface_type_map().get(name, IFACE_TYPE_UNKNOWN)
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
@staticmethod
|
|
Packit Service |
0535c1 |
def get_iface_type_map():
|
|
Packit Service |
0535c1 |
if Api2Nm._iface_types_map is None:
|
|
Packit Service |
0535c1 |
Api2Nm._iface_types_map = {
|
|
Packit Service |
0535c1 |
"ethernet": NM.SETTING_WIRED_SETTING_NAME,
|
|
Packit Service |
0535c1 |
"bond": NM.SETTING_BOND_SETTING_NAME,
|
|
Packit Service |
0535c1 |
"dummy": NM.SETTING_DUMMY_SETTING_NAME,
|
|
Packit Service |
0535c1 |
"team": NM.SETTING_TEAM_SETTING_NAME,
|
|
Packit Service |
0535c1 |
"vlan": NM.SETTING_VLAN_SETTING_NAME,
|
|
Packit Service |
0535c1 |
"vxlan": NM.SETTING_VXLAN_SETTING_NAME,
|
|
Packit Service |
0535c1 |
"linux-bridge": NM.SETTING_BRIDGE_SETTING_NAME,
|
|
Packit Service |
0535c1 |
}
|
|
Packit Service |
0535c1 |
try:
|
|
Packit Service |
0535c1 |
ovs_types = {
|
|
Packit Service |
0535c1 |
"ovs-bridge": NM.SETTING_OVS_BRIDGE_SETTING_NAME,
|
|
Packit Service |
0535c1 |
"ovs-port": NM.SETTING_OVS_PORT_SETTING_NAME,
|
|
Packit Service |
0535c1 |
"ovs-interface": NM.SETTING_OVS_INTERFACE_SETTING_NAME,
|
|
Packit Service |
0535c1 |
}
|
|
Packit Service |
0535c1 |
Api2Nm._iface_types_map.update(ovs_types)
|
|
Packit Service |
0535c1 |
except AttributeError:
|
|
Packit Service |
0535c1 |
pass
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
return Api2Nm._iface_types_map
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
@staticmethod
|
|
Packit Service |
0535c1 |
def get_bond_options(iface_desired_state):
|
|
Packit Service |
0535c1 |
iface_type = Api2Nm.get_iface_type(iface_desired_state["type"])
|
|
Packit Service |
0535c1 |
if iface_type == "bond":
|
|
Packit Service |
0535c1 |
# Is the mode a must config parameter?
|
|
Packit Service |
0535c1 |
bond_conf = iface_desired_state["link-aggregation"]
|
|
Packit Service |
0535c1 |
bond_opts = {"mode": bond_conf["mode"]}
|
|
Packit Service |
0535c1 |
bond_opts.update(bond_conf.get("options", {}))
|
|
Packit Service |
0535c1 |
else:
|
|
Packit Service |
0535c1 |
bond_opts = {}
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
return bond_opts
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
class Nm2Api:
|
|
Packit Service |
0535c1 |
_iface_types_map = None
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
@staticmethod
|
|
Packit Service |
0535c1 |
def get_common_device_info(devinfo):
|
|
Packit Service |
0535c1 |
type_name = devinfo["type_name"]
|
|
Packit Service |
0535c1 |
if type_name != "ethernet":
|
|
Packit Service |
0535c1 |
type_name = Nm2Api.get_iface_type(type_name)
|
|
Packit Service |
0535c1 |
return {
|
|
Packit Service |
0535c1 |
"name": devinfo["name"],
|
|
Packit Service |
0535c1 |
"type": type_name,
|
|
Packit Service |
0535c1 |
"state": Nm2Api.get_iface_admin_state(devinfo["state"]),
|
|
Packit Service |
0535c1 |
}
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
@staticmethod
|
|
Packit Service |
0535c1 |
def get_bond_info(bondinfo):
|
|
Packit Service |
0535c1 |
bond_options = copy.deepcopy(bondinfo.get("options"))
|
|
Packit Service |
0535c1 |
if not bond_options:
|
|
Packit Service |
0535c1 |
return {}
|
|
Packit Service |
0535c1 |
bond_slaves = bondinfo["slaves"]
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
bond_mode = bond_options["mode"]
|
|
Packit Service |
0535c1 |
del bond_options["mode"]
|
|
Packit Service |
0535c1 |
return {
|
|
Packit Service |
0535c1 |
"link-aggregation": {
|
|
Packit Service |
0535c1 |
"mode": bond_mode,
|
|
Packit Service |
0535c1 |
"slaves": [slave.props.interface for slave in bond_slaves],
|
|
Packit Service |
0535c1 |
"options": bond_options,
|
|
Packit Service |
0535c1 |
}
|
|
Packit Service |
0535c1 |
}
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
@staticmethod
|
|
Packit Service |
0535c1 |
def get_iface_type(name):
|
|
Packit Service |
0535c1 |
if Nm2Api._iface_types_map is None:
|
|
Packit Service |
0535c1 |
Nm2Api._iface_types_map = Nm2Api._swap_dict_keyval(
|
|
Packit Service |
0535c1 |
Api2Nm.get_iface_type_map()
|
|
Packit Service |
0535c1 |
)
|
|
Packit Service |
0535c1 |
return Nm2Api._iface_types_map.get(name, IFACE_TYPE_UNKNOWN)
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
@staticmethod
|
|
Packit Service |
0535c1 |
def get_iface_admin_state(dev_state):
|
|
Packit Service |
0535c1 |
if NM.DeviceState.IP_CONFIG <= dev_state <= NM.DeviceState.ACTIVATED:
|
|
Packit Service |
0535c1 |
return ApiIfaceAdminState.UP
|
|
Packit Service |
0535c1 |
return ApiIfaceAdminState.DOWN
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
@staticmethod
|
|
Packit Service |
0535c1 |
def _swap_dict_keyval(dictionary):
|
|
Packit Service |
0535c1 |
return {val: key for key, val in dictionary.items()}
|