Skip to main content

nv_redfish/chassis/
network_adapter.rs

1// SPDX-FileCopyrightText: Copyright (c) 2025 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 adapters
17
18use crate::hardware_id::HardwareIdRef;
19use crate::hardware_id::Manufacturer as HardwareIdManufacturer;
20use crate::hardware_id::Model as HardwareIdModel;
21use crate::hardware_id::PartNumber as HardwareIdPartNumber;
22use crate::hardware_id::SerialNumber as HardwareIdSerialNumber;
23use crate::schema::redfish::network_adapter::NetworkAdapter as NetworkAdapterSchema;
24use crate::schema::redfish::network_adapter_collection::NetworkAdapterCollection as NetworkAdapterCollectionSchema;
25use crate::Error;
26use crate::NvBmc;
27use crate::Resource;
28use crate::ResourceSchema;
29use nv_redfish_core::Bmc;
30use nv_redfish_core::NavProperty;
31use std::sync::Arc;
32
33#[cfg(feature = "network-device-functions")]
34use crate::network_device_function::NetworkDeviceFunctionCollection;
35
36/// Network adapters collection.
37///
38/// Provides functions to access collection members.
39pub struct NetworkAdapterCollection<B: Bmc> {
40    bmc: NvBmc<B>,
41    collection: Arc<NetworkAdapterCollectionSchema>,
42}
43
44impl<B: Bmc> NetworkAdapterCollection<B> {
45    /// Create a new manager collection handle.
46    pub(crate) async fn new(
47        bmc: &NvBmc<B>,
48        nav: &NavProperty<NetworkAdapterCollectionSchema>,
49    ) -> Result<Self, Error<B>> {
50        let collection = bmc.expand_property(nav).await?;
51        Ok(Self {
52            bmc: bmc.clone(),
53            collection,
54        })
55    }
56
57    /// List all managers available in this BMC.
58    ///
59    /// # Errors
60    ///
61    /// Returns an error if fetching manager data fails.
62    pub async fn members(&self) -> Result<Vec<NetworkAdapter<B>>, Error<B>> {
63        let mut members = Vec::new();
64        for m in &self.collection.members {
65            members.push(NetworkAdapter::new(&self.bmc, m).await?);
66        }
67        Ok(members)
68    }
69}
70
71#[doc(hidden)]
72pub enum NetworkAdapterTag {}
73
74/// Network adapter manufacturer.
75pub type Manufacturer<T> = HardwareIdManufacturer<T, NetworkAdapterTag>;
76
77/// Network adapter model.
78pub type Model<T> = HardwareIdModel<T, NetworkAdapterTag>;
79
80/// Network adapter part number.
81pub type PartNumber<T> = HardwareIdPartNumber<T, NetworkAdapterTag>;
82
83/// Network adapter serial number.
84pub type SerialNumber<T> = HardwareIdSerialNumber<T, NetworkAdapterTag>;
85
86/// Network Adapter.
87///
88/// Provides functions to access log entries and perform log operations.
89pub struct NetworkAdapter<B: Bmc> {
90    #[allow(dead_code)] // used if any feature enabled.
91    bmc: NvBmc<B>,
92    data: Arc<NetworkAdapterSchema>,
93}
94
95impl<B: Bmc> NetworkAdapter<B> {
96    /// Create a new log service handle.
97    pub(crate) async fn new(
98        bmc: &NvBmc<B>,
99        nav: &NavProperty<NetworkAdapterSchema>,
100    ) -> Result<Self, Error<B>> {
101        nav.get(bmc.as_ref())
102            .await
103            .map_err(crate::Error::Bmc)
104            .map(|data| Self {
105                bmc: bmc.clone(),
106                data,
107            })
108    }
109
110    /// Get the raw schema data for this ethernet adapter.
111    #[must_use]
112    pub fn raw(&self) -> Arc<NetworkAdapterSchema> {
113        self.data.clone()
114    }
115
116    /// Get hardware identifier of the network adpater.
117    #[must_use]
118    pub fn hardware_id(&self) -> HardwareIdRef<'_, NetworkAdapterTag> {
119        HardwareIdRef {
120            manufacturer: self
121                .data
122                .manufacturer
123                .as_ref()
124                .and_then(Option::as_deref)
125                .map(Manufacturer::new),
126            model: self
127                .data
128                .model
129                .as_ref()
130                .and_then(Option::as_deref)
131                .map(Model::new),
132            part_number: self
133                .data
134                .part_number
135                .as_ref()
136                .and_then(Option::as_deref)
137                .map(PartNumber::new),
138            serial_number: self
139                .data
140                .serial_number
141                .as_ref()
142                .and_then(Option::as_deref)
143                .map(SerialNumber::new),
144        }
145    }
146
147    /// Get network device functions for this adapter.
148    ///
149    /// Returns `Ok(None)` when the network device functions link is absent.
150    ///
151    /// # Errors
152    ///
153    /// Returns an error if fetching network device functions data fails.
154    #[cfg(feature = "network-device-functions")]
155    pub async fn network_device_functions(
156        &self,
157    ) -> Result<Option<NetworkDeviceFunctionCollection<B>>, Error<B>> {
158        if let Some(p) = &self.data.network_device_functions {
159            NetworkDeviceFunctionCollection::new(&self.bmc, p)
160                .await
161                .map(Some)
162        } else {
163            Ok(None)
164        }
165    }
166}
167
168impl<B: Bmc> Resource for NetworkAdapter<B> {
169    fn resource_ref(&self) -> &ResourceSchema {
170        &self.data.as_ref().base
171    }
172}