Skip to content

Commit

Permalink
Merge pull request #789 from mchf/bnc964856-drop-obsolete-NetworkInte…
Browse files Browse the repository at this point in the history
…rfaces-api

Replaced obsolete API
  • Loading branch information
mchf committed Sep 19, 2018
2 parents 6782942 + 9c66fe5 commit 762d4fc
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 717 deletions.
184 changes: 11 additions & 173 deletions library/network/src/modules/NetworkInterfaces.rb
Expand Up @@ -101,7 +101,7 @@ def main
@CardRegex =
# other: irlan|lo|plip|...
{
"netcard" => "arc|ath|bnep|ci|ctc|slc|dummy|bond|escon|eth|fddi|ficon|hsi|qeth|lcs|iucv|myri|tr|usb|wlan|xp|vlan|br|tun|tap|ib|em|p|p[0-9]+p",
"netcard" => "ath|bnep|ci|ctc|slc|dummy|bond|escon|eth|fddi|ficon|hsi|qeth|lcs|iucv|myri|tr|usb|wlan|xp|vlan|br|tun|tap|ib|em|p|p[0-9]+p",
"modem" => "ppp|modem",
"isdn" => "isdn|ippp",
"dsl" => "dsl"
Expand Down Expand Up @@ -185,21 +185,6 @@ def IsEmpty(value)
value.nil? ? true : value.empty?
end

def ifcfg_part(ifcfg, part)
return "" if Builtins.regexpmatch(ifcfg, @ifcfg_name_regex) != true
ret = Builtins.regexpsub(ifcfg, @ifcfg_name_regex, "\\#{part}")
ret.nil? ? "" : ret
end

# Return a device type
# @param [String] dev device
# @return device type
# @example device_type("eth1") -> "eth"
# @example device_type("eth-pcmcia-0") -> "eth"
def device_type(dev)
ifcfg_part(dev, "1")
end

# Detects a subtype of Ethernet device type according /sys or /proc content
#
# @example
Expand Down Expand Up @@ -269,7 +254,6 @@ def GetTypeFromSysfs(dev)
sys_type = Convert.to_string(
SCR.Read(path(".target.string"), sys_type_path)
)

sys_type = if sys_type
Builtins.regexpsub(sys_type, "(.*)\n", "\\1")
else
Expand Down Expand Up @@ -312,7 +296,7 @@ def GetTypeFromIfcfg(ifcfg)
rule_key = Ops.get(key_type, 0, "")
rule_value = Ops.get(key_type, 1, "")
rule_type = Ops.get(key_type, 2, "")
type = rule_type if Ops.get_string(ifcfg, rule_key, "") == rule_value
type = rule_type if (ifcfg[rule_key] || "").casecmp?(rule_value)
end

Builtins.foreach(@TypeByKeyExistence) do |key_type|
Expand Down Expand Up @@ -342,7 +326,9 @@ def GetTypeFromIfcfgOrName(dev, ifcfg)

type = GetTypeFromIfcfg(ifcfg) if IsEmpty(type)

type = device_type(dev) if type.nil?
# last instance - no record in sysfs, no configuration, device
# name is not bounded to a type -> use fallbac "eth" as wicked already does
type = "eth" if type.nil?

log.debug("GetTypeFromIfcfgOrName: device='#{dev}' type='#{type}'")

Expand Down Expand Up @@ -400,27 +386,6 @@ def GetDeviceTypeName(dev)
end
end

# Return a device number
# @param [String] dev device
# @return device number
# @example device_num("eth1") -> "1"
# @example device_num("lo") -> ""
#
# Obsolete: It is incompatible with new device naming scheme.
def device_num(dev)
log.warn("Do not use device_num.")
ifcfg_part(dev, "2")
end

# Return a device alias number
# @param [String] dev device
# @return alias number
# @example alias_num("eth1#2") -> "2"
# @example alias_num("eth1#blah") -> "blah"
def alias_num(dev)
ifcfg_part(dev, "3")
end

# Create a device name from its type and number
# @param [String] typ device type
# @param [String] num device number
Expand Down Expand Up @@ -835,26 +800,10 @@ def Write(devregex)
# remove deleted devices
log.info("Deleted=#{@Deleted}")
Builtins.foreach(@Deleted) do |d|
anum = alias_num(d)
if anum == ""
# delete config file
p = Builtins.add(path(".network.section"), d)
log.debug("deleting: #{p}")
SCR.Write(p, nil)
else
dev = device_name_from_alias(d)
typ = GetType(dev)
base = Builtins.add(path(".network.value"), dev)
# look in OriginalDevs because we need to catch all variables
# of the alias

dev_aliases = original_devs.fetch(typ, {}).fetch(dev, {}).fetch("_aliases", {})
dev_aliases.fetch(anum, {}).keys.each do |key|
p = base + "#{key}_#{anum}"
log.debug("deleting: #{p}")
SCR.Write(p, nil)
end
end
# delete config file
p = Builtins.add(path(".network.section"), d)
log.debug("deleting: #{p}")
SCR.Write(p, nil)
end
@Deleted = []

Expand Down Expand Up @@ -1071,7 +1020,6 @@ def GetDeviceTypes

# device types which cannot be present on s390 arch
s390_unknown_dev_types = [
"arc",
"bnep",
"dummy",
"fddi",
Expand Down Expand Up @@ -1312,63 +1260,12 @@ def Modified(devregex)
devs == original_devs
end

# It returns an array of <num> elements corresponding to the integer
# part of the free device names available for the given device type.
#
# @example GetFreeDevices("eth", 2) -> [1, 2]
#
# @param [String] type device type
# @param [String] num number of free devices to return
# @return [Array] of free devices for given type
def GetFreeDevices(type, num)
log.debug("Devices=#{@Devices}")
log.debug("type,num=#{type},#{num}")
log.debug("Devices[#{type}]=#{@Devices[type]}")

curdevs = @Devices.fetch(type, {}).keys
curdevs.map! { |d| d.include?(type) ? device_num(d) : d }

i = 0
count = 0
ret = []

# Remaining numbered devices
while count < num
if !curdevs.include?(i.to_s)
ret << i.to_s
count += 1
end
i += 1
end

log.debug("Free devices=#{ret}")

ret
end

# Return free device
# @param [String] type device type
# @return free device
# @example GetFreeDevice("eth") -&gt; "1"
def GetFreeDevice(type)
log.debug("type=#{type}")

free_dev = GetFreeDevices(type, 1).first

log.error("Free device location error: #{free_dev}") if free_dev.nil?
log.debug("Free device=#{free_dev}")

free_dev
end

# Check presence of the device (alias)
# @param [String] dev device identifier
# @return true if device is present
def Check(dev)
Builtins.y2debug("Check(%1)", dev)
typ = GetType(dev)
# string num = device_num(dev);
# string anum = alias_num(dev);
return false if !Builtins.haskey(@Devices, typ)

devsmap = Ops.get(@Devices, typ, {})
Expand All @@ -1394,17 +1291,6 @@ def Select(name)
@Name = name
t = GetType(@Name)
@Current = Ops.get(@Devices, [t, @Name], {})
a = alias_num(@Name)
if !a.nil? && a != ""
@Current = Ops.get_map(@Current, ["_aliases", a], {})
end

if @Current == {}
# Default device map
@Current =
# FIXME: remaining items
{}
end

Builtins.y2debug("Name=%1", @Name)
Builtins.y2debug("Current=%1", @Current)
Expand Down Expand Up @@ -1466,19 +1352,9 @@ def Change2(name, newdev, check)

t = int_type if Ops.greater_than(Builtins.size(int_type), 0)
end
a = alias_num(name)
Builtins.y2debug("ChangeDevice(%1)", name)

devsmap = Ops.get(@Devices, t, {})
devmap = Ops.get(devsmap, name, {})
amap = Ops.get_map(devmap, "_aliases", {})

if a != ""
Ops.set(amap, a, newdev)
Ops.set(devmap, "_aliases", amap)
else
devmap = deep_copy(newdev)
end
devmap = deep_copy(newdev)

Ops.set(devsmap, name, devmap)
Ops.set(@Devices, t, devsmap)
Expand All @@ -1494,16 +1370,9 @@ def Delete2(name)
end

t = GetType(name)
a = alias_num(name)
devsmap = Ops.get(@Devices, t, {})

if a != ""
amap = Ops.get_map(devsmap, [name, "_aliases"], {})
amap = Builtins.remove(amap, a)
Ops.set(devsmap, [name, "_aliases"], amap)
else
devsmap = Builtins.remove(devsmap, name)
end
devsmap = Builtins.remove(devsmap, name)

Ops.set(@Devices, t, devsmap)

Expand Down Expand Up @@ -1595,23 +1464,6 @@ def Locate(key, val)
ret
end

# Locate devices which attributes doesn't match given key and value
#
# @param [String] key device key
# @param [String] val device value
# @return [Array] of devices with key!=val
def LocateNOT(key, val)
ret = []

@Devices.values.each do |devsmap|
devsmap.each do |device, conf|
ret << device if conf[key] != val
end
end

ret
end

# @deprecated No longer needed
# @return true
def CleanHotplugSymlink
Expand Down Expand Up @@ -1701,14 +1553,6 @@ def ValidCharsIfcfg
String.ValidCharsFilename
end

# list of all devices except given one by parameter dev
# also loopback is ommited

def ListDevicesExcept(dev)
devices = Builtins.filter(LocateNOT("DEVICE", dev)) { |s| s != "lo" }
deep_copy(devices)
end

private

# Device configuration files are matched against this regexp
Expand Down Expand Up @@ -1749,12 +1593,9 @@ def get_devices(devregex)
publish variable: :Name, type: "string"
publish variable: :Current, type: "map <string, any>"
publish variable: :CardRegex, type: "map <string, string>"
publish function: :device_type, type: "string (string)"
publish function: :GetTypeFromIfcfg, type: "string (map <string, any>)"
publish function: :GetType, type: "string (string)"
publish function: :GetDeviceTypeName, type: "string (string)"
publish function: :device_num, type: "string (string)"
publish function: :alias_num, type: "string (string)"
publish function: :IsHotplug, type: "boolean (string)"
publish function: :IsConnected, type: "boolean (string)"
publish function: :RealType, type: "string (string, string)"
Expand All @@ -1769,8 +1610,6 @@ def get_devices(devregex)
publish function: :GetDeviceTypes, type: "list <string> ()"
publish function: :GetDevTypeDescription, type: "string (string, boolean)"
publish function: :Export, type: "map <string, map> (string)"
publish function: :GetFreeDevices, type: "list <string> (string, integer)"
publish function: :GetFreeDevice, type: "string (string)"
publish function: :Check, type: "boolean (string)"
publish function: :Select, type: "boolean (string)"
publish function: :Add, type: "boolean ()"
Expand All @@ -1788,7 +1627,6 @@ def get_devices(devregex)
publish function: :Fastest, type: "string ()"
publish function: :FastestType, type: "string (string)"
publish function: :ValidCharsIfcfg, type: "string ()"
publish function: :ListDevicesExcept, type: "list <string> (string)"
end

NetworkInterfaces = NetworkInterfacesClass.new
Expand Down
63 changes: 0 additions & 63 deletions library/network/test/network_interfaces_helpers_test.rb
Expand Up @@ -6,69 +6,6 @@

module Yast
describe NetworkInterfaces do
context "Parsing device name" do
DEVICE_DESCS = [
{
name: "",
alias_id: "",
type_by_regex: ""
},
{
name: "eth0",
alias_id: "",
type_by_regex: "eth"
},
{
name: "eth-pcmcia-0",
alias_id: "",
type_by_regex: "eth"
},
{
name: "tr-pcmcia-1#0",
alias_id: "0",
type_by_regex: "tr"
},
{
name: "enp0s3",
alias_id: "",
type_by_regex: "enp"
},
{
name: "eth0#1",
alias_id: "1",
type_by_regex: "eth"
},
{
name: "enp0s3#0",
alias_id: "0",
type_by_regex: "enp"
},
{
name: "eth-id-00:07:e9:d5:8e:e8",
alias_id: "",
type_by_regex: "eth"
}
].freeze

DEVICE_DESCS.each do |device_desc|
device_name = device_desc[:name]
alias_id = device_desc[:alias_id]
type_by_regex = device_desc[:type_by_regex]

describe "#alias_num" do
it "returns alias_id: <#{alias_id}> for name: <#{device_name}>" do
expect(NetworkInterfaces.alias_num(device_name)).to be_eql alias_id
end
end

describe "#device_type" do
it "returns type by regex: <#{type_by_regex}> for name: <#{device_name}>" do
expect(NetworkInterfaces.device_type(device_name)).to be_eql type_by_regex
end
end
end
end

describe "NetworkInterfaces#filter_interfacetype" do
it "drops interface type if present and not set to \"lo\" or \"dummy\"" do
devmap = { "INTERFACETYPE" => "eth" }
Expand Down

0 comments on commit 762d4fc

Please sign in to comment.