nv_redfish/
service_root.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
16use crate::schema::redfish::service_root::ServiceRoot as SchemaServiceRoot;
17use crate::Error;
18use crate::NvBmc;
19use crate::ProtocolFeatures;
20use crate::Resource;
21use crate::ResourceSchema;
22use nv_redfish_core::Bmc;
23use nv_redfish_core::NavProperty;
24use nv_redfish_core::ODataId;
25use std::sync::Arc;
26use tagged_types::TaggedType;
27
28#[cfg(feature = "accounts")]
29use crate::account::AccountService;
30#[cfg(feature = "accounts")]
31use crate::account::SlotDefinedConfig as SlotDefinedUserAccountsConfig;
32#[cfg(feature = "chassis")]
33use crate::chassis::ChassisCollection;
34#[cfg(feature = "computer-systems")]
35use crate::computer_system::SystemCollection;
36#[cfg(feature = "managers")]
37use crate::manager::ManagerCollection;
38#[cfg(feature = "update-service")]
39use crate::update_service::UpdateService;
40
41/// The vendor or manufacturer associated with Redfish service.
42pub type Vendor<T> = TaggedType<T, VendorTag>;
43#[doc(hidden)]
44#[derive(tagged_types::Tag)]
45#[implement(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
46#[transparent(Debug, Display, Serialize, Deserialize)]
47#[capability(inner_access, cloned)]
48pub enum VendorTag {}
49
50/// The product associated with Redfish service..
51pub type Product<T> = TaggedType<T, ProductTag>;
52#[doc(hidden)]
53#[derive(tagged_types::Tag)]
54#[implement(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
55#[transparent(Debug, Display, Serialize, Deserialize)]
56#[capability(inner_access, cloned)]
57pub enum ProductTag {}
58
59/// Represents `ServiceRoot` in the BMC model.
60#[derive(Clone)]
61pub struct ServiceRoot<B: Bmc> {
62    /// Content of the root.
63    pub root: Arc<SchemaServiceRoot>,
64    #[allow(dead_code)] // feature-enabled field
65    bmc: NvBmc<B>,
66}
67
68impl<B: Bmc> ServiceRoot<B> {
69    /// Create a new service root.
70    ///
71    /// # Errors
72    ///
73    /// Returns error if retrieving the root path via Redfish fails.
74    pub async fn new(bmc: Arc<B>) -> Result<Self, Error<B>> {
75        let root = NavProperty::<SchemaServiceRoot>::new_reference(ODataId::service_root())
76            .get(bmc.as_ref())
77            .await
78            .map_err(Error::Bmc)?;
79        let mut protocol_features = root
80            .protocol_features_supported
81            .as_ref()
82            .map(ProtocolFeatures::new)
83            .unwrap_or_default();
84
85        if Self::expand_is_not_working_properly(&root) {
86            protocol_features.expand.expand_all = false;
87            protocol_features.expand.no_links = false;
88        }
89
90        let bmc = NvBmc::new(bmc, protocol_features);
91        Ok(Self { root, bmc })
92    }
93
94    /// The vendor or manufacturer associated with this Redfish service.
95    pub fn vendor(&self) -> Option<Vendor<&String>> {
96        self.root
97            .vendor
98            .as_ref()
99            .and_then(Option::as_ref)
100            .map(Vendor::new)
101    }
102
103    /// The product associated with this Redfish service.
104    pub fn product(&self) -> Option<Product<&String>> {
105        self.root
106            .product
107            .as_ref()
108            .and_then(Option::as_ref)
109            .map(Product::new)
110    }
111
112    /// Get the account service belonging to the BMC.
113    ///
114    /// # Errors
115    ///
116    /// Returns error if retrieving account service data fails.
117    #[cfg(feature = "accounts")]
118    pub async fn account_service(&self) -> Result<AccountService<B>, Error<B>> {
119        AccountService::new(&self.bmc, self).await
120    }
121
122    /// Get chassis collection in BMC
123    ///
124    /// # Errors
125    ///
126    /// Returns error if chassis list is not avaiable in BMC
127    #[cfg(feature = "chassis")]
128    pub async fn chassis(&self) -> Result<ChassisCollection<B>, Error<B>> {
129        ChassisCollection::new(&self.bmc, self).await
130    }
131
132    /// Get computer system collection in BMC
133    ///
134    /// # Errors
135    ///
136    /// Returns error if system list is not available in BMC
137    #[cfg(feature = "computer-systems")]
138    pub async fn systems(&self) -> Result<SystemCollection<B>, Error<B>> {
139        SystemCollection::new(&self.bmc, self).await
140    }
141
142    /// Get update service in BMC
143    ///
144    /// # Errors
145    ///
146    /// Returns error if update service is not available in BMC
147    #[cfg(feature = "update-service")]
148    pub async fn update_service(&self) -> Result<UpdateService<B>, Error<B>> {
149        UpdateService::new(&self.bmc, self).await
150    }
151
152    /// Get manager collection in BMC
153    ///
154    /// # Errors
155    ///
156    /// Returns error if manager list is not available in BMC
157    #[cfg(feature = "managers")]
158    pub async fn managers(&self) -> Result<ManagerCollection<B>, Error<B>> {
159        ManagerCollection::new(&self.bmc, self).await
160    }
161}
162
163// Known Redfish implementation bug checks.
164impl<B: Bmc> ServiceRoot<B> {
165    // Account type is required according to schema specification
166    // (marked with Redfish.Required annotation) but some vendors
167    // ignores this flag. A workaround for this bug is supported by
168    // `nv-redfish`.
169    #[cfg(feature = "accounts")]
170    pub(crate) fn bug_no_account_type_in_accounts(&self) -> bool {
171        self.root
172            .vendor
173            .as_ref()
174            .and_then(Option::as_ref)
175            .is_some_and(|v| v == "HPE")
176    }
177
178    // In some implementations BMC cannot create / delete Redfish
179    // accounts but have pre-created accounts (slots). Workflow is as
180    // following: to "create" new account user should update
181    // precreated account with new parameters and enable it. To delete
182    // account user should just disable it.
183    #[cfg(feature = "accounts")]
184    pub(crate) fn slot_defined_user_accounts(&self) -> Option<SlotDefinedUserAccountsConfig> {
185        if self
186            .root
187            .vendor
188            .as_ref()
189            .and_then(Option::as_ref)
190            .is_some_and(|v| v == "Dell")
191        {
192            Some(SlotDefinedUserAccountsConfig {
193                min_slot: Some(3),
194                hide_disabled: true,
195                disable_account_on_delete: true,
196            })
197        } else {
198            None
199        }
200    }
201
202    // In some implementations BMC ReleaseDate is incorrectly set to
203    // 00:00:00Z in FirmwareInventory (which is
204    // SoftwareInventoryCollection).
205    #[cfg(feature = "update-service")]
206    pub(crate) fn fw_inventory_wrong_release_date(&self) -> bool {
207        self.root
208            .vendor
209            .as_ref()
210            .and_then(Option::as_ref)
211            .is_some_and(|v| v == "Dell")
212    }
213
214    /// In some cases thre is addtional fields in Links.ContainedBy in
215    /// Chassis resource, this flag aims to patch this invalid links
216    #[cfg(feature = "chassis")]
217    pub(crate) fn bug_invalid_contained_by_fields(&self) -> bool {
218        self.root
219            .vendor
220            .as_ref()
221            .and_then(Option::as_ref)
222            .is_some_and(|v| v == "AMI")
223            && self
224                .root
225                .redfish_version
226                .as_ref()
227                .is_some_and(|version| version == "1.11.0")
228    }
229
230    /// In some implementations BMC ReleaseDate is incorrectly set to
231    /// "0000-00-00T00:00:00+00:00" in ComputerSystem/LastResetTime
232    /// This prevents ComputerSystem to be correctly parsed because
233    /// this is invalid Edm.DateTimeOffset.
234    #[cfg(feature = "computer-systems")]
235    pub(crate) fn computer_systems_wrong_last_reset_time(&self) -> bool {
236        self.root
237            .vendor
238            .as_ref()
239            .and_then(Option::as_ref)
240            .is_some_and(|v| v == "Dell")
241    }
242
243    /// In some cases we expand is not working according to spec,
244    /// if it is the case for specific chassis, we would disable
245    /// expand api
246    fn expand_is_not_working_properly(root: &SchemaServiceRoot) -> bool {
247        root.vendor
248            .as_ref()
249            .and_then(Option::as_ref)
250            .is_some_and(|v| v == "AMI")
251            && root
252                .redfish_version
253                .as_ref()
254                .is_some_and(|version| version == "1.11.0")
255    }
256}
257
258impl<B: Bmc> Resource for ServiceRoot<B> {
259    fn resource_ref(&self) -> &ResourceSchema {
260        &self.root.as_ref().base
261    }
262}