Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
zvfvrv committed Feb 9, 2022
1 parent a4ec2e3 commit 4eed90c
Show file tree
Hide file tree
Showing 4 changed files with 228 additions and 24 deletions.
116 changes: 111 additions & 5 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ package cmd

import (
"fmt"
"io/ioutil"
"net"

"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/any"
api "github.com/osrg/gobgp/api"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/timestamppb"
"gopkg.in/yaml.v2"
)

type SRPolicyNLRI struct {
Expand All @@ -35,6 +38,15 @@ func (nlri *SRPolicyNLRI) toString() string {
return fmt.Sprintf("Distinguisher: %d, Color: %d, Endpoint: %s", nlri.Distinguisher, nlri.Color, nlri.Endpoint)
}

func (nlri *SRPolicyNLRI) toNLRI() (srnlri *api.SRPolicyNLRI, err error) {
return &api.SRPolicyNLRI{
Length: 192,
Distinguisher: nlri.Distinguisher,
Color: nlri.Color,
Endpoint: nlri.Endpoint,
}, nil
}

type SegmentTypeB struct {
Sid net.IP
Behavior uint8
Expand All @@ -44,6 +56,18 @@ func (seg *SegmentTypeB) toString() string {
return fmt.Sprintf("Sid: %s, Behavior: %d", seg.Sid, seg.Behavior)
}

func (seg *SegmentTypeB) toBGPSegmentTypeB() (srnlri *api.SegmentTypeB, err error) {
var epbs = &api.SRv6EndPointBehavior{
Behavior: api.SRv6Behavior_END_DT4,
}

return &api.SegmentTypeB{
Flags: &api.SegmentFlags{SFlag: true},
Sid: seg.Sid,
EndpointBehaviorStructure: epbs,
}, nil
}

type SRv6SegmentList struct {
Weight uint32
Segments []*SegmentTypeB
Expand All @@ -68,6 +92,16 @@ type SRv6PolicyPath struct {
SegmentList *SRv6SegmentList
Bsid net.IP
Priority uint32
NextHop net.IP
}

func (p *SRv6PolicyPath) fromFile(filePath string) error {
yamlFile, err := ioutil.ReadFile(filePath)
if err != nil {
fmt.Printf("Error reading YAML file: %s\n", err)
return err
}
return yaml.Unmarshal(yamlFile, p)
}

func (p *SRv6PolicyPath) fromPath(path *api.Path) (err error) {
Expand Down Expand Up @@ -135,20 +169,92 @@ func (p *SRv6PolicyPath) toPath() (path *api.Path, err error) {
SourceAsn: p.SourceAsn,
Family: p.Family,
NeighborIp: p.NeighborIp,
Pattrs: []*anypb.Any{},
}
srnlri := &api.SRPolicyNLRI{
Distinguisher: p.Nlri.Distinguisher,
Color: p.Nlri.Color,
Endpoint: p.Nlri.Endpoint,

//
originAttr, err := ptypes.MarshalAny(&api.OriginAttribute{Origin: 0})
if err != nil {
fmt.Println(err)
}
path.Nlri, err = ptypes.MarshalAny(srnlri)
path.Pattrs = append(path.Pattrs, originAttr)

// NextHopAttribute
nhAttr, err := ptypes.MarshalAny(&api.NextHopAttribute{
NextHop: p.NextHop.String(),
})
if err != nil {
fmt.Println(err)
}
path.Pattrs = append(path.Pattrs, nhAttr)

bsid, err := ptypes.MarshalAny(&api.SRBindingSID{
SFlag: true,
IFlag: false,
Sid: p.Bsid,
})

if err != nil {
return nil, err
}

tlvbsid, err := ptypes.MarshalAny(&api.TunnelEncapSubTLVSRBindingSID{
Bsid: bsid,
})
if err != nil {
return nil, err
}

seglist := &api.TunnelEncapSubTLVSRSegmentList{
Weight: &api.SRWeight{
Flags: 0,
Weight: 12,
},
Segments: []*any.Any{},
}
for _, seg := range p.SegmentList.Segments {

var epbs = &api.SRv6EndPointBehavior{
Behavior: api.SRv6Behavior(seg.Behavior),
}
segment, err := ptypes.MarshalAny(&api.SegmentTypeB{
Flags: &api.SegmentFlags{SFlag: true},
Sid: net.ParseIP(seg.Sid.String()),
EndpointBehaviorStructure: epbs,
})
if err != nil {
return nil, err
}
seglist.Segments = append(seglist.Segments, segment)

}

seglistMarshal, err := ptypes.MarshalAny(seglist)

// TunnelEncapAttribute
tunnelEncapAttr, err := ptypes.MarshalAny(&api.TunnelEncapAttribute{
Tlvs: []*api.TunnelEncapTLV{
{
Type: 15,
Tlvs: []*anypb.Any{tlvbsid, seglistMarshal /*, pref, pri */},
},
},
})
path.Pattrs = append(path.Pattrs, tunnelEncapAttr)

srnlri, _ := p.Nlri.toNLRI()
path.Nlri, err = ptypes.MarshalAny(srnlri)
if err != nil {
return path, err
}
return path, err
}

func (p *SRv6PolicyPath) String() string {
return fmt.Sprintf("NLRI: %s \nIsWithdraw: %t \nAge: %s \nSourceAsn: %d \nFamily: %s \nNeighborIp: %s \nSegmentList:\n %s \nBsid: %s \nPriority: %d",
p.Nlri.toString(), p.IsWithdraw, p.Age, p.SourceAsn, p.Family.String(), p.NeighborIp, p.SegmentList.toString(), p.Bsid, p.Priority)
}

func PrintSRv6PolicyPath(path *SRv6PolicyPath) {
fmt.Println(path.String())
}
90 changes: 71 additions & 19 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,44 +17,45 @@ package cmd

import (
"fmt"
"io/ioutil"
"net"

"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/any"
api "github.com/osrg/gobgp/api"
"github.com/spf13/cobra"
"google.golang.org/protobuf/types/known/anypb"

"gopkg.in/yaml.v2"
)

var policiesFile string

// createCmd represents the create command
var createCmd = &cobra.Command{
Use: "create",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Short: "Create SRv6 Policy Path",
Long: `Create SRv6 Policy Path defined in a YAML file. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("create called")
isWithdrawal := false
// isWithdrawal := false
attrs := []*any.Any{}
/*
nlrisr, _ := ptypes.MarshalAny(&api.SRPolicyNLRI{
Length: 192,
Distinguisher: 2,
Color: 99,
Endpoint: net.ParseIP("10.0.0.15").To4(),
})
Length: 192,
Endpoint: net.ParseIP("fd11::1000"),
}) */
originAttr, err := ptypes.MarshalAny(&api.OriginAttribute{Origin: 0})
if err != nil {
fmt.Println(err)
}
attrs = append(attrs, originAttr)
nhAttr, err := ptypes.MarshalAny(&api.NextHopAttribute{
NextHop: "10.0.0.15",
NextHop: "fd11::1000",
})
if err != nil {
fmt.Println(err)
Expand Down Expand Up @@ -106,23 +107,74 @@ to quickly create a Cobra application.`,
},
})

attrs = append(attrs, tun)
attrs = append(attrs, tun)
/*
spp_nlri := &SRPolicyNLRI{
Distinguisher: 2,
Color: 99,
Endpoint: net.ParseIP("fd11::1000"),
}
spp_age := ptypes.TimestampNow()
spp_asn := uint32(5600)
spp_famiy := &BgpFamilySRv6IPv6
spp_seglist := &SRv6SegmentList{
Weight: 0,
Segments: []*SegmentTypeB{
{
Sid: net.ParseIP("fcff:0:0:20AF::F"),
Behavior: 19,
},
{
Sid: net.ParseIP("fcff:0:0:30AF::F"),
Behavior: 19,
},
},
}
spp_bsid := net.ParseIP("cafe::01")
spp_priority := uint32(0)
srv6policypath := SRv6PolicyPath{
Nlri: spp_nlri,
IsWithdraw: isWithdrawal,
Age: spp_age,
SourceAsn: spp_asn,
Family: spp_famiy,
NeighborIp: "10.0.0.18",
SegmentList: spp_seglist,
Bsid: spp_bsid,
Priority: spp_priority,
}
file, _ := yaml.Marshal(srv6policypath)
_ = ioutil.WriteFile("testw.yaml", file, 0644) */

srv6policypathFromFile := SRv6PolicyPath{}
srv6policypathFromFile.fromFile("test.yaml")
file, _ := yaml.Marshal(srv6policypathFromFile)
_ = ioutil.WriteFile("testw.yaml", file, 0644)
spp_path, err := srv6policypathFromFile.toPath()
client.AddPath(ctx, &api.AddPathRequest{
TableType: api.TableType_GLOBAL,
Path: &api.Path{
Nlri: nlrisr,
IsWithdraw: isWithdrawal,
Pattrs: attrs,
Age: ptypes.TimestampNow(),
SourceAsn: 64512,
Family: &BgpFamilySRv6IPv6,
},
Path: spp_path,
})

// newPAth := &api.Path{
// Nlri: nlrisr,
// IsWithdraw: isWithdrawal,
// Pattrs: attrs,
// Age: ptypes.TimestampNow(),
// SourceAsn: 65000,
// Family: &BgpFamilySRv6IPv6,
// }

// client.AddPath(ctx, &api.AddPathRequest{
// TableType: api.TableType_GLOBAL,
// Path: newPAth,
// })

if err != nil {
fmt.Println(err)
}
fmt.Println("path created")
},
}

Expand Down
23 changes: 23 additions & 0 deletions test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
nlri:
distinguisher: 4
color: 94
endpoint: fd00::1000
iswithdraw: false
age:
seconds: 1638267871
nanos: 992981348
sourceasn: 5600
family:
afi: 2
safi: 73
neighborip: fd00::1011
segmentlist:
weight: 0
segments:
- sid: fcff:0:0:20af::f
behavior: 19
- sid: fcff:0:0:30af::f
behavior: 19
bsid: cafe::1
priority: 0
nexthop: "fd00::1000"
23 changes: 23 additions & 0 deletions testw.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
nlri:
distinguisher: 4
color: 94
endpoint: fd00::1000
iswithdraw: false
age:
seconds: 1638267871
nanos: 992981348
sourceasn: 5600
family:
afi: 2
safi: 73
neighborip: fd00::1011
segmentlist:
weight: 0
segments:
- sid: fcff:0:0:20af::f
behavior: 19
- sid: fcff:0:0:30af::f
behavior: 19
bsid: cafe::1
priority: 0
nexthop: fd00::1000

0 comments on commit 4eed90c

Please sign in to comment.