Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
module ForemanDiscovery | ||
class LldpNeighbors | ||
def set(iface, neighbor) | ||
interfaces[iface] = neighbor | ||
end | ||
|
||
def get(iface) | ||
interfaces[iface] | ||
end | ||
|
||
def list_by_pvid | ||
list = {} | ||
interfaces.each do |name, neighbor| | ||
next unless neighbor.has_key? 'PVID' | ||
vlan = neighbor['PVID'] | ||
list[vlan] = [] unless list.has_key? vlan | ||
list[vlan].push name | ||
end | ||
|
||
list | ||
end | ||
|
||
def interfaces | ||
@interfaces ||= {} | ||
end | ||
|
||
def get_neighbors_by_interface(ifname) | ||
return nil unless interfaces.has_key? ifname | ||
|
||
interface = get(ifname) | ||
return nil unless interface.has_key? 'PVID' | ||
|
||
list = [] | ||
interfaces.each do |name, neighbor| | ||
next unless neighbor.has_key? 'PVID' | ||
list.push name if neighbor['PVID'] == interface['PVID'] | ||
end | ||
|
||
return if list.size < 2 | ||
list | ||
end | ||
|
||
def self.from_facts(facts) | ||
neighbors = self.new | ||
interfaces = {} | ||
facts.keys.each do |key| | ||
key_s = key.to_s | ||
next unless key_s.start_with? 'lldp_neighbor_' | ||
|
||
property, iface = key_s[14..-1].split('_', 2) | ||
if property == 'mngAddr' | ||
protocol, iface = iface.split('_', 2) | ||
property += '_' + protocol | ||
end | ||
interfaces[iface] = {} unless interfaces.has_key? iface | ||
interfaces[iface][property] = facts[key] | ||
end | ||
|
||
interfaces.each do |name, properties| | ||
neighbors.set name, properties | ||
end | ||
|
||
neighbors | ||
end | ||
end | ||
end |