Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing the connection b/w ports #194

Merged
merged 1 commit into from
Jul 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,59 @@ def parse_network_device_ports(ports)
# it ports parsed
#
def parse_physical_switch_ports(physical_switch)
physical_switch.ports&.map { |port| parse_switch_port(port) }
physical_switch.ports&.map { |port| parse_switch_port(port, physical_switch) }
end

#
# Binds the connected ports
#
# @param [Array<Hash>] ports - parsed ports to be bind
#
def bind_network_ports!(ports)
ports.each do |origin_port|
connected_port = ports.find { |destination_port| connected_port?(origin_port, destination_port) }
origin_port[:connected_port_uid] = connected_port[:uid_ems] if connected_port
end
end

#
# Selects all the physical network port from physical servers list.
#
# @param [Array<Hash>] physical_servers - list of physical servers that must
# have its ports selecteds.
#
# @return [Array<Hash>] the list of physical network ports
#
def extract_physical_servers_ports(physical_servers)
ports = []

physical_servers.each do |server|
network_devices = server[:computer_system][:hardware][:guest_devices]

network_devices.each do |device|
ports.concat(device[:physical_network_ports]) if device[:physical_network_ports].present?
end
end

ports
end

#
# Selects all the physical network port from physical switches list.
#
# @param [Array<Hash>] physical_switches - list of physical switches that must
# have its ports selecteds.
#
# @return [Array<Hash>] the list of physical network ports
#
def extract_physical_switches_ports(physical_switches)
ports = []

physical_switches.each do |switch|
ports.concat(switch[:physical_network_ports]) if switch[:physical_network_ports].present?
end

ports
end

private
Expand All @@ -34,18 +86,20 @@ def parse_physical_server_ports(port)
parsed_physical_port = parse_physical_port(physical_port)
logical_ports = physical_port["logicalPorts"]
parsed_logical_port = parse_logical_port(logical_ports[0])
parsed_logical_port[:uid_ems] = mount_uuid(port, physical_port['physicalPortIndex'])
parsed_logical_port[:uid_ems] = mount_uuid_server_port(port, physical_port['physicalPortIndex'])
parsed_logical_port.merge!(parsed_physical_port)

parsed_logical_port
end
end

def parse_switch_port(port)
def parse_switch_port(port, physical_switch)
result = parse(port, parent::ParserDictionaryConstants::PHYSICAL_SWITCH_PORT)
result.merge(
:port_name => port["portName"].presence || port["port"],
:port_type => "physical_port",
:vlan_enabled => port["PVID"].present?
:vlan_enabled => port["PVID"].present?,
:uid_ems => mount_uuid_switch_port(port, physical_switch)
)
end

Expand All @@ -67,9 +121,26 @@ def format_mac_address(mac_address)
mac_address.scan(/\w{2}/).join(":")
end

def mount_uuid(device, port_number = nil)
def mount_uuid_server_port(device, port_number = nil)
(device["uuid"] || "#{device['pciBusNumber']}#{device['pciDeviceNumber']}") + port_number.to_s
end

def mount_uuid_switch_port(port, physical_switch)
physical_switch.uuid + port["interfaceIndex"]
end

#
# Verifies if two ports are connected
#
# @return [Boolean] true if they are connected
#
def connected_port?(origin_port, destination_port)
return origin_port[:mac_address] == destination_port[:peer_mac_address] if origin_port[:mac_address].present?

return origin_port[:peer_mac_address] == destination_port[:mac_address] if origin_port[:peer_mac_address].present?

false
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ def ems_inv_to_hashes
inventory[:physical_switches] = get_physical_switches
inventory[:customization_scripts] = get_config_patterns

bind_network_ports(inventory)

$log.info("#{log_header}...Complete")

inventory
Expand Down Expand Up @@ -139,6 +141,18 @@ def get_config_patterns
config_patterns = @connection.discover_config_pattern
config_patterns.map { |config_pattern| @parser.parse_config_pattern(config_pattern) }
end

def bind_network_ports(inventory)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the method changes the inventory hash, shouldn't It be bind_network_ports! (with the !)? The same way you put on the parser method.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, @caiocmpaes, it's a fair concern, but our methods in this class doesn't use this pattern and to keep the consistence I would say to keep this name. Also the method that is really doing the changes has already its name ending with !. What do you think? 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I Agree with you. Let It be the current name, then. 👍

physical_servers = inventory[:physical_servers]
physical_switches = inventory[:physical_switches]

ports = []

ports.concat(PhysicalInfraManager::Parser::PhysicalNetworkPortsParser.extract_physical_servers_ports(physical_servers))
ports.concat(PhysicalInfraManager::Parser::PhysicalNetworkPortsParser.extract_physical_switches_ports(physical_switches))

PhysicalInfraManager::Parser::PhysicalNetworkPortsParser.bind_network_ports!(ports)
end
end
end
# rubocop:enable Naming/AccessorMethodName
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
switch = @result[:physical_switches].first
port = switch[:physical_network_ports].first

expect(port[:peer_mac_address]).to eq("7c:d3:0a:e6:47:51")
expect(port[:peer_mac_address]).to eq("5C:F3:FC:7F:0B:50")
expect(port[:port_type]).to eq("physical_port")
expect(port[:vlan_enabled]).to eq(true)
expect(port[:vlan_key]).to eq("\"Lenovo-Network-VLAN546\"")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
assert_specific_storage
assert_guest_table_contents
assert_physical_network_ports_table_content
assert_physical_network_ports_connection
end
end

Expand Down Expand Up @@ -145,4 +146,12 @@ def assert_physical_network_ports_table_content
expect(port2.port_name).to eq("Physical Port 2")
expect(port2.mac_address).to eq("00:0A:F7:25:67:39")
end

def assert_physical_network_ports_connection
port1 = PhysicalNetworkPort.find_by(:mac_address => "00:0A:F7:25:67:39")
port2 = PhysicalNetworkPort.find_by(:peer_mac_address => "00:0A:F7:25:67:39")

expect(port1.connected_port).to eq(port2)
expect(port2.connected_port).to eq(port1)
end
end

Large diffs are not rendered by default.

Large diffs are not rendered by default.