rs-pfcp 0.4.0

High-performance Rust implementation of PFCP (Packet Forwarding Control Protocol) for 5G networks with 100% 3GPP TS 29.244 Release 18 compliance
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
// src/ie/update_qer.rs

//! Update QER Information Element.

use crate::error::PfcpError;
use crate::ie::gate_status::GateStatus;
use crate::ie::gbr::Gbr;
use crate::ie::mbr::Mbr;
use crate::ie::qer_correlation_id::QerCorrelationId;
use crate::ie::qer_id::QerId;
use crate::ie::{marshal_ies, Ie, IeIterator, IeType};

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UpdateQer {
    pub qer_id: QerId,
    pub qer_correlation_id: Option<QerCorrelationId>,
    pub gate_status: Option<GateStatus>,
    pub mbr: Option<Mbr>,
    pub gbr: Option<Gbr>,
}

impl UpdateQer {
    pub fn new(
        qer_id: QerId,
        qer_correlation_id: Option<QerCorrelationId>,
        gate_status: Option<GateStatus>,
        mbr: Option<Mbr>,
        gbr: Option<Gbr>,
    ) -> Self {
        UpdateQer {
            qer_id,
            qer_correlation_id,
            gate_status,
            mbr,
            gbr,
        }
    }

    pub fn marshal(&self) -> Vec<u8> {
        let mut ies = vec![self.qer_id.to_ie()];
        if let Some(qer_corr_id) = &self.qer_correlation_id {
            ies.push(Ie::new(
                IeType::QerCorrelationId,
                qer_corr_id.marshal().to_vec(),
            ));
        }
        if let Some(gate_status) = &self.gate_status {
            ies.push(Ie::new(IeType::GateStatus, gate_status.marshal().to_vec()));
        }
        if let Some(mbr) = &self.mbr {
            ies.push(Ie::new(IeType::Mbr, mbr.marshal().to_vec()));
        }
        if let Some(gbr) = &self.gbr {
            ies.push(Ie::new(IeType::Gbr, gbr.marshal().to_vec()));
        }
        marshal_ies(&ies)
    }

    pub fn unmarshal(payload: &[u8]) -> Result<Self, PfcpError> {
        let mut qer_id = None;
        let mut qer_correlation_id = None;
        let mut gate_status = None;
        let mut mbr = None;
        let mut gbr = None;

        for ie_result in IeIterator::new(payload) {
            let ie = ie_result?;
            match ie.ie_type {
                IeType::QerId => qer_id = Some(QerId::unmarshal(&ie.payload)?),
                IeType::QerCorrelationId => {
                    qer_correlation_id = Some(QerCorrelationId::unmarshal(&ie.payload)?)
                }
                IeType::GateStatus => gate_status = Some(GateStatus::unmarshal(&ie.payload)?),
                IeType::Mbr => mbr = Some(Mbr::unmarshal(&ie.payload)?),
                IeType::Gbr => gbr = Some(Gbr::unmarshal(&ie.payload)?),
                _ => (),
            }
        }

        Ok(UpdateQer {
            qer_id: qer_id.ok_or_else(|| {
                PfcpError::missing_ie_in_grouped(IeType::QerId, IeType::UpdateQer)
            })?,
            qer_correlation_id,
            gate_status,
            mbr,
            gbr,
        })
    }

    pub fn to_ie(&self) -> Ie {
        Ie::new(IeType::UpdateQer, self.marshal())
    }

    /// Returns a builder for constructing Update QER instances.
    pub fn builder(qer_id: QerId) -> UpdateQerBuilder {
        UpdateQerBuilder::new(qer_id)
    }
}

/// Builder for Update QER Information Elements.
///
/// The Update QER builder provides an ergonomic way to construct QER update IEs
/// for modifying existing QoS enforcement rules.
///
/// # Examples
///
/// ```rust
/// use rs_pfcp::ie::update_qer::UpdateQerBuilder;
/// use rs_pfcp::ie::qer_id::QerId;
/// use rs_pfcp::ie::gate_status::{GateStatus, GateStatusValue};
/// use rs_pfcp::ie::mbr::Mbr;
///
/// // Update QER to change gate status
/// let qer = UpdateQerBuilder::new(QerId::new(1))
///     .gate_status(GateStatus::new(GateStatusValue::Closed, GateStatusValue::Closed))
///     .build()
///     .unwrap();
///
/// // Update QER with new rate limits
/// let rate_limited_qer = UpdateQerBuilder::new(QerId::new(2))
///     .mbr(Mbr::new(2000000, 4000000)) // 2Mbps up, 4Mbps down
///     .build()
///     .unwrap();
///
/// // Using convenience methods
/// let open_qer = UpdateQerBuilder::open_gate(QerId::new(3)).build().unwrap();
/// let closed_qer = UpdateQerBuilder::closed_gate(QerId::new(4)).build().unwrap();
/// ```
#[derive(Debug, Default)]
pub struct UpdateQerBuilder {
    qer_id: Option<QerId>,
    qer_correlation_id: Option<QerCorrelationId>,
    gate_status: Option<GateStatus>,
    mbr: Option<Mbr>,
    gbr: Option<Gbr>,
}

impl UpdateQerBuilder {
    /// Creates a new Update QER builder with the specified QER ID.
    pub fn new(qer_id: QerId) -> Self {
        UpdateQerBuilder {
            qer_id: Some(qer_id),
            ..Default::default()
        }
    }

    /// Sets the QER correlation ID for tracking across multiple nodes.
    pub fn qer_correlation_id(mut self, qer_correlation_id: QerCorrelationId) -> Self {
        self.qer_correlation_id = Some(qer_correlation_id);
        self
    }

    /// Sets the gate status for uplink and downlink traffic control.
    pub fn gate_status(mut self, gate_status: GateStatus) -> Self {
        self.gate_status = Some(gate_status);
        self
    }

    /// Sets the Maximum Bit Rate (MBR) for rate limiting.
    pub fn mbr(mut self, mbr: Mbr) -> Self {
        self.mbr = Some(mbr);
        self
    }

    /// Sets the Guaranteed Bit Rate (GBR) for QoS guarantees.
    pub fn gbr(mut self, gbr: Gbr) -> Self {
        self.gbr = Some(gbr);
        self
    }

    /// Sets both uplink and downlink rates with the same values for MBR.
    pub fn rate_limit(mut self, uplink_bps: u64, downlink_bps: u64) -> Self {
        self.mbr = Some(Mbr::new(uplink_bps, downlink_bps));
        self
    }

    /// Sets guaranteed bit rates for both directions.
    pub fn guaranteed_rate(mut self, uplink_bps: u64, downlink_bps: u64) -> Self {
        self.gbr = Some(Gbr::new(uplink_bps, downlink_bps));
        self
    }

    /// Convenience method: Creates an Update QER that opens both gates.
    pub fn open_gate(qer_id: QerId) -> Self {
        use crate::ie::gate_status::GateStatusValue;
        UpdateQerBuilder::new(qer_id).gate_status(GateStatus::new(
            GateStatusValue::Open,
            GateStatusValue::Open,
        ))
    }

    /// Convenience method: Creates an Update QER that closes both gates.
    pub fn closed_gate(qer_id: QerId) -> Self {
        use crate::ie::gate_status::GateStatusValue;
        UpdateQerBuilder::new(qer_id).gate_status(GateStatus::new(
            GateStatusValue::Closed,
            GateStatusValue::Closed,
        ))
    }

    /// Convenience method: Creates an Update QER for uplink-only traffic control.
    pub fn uplink_only(qer_id: QerId) -> Self {
        use crate::ie::gate_status::GateStatusValue;
        UpdateQerBuilder::new(qer_id).gate_status(GateStatus::new(
            GateStatusValue::Closed, // downlink closed
            GateStatusValue::Open,   // uplink open
        ))
    }

    /// Convenience method: Creates an Update QER for downlink-only traffic control.
    pub fn downlink_only(qer_id: QerId) -> Self {
        use crate::ie::gate_status::GateStatusValue;
        UpdateQerBuilder::new(qer_id).gate_status(GateStatus::new(
            GateStatusValue::Open,   // downlink open
            GateStatusValue::Closed, // uplink closed
        ))
    }

    /// Builds the Update QER with validation.
    ///
    /// # Errors
    ///
    /// Returns an error if the QER ID is not set.
    pub fn build(self) -> Result<UpdateQer, PfcpError> {
        let qer_id = self.qer_id.ok_or_else(|| {
            PfcpError::validation_error("UpdateQerBuilder", "qer_id", "QER ID is required")
        })?;

        Ok(UpdateQer {
            qer_id,
            qer_correlation_id: self.qer_correlation_id,
            gate_status: self.gate_status,
            mbr: self.mbr,
            gbr: self.gbr,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ie::gate_status::GateStatusValue;

    #[test]
    fn test_update_qer_builder_basic() {
        let qer = UpdateQerBuilder::new(QerId::new(1))
            .gate_status(GateStatus::new(
                GateStatusValue::Open,
                GateStatusValue::Open,
            ))
            .build()
            .unwrap();

        assert_eq!(qer.qer_id, QerId::new(1));
        assert!(qer.gate_status.is_some());
        assert!(qer.mbr.is_none());
        assert!(qer.gbr.is_none());
    }

    #[test]
    fn test_update_qer_builder_with_rate_limits() {
        let qer = UpdateQerBuilder::new(QerId::new(2))
            .rate_limit(1000000, 2000000)
            .build()
            .unwrap();

        assert_eq!(qer.qer_id, QerId::new(2));
        assert!(qer.mbr.is_some());
        let mbr = qer.mbr.unwrap();
        assert_eq!(mbr.uplink, 1000000);
        assert_eq!(mbr.downlink, 2000000);
    }

    #[test]
    fn test_update_qer_builder_with_guaranteed_rate() {
        let qer = UpdateQerBuilder::new(QerId::new(3))
            .guaranteed_rate(500000, 1000000)
            .build()
            .unwrap();

        assert_eq!(qer.qer_id, QerId::new(3));
        assert!(qer.gbr.is_some());
        let gbr = qer.gbr.unwrap();
        assert_eq!(gbr.uplink, 500000);
        assert_eq!(gbr.downlink, 1000000);
    }

    #[test]
    fn test_update_qer_builder_comprehensive() {
        let qer = UpdateQerBuilder::new(QerId::new(4))
            .qer_correlation_id(QerCorrelationId::new(42))
            .gate_status(GateStatus::new(
                GateStatusValue::Open,
                GateStatusValue::Open,
            ))
            .rate_limit(2000000, 4000000)
            .guaranteed_rate(1000000, 2000000)
            .build()
            .unwrap();

        assert_eq!(qer.qer_id, QerId::new(4));
        assert!(qer.qer_correlation_id.is_some());
        assert!(qer.gate_status.is_some());
        assert!(qer.mbr.is_some());
        assert!(qer.gbr.is_some());
    }

    #[test]
    fn test_update_qer_builder_open_gate() {
        let qer = UpdateQerBuilder::open_gate(QerId::new(5))
            .build()
            .expect("Failed to build open_gate update QER");

        assert_eq!(qer.qer_id, QerId::new(5));
        assert!(qer.gate_status.is_some());
        let gate = qer.gate_status.unwrap();
        assert_eq!(gate.uplink_gate, GateStatusValue::Open);
        assert_eq!(gate.downlink_gate, GateStatusValue::Open);
    }

    #[test]
    fn test_update_qer_builder_closed_gate() {
        let qer = UpdateQerBuilder::closed_gate(QerId::new(6))
            .build()
            .unwrap();

        assert_eq!(qer.qer_id, QerId::new(6));
        assert!(qer.gate_status.is_some());
        let gate = qer.gate_status.unwrap();
        assert_eq!(gate.uplink_gate, GateStatusValue::Closed);
        assert_eq!(gate.downlink_gate, GateStatusValue::Closed);
    }

    #[test]
    fn test_update_qer_builder_uplink_only() {
        let qer = UpdateQerBuilder::uplink_only(QerId::new(7))
            .build()
            .unwrap();

        assert_eq!(qer.qer_id, QerId::new(7));
        assert!(qer.gate_status.is_some());
        let gate = qer.gate_status.unwrap();
        assert_eq!(gate.uplink_gate, GateStatusValue::Open);
        assert_eq!(gate.downlink_gate, GateStatusValue::Closed);
    }

    #[test]
    fn test_update_qer_builder_downlink_only() {
        let qer = UpdateQerBuilder::downlink_only(QerId::new(8))
            .build()
            .unwrap();

        assert_eq!(qer.qer_id, QerId::new(8));
        assert!(qer.gate_status.is_some());
        let gate = qer.gate_status.unwrap();
        assert_eq!(gate.uplink_gate, GateStatusValue::Closed);
        assert_eq!(gate.downlink_gate, GateStatusValue::Open);
    }

    #[test]
    fn test_update_qer_builder_method() {
        let qer = UpdateQer::builder(QerId::new(9))
            .gate_status(GateStatus::new(
                GateStatusValue::Open,
                GateStatusValue::Open,
            ))
            .build()
            .unwrap();

        assert_eq!(qer.qer_id, QerId::new(9));
        assert!(qer.gate_status.is_some());
    }

    #[test]
    fn test_update_qer_builder_round_trip_marshal() {
        let qer = UpdateQerBuilder::new(QerId::new(10))
            .rate_limit(3000000, 5000000)
            .gate_status(GateStatus::new(
                GateStatusValue::Open,
                GateStatusValue::Open,
            ))
            .build()
            .unwrap();

        let marshaled = qer.marshal();
        let unmarshaled = UpdateQer::unmarshal(&marshaled).unwrap();

        assert_eq!(qer, unmarshaled);
    }

    #[test]
    fn test_update_qer_builder_with_mbr_and_gbr() {
        let qer = UpdateQerBuilder::new(QerId::new(11))
            .mbr(Mbr::new(4000000, 6000000))
            .gbr(Gbr::new(2000000, 3000000))
            .build()
            .unwrap();

        assert_eq!(qer.qer_id, QerId::new(11));
        assert!(qer.mbr.is_some());
        assert!(qer.gbr.is_some());

        let marshaled = qer.marshal();
        let unmarshaled = UpdateQer::unmarshal(&marshaled).unwrap();
        assert_eq!(qer, unmarshaled);
    }

    #[test]
    fn test_update_qer_builder_chain_all_methods() {
        let qer = UpdateQerBuilder::new(QerId::new(12))
            .qer_correlation_id(QerCorrelationId::new(99))
            .gate_status(GateStatus::new(
                GateStatusValue::Closed,
                GateStatusValue::Closed,
            ))
            .rate_limit(1500000, 2500000)
            .guaranteed_rate(750000, 1250000)
            .build()
            .unwrap();

        // Verify all fields are set
        assert_eq!(qer.qer_id, QerId::new(12));
        assert_eq!(qer.qer_correlation_id, Some(QerCorrelationId::new(99)));
        assert!(qer.gate_status.is_some());
        assert!(qer.mbr.is_some());
        assert!(qer.gbr.is_some());

        // Test round-trip
        let marshaled = qer.marshal();
        let unmarshaled = UpdateQer::unmarshal(&marshaled).unwrap();
        assert_eq!(qer, unmarshaled);
    }
}