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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
// src/ie/create_far.rs

//! Create FAR Information Element.

use crate::error::PfcpError;
use crate::ie::apply_action::ApplyAction;
use crate::ie::bar_id::BarId;
use crate::ie::destination_interface::{DestinationInterface, Interface};
use crate::ie::duplicating_parameters::DuplicatingParameters;
use crate::ie::far_id::FarId;
use crate::ie::forwarding_parameters::ForwardingParameters;
use crate::ie::network_instance::NetworkInstance;
use crate::ie::{marshal_ies, Ie, IeIterator, IeType};

/// Traffic direction for FAR rules
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TrafficDirection {
    Uplink,
    Downlink,
}

/// Common FAR actions
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FarAction {
    Forward,
    Drop,
    Buffer,
    Duplicate,
    ForwardAndDuplicate,
}

impl FarAction {
    fn to_apply_action(self) -> ApplyAction {
        match self {
            FarAction::Forward => ApplyAction::FORW,
            FarAction::Drop => ApplyAction::DROP,
            FarAction::Buffer => ApplyAction::BUFF,
            FarAction::Duplicate => ApplyAction::DUPL,
            FarAction::ForwardAndDuplicate => ApplyAction::FORW | ApplyAction::DUPL,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CreateFar {
    pub far_id: FarId,
    pub apply_action: ApplyAction,
    pub forwarding_parameters: Option<ForwardingParameters>,
    pub duplicating_parameters: Option<DuplicatingParameters>,
    pub bar_id: Option<BarId>,
}

impl CreateFar {
    /// Creates a new Create FAR IE.
    pub fn new(far_id: FarId, apply_action: ApplyAction) -> Self {
        CreateFar {
            far_id,
            apply_action,
            forwarding_parameters: None,
            duplicating_parameters: None,
            bar_id: None,
        }
    }

    /// Creates a FAR with a specific action type.
    pub fn with_action(far_id: FarId, action: FarAction) -> Self {
        CreateFar::new(far_id, action.to_apply_action())
    }

    /// Adds forwarding parameters to the FAR.
    pub fn with_forwarding_parameters(mut self, params: ForwardingParameters) -> Self {
        self.forwarding_parameters = Some(params);
        self
    }

    /// Adds duplicating parameters to the FAR.
    pub fn with_duplicating_parameters(mut self, params: DuplicatingParameters) -> Self {
        self.duplicating_parameters = Some(params);
        self
    }

    /// Adds BAR ID for buffering actions.
    pub fn with_bar_id(mut self, bar_id: BarId) -> Self {
        self.bar_id = Some(bar_id);
        self
    }

    /// Creates a simple forwarding FAR for uplink traffic.
    pub fn uplink_forward(far_id: FarId, destination: Interface) -> Self {
        let dest_interface = DestinationInterface::new(destination);
        let forwarding_params = ForwardingParameters::new(dest_interface);

        CreateFar::with_action(far_id, FarAction::Forward)
            .with_forwarding_parameters(forwarding_params)
    }

    /// Creates a simple forwarding FAR for downlink traffic.
    pub fn downlink_forward(far_id: FarId, destination: Interface) -> Self {
        let dest_interface = DestinationInterface::new(destination);
        let forwarding_params = ForwardingParameters::new(dest_interface);

        CreateFar::with_action(far_id, FarAction::Forward)
            .with_forwarding_parameters(forwarding_params)
    }

    /// Creates a drop FAR.
    pub fn drop(far_id: FarId) -> Self {
        CreateFar::with_action(far_id, FarAction::Drop)
    }

    /// Creates a buffer FAR with BAR ID.
    pub fn buffer(far_id: FarId, bar_id: BarId) -> Self {
        CreateFar::with_action(far_id, FarAction::Buffer).with_bar_id(bar_id)
    }

    /// Marshals the Create FAR into bytes.
    pub fn marshal(&self) -> Vec<u8> {
        let mut ies = Vec::new();

        // FAR ID is mandatory
        ies.push(self.far_id.to_ie());

        // Apply Action is mandatory
        ies.push(Ie::new(
            IeType::ApplyAction,
            self.apply_action.marshal().to_vec(),
        ));

        // Optional IEs
        if let Some(ref fp) = self.forwarding_parameters {
            ies.push(fp.to_ie());
        }
        if let Some(ref dp) = self.duplicating_parameters {
            ies.push(dp.to_ie());
        }
        if let Some(ref bar_id) = self.bar_id {
            ies.push(bar_id.to_ie());
        }

        // Serialize all IEs
        marshal_ies(&ies)
    }

    /// Unmarshals Create FAR from bytes.
    pub fn unmarshal(data: &[u8]) -> Result<Self, PfcpError> {
        let mut far_id = None;
        let mut apply_action = None;
        let mut forwarding_parameters = None;
        let mut duplicating_parameters = None;
        let mut bar_id = None;

        for ie_result in IeIterator::new(data) {
            let ie = ie_result?;
            match ie.ie_type {
                IeType::FarId => far_id = Some(FarId::unmarshal(&ie.payload)?),
                IeType::ApplyAction => apply_action = Some(ApplyAction::unmarshal(&ie.payload)?),
                IeType::ForwardingParameters => {
                    forwarding_parameters = Some(ForwardingParameters::unmarshal(&ie.payload)?)
                }
                IeType::DuplicatingParameters => {
                    duplicating_parameters = Some(DuplicatingParameters::unmarshal(&ie.payload)?)
                }
                IeType::BarId => bar_id = Some(BarId::unmarshal(&ie.payload)?),
                _ => {} // Ignore unknown IEs
            }
        }

        Ok(CreateFar {
            far_id: far_id.ok_or(PfcpError::missing_ie_in_grouped(
                IeType::FarId,
                IeType::CreateFar,
            ))?,
            apply_action: apply_action.ok_or(PfcpError::missing_ie_in_grouped(
                IeType::ApplyAction,
                IeType::CreateFar,
            ))?,
            forwarding_parameters,
            duplicating_parameters,
            bar_id,
        })
    }

    /// Converts to IE.
    pub fn to_ie(&self) -> Ie {
        Ie::new(IeType::CreateFar, self.marshal())
    }

    /// Returns a builder for constructing Create FAR instances.
    pub fn builder(far_id: FarId) -> CreateFarBuilder {
        CreateFarBuilder::new(far_id)
    }
}

/// Builder for Create FAR Information Elements with validation.
///
/// The Create FAR builder provides an ergonomic way to construct FAR IEs for
/// traffic forwarding rules with proper validation of action and parameter combinations.
///
/// # Examples
///
/// ```rust
/// use rs_pfcp::ie::create_far::{CreateFarBuilder, FarAction};
/// use rs_pfcp::ie::far_id::FarId;
/// use rs_pfcp::ie::destination_interface::{DestinationInterface, Interface};
/// use rs_pfcp::ie::forwarding_parameters::ForwardingParameters;
///
/// // Simple forwarding FAR
/// let far = CreateFarBuilder::new(FarId::new(1))
///     .forward_to(Interface::Core)
///     .build()
///     .unwrap();
///
/// // Complex FAR with validation
/// let buffer_far = CreateFarBuilder::new(FarId::new(2))
///     .action(FarAction::Buffer)
///     .bar_id(rs_pfcp::ie::bar_id::BarId::new(1))
///     .build()
///     .unwrap();
/// ```
#[derive(Debug, Default)]
pub struct CreateFarBuilder {
    far_id: Option<FarId>,
    apply_action: Option<ApplyAction>,
    forwarding_parameters: Option<ForwardingParameters>,
    duplicating_parameters: Option<DuplicatingParameters>,
    bar_id: Option<BarId>,
}

impl CreateFarBuilder {
    /// Creates a new Create FAR builder with the specified FAR ID.
    ///
    /// FAR ID is mandatory for all FAR instances.
    pub fn new(far_id: FarId) -> Self {
        CreateFarBuilder {
            far_id: Some(far_id),
            ..Default::default()
        }
    }

    /// Sets the apply action.
    pub fn apply_action(mut self, action: ApplyAction) -> Self {
        self.apply_action = Some(action);
        self
    }

    /// Sets the apply action using the enum helper.
    pub fn action(mut self, action: FarAction) -> Self {
        self.apply_action = Some(action.to_apply_action());
        self
    }

    /// Adds forwarding parameters.
    pub fn forwarding_parameters(mut self, params: ForwardingParameters) -> Self {
        self.forwarding_parameters = Some(params);
        self
    }

    /// Quick method to add forwarding to specific interface.
    pub fn forward_to(mut self, destination: Interface) -> Self {
        let dest_interface = DestinationInterface::new(destination);
        let forwarding_params = ForwardingParameters::new(dest_interface);
        self.forwarding_parameters = Some(forwarding_params);
        // Only set FORW action if no action is already set
        if self.apply_action.is_none() {
            self.apply_action = Some(ApplyAction::FORW);
        }
        self
    }

    /// Quick method to add forwarding with network instance.
    pub fn forward_to_network(
        mut self,
        destination: Interface,
        network_instance: NetworkInstance,
    ) -> Self {
        let dest_interface = DestinationInterface::new(destination);
        let forwarding_params =
            ForwardingParameters::new(dest_interface).with_network_instance(network_instance);
        self.forwarding_parameters = Some(forwarding_params);
        // Only set FORW action if no action is already set
        if self.apply_action.is_none() {
            self.apply_action = Some(ApplyAction::FORW);
        }
        self
    }

    /// Adds duplicating parameters.
    pub fn duplicating_parameters(mut self, params: DuplicatingParameters) -> Self {
        self.duplicating_parameters = Some(params);
        self
    }

    /// Adds BAR ID.
    pub fn bar_id(mut self, bar_id: BarId) -> Self {
        self.bar_id = Some(bar_id);
        self
    }

    /// Builds the Create FAR with comprehensive validation.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - FAR ID is not set (should not happen with current API)
    /// - Apply Action is not set
    /// - Action and parameter combinations are invalid (e.g., BUFF without BAR ID)
    /// - FORW action without forwarding parameters
    /// - DUPL action without duplicating parameters
    pub fn build(self) -> Result<CreateFar, PfcpError> {
        let far_id = self.far_id.ok_or(PfcpError::MissingMandatoryIe {
            ie_type: IeType::FarId,
            message_type: None,
            parent_ie: Some(IeType::CreateFar),
        })?;

        let apply_action = self.apply_action.ok_or(PfcpError::MissingMandatoryIe {
            ie_type: IeType::ApplyAction,
            message_type: None,
            parent_ie: Some(IeType::CreateFar),
        })?;

        // Validate action and parameter combinations
        self.validate_action_parameters(&apply_action)?;

        Ok(CreateFar {
            far_id,
            apply_action,
            forwarding_parameters: self.forwarding_parameters,
            duplicating_parameters: self.duplicating_parameters,
            bar_id: self.bar_id,
        })
    }

    /// Validates that action and parameter combinations are correct.
    fn validate_action_parameters(&self, apply_action: &ApplyAction) -> Result<(), PfcpError> {
        // Check BUFF requires BAR ID
        if apply_action.contains(ApplyAction::BUFF) && self.bar_id.is_none() {
            return Err(PfcpError::validation_error(
                "CreateFarBuilder",
                "bar_id",
                "BUFF action requires BAR ID to be set",
            ));
        }

        // Check FORW should have forwarding parameters (warning, not error)
        if apply_action.contains(ApplyAction::FORW) && self.forwarding_parameters.is_none() {
            // This is valid according to spec, but unusual - could be a warning in real implementation
        }

        // Check DUPL should have duplicating parameters (warning, not error)
        if apply_action.contains(ApplyAction::DUPL) && self.duplicating_parameters.is_none() {
            // This is valid according to spec, but unusual - could be a warning in real implementation
        }

        // Check BAR ID without BUFF action (unusual but not invalid)
        if self.bar_id.is_some() && !apply_action.contains(ApplyAction::BUFF) {
            // This is technically valid but unusual
        }

        Ok(())
    }

    /// Creates a FAR builder for dropping traffic.
    pub fn drop_traffic(far_id: FarId) -> Self {
        CreateFarBuilder::new(far_id).action(FarAction::Drop)
    }

    /// Creates a FAR builder for buffering traffic.
    pub fn buffer_traffic(far_id: FarId, bar_id: BarId) -> Self {
        CreateFarBuilder::new(far_id)
            .action(FarAction::Buffer)
            .bar_id(bar_id)
    }

    /// Creates a FAR builder for uplink forwarding to core.
    pub fn uplink_to_core(far_id: FarId) -> Self {
        CreateFarBuilder::new(far_id).forward_to(Interface::Core)
    }

    /// Creates a FAR builder for downlink forwarding to access.
    pub fn downlink_to_access(far_id: FarId) -> Self {
        CreateFarBuilder::new(far_id).forward_to(Interface::Access)
    }

    /// Creates a FAR builder for forwarding to DN (Data Network).
    pub fn to_data_network(far_id: FarId) -> Self {
        CreateFarBuilder::new(far_id).forward_to(Interface::Dn)
    }

    /// Creates a FAR builder with forwarding and duplication.
    pub fn forward_and_duplicate(far_id: FarId, destination: Interface) -> Self {
        let dest_interface = DestinationInterface::new(destination);
        let forwarding_params = ForwardingParameters::new(dest_interface);

        CreateFarBuilder::new(far_id)
            .action(FarAction::ForwardAndDuplicate)
            .forwarding_parameters(forwarding_params)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ie::bar_id::BarId;
    use crate::ie::far_id::FarId;

    #[test]
    fn test_create_far_basic_construction() {
        let far_id = FarId::new(1);
        let apply_action = ApplyAction::FORW;

        let far = CreateFar::new(far_id, apply_action);

        assert_eq!(far.far_id, far_id);
        assert_eq!(far.apply_action, apply_action);
        assert!(far.forwarding_parameters.is_none());
        assert!(far.duplicating_parameters.is_none());
        assert!(far.bar_id.is_none());
    }

    #[test]
    fn test_create_far_with_action_enum() {
        let far_id = FarId::new(2);

        let forward_far = CreateFar::with_action(far_id, FarAction::Forward);
        assert_eq!(forward_far.apply_action, ApplyAction::FORW);

        let drop_far = CreateFar::with_action(far_id, FarAction::Drop);
        assert_eq!(drop_far.apply_action, ApplyAction::DROP);

        let buffer_far = CreateFar::with_action(far_id, FarAction::Buffer);
        assert_eq!(buffer_far.apply_action, ApplyAction::BUFF);

        let forward_dup_far = CreateFar::with_action(far_id, FarAction::ForwardAndDuplicate);
        assert_eq!(
            forward_dup_far.apply_action,
            ApplyAction::FORW | ApplyAction::DUPL
        );
    }

    #[test]
    fn test_create_far_uplink_forward() {
        let far_id = FarId::new(1);
        let far = CreateFar::uplink_forward(far_id, Interface::Core);

        assert_eq!(far.far_id, far_id);
        assert_eq!(far.apply_action, ApplyAction::FORW);
        assert!(far.forwarding_parameters.is_some());

        let forwarding_params = far.forwarding_parameters.unwrap();
        assert_eq!(
            forwarding_params.destination_interface.interface,
            Interface::Core
        );
    }

    #[test]
    fn test_create_far_downlink_forward() {
        let far_id = FarId::new(2);
        let far = CreateFar::downlink_forward(far_id, Interface::Access);

        assert_eq!(far.far_id, far_id);
        assert_eq!(far.apply_action, ApplyAction::FORW);
        assert!(far.forwarding_parameters.is_some());

        let forwarding_params = far.forwarding_parameters.unwrap();
        assert_eq!(
            forwarding_params.destination_interface.interface,
            Interface::Access
        );
    }

    #[test]
    fn test_create_far_drop() {
        let far_id = FarId::new(3);
        let far = CreateFar::drop(far_id);

        assert_eq!(far.far_id, far_id);
        assert_eq!(far.apply_action, ApplyAction::DROP);
        assert!(far.forwarding_parameters.is_none());
    }

    #[test]
    fn test_create_far_buffer() {
        let far_id = FarId::new(4);
        let bar_id = BarId::new(1);
        let far = CreateFar::buffer(far_id, bar_id.clone());

        assert_eq!(far.far_id, far_id);
        assert_eq!(far.apply_action, ApplyAction::BUFF);
        assert_eq!(far.bar_id, Some(bar_id));
    }

    #[test]
    fn test_create_far_builder_basic() {
        let far_id = FarId::new(5);
        let far = CreateFarBuilder::new(far_id)
            .action(FarAction::Forward)
            .build()
            .unwrap();

        assert_eq!(far.far_id, far_id);
        assert_eq!(far.apply_action, ApplyAction::FORW);
    }

    #[test]
    fn test_create_far_builder_comprehensive() {
        let far_id = FarId::new(6);
        let bar_id = BarId::new(2);

        let far = CreateFarBuilder::new(far_id)
            .forward_to(Interface::Core)
            .bar_id(bar_id.clone())
            .build()
            .unwrap();

        assert_eq!(far.far_id, far_id);
        assert_eq!(far.apply_action, ApplyAction::FORW);
        assert!(far.forwarding_parameters.is_some());
        assert_eq!(far.bar_id, Some(bar_id));
    }

    #[test]
    fn test_create_far_builder_forward_to_network() {
        let far_id = FarId::new(7);
        let network_instance = NetworkInstance::new("internet");

        let far = CreateFarBuilder::new(far_id)
            .forward_to_network(Interface::Dn, network_instance.clone())
            .build()
            .unwrap();

        assert_eq!(far.far_id, far_id);
        assert_eq!(far.apply_action, ApplyAction::FORW);
        assert!(far.forwarding_parameters.is_some());

        let forwarding_params = far.forwarding_parameters.unwrap();
        assert_eq!(
            forwarding_params.destination_interface.interface,
            Interface::Dn
        );
        assert_eq!(forwarding_params.network_instance, Some(network_instance));
    }

    #[test]
    fn test_create_far_builder_missing_action() {
        let far_id = FarId::new(8);
        let result = CreateFarBuilder::new(far_id).build();

        assert!(result.is_err());
        match result.unwrap_err() {
            PfcpError::MissingMandatoryIe { ie_type, .. } => {
                assert_eq!(ie_type, IeType::ApplyAction);
            }
            _ => panic!("Expected MissingMandatoryIe error"),
        }
    }

    #[test]
    fn test_create_far_marshal_unmarshal() {
        let far_id = FarId::new(9);
        let far = CreateFar::uplink_forward(far_id, Interface::Core);

        let marshaled = far.marshal();
        let unmarshaled = CreateFar::unmarshal(&marshaled).unwrap();

        assert_eq!(far, unmarshaled);
    }

    #[test]
    fn test_create_far_marshal_unmarshal_with_bar() {
        let far_id = FarId::new(10);
        let bar_id = BarId::new(3);
        let far = CreateFar::buffer(far_id, bar_id);

        let marshaled = far.marshal();
        let unmarshaled = CreateFar::unmarshal(&marshaled).unwrap();

        assert_eq!(far, unmarshaled);
    }

    #[test]
    fn test_create_far_to_ie() {
        let far_id = FarId::new(11);
        let far = CreateFar::drop(far_id);

        let ie = far.to_ie();
        assert_eq!(ie.ie_type, IeType::CreateFar);
        assert!(!ie.payload.is_empty());

        // Test round-trip through IE
        let unmarshaled = CreateFar::unmarshal(&ie.payload).unwrap();
        assert_eq!(far, unmarshaled);
    }

    #[test]
    fn test_create_far_unmarshal_missing_mandatory() {
        // Test with empty data
        let result = CreateFar::unmarshal(&[]);
        assert!(result.is_err());

        // Test with only FAR ID (missing apply action)
        let far_id = FarId::new(12);
        let far_id_ie = far_id.to_ie();
        let data = far_id_ie.marshal();

        let result = CreateFar::unmarshal(&data);
        assert!(result.is_err());
    }

    #[test]
    fn test_far_action_conversions() {
        assert_eq!(FarAction::Forward.to_apply_action(), ApplyAction::FORW);
        assert_eq!(FarAction::Drop.to_apply_action(), ApplyAction::DROP);
        assert_eq!(FarAction::Buffer.to_apply_action(), ApplyAction::BUFF);
        assert_eq!(FarAction::Duplicate.to_apply_action(), ApplyAction::DUPL);
        assert_eq!(
            FarAction::ForwardAndDuplicate.to_apply_action(),
            ApplyAction::FORW | ApplyAction::DUPL
        );
    }

    #[test]
    fn test_traffic_direction_enum() {
        // Test that TrafficDirection enum works (even though not used in current implementation)
        let uplink = TrafficDirection::Uplink;
        let downlink = TrafficDirection::Downlink;

        assert_ne!(uplink, downlink);
        assert_eq!(uplink, TrafficDirection::Uplink);
        assert_eq!(downlink, TrafficDirection::Downlink);
    }

    // Enhanced builder pattern tests
    #[test]
    fn test_builder_validation_buffer_requires_bar_id() {
        let far_id = FarId::new(100);
        let result = CreateFarBuilder::new(far_id)
            .action(FarAction::Buffer)
            .build();

        assert!(result.is_err());
        match result.unwrap_err() {
            PfcpError::ValidationError {
                builder,
                field,
                reason,
            } => {
                assert_eq!(builder, "CreateFarBuilder");
                assert_eq!(field, "bar_id");
                assert!(reason.contains("BUFF action requires BAR ID"));
            }
            _ => panic!("Expected ValidationError"),
        }
    }

    #[test]
    fn test_builder_validation_buffer_with_bar_id() {
        let far_id = FarId::new(101);
        let bar_id = BarId::new(10);
        let result = CreateFarBuilder::new(far_id)
            .action(FarAction::Buffer)
            .bar_id(bar_id.clone())
            .build();

        assert!(result.is_ok());
        let far = result.unwrap();
        assert_eq!(far.apply_action, ApplyAction::BUFF);
        assert_eq!(far.bar_id, Some(bar_id));
    }

    #[test]
    fn test_builder_convenience_drop_traffic() {
        let far_id = FarId::new(102);
        let far = CreateFarBuilder::drop_traffic(far_id)
            .build()
            .expect("Failed to build drop_traffic FAR");

        assert_eq!(far.far_id, far_id);
        assert_eq!(far.apply_action, ApplyAction::DROP);
        assert!(far.forwarding_parameters.is_none());
        assert!(far.bar_id.is_none());
    }

    #[test]
    fn test_builder_convenience_buffer_traffic() {
        let far_id = FarId::new(103);
        let bar_id = BarId::new(11);
        let far = CreateFarBuilder::buffer_traffic(far_id, bar_id.clone())
            .build()
            .unwrap();

        assert_eq!(far.far_id, far_id);
        assert_eq!(far.apply_action, ApplyAction::BUFF);
        assert_eq!(far.bar_id, Some(bar_id));
    }

    #[test]
    fn test_builder_convenience_uplink_to_core() {
        let far_id = FarId::new(104);
        let far = CreateFarBuilder::uplink_to_core(far_id)
            .build()
            .expect("Failed to build uplink_to_core FAR");

        assert_eq!(far.far_id, far_id);
        assert_eq!(far.apply_action, ApplyAction::FORW);
        assert!(far.forwarding_parameters.is_some());

        let forwarding_params = far.forwarding_parameters.unwrap();
        assert_eq!(
            forwarding_params.destination_interface.interface,
            Interface::Core
        );
    }

    #[test]
    fn test_builder_convenience_downlink_to_access() {
        let far_id = FarId::new(105);
        let far = CreateFarBuilder::downlink_to_access(far_id)
            .build()
            .unwrap();

        assert_eq!(far.far_id, far_id);
        assert_eq!(far.apply_action, ApplyAction::FORW);
        assert!(far.forwarding_parameters.is_some());

        let forwarding_params = far.forwarding_parameters.unwrap();
        assert_eq!(
            forwarding_params.destination_interface.interface,
            Interface::Access
        );
    }

    #[test]
    fn test_builder_convenience_to_data_network() {
        let far_id = FarId::new(106);
        let far = CreateFarBuilder::to_data_network(far_id)
            .build()
            .expect("Failed to build to_data_network FAR");

        assert_eq!(far.far_id, far_id);
        assert_eq!(far.apply_action, ApplyAction::FORW);
        assert!(far.forwarding_parameters.is_some());

        let forwarding_params = far.forwarding_parameters.unwrap();
        assert_eq!(
            forwarding_params.destination_interface.interface,
            Interface::Dn
        );
    }

    #[test]
    fn test_builder_convenience_forward_and_duplicate() {
        let far_id = FarId::new(107);
        let far = CreateFarBuilder::forward_and_duplicate(far_id, Interface::Core)
            .build()
            .unwrap();

        assert_eq!(far.far_id, far_id);
        assert_eq!(far.apply_action, ApplyAction::FORW | ApplyAction::DUPL);
        assert!(far.forwarding_parameters.is_some());

        let forwarding_params = far.forwarding_parameters.unwrap();
        assert_eq!(
            forwarding_params.destination_interface.interface,
            Interface::Core
        );
    }

    #[test]
    fn test_builder_enhanced_validation_missing_far_id() {
        // This shouldn't happen with current API, but test for completeness
        let builder = CreateFarBuilder {
            apply_action: Some(ApplyAction::FORW),
            ..Default::default()
        };

        let result = builder.build();
        assert!(result.is_err());
        match result.unwrap_err() {
            PfcpError::MissingMandatoryIe { ie_type, .. } => {
                assert_eq!(ie_type, IeType::FarId);
            }
            _ => panic!("Expected MissingMandatoryIe error"),
        }
    }

    #[test]
    fn test_builder_round_trip_marshal() {
        let far_id = FarId::new(108);
        let network_instance = NetworkInstance::new("internet");

        let original = CreateFarBuilder::new(far_id)
            .forward_to_network(Interface::Dn, network_instance.clone())
            .build()
            .unwrap();

        let marshaled = original.marshal();
        let unmarshaled = CreateFar::unmarshal(&marshaled).unwrap();

        assert_eq!(original, unmarshaled);
    }

    #[test]
    fn test_create_far_builder_method() {
        let far_id = FarId::new(109);
        let far = CreateFar::builder(far_id)
            .action(FarAction::Forward)
            .forward_to(Interface::Core)
            .build()
            .unwrap();

        assert_eq!(far.far_id, far_id);
        assert_eq!(far.apply_action, ApplyAction::FORW);
        assert!(far.forwarding_parameters.is_some());
    }

    #[test]
    fn test_builder_enhanced_comprehensive() {
        let far_id = FarId::new(110);
        let bar_id = BarId::new(12);
        let network_instance = NetworkInstance::new("enterprise");

        // Test complex builder with multiple parameters
        let far = CreateFarBuilder::new(far_id)
            .forward_to_network(Interface::Dn, network_instance.clone())
            .bar_id(bar_id.clone())
            .build()
            .unwrap();

        assert_eq!(far.far_id, far_id);
        assert_eq!(far.apply_action, ApplyAction::FORW);
        assert_eq!(far.bar_id, Some(bar_id));
        assert!(far.forwarding_parameters.is_some());

        let forwarding_params = far.forwarding_parameters.unwrap();
        assert_eq!(
            forwarding_params.destination_interface.interface,
            Interface::Dn
        );
        assert_eq!(forwarding_params.network_instance, Some(network_instance));
    }
}