Skip to content

Commit

Permalink
clean code; use filename from parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
zvfvrv committed Mar 6, 2022
1 parent 4eed90c commit 5efab2c
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 144 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@
# Dependency directories (remove the comment below to include it)
# vendor/

goBGPSRv6PolicyClient
goBGPSRv6PolicyClient

*.swp
133 changes: 3 additions & 130 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,12 @@ 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
var policyFile string

// createCmd represents the create command
var createCmd = &cobra.Command{
Expand All @@ -42,135 +35,15 @@ 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
attrs := []*any.Any{}
/*
nlrisr, _ := ptypes.MarshalAny(&api.SRPolicyNLRI{
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: "fd11::1000",
})
if err != nil {
fmt.Println(err)
}
attrs = append(attrs, nhAttr)

sid, err := ptypes.MarshalAny(&api.SRBindingSID{
SFlag: true,
IFlag: false,
Sid: net.ParseIP("cafe::01"),
})
bsid, err := ptypes.MarshalAny(&api.TunnelEncapSubTLVSRBindingSID{
Bsid: sid,
})

var epbs = &api.SRv6EndPointBehavior{
Behavior: api.SRv6Behavior_END_DT4,
}
segment, err := ptypes.MarshalAny(&api.SegmentTypeB{
Flags: &api.SegmentFlags{SFlag: true},
Sid: net.ParseIP("fcff:0:0:20AF::F"),
EndpointBehaviorStructure: epbs,
})

seglist, err := ptypes.MarshalAny(&api.TunnelEncapSubTLVSRSegmentList{
Weight: &api.SRWeight{
Flags: 0,
Weight: 12,
},
Segments: []*any.Any{segment},
})

pref, err := ptypes.MarshalAny(&api.TunnelEncapSubTLVSRPreference{
Flags: 0,
Preference: 11,
})

pri, err := ptypes.MarshalAny(&api.TunnelEncapSubTLVSRPriority{
Priority: 10,
})

// Tunnel Encapsulation attribute for SR Policy
tun, err := ptypes.MarshalAny(&api.TunnelEncapAttribute{
Tlvs: []*api.TunnelEncapTLV{
{
Type: 15,
Tlvs: []*anypb.Any{bsid, seglist, pref, pri},
},
},
})

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)
srv6policypathFromFile.fromFile(policyFile)
spp_path, err := srv6policypathFromFile.toPath()
client.AddPath(ctx, &api.AddPathRequest{
TableType: api.TableType_GLOBAL,
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)
}
Expand All @@ -185,7 +58,7 @@ func init() {

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
createCmd.PersistentFlags().StringVar(&policiesFile, "policiesFile", "policiesFile.yaml", "policies file (default is ~/policiesFile.yaml)")
createCmd.PersistentFlags().StringVar(&policyFile, "policyFile", "policyFile.yaml", "policy file (default is ~/policyFile.yaml)")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
Expand Down
25 changes: 25 additions & 0 deletions policyFile.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
nlri:
distinguisher: 4
color: 94
endpoint: fd12::1000
iswithdraw: false
age:
seconds: 1638267871
nanos: 992981348
sourceasn: 5600
family:
afi: 2
safi: 73
neighborip: fd12::1000
segmentlist:
weight: 0
segments:
- sid: fcff:6::1
behavior: 18
- sid: fcff:8::1
behavior: 18
- sid: fcdd::12aa:d460:b250:45:b05
behavior: 18
bsid: cafe::25
priority: 0
nexthop: fd12::1000
18 changes: 10 additions & 8 deletions test.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
nlri:
distinguisher: 4
color: 94
endpoint: fd00::1000
endpoint: fd12::1000
iswithdraw: false
age:
seconds: 1638267871
Expand All @@ -10,14 +10,16 @@ sourceasn: 5600
family:
afi: 2
safi: 73
neighborip: fd00::1011
neighborip: fd12::1000
segmentlist:
weight: 0
segments:
- sid: fcff:0:0:20af::f
behavior: 19
- sid: fcff:0:0:30af::f
behavior: 19
bsid: cafe::1
- sid: fcff:6::1
behavior: 18
- sid: fcff:8::1
behavior: 18
- sid: fcdd::12aa:d460:b250:45:b05
behavior: 18
bsid: cafe::25
priority: 0
nexthop: "fd00::1000"
nexthop: fd12::1000
10 changes: 5 additions & 5 deletions testw.yaml → testv4.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
nlri:
distinguisher: 4
color: 94
endpoint: fd00::1000
endpoint: fd12::1000
iswithdraw: false
age:
seconds: 1638267871
Expand All @@ -10,14 +10,14 @@ sourceasn: 5600
family:
afi: 2
safi: 73
neighborip: fd00::1011
neighborip: fd12::1000
segmentlist:
weight: 0
segments:
- sid: fcff:0:0:20af::f
- sid: fcff:6::1
behavior: 19
- sid: fcff:0:0:30af::f
- sid: fcdd::12aa:d460:b250:45:b05
behavior: 19
bsid: cafe::1
priority: 0
nexthop: fd00::1000
nexthop: fd12::1000
Empty file added testv6.yaml
Empty file.

0 comments on commit 5efab2c

Please sign in to comment.