1use std::collections::HashMap;
2
3use crate::usp::mod_Body::OneOfmsg_body::{request, response};
4use crate::usp::mod_Notify::mod_OperationComplete::OneOfoperation_resp;
5use crate::usp::mod_Notify::OneOfnotification::{
6 event, obj_creation, obj_deletion, on_board_req, oper_complete, value_change,
7};
8use crate::usp::mod_Notify::{
9 Event, ObjectCreation, ObjectDeletion, OnBoardRequest, OperationComplete, ValueChange,
10};
11use crate::usp::mod_Request::OneOfreq_type::notify;
12use crate::usp::mod_Response::OneOfresp_type::notify_resp;
13use crate::usp::{Body, Notify, Request};
14use crate::usp::{NotifyResp, Response};
15
16use anyhow::{Context, Result};
17
18#[derive(Clone)]
19pub enum OperationCompleteType {
20 OutputArgs(HashMap<String, String>),
21 CommandFailure(u32, String),
22}
23
24#[derive(Clone)]
25pub enum NotifyType {
26 OnBoardRequest {
28 oui: String,
30
31 product_class: String,
33
34 serial_number: String,
36
37 agent_supported_protocol_versions: String,
39 },
40 ValueChange {
42 param_path: String,
44 param_value: String,
46 },
47 Event {
49 obj_path: String,
51 event_name: String,
53 params: HashMap<String, String>,
55 },
56 ObjectCreation {
58 obj_path: String,
60 unique_keys: HashMap<String, String>,
62 },
63 ObjectDeletion {
65 obj_path: String,
67 },
68
69 OperationComplete {
71 obj_path: String,
73 command_name: String,
75 command_key: String,
77 operation_resp: OperationCompleteType,
79 },
80}
81
82#[derive(Clone)]
83pub struct NotifyBuilder {
84 subscription_id: String,
85 send_resp: bool,
86 notify_type: Option<NotifyType>,
87}
88
89impl NotifyBuilder {
90 #[must_use]
91 pub const fn new(subscription_id: String) -> Self {
92 Self {
93 subscription_id,
94 send_resp: false,
95 notify_type: None,
96 }
97 }
98
99 #[must_use]
100 pub const fn with_send_resp(mut self, send_resp: bool) -> Self {
101 self.send_resp = send_resp;
102 self
103 }
104
105 #[must_use]
106 pub fn with_onboard_request(
107 mut self,
108 oui: String,
109 product_class: String,
110 serial_number: String,
111 aspv: String,
112 ) -> Self {
113 self.notify_type = Some(NotifyType::OnBoardRequest {
114 oui,
115 product_class,
116 serial_number,
117 agent_supported_protocol_versions: aspv,
118 });
119 self
120 }
121
122 #[must_use]
123 pub fn with_value_change(mut self, param_path: String, param_value: String) -> Self {
124 self.notify_type = Some(NotifyType::ValueChange {
125 param_path,
126 param_value,
127 });
128 self
129 }
130
131 #[must_use]
132 pub fn with_event(
133 mut self,
134 obj_path: String,
135 event_name: String,
136 params: HashMap<String, String>,
137 ) -> Self {
138 self.notify_type = Some(NotifyType::Event {
139 obj_path,
140 event_name,
141 params,
142 });
143 self
144 }
145
146 #[must_use]
147 pub fn with_object_creation(
148 mut self,
149 obj_path: String,
150 unique_keys: HashMap<String, String>,
151 ) -> Self {
152 self.notify_type = Some(NotifyType::ObjectCreation {
153 obj_path,
154 unique_keys,
155 });
156 self
157 }
158
159 #[must_use]
160 pub fn with_object_deletion(mut self, obj_path: String) -> Self {
161 self.notify_type = Some(NotifyType::ObjectDeletion { obj_path });
162 self
163 }
164
165 #[must_use]
166 pub fn with_operation_complete_output_args(
167 mut self,
168 obj_path: String,
169 command_name: String,
170 command_key: String,
171 output_args: HashMap<String, String>,
172 ) -> Self {
173 self.notify_type = Some(NotifyType::OperationComplete {
174 obj_path,
175 command_name,
176 command_key,
177 operation_resp: OperationCompleteType::OutputArgs(output_args),
178 });
179 self
180 }
181
182 #[must_use]
183 pub fn with_operation_complete_cmd_failure(
184 mut self,
185 obj_path: String,
186 command_name: String,
187 command_key: String,
188 err_code: u32,
189 err_msg: String,
190 ) -> Self {
191 self.notify_type = Some(NotifyType::OperationComplete {
192 obj_path,
193 command_name,
194 command_key,
195 operation_resp: OperationCompleteType::CommandFailure(err_code, err_msg),
196 });
197 self
198 }
199
200 pub fn build(self) -> Result<Body> {
201 let notify_type = self
202 .notify_type
203 .context("Must specify a notification type")?;
204
205 let notify_type = match notify_type {
206 NotifyType::OnBoardRequest {
207 oui,
208 product_class,
209 serial_number,
210 agent_supported_protocol_versions,
211 } => on_board_req(OnBoardRequest {
212 oui,
213 product_class,
214 serial_number,
215 agent_supported_protocol_versions,
216 }),
217 NotifyType::ValueChange {
218 param_path,
219 param_value,
220 } => value_change(ValueChange {
221 param_path,
222 param_value,
223 }),
224 NotifyType::Event {
225 obj_path,
226 event_name,
227 params,
228 } => event(Event {
229 obj_path,
230 event_name,
231 params: params.into_iter().collect::<HashMap<_, _>>(),
232 }),
233 NotifyType::ObjectCreation {
234 obj_path,
235 unique_keys,
236 } => obj_creation(ObjectCreation {
237 obj_path,
238 unique_keys: unique_keys.into_iter().collect::<HashMap<_, _>>(),
239 }),
240 NotifyType::ObjectDeletion { obj_path } => obj_deletion(ObjectDeletion { obj_path }),
241 NotifyType::OperationComplete {
242 obj_path,
243 command_name,
244 command_key,
245 operation_resp,
246 } => oper_complete(OperationComplete {
247 obj_path,
248 command_name,
249 command_key,
250 operation_resp: match operation_resp {
251 OperationCompleteType::OutputArgs(h) => OneOfoperation_resp::req_output_args(
252 crate::usp::mod_Notify::mod_OperationComplete::OutputArgs {
253 output_args: h.into_iter().collect::<HashMap<_, _>>(),
254 },
255 ),
256 OperationCompleteType::CommandFailure(code, msg) => {
257 OneOfoperation_resp::cmd_failure(
258 crate::usp::mod_Notify::mod_OperationComplete::CommandFailure {
259 err_code: code,
260 err_msg: msg,
261 },
262 )
263 }
264 },
265 }),
266 };
267
268 Ok(Body {
269 msg_body: request({
270 Request {
271 req_type: notify({
272 Notify {
273 subscription_id: self.subscription_id,
274 send_resp: self.send_resp,
275 notification: notify_type,
276 }
277 }),
278 }
279 }),
280 })
281 }
282}
283
284#[derive(Clone)]
285pub struct NotifyRespBuilder {
286 subscription_id: String,
287}
288
289impl NotifyRespBuilder {
290 #[must_use]
291 pub const fn new(subscription_id: String) -> Self {
292 Self { subscription_id }
293 }
294
295 pub fn build(self) -> Result<Body> {
296 Ok(Body {
297 msg_body: response({
298 Response {
299 resp_type: notify_resp({
300 NotifyResp {
301 subscription_id: self.subscription_id,
302 }
303 }),
304 }
305 }),
306 })
307 }
308}