Modified cases for ipmi network

"REDFISH_NW_ETH0" is hardcode as
"Managers/bmc/EthernetInterfaces/eth0/" and the command "lan print"
didn't specify the channel number. Therefore, the default will print
information on the first found LAN channel.

Signed-off-by: Tony Lee <tony.lee@quantatw.com>
Change-Id: I86a0a41275cdfb98aa502fc29f8bc9074dc62280
diff --git a/data/variables.py b/data/variables.py
index 4894632..00120bb 100755
--- a/data/variables.py
+++ b/data/variables.py
@@ -157,6 +157,7 @@
 REDFISH_SESSION_URI = 'SessionService/Sessions/'
 REDFISH_NW_ETH0 = 'Managers/bmc/EthernetInterfaces/eth0/'
 REDFISH_NW_ETH0_URI = REDFISH_BASE_URI + REDFISH_NW_ETH0
+REDFISH_NW_ETH_IFACE = REDFISH_BASE_URI + 'Managers/bmc/EthernetInterfaces/'
 REDFISH_NW_PROTOCOL = 'Managers/bmc/NetworkProtocol'
 REDFISH_NW_PROTOCOL_URI = REDFISH_BASE_URI + REDFISH_NW_PROTOCOL
 REDFISH_ACCOUNTS_SERVICE = 'AccountService/'
diff --git a/ipmi/test_ipmi_network.robot b/ipmi/test_ipmi_network.robot
index e9d7d60..93ca380 100644
--- a/ipmi/test_ipmi_network.robot
+++ b/ipmi/test_ipmi_network.robot
@@ -6,7 +6,11 @@
 Resource               ../lib/bmc_network_utils.robot
 Library                ../lib/ipmi_utils.py
 Library                ../lib/gen_robot_valid.py
+Library                ../lib/var_funcs.py
+Library                ../lib/bmc_network_utils.py
 
+Suite Setup            Redfish.Login
+Test Setup             Printn
 Test Teardown          FFDC On Test Case Fail
 
 Force Tags             IPMI_Network
@@ -23,18 +27,10 @@
     [Documentation]  Retrieve IP address using IPMI and verify using Redfish.
     [Tags]  Retrieve_IP_Address_Via_IPMI_And_Verify_Using_Redish
 
-    ${lan_print_ipmi}=  Get LAN Print Dict
-
-    # Fetch IP address list using redfish.
-    ${ip_list_redfish}=  Create List
-    Redfish.Login
-    ${resp}=  Redfish.Get  ${REDFISH_NW_ETH0_URI}
-    @{network_config_redfish}=  Get From Dictionary  ${resp.dict}  IPv4StaticAddresses
-    : FOR  ${network_config_redfish}  IN  @{network_config_redfish}
-    \  Append To List  ${ip_list_redfish}  ${network_config_redfish['Address']}
-
-    Valid Value  lan_print_ipmi['IP Address']  ${ip_list_redfish}
-
+    ${active_channel_config}=  Get Active Channel Config
+    :FOR  ${channel_number}  IN  @{active_channel_config.keys()}
+    \  Verify Channel Info  ${channel_number}  IPv4StaticAddresses  ${active_channel_config}
+    END
 
 Retrieve Default Gateway Via IPMI And Verify
     [Documentation]  Retrieve default gateway via IPMI and verify it's existence on the BMC.
@@ -49,13 +45,10 @@
     [Documentation]  Retrieve MAC address via IPMI and verify using Redfish.
     [Tags]  Retrieve_MAC_Address_Via_IPMI_And_Verify_Using_Redfish
 
-    ${lan_print_ipmi}=  Get LAN Print Dict
-
-    Redfish.Login
-    ${resp}=  Redfish.Get  ${REDFISH_NW_ETH0_URI}
-    ${mac_address_redfish}=  Get From Dictionary  ${resp.dict}  MACAddress
-
-    Valid Value  lan_print_ipmi['MAC Address']  ${mac_address_redfish}
+    ${active_channel_config}=  Get Active Channel Config
+    :FOR  ${channel_number}  IN  @{active_channel_config.keys()}
+    \  Verify Channel Info  ${channel_number}  MACAddress  ${active_channel_config}
+    END
 
 
 Test Valid IPMI Channels Supported
@@ -95,13 +88,13 @@
     [Teardown]  Run Keywords  Restore Configuration  AND  FFDC On Test Case Fail
 
     Redfish Power On
-    ${initial_lan_config}=  Get LAN Print Dict  inband
+    ${initial_lan_config}=  Get LAN Print Dict  ipmi_cmd_type=inband
     Set Suite Variable  ${initial_lan_config}
 
     Set IPMI Inband Network Configuration  10.10.10.10  255.255.255.0  10.10.10.10
     Sleep  10
 
-    ${lan_print_output}=  Get LAN Print Dict  inband
+    ${lan_print_output}=  Get LAN Print Dict  ipmi_cmd_type=inband
     Valid Value  lan_print_output['IP Address']  ["10.10.10.10"]
     Valid Value  lan_print_output['Subnet Mask']  ["255.255.255.0"]
     Valid Value  lan_print_output['Default Gateway IP']  ["10.10.10.10"]
@@ -151,3 +144,35 @@
     ...  ${initial_lan_config['Subnet Mask']}
     ...  ${initial_lan_config['Default Gateway IP']}  login=${0}
 
+
+Verify Channel Info
+    [Documentation]  Verify the channel info.
+    [Arguments]  ${channel_number}  ${network_parameter}  ${active_channel_config}
+
+    Run Keyword If  '${network_parameter}' == 'IPv4StaticAddresses'
+    ...    Verify IPv4 Static Address  ${channel_number}  ${active_channel_config}
+    ...  ELSE IF  '${network_parameter}' == 'MACAddress'
+    ...    Verify MAC Address  ${channel_number}  ${active_channel_config}
+
+
+Verify IPv4 Static Address
+    [Documentation]  Verify the IPv4 Static Address.
+    [Arguments]  ${channel_number}  ${active_channel_config}
+
+    ${lan_print_ipmi}=  Get LAN Print Dict  ${channel_number}
+    ${ipv4_static_addresses}=  Redfish.Get Attribute
+    ...  ${REDFISH_NW_ETH_IFACE}${active_channel_config['${channel_number}']['name']}  IPv4StaticAddresses
+    ${redfish_ips}=  Nested Get  Address  ${ipv4_static_addresses}
+    Rprint Vars  lan_print_ipmi  ipv4_static_addresses  redfish_ips
+    Valid Value  lan_print_ipmi['IP Address']  ${redfish_ips}
+
+
+Verify MAC Address
+    [Documentation]  Verify the MAC Address.
+    [Arguments]  ${channel_number}  ${active_channel_config}
+
+    ${lan_print_ipmi}=  Get LAN Print Dict  ${channel_number}
+    ${redfish_mac_address}=  Redfish.Get Attribute
+    ...  ${REDFISH_NW_ETH_IFACE}${active_channel_config['${channel_number}']['name']}  MACAddress
+    Rprint Vars  lan_print_ipmi  redfish_mac_address
+    Valid Value  lan_print_ipmi['MAC Address']  ['${redfish_mac_address}']
diff --git a/lib/bmc_network_utils.py b/lib/bmc_network_utils.py
index 33c5166..93f932b 100644
--- a/lib/bmc_network_utils.py
+++ b/lib/bmc_network_utils.py
@@ -13,6 +13,8 @@
 import re
 import ipaddress
 from robot.libraries.BuiltIn import BuiltIn
+import json
+import bmc_ssh_utils as bsu
 
 
 def netmask_prefix_length(netmask):
@@ -122,3 +124,55 @@
         return parse_nping_output(output)
 
     return output
+
+
+def get_channel_config():
+    r"""
+    Get the channel config data and return as a dictionary.
+
+    Example:
+    channel_config = get_channel_config()
+    print_vars(channel_config)
+
+    channel_config:
+      [0]:
+        [name]:                  IPMB
+        [is_valid]:              True
+        [active_sessions]:       0
+        [channel_info]:
+          [medium_type]:         ipmb
+          [protocol_type]:       ipmb-1.0
+          [session_supported]:   session-less
+          [is_ipmi]:             True
+      [1]:
+        [name]:                  eth0
+        [is_valid]:              True
+        [active_sessions]:       0
+        [channel_info]:
+          [medium_type]:         other-lan
+          [protocol_type]:       ipmb-1.0
+          [session_supported]:   multi-session
+          [is_ipmi]:             True
+      [2]:
+        [name]:                  eth1
+        [is_valid]:              True
+        [active_sessions]:       0
+        [channel_info]:
+          [medium_type]:         lan-802.3
+          [protocol_type]:       ipmb-1.0
+          [session_supported]:   multi-session
+          [is_ipmi]:             True
+    (etc.)
+    """
+
+    stdout, stderr, rc = bsu.bmc_execute_command("cat /usr/share/ipmi-providers/channel_config.json")
+    return json.loads(stdout)
+
+
+def get_active_channel_config():
+    r"""
+    Channel configs which medium_type are 'other-lan' or 'lan-802.3' returned by
+     this function.
+    """
+
+    return vf.filter_struct(get_channel_config(), "[('medium_type', 'other-lan|lan-802.3')]", regex=1)
diff --git a/lib/ipmi_utils.py b/lib/ipmi_utils.py
index 2a75994..7c474a9 100644
--- a/lib/ipmi_utils.py
+++ b/lib/ipmi_utils.py
@@ -121,7 +121,7 @@
         return stdout, stderr, rc
 
 
-def get_lan_print_dict(ipmi_cmd_type='external'):
+def get_lan_print_dict(channel_number='', ipmi_cmd_type='external'):
     r"""
     Get IPMI 'lan print' output and return it as a dictionary.
 
@@ -168,18 +168,19 @@
                                     'inband', 'external').
     """
 
+    channel_number = str(channel_number)
     # Notice in the example of data above that 'Auth Type Enable' needs some
     # special processing.  We essentially want to isolate its data and remove
     # the 'Auth Type Enable' string so that key_value_outbuf_to_dict can
     # process it as a sub-dictionary.
-    cmd_buf = "lan print | grep -E '^(Auth Type Enable)" +\
+    cmd_buf = "lan print " + channel_number + " | grep -E '^(Auth Type Enable)" +\
         "?[ ]+: ' | sed -re 's/^(Auth Type Enable)?[ ]+: //g'"
     stdout1, stderr, rc = execute_ipmi_cmd(cmd_buf, ipmi_cmd_type,
                                            print_output=0)
 
     # Now get the remainder of the data and exclude the lines with no field
     # names (i.e. the 'Auth Type Enable' sub-fields).
-    cmd_buf = "lan print | grep -E -v '^[ ]+: '"
+    cmd_buf = "lan print " + channel_number + " | grep -E -v '^[ ]+: '"
     stdout2, stderr, rc = execute_ipmi_cmd(cmd_buf, ipmi_cmd_type,
                                            print_output=0)