Skip to main content

nv_redfish/
network_device_function.rs

1// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! Network device functions.
17
18use crate::mac_address::MacAddress;
19use crate::schema::redfish::network_device_function::NetworkDeviceFunction as NetworkDeviceFunctionSchema;
20use crate::schema::redfish::network_device_function_collection::NetworkDeviceFunctionCollection as NetworkDeviceFunctionCollectionSchema;
21use crate::Error;
22use crate::NvBmc;
23use crate::Resource;
24use crate::ResourceSchema;
25use nv_redfish_core::Bmc;
26use nv_redfish_core::NavProperty;
27use std::marker::PhantomData;
28use std::sync::Arc;
29
30/// Network device functions collection.
31///
32/// Provides functions to access collection members.
33pub struct NetworkDeviceFunctionCollection<B: Bmc> {
34    bmc: NvBmc<B>,
35    collection: Arc<NetworkDeviceFunctionCollectionSchema>,
36}
37
38impl<B: Bmc> NetworkDeviceFunctionCollection<B> {
39    /// Create a new manager collection handle.
40    pub(crate) async fn new(
41        bmc: &NvBmc<B>,
42        nav: &NavProperty<NetworkDeviceFunctionCollectionSchema>,
43    ) -> Result<Self, Error<B>> {
44        let collection = bmc.expand_property(nav).await?;
45        Ok(Self {
46            bmc: bmc.clone(),
47            collection,
48        })
49    }
50
51    /// List all managers available in this BMC.
52    ///
53    /// # Errors
54    ///
55    /// Returns an error if fetching manager data fails.
56    pub async fn members(&self) -> Result<Vec<NetworkDeviceFunction<B>>, Error<B>> {
57        let mut members = Vec::new();
58        for m in &self.collection.members {
59            members.push(NetworkDeviceFunction::new(&self.bmc, m).await?);
60        }
61        Ok(members)
62    }
63}
64
65/// Network device function.
66///
67/// Provides functions to access network device function.
68pub struct NetworkDeviceFunction<B: Bmc> {
69    data: Arc<NetworkDeviceFunctionSchema>,
70    _marker: PhantomData<B>,
71}
72
73impl<B: Bmc> NetworkDeviceFunction<B> {
74    /// Create a new log service handle.
75    pub(crate) async fn new(
76        bmc: &NvBmc<B>,
77        nav: &NavProperty<NetworkDeviceFunctionSchema>,
78    ) -> Result<Self, Error<B>> {
79        nav.get(bmc.as_ref())
80            .await
81            .map_err(crate::Error::Bmc)
82            .map(|data| Self {
83                data,
84                _marker: PhantomData,
85            })
86    }
87
88    /// Get the raw schema data for this network device function.
89    #[must_use]
90    pub fn raw(&self) -> Arc<NetworkDeviceFunctionSchema> {
91        self.data.clone()
92    }
93
94    /// The permanent MAC address assigned to this function.
95    pub fn ethernet_permanent_mac_address(&self) -> Option<MacAddress<'_>> {
96        self.data
97            .ethernet
98            .as_ref()
99            .and_then(|eth| eth.permanent_mac_address.as_ref())
100            .and_then(Option::as_deref)
101            .map(MacAddress::new)
102    }
103}
104
105impl<B: Bmc> Resource for NetworkDeviceFunction<B> {
106    fn resource_ref(&self) -> &ResourceSchema {
107        &self.data.as_ref().base
108    }
109}