Skip to main content

nv_redfish/port/
item.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
16use crate::mac_address::MacAddress;
17use crate::schema::port::Port as PortSchema;
18use crate::Error;
19use crate::NvBmc;
20use crate::Resource;
21use crate::ResourceSchema;
22use nv_redfish_core::Bmc;
23use nv_redfish_core::NavProperty;
24use std::marker::PhantomData;
25use std::sync::Arc;
26
27#[cfg(feature = "oem-lenovo")]
28use crate::oem::lenovo::port::LenovoPort;
29
30/// Network port.
31pub struct Port<B: Bmc> {
32    data: Arc<PortSchema>,
33    _marker: PhantomData<B>,
34}
35
36impl<B: Bmc> Port<B> {
37    pub(crate) async fn new(
38        bmc: &NvBmc<B>,
39        nav: &NavProperty<PortSchema>,
40    ) -> Result<Self, Error<B>> {
41        nav.get(bmc.as_ref())
42            .await
43            .map_err(Error::Bmc)
44            .map(|data| Self {
45                data,
46                _marker: PhantomData,
47            })
48    }
49
50    /// Get the raw schema data for this port.
51    #[must_use]
52    pub fn raw(&self) -> Arc<PortSchema> {
53        self.data.clone()
54    }
55
56    /// Get the standard Ethernet MAC addresses associated with this port.
57    ///
58    /// Returns the values reported in `Ethernet.AssociatedMACAddresses`.
59    #[must_use]
60    pub fn associated_mac_addresses(&self) -> Vec<MacAddress<'_>> {
61        self.data
62            .ethernet
63            .as_ref()
64            .and_then(Option::as_ref)
65            .and_then(|ethernet| ethernet.associated_mac_addresses.as_ref())
66            .and_then(Option::as_ref)
67            .map(|addresses| {
68                addresses
69                    .iter()
70                    .map(String::as_str)
71                    .map(MacAddress::new)
72                    .collect()
73            })
74            .unwrap_or_default()
75    }
76
77    /// Get Lenovo Port OEM data.
78    ///
79    /// Returns `Ok(None)` when the port does not include `Oem.Lenovo`.
80    ///
81    /// # Errors
82    ///
83    /// Returns an error if parsing Lenovo port OEM data fails.
84    #[cfg(feature = "oem-lenovo")]
85    pub fn oem_lenovo(&self) -> Result<Option<LenovoPort>, Error<B>> {
86        LenovoPort::new(&self.data).map_err(Error::Json)
87    }
88}
89
90impl<B: Bmc> Resource for Port<B> {
91    fn resource_ref(&self) -> &ResourceSchema {
92        &self.data.as_ref().base
93    }
94}