nv_redfish/chassis/power_supply.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::resource::ResetType;
17use crate::schema::power_supply::PowerSupply as PowerSupplySchema;
18use crate::schema::power_supply_metrics::PowerSupplyMetrics;
19use crate::Error;
20use crate::NvBmc;
21use crate::Resource;
22use crate::ResourceSchema;
23use nv_redfish_core::Bmc;
24use nv_redfish_core::ModificationResponse;
25use nv_redfish_core::NavProperty;
26use std::sync::Arc;
27
28#[cfg(feature = "sensors")]
29use crate::extract_sensor_uris;
30#[cfg(feature = "oem-delta")]
31use crate::oem::delta::DeltaPowerSupply;
32#[cfg(feature = "sensors")]
33use crate::sensor::SensorLink;
34#[cfg(feature = "oem-delta")]
35use std::convert::identity;
36
37/// Represents a power supply in a chassis.
38///
39/// Provides access to power supply information and associated metrics/sensors.
40pub struct PowerSupply<B: Bmc> {
41 bmc: NvBmc<B>,
42 data: Arc<PowerSupplySchema>,
43}
44
45impl<B: Bmc> PowerSupply<B> {
46 /// Create a new power supply handle.
47 pub(crate) async fn new(
48 bmc: &NvBmc<B>,
49 nav: &NavProperty<PowerSupplySchema>,
50 ) -> Result<Self, Error<B>> {
51 nav.get(bmc.as_ref())
52 .await
53 .map_err(Error::Bmc)
54 .map(|data| Self {
55 bmc: bmc.clone(),
56 data,
57 })
58 }
59
60 /// Get the raw schema data for this power supply.
61 ///
62 /// Returns an `Arc` to the underlying schema, allowing cheap cloning
63 /// and sharing of the data.
64 #[must_use]
65 pub fn raw(&self) -> Arc<PowerSupplySchema> {
66 self.data.clone()
67 }
68
69 /// Reset this power supply.
70 ///
71 /// # Errors
72 ///
73 /// Returns an error if the power supply does not support the `Reset`
74 /// action or if invoking the action fails.
75 pub async fn reset(
76 &self,
77 reset_type: Option<ResetType>,
78 ) -> Result<ModificationResponse<()>, Error<B>>
79 where
80 B::Error: nv_redfish_core::ActionError,
81 {
82 let actions = self
83 .data
84 .actions
85 .as_ref()
86 .ok_or(Error::ActionNotAvailable)?;
87
88 if actions.reset.is_none() {
89 return Err(Error::ActionNotAvailable);
90 }
91
92 actions
93 .reset(self.bmc.as_ref(), reset_type)
94 .await
95 .map_err(Error::Bmc)
96 }
97
98 /// Get power supply metrics.
99 ///
100 /// Returns the power supply's performance and state metrics if available.
101 ///
102 /// # Errors
103 ///
104 /// Returns an error if:
105 /// - The power supply does not have metrics
106 /// - Fetching metrics data fails
107 pub async fn metrics(&self) -> Result<Option<Arc<PowerSupplyMetrics>>, Error<B>> {
108 if let Some(metrics_ref) = &self.data.metrics {
109 metrics_ref
110 .get(self.bmc.as_ref())
111 .await
112 .map_err(Error::Bmc)
113 .map(Some)
114 } else {
115 Ok(None)
116 }
117 }
118
119 /// Get the metrics sensors for this power supply.
120 ///
121 /// Returns a vector of `Sensor<B>` obtained from metrics metrics, if available.
122 /// # Errors
123 ///
124 /// Returns an error if get of metrics failed.
125 #[cfg(feature = "sensors")]
126 pub async fn metrics_sensor_links(&self) -> Result<Vec<SensorLink<B>>, Error<B>> {
127 let sensor_refs = if let Some(metrics_ref) = &self.data.metrics {
128 metrics_ref
129 .get(self.bmc.as_ref())
130 .await
131 .map_err(Error::Bmc)
132 .map(|m| {
133 extract_sensor_uris!(m,
134 single: input_voltage,
135 single: input_current_amps,
136 single: input_power_watts,
137 single: energyk_wh,
138 single: frequency_hz,
139 single: output_power_watts,
140 single: temperature_celsius,
141 single: fan_speed_percent,
142 vec: rail_voltage,
143 vec: rail_current_amps,
144 vec: rail_power_watts,
145 vec: fan_speeds_percent
146 )
147 })?
148 } else {
149 Vec::new()
150 };
151
152 Ok(sensor_refs
153 .into_iter()
154 .map(|r| SensorLink::new(&self.bmc, r))
155 .collect())
156 }
157
158 /// Delta Energy Systems OEM extension for this power supply.
159 ///
160 /// Delta power shelves report per-PSU power state under
161 /// `Oem/deltaenergysystems` rather than the standard `PowerState` field.
162 ///
163 /// Returns `Ok(None)` when the power supply does not include Delta OEM
164 /// extension data.
165 ///
166 /// # Errors
167 ///
168 /// Returns an error if parsing the Delta OEM data fails.
169 #[cfg(feature = "oem-delta")]
170 pub fn oem_delta(&self) -> Result<Option<DeltaPowerSupply<B>>, Error<B>> {
171 self.data
172 .base
173 .base
174 .oem
175 .as_ref()
176 .map(DeltaPowerSupply::new)
177 .transpose()
178 .map(|v| v.and_then(identity))
179 }
180}
181
182impl<B: Bmc> Resource for PowerSupply<B> {
183 fn resource_ref(&self) -> &ResourceSchema {
184 &self.data.as_ref().base
185 }
186}