Skip to main content

nv_redfish/oem/lenovo/
computer_system.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//! Support Lenovo Computer System OEM extension.
17
18use crate::core::Bmc;
19use crate::oem::lenovo::schema::redfish::lenovo_computer_system::LenovoSystemProperties as LenovoSystemPropertiesSchema;
20use crate::schema::redfish::computer_system::ComputerSystem as ComputerSystemSchema;
21use crate::Error;
22use crate::NvBmc;
23use std::convert::identity;
24use std::marker::PhantomData;
25use std::sync::Arc;
26
27#[doc(inline)]
28pub use crate::oem::lenovo::schema::redfish::lenovo_computer_system::FpMode;
29#[doc(inline)]
30pub use crate::oem::lenovo::schema::redfish::lenovo_computer_system::PortSwitchingTo;
31
32/// Dell OEM Attributes.
33pub struct LenovoComputerSystem<B: Bmc> {
34    data: Arc<LenovoSystemPropertiesSchema>,
35    _marker: PhantomData<B>,
36}
37
38impl<B: Bmc> LenovoComputerSystem<B> {
39    /// Create Lenovo OEM computer system.
40    ///
41    /// Returns `Ok(None)` when the system does not include `Oem.Lenovo`.
42    ///
43    /// # Errors
44    ///
45    /// Returns an error if parsing Lenovo computer system OEM data fails.
46    pub(crate) fn new(
47        _bmc: &NvBmc<B>,
48        computer_system: &ComputerSystemSchema,
49    ) -> Result<Option<Self>, Error<B>> {
50        if let Some(oem) = computer_system
51            .base
52            .base
53            .oem
54            .as_ref()
55            .and_then(|oem| oem.additional_properties.get("Lenovo"))
56        {
57            let data = Arc::new(serde_json::from_value(oem.clone()).map_err(Error::Json)?);
58            Ok(Some(Self {
59                data,
60                _marker: PhantomData,
61            }))
62        } else {
63            Ok(None)
64        }
65    }
66
67    /// Get the raw schema data for this Lenovo Computer system.
68    ///
69    /// Returns an `Arc` to the underlying schema, allowing cheap cloning
70    /// and sharing of the data.
71    #[must_use]
72    pub fn raw(&self) -> Arc<LenovoSystemPropertiesSchema> {
73        self.data.clone()
74    }
75
76    /// Front panel mode.
77    pub fn front_panel_mode(&self) -> Option<FpMode> {
78        self.data
79            .usb_management_port_assignment
80            .as_ref()
81            .or_else(|| self.data.front_panel_usb.as_ref())
82            .and_then(Option::as_ref)
83            .and_then(|v| v.fp_mode)
84            .and_then(identity)
85    }
86
87    /// USB management port switching direction.
88    pub fn port_switching_to(&self) -> Option<PortSwitchingTo> {
89        self.data
90            .usb_management_port_assignment
91            .as_ref()
92            .or_else(|| self.data.front_panel_usb.as_ref())
93            .and_then(Option::as_ref)
94            .and_then(|v| v.port_switching_to)
95            .and_then(identity)
96    }
97}