Adds EthernetInterface to the library

This commit adds the EthernetInterface to the library.
This returns the MAC addresses and its status as a dictionary to
its caller.

This has been tested on HPE Redfish hardware.

Change-Id: If57184d71d244cdc6f04d3f66d56c374d4336d24
This commit is contained in:
Nisha Agarwal 2017-04-02 04:14:47 -07:00
parent 2aa31ddf4f
commit 8fe2904a62
10 changed files with 327 additions and 5 deletions

View File

@ -0,0 +1,5 @@
---
features:
- |
Adds the "EthernetInterfaces" to the library.
It also returns the list of connected MACs.

View File

@ -108,3 +108,10 @@ PROCESSOR_ARCH_IA_64 = 'Intel Itanium'
PROCESSOR_ARCH_ARM = 'ARM'
PROCESSOR_ARCH_MIPS = 'MIPS'
PROCESSOR_ARCH_OEM = 'OEM-defined'
# Health related constants.
HEALTH_STATE_ENABLED = 'Enabled'
HEALTH_STATE_DISABLED = 'Disabled'
HEALTH_OK = 'OK'
HEALTH_WARNING = 'Warning'
HEALTH_CRITICAL = 'Critical'

View File

@ -0,0 +1,84 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
# This is referred from Redfish standard schema.
# http://redfish.dmtf.org/schemas/EthernetInterface.v1_3_0.json
import logging
from sushy.resources import base
from sushy.resources.system import constants as sys_cons
from sushy.resources.system import mappings as sys_map
LOG = logging.getLogger(__name__)
class HealthStatusField(base.CompositeField):
state = base.MappedField(
'State', sys_map.HEALTH_STATE_VALUE_MAP)
health = base.Field('Health')
class EthernetInterface(base.ResourceBase):
"""This class adds the EthernetInterface resource"""
identity = base.Field('Id', required=True)
"""The Ethernet Interface identity string"""
name = base.Field('Name')
"""The name of the resource or array element"""
description = base.Field('Description')
"""Description"""
permanent_mac_address = base.Field('PermanentMACAddress')
"""This is the permanent MAC address assigned to this interface (port) """
mac_address = base.Field('MACAddress')
"""This is the currently configured MAC address of the interface."""
speed_mbps = base.Field('SpeedMbps')
"""This is the current speed in Mbps of this interface."""
status = HealthStatusField("Status")
class EthernetInterfaceCollection(base.ResourceCollectionBase):
_summary = None
@property
def _resource_type(self):
return EthernetInterface
@property
def summary(self):
"""Summary MAC addresses and interfaces state
This filters the MACs whose health is OK,
which means the MACs in both 'Enabled' and 'Disabled' States
are returned.
:returns dictionary in the format {'aa:bb:cc:dd:ee:ff': 'Enabled'}
"""
if self._summary is None:
mac_dict = {}
for eth in self.get_members():
if eth.mac_address is not None:
if (eth.status is not None and
eth.status.health == sys_cons.HEALTH_OK):
mac_dict[eth.mac_address] = eth.status.state
self._summary = mac_dict
return self._summary
def refresh(self):
super(EthernetInterfaceCollection, self).refresh()
self._summary = None

View File

@ -82,3 +82,20 @@ PROCESSOR_ARCH_VALUE_MAP = {
PROCESSOR_ARCH_VALUE_MAP_REV = (
utils.revert_dictionary(PROCESSOR_ARCH_VALUE_MAP))
HEALTH_STATE_VALUE_MAP = {
'Enabled': sys_cons.HEALTH_STATE_ENABLED,
'Disabled': sys_cons.HEALTH_STATE_DISABLED,
}
HEALTH_STATE_VALUE_MAP_REV = (
utils.revert_dictionary(HEALTH_STATE_VALUE_MAP))
HEALTH_VALUE_MAP = {
'OK': sys_cons.HEALTH_OK,
'Warning': sys_cons.HEALTH_WARNING,
'Critical': sys_cons.HEALTH_CRITICAL
}
HEALTH_VALUE_MAP_REV = (
utils.revert_dictionary(HEALTH_VALUE_MAP))

View File

@ -19,6 +19,7 @@ from sushy import exceptions
from sushy.resources import base
from sushy.resources import common
from sushy.resources.system import constants as sys_cons
from sushy.resources.system import ethernet_interface
from sushy.resources.system import mappings as sys_maps
from sushy.resources.system import processor
from sushy import utils
@ -118,6 +119,8 @@ class System(base.ResourceBase):
_actions = ActionsField('Actions', required=True)
_ethernet_interfaces = None
def __init__(self, connector, identity, redfish_version=None):
"""A class representing a ComputerSystem
@ -241,11 +244,7 @@ class System(base.ResourceBase):
def _get_processor_collection_path(self):
"""Helper function to find the ProcessorCollection path"""
processor_col = self.json.get('Processors')
if not processor_col:
raise exceptions.MissingAttributeError(attribute='Processors',
resource=self._path)
return processor_col.get('@odata.id')
return utils.get_sub_resource_path_by(self, 'Processors')
@property
def processors(self):
@ -264,6 +263,18 @@ class System(base.ResourceBase):
def refresh(self):
super(System, self).refresh()
self._processors = None
self._ethernet_interfaces = None
@property
def ethernet_interfaces(self):
if self._ethernet_interfaces is None:
self._ethernet_interfaces = (
ethernet_interface.EthernetInterfaceCollection(
self._conn,
utils.get_sub_resource_path_by(self, "EthernetInterfaces"),
redfish_version=self.redfish_version))
return self._ethernet_interfaces
class SystemCollection(base.ResourceCollectionBase):

View File

@ -0,0 +1,37 @@
{
"@odata.type": "#EthernetInterface.v1_0_2.EthernetInterface",
"Id": "1",
"Name": "Ethernet Interface",
"Description": "System NIC 1",
"Status": {
"State": "Enabled",
"Health": "OK"
},
"PermanentMACAddress": "12:44:6A:3B:04:11",
"MACAddress": "12:44:6A:3B:04:11",
"SpeedMbps": 1000,
"FullDuplex": true,
"HostName": "web483",
"FQDN": "web483.contoso.com",
"IPv6DefaultGateway": "fe80::3ed9:2bff:fe34:600",
"NameServers": [
"names.contoso.com"
],
"IPv4Addresses": [{
"Address": "192.168.0.10",
"SubnetMask": "255.255.252.0",
"AddressOrigin": "Static",
"Gateway": "192.168.0.1"
}],
"IPv6Addresses": [{
"Address": "fe80::1ec1:deff:fe6f:1e24",
"PrefixLength": 64,
"AddressOrigin": "Static",
"AddressState": "Preferred"
}],
"VLANs": {
"@odata.id": "/redfish/v1/Systems/437XR1138R2/EthernetInterfaces/12446A3B0411/VLANs"
},
"@odata.context": "/redfish/v1/$metadata#EthernetInterface.EthernetInterface",
"@odata.id": "/redfish/v1/Systems/437XR1138R2/EthernetInterfaces/12446A3B0411"
}

View File

@ -0,0 +1,12 @@
{
"@odata.type": "#EthernetInterfaceCollection.EthernetInterfaceCollection",
"Name": "Ethernet Interface Collection",
"Description": "System NICs on Contoso Servers",
"Members@odata.count": 1,
"Members": [{
"@odata.id": "/redfish/v1/Systems/437XR1138R2/EthernetInterfaces/12446A3B0411"
}],
"Oem": {},
"@odata.context": "/redfish/v1/$metadata#EthernetInterfaceCollection.EthernetInterfaceCollection",
"@odata.id": "/redfish/v1/Systems/437XR1138R2/EthernetInterfaces"
}

View File

@ -0,0 +1,103 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import json
import mock
from sushy.resources.system import constants as sys_cons
from sushy.resources.system import ethernet_interface
from sushy.tests.unit import base
class EthernetInterfaceTestCase(base.TestCase):
def setUp(self):
super(EthernetInterfaceTestCase, self).setUp()
self.conn = mock.Mock()
eth_file = 'sushy/tests/unit/json_samples/ethernet_interfaces.json'
with open(eth_file, 'r') as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
eth_path = ("/redfish/v1/Systems/437XR1138R2/EthernetInterfaces/"
"12446A3B0411")
self.sys_eth = ethernet_interface.EthernetInterface(
self.conn, eth_path, redfish_version='1.0.2')
def test__parse_attributes(self):
self.sys_eth._parse_attributes()
self.assertEqual('1.0.2', self.sys_eth.redfish_version)
self.assertEqual('1', self.sys_eth.identity)
self.assertEqual('Ethernet Interface', self.sys_eth.name)
self.assertEqual('System NIC 1', self.sys_eth.description)
self.assertEqual(
'12:44:6A:3B:04:11', self.sys_eth.permanent_mac_address)
self.assertEqual('12:44:6A:3B:04:11', self.sys_eth.mac_address)
self.assertEqual('Enabled', self.sys_eth.status.state)
self.assertEqual('OK', self.sys_eth.status.health)
self.assertEqual(1000, self.sys_eth.speed_mbps)
class EthernetInterfaceCollectionTestCase(base.TestCase):
def setUp(self):
super(EthernetInterfaceCollectionTestCase, self).setUp()
self.conn = mock.Mock()
with open('sushy/tests/unit/json_samples/'
'ethernet_interfaces_collection.json', 'r') as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
self.sys_eth_col = ethernet_interface.EthernetInterfaceCollection(
self.conn, '/redfish/v1/Systems/437XR1138R2/EthernetInterfaces',
redfish_version='1.0.2')
def test__parse_attributes(self):
self.sys_eth_col._parse_attributes()
self.assertEqual('1.0.2', self.sys_eth_col.redfish_version)
self.assertEqual('Ethernet Interface Collection',
self.sys_eth_col.name)
eth_path = ('/redfish/v1/Systems/437XR1138R2/EthernetInterfaces/'
'12446A3B0411',)
self.assertEqual(eth_path, self.sys_eth_col.members_identities)
@mock.patch.object(ethernet_interface, 'EthernetInterface', autospec=True)
def test_get_member(self, mock_eth):
self.sys_eth_col.get_member(
'/redfish/v1/Systems/437XR1138R2/EthernetInterfaces/'
'12446A3B0411')
mock_eth.assert_called_once_with(
self.sys_eth_col._conn,
('/redfish/v1/Systems/437XR1138R2/EthernetInterfaces/'
'12446A3B0411'),
redfish_version=self.sys_eth_col.redfish_version)
@mock.patch.object(ethernet_interface, 'EthernetInterface', autospec=True)
def test_get_members(self, mock_eth):
members = self.sys_eth_col.get_members()
eth_path = ("/redfish/v1/Systems/437XR1138R2/EthernetInterfaces/"
"12446A3B0411")
calls = [
mock.call(self.sys_eth_col._conn, eth_path,
redfish_version=self.sys_eth_col.redfish_version),
]
mock_eth.assert_has_calls(calls)
self.assertIsInstance(members, list)
self.assertEqual(1, len(members))
def test_eth_summary(self):
self.assertIsNone(self.sys_eth_col._summary)
self.conn.get.return_value.json.reset_mock()
path = 'sushy/tests/unit/json_samples/ethernet_interfaces.json'
with open(path, 'r') as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
expected_summary = {'12:44:6A:3B:04:11': sys_cons.HEALTH_STATE_ENABLED}
actual_summary = self.sys_eth_col.summary
self.assertEqual(expected_summary, actual_summary)

View File

@ -19,6 +19,8 @@ import mock
import sushy
from sushy import exceptions
from sushy.resources.system import constants as sys_cons
from sushy.resources.system import ethernet_interface
from sushy.resources.system import processor
from sushy.resources.system import system
from sushy.tests.unit import base
@ -58,6 +60,7 @@ class SystemTestCase(base.TestCase):
self.assertEqual(96, self.sys_inst.memory_summary.size_gib)
self.assertEqual("OK", self.sys_inst.memory_summary.health)
self.assertIsNone(self.sys_inst._processors)
self.assertIsNone(self.sys_inst._ethernet_interfaces)
def test__parse_attributes_missing_actions(self):
self.sys_inst.json.pop('Actions')
@ -338,6 +341,28 @@ class SystemTestCase(base.TestCase):
self.sys_inst.processors.summary)
self.conn.get.return_value.json.assert_not_called()
def test_ethernet_interfaces(self):
self.conn.get.return_value.json.reset_mock()
eth_coll_return_value = None
eth_return_value = None
path = ('sushy/tests/unit/json_samples/'
'ethernet_interfaces_collection.json')
with open(path, 'r') as f:
eth_coll_return_value = json.loads(f.read())
with open('sushy/tests/unit/json_samples/ethernet_interfaces.json',
'r') as f:
eth_return_value = (json.loads(f.read()))
self.conn.get.return_value.json.side_effect = [eth_coll_return_value,
eth_return_value]
self.assertIsNone(self.sys_inst._ethernet_interfaces)
actual_macs = self.sys_inst.ethernet_interfaces.summary
self.assertEqual({'12:44:6A:3B:04:11': sys_cons.HEALTH_STATE_ENABLED},
actual_macs)
self.assertIsInstance(self.sys_inst._ethernet_interfaces,
ethernet_interface.EthernetInterfaceCollection)
class SystemCollectionTestCase(base.TestCase):

View File

@ -15,6 +15,8 @@
import logging
from sushy import exceptions
LOG = logging.getLogger(__name__)
@ -57,3 +59,22 @@ def int_or_none(x):
if x is None:
return None
return int(x)
def get_sub_resource_path_by(resource, subresource_name):
"""Helper function to find the subresource path
:param resource: ResourceBase instance on which the name
gets queried upon.
:param subresource_name: name of the resource field to
fetch the '@odata.id' from.
"""
subresource_element = resource.json.get(subresource_name)
if not subresource_element:
raise exceptions.MissingAttributeError(attribute=subresource_name,
resource=resource.path)
if '@odata.id' not in subresource_element:
raise exceptions.MissingAttributeError(
attribute=(subresource_name + '/@odata.id'),
resource=resource.path)
return subresource_element['@odata.id']