(values []*api.FlowSpecRule)
| 616 | } |
| 617 | |
| 618 | func UnmarshalFlowSpecRules(values []*api.FlowSpecRule) ([]bgp.FlowSpecComponentInterface, error) { |
| 619 | rules := make([]bgp.FlowSpecComponentInterface, 0, len(values)) |
| 620 | for _, value := range values { |
| 621 | var rule bgp.FlowSpecComponentInterface |
| 622 | switch r := value.GetRule().(type) { |
| 623 | case *api.FlowSpecRule_IpPrefix: |
| 624 | v := r.IpPrefix |
| 625 | typ := bgp.BGPFlowSpecType(v.Type) |
| 626 | ip, err := netip.ParseAddr(v.Prefix) |
| 627 | if err != nil { |
| 628 | return nil, fmt.Errorf("invalid ip address for %s flow spec component: %s", typ.String(), v.Prefix) |
| 629 | } |
| 630 | isIPv4 := ip.Is4() |
| 631 | switch { |
| 632 | case typ == bgp.FLOW_SPEC_TYPE_DST_PREFIX && isIPv4: |
| 633 | prefix, _ := bgp.NewIPAddrPrefix(netip.MustParsePrefix(fmt.Sprintf("%s/%d", v.Prefix, v.PrefixLen))) |
| 634 | rule = bgp.NewFlowSpecDestinationPrefix(prefix) |
| 635 | case typ == bgp.FLOW_SPEC_TYPE_SRC_PREFIX && isIPv4: |
| 636 | prefix, _ := bgp.NewIPAddrPrefix(netip.MustParsePrefix(fmt.Sprintf("%s/%d", v.Prefix, v.PrefixLen))) |
| 637 | rule = bgp.NewFlowSpecSourcePrefix(prefix) |
| 638 | case typ == bgp.FLOW_SPEC_TYPE_DST_PREFIX && !isIPv4: |
| 639 | prefix, _ := bgp.NewIPAddrPrefix(netip.MustParsePrefix(fmt.Sprintf("%s/%d", v.Prefix, v.PrefixLen))) |
| 640 | rule = bgp.NewFlowSpecDestinationPrefix6(prefix, uint8(v.Offset)) |
| 641 | case typ == bgp.FLOW_SPEC_TYPE_SRC_PREFIX && !isIPv4: |
| 642 | prefix, _ := bgp.NewIPAddrPrefix(netip.MustParsePrefix(fmt.Sprintf("%s/%d", v.Prefix, v.PrefixLen))) |
| 643 | rule = bgp.NewFlowSpecSourcePrefix6(prefix, uint8(v.Offset)) |
| 644 | } |
| 645 | case *api.FlowSpecRule_Mac: |
| 646 | v := r.Mac |
| 647 | typ := bgp.BGPFlowSpecType(v.Type) |
| 648 | mac, err := net.ParseMAC(v.Address) |
| 649 | if err != nil { |
| 650 | return nil, fmt.Errorf("invalid mac address for %s flow spec component: %s", typ.String(), v.Address) |
| 651 | } |
| 652 | switch typ { |
| 653 | case bgp.FLOW_SPEC_TYPE_SRC_MAC: |
| 654 | rule = bgp.NewFlowSpecSourceMac(mac) |
| 655 | case bgp.FLOW_SPEC_TYPE_DST_MAC: |
| 656 | rule = bgp.NewFlowSpecDestinationMac(mac) |
| 657 | } |
| 658 | case *api.FlowSpecRule_Component: |
| 659 | v := r.Component |
| 660 | items := make([]*bgp.FlowSpecComponentItem, 0, len(v.Items)) |
| 661 | for _, item := range v.Items { |
| 662 | items = append(items, bgp.NewFlowSpecComponentItem(uint8(item.Op), item.Value)) |
| 663 | } |
| 664 | rule = bgp.NewFlowSpecComponent(bgp.BGPFlowSpecType(v.Type), items) |
| 665 | } |
| 666 | if rule == nil { |
| 667 | return nil, fmt.Errorf("invalid flow spec component: %v", value) |
| 668 | } |
| 669 | rules = append(rules, rule) |
| 670 | } |
| 671 | return rules, nil |
| 672 | } |
| 673 | |
| 674 | func MarshalLsNodeDescriptor(d *bgp.LsNodeDescriptor) (*api.LsNodeDescriptor, error) { |
| 675 | return &api.LsNodeDescriptor{ |
no test coverage detected
searching dependent graphs…