diff --git a/plugins/inputs/p4runtime/p4runtime.go b/plugins/inputs/p4runtime/p4runtime.go index 1ce95bd7b8761..accc0d86c98ce 100644 --- a/plugins/inputs/p4runtime/p4runtime.go +++ b/plugins/inputs/p4runtime/p4runtime.go @@ -12,7 +12,7 @@ import ( "sync" p4_config "github.com/p4lang/p4runtime/go/p4/config/v1" - "github.com/p4lang/p4runtime/go/p4/v1" + p4 "github.com/p4lang/p4runtime/go/p4/v1" "google.golang.org/grpc" "google.golang.org/grpc/credentials" "google.golang.org/grpc/credentials/insecure" @@ -39,7 +39,7 @@ type P4runtime struct { common_tls.ClientConfig conn *grpc.ClientConn - client v1.P4RuntimeClient + client p4.P4RuntimeClient wg sync.WaitGroup } @@ -137,9 +137,9 @@ func initConnection(endpoint string, tlscfg *tls.Config) (*grpc.ClientConn, erro } func (p *P4runtime) getP4Info() (*p4_config.P4Info, error) { - req := &v1.GetForwardingPipelineConfigRequest{ + req := &p4.GetForwardingPipelineConfigRequest{ DeviceId: p.DeviceID, - ResponseType: v1.GetForwardingPipelineConfigRequest_ALL, + ResponseType: p4.GetForwardingPipelineConfigRequest_ALL, } resp, err := p.client.GetForwardingPipelineConfig(context.Background(), req) if err != nil { @@ -197,16 +197,16 @@ func (p *P4runtime) newP4RuntimeClient() error { return fmt.Errorf("cannot connect to the server: %w", err) } p.conn = conn - p.client = v1.NewP4RuntimeClient(conn) + p.client = p4.NewP4RuntimeClient(conn) return nil } -func (p *P4runtime) readAllEntries(counterID uint32) ([]*v1.Entity, error) { - readRequest := &v1.ReadRequest{ +func (p *P4runtime) readAllEntries(counterID uint32) ([]*p4.Entity, error) { + readRequest := &p4.ReadRequest{ DeviceId: p.DeviceID, - Entities: []*v1.Entity{{ - Entity: &v1.Entity_CounterEntry{ - CounterEntry: &v1.CounterEntry{ + Entities: []*p4.Entity{{ + Entity: &p4.Entity_CounterEntry{ + CounterEntry: &p4.CounterEntry{ CounterId: counterID}}}}} stream, err := p.client.Read(context.Background(), readRequest) diff --git a/plugins/inputs/p4runtime/p4runtime_fake_client_test.go b/plugins/inputs/p4runtime/p4runtime_fake_client_test.go index 0e25c0ece3efd..5dc2413c4004c 100644 --- a/plugins/inputs/p4runtime/p4runtime_fake_client_test.go +++ b/plugins/inputs/p4runtime/p4runtime_fake_client_test.go @@ -3,49 +3,49 @@ package p4runtime import ( "context" - "github.com/p4lang/p4runtime/go/p4/v1" + p4 "github.com/p4lang/p4runtime/go/p4/v1" "google.golang.org/grpc" ) type fakeP4RuntimeClient struct { writeFn func( ctx context.Context, - in *v1.WriteRequest, + in *p4.WriteRequest, opts ...grpc.CallOption, - ) (*v1.WriteResponse, error) + ) (*p4.WriteResponse, error) readFn func( - in *v1.ReadRequest, - ) (v1.P4Runtime_ReadClient, error) + in *p4.ReadRequest, + ) (p4.P4Runtime_ReadClient, error) setForwardingPipelineConfigFn func( ctx context.Context, - in *v1.SetForwardingPipelineConfigRequest, + in *p4.SetForwardingPipelineConfigRequest, opts ...grpc.CallOption, - ) (*v1.SetForwardingPipelineConfigResponse, error) + ) (*p4.SetForwardingPipelineConfigResponse, error) - getForwardingPipelineConfigFn func() (*v1.GetForwardingPipelineConfigResponse, error) + getForwardingPipelineConfigFn func() (*p4.GetForwardingPipelineConfigResponse, error) streamChannelFn func( ctx context.Context, opts ...grpc.CallOption, - ) (v1.P4Runtime_StreamChannelClient, error) + ) (p4.P4Runtime_StreamChannelClient, error) capabilitiesFn func( ctx context.Context, - in *v1.CapabilitiesRequest, + in *p4.CapabilitiesRequest, opts ...grpc.CallOption, - ) (*v1.CapabilitiesResponse, error) + ) (*p4.CapabilitiesResponse, error) } // fakeP4RuntimeClient implements the v1.P4RuntimeClient interface -var _ v1.P4RuntimeClient = &fakeP4RuntimeClient{} +var _ p4.P4RuntimeClient = &fakeP4RuntimeClient{} func (c *fakeP4RuntimeClient) Write( ctx context.Context, - in *v1.WriteRequest, + in *p4.WriteRequest, opts ...grpc.CallOption, -) (*v1.WriteResponse, error) { +) (*p4.WriteResponse, error) { if c.writeFn == nil { panic("No mock defined for Write RPC") } @@ -54,9 +54,9 @@ func (c *fakeP4RuntimeClient) Write( func (c *fakeP4RuntimeClient) Read( _ context.Context, - in *v1.ReadRequest, + in *p4.ReadRequest, _ ...grpc.CallOption, -) (v1.P4Runtime_ReadClient, error) { +) (p4.P4Runtime_ReadClient, error) { if c.readFn == nil { panic("No mock defined for Read RPC") } @@ -65,9 +65,9 @@ func (c *fakeP4RuntimeClient) Read( func (c *fakeP4RuntimeClient) SetForwardingPipelineConfig( ctx context.Context, - in *v1.SetForwardingPipelineConfigRequest, + in *p4.SetForwardingPipelineConfigRequest, opts ...grpc.CallOption, -) (*v1.SetForwardingPipelineConfigResponse, error) { +) (*p4.SetForwardingPipelineConfigResponse, error) { if c.setForwardingPipelineConfigFn == nil { panic("No mock defined for SetForwardingPipelineConfig RPC") } @@ -76,9 +76,9 @@ func (c *fakeP4RuntimeClient) SetForwardingPipelineConfig( func (c *fakeP4RuntimeClient) GetForwardingPipelineConfig( context.Context, - *v1.GetForwardingPipelineConfigRequest, + *p4.GetForwardingPipelineConfigRequest, ...grpc.CallOption, -) (*v1.GetForwardingPipelineConfigResponse, error) { +) (*p4.GetForwardingPipelineConfigResponse, error) { if c.getForwardingPipelineConfigFn == nil { panic("No mock defined for GetForwardingPipelineConfig RPC") } @@ -88,7 +88,7 @@ func (c *fakeP4RuntimeClient) GetForwardingPipelineConfig( func (c *fakeP4RuntimeClient) StreamChannel( ctx context.Context, opts ...grpc.CallOption, -) (v1.P4Runtime_StreamChannelClient, error) { +) (p4.P4Runtime_StreamChannelClient, error) { if c.streamChannelFn == nil { panic("No mock defined for StreamChannel") } @@ -97,9 +97,9 @@ func (c *fakeP4RuntimeClient) StreamChannel( func (c *fakeP4RuntimeClient) Capabilities( ctx context.Context, - in *v1.CapabilitiesRequest, + in *p4.CapabilitiesRequest, opts ...grpc.CallOption, -) (*v1.CapabilitiesResponse, error) { +) (*p4.CapabilitiesResponse, error) { if c.capabilitiesFn == nil { panic("No mock defined for Capabilities RPC") } @@ -108,13 +108,13 @@ func (c *fakeP4RuntimeClient) Capabilities( type fakeP4RuntimeReadClient struct { grpc.ClientStream - recvFn func() (*v1.ReadResponse, error) + recvFn func() (*p4.ReadResponse, error) } // fakeP4RuntimeReadClient implements the v1.P4Runtime_ReadClient interface -var _ v1.P4Runtime_ReadClient = &fakeP4RuntimeReadClient{} +var _ p4.P4Runtime_ReadClient = &fakeP4RuntimeReadClient{} -func (c *fakeP4RuntimeReadClient) Recv() (*v1.ReadResponse, error) { +func (c *fakeP4RuntimeReadClient) Recv() (*p4.ReadResponse, error) { if c.recvFn == nil { panic("No mock provided for Recv function") } diff --git a/plugins/inputs/p4runtime/p4runtime_test.go b/plugins/inputs/p4runtime/p4runtime_test.go index d6f95f944a602..8db9c245d6e1c 100644 --- a/plugins/inputs/p4runtime/p4runtime_test.go +++ b/plugins/inputs/p4runtime/p4runtime_test.go @@ -8,7 +8,7 @@ import ( "time" p4_config "github.com/p4lang/p4runtime/go/p4/config/v1" - "github.com/p4lang/p4runtime/go/p4/v1" + p4 "github.com/p4lang/p4runtime/go/p4/v1" "github.com/stretchr/testify/require" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" @@ -32,12 +32,12 @@ func createCounter( func createEntityCounterEntry( counterID uint32, index int64, - data *v1.CounterData, -) *v1.Entity_CounterEntry { - return &v1.Entity_CounterEntry{ - CounterEntry: &v1.CounterEntry{ + data *p4.CounterData, +) *p4.Entity_CounterEntry { + return &p4.Entity_CounterEntry{ + CounterEntry: &p4.CounterEntry{ CounterId: counterID, - Index: &v1.Index{Index: index}, + Index: &p4.Index{Index: index}, Data: data, }, } @@ -73,20 +73,20 @@ func TestInitDefault(t *testing.T) { func TestErrorGetP4Info(t *testing.T) { responses := []struct { - getForwardingPipelineConfigResponse *v1.GetForwardingPipelineConfigResponse + getForwardingPipelineConfigResponse *p4.GetForwardingPipelineConfigResponse getForwardingPipelineConfigResponseError error }{ { getForwardingPipelineConfigResponse: nil, getForwardingPipelineConfigResponseError: errors.New("error when retrieving forwarding pipeline config"), }, { - getForwardingPipelineConfigResponse: &v1.GetForwardingPipelineConfigResponse{ + getForwardingPipelineConfigResponse: &p4.GetForwardingPipelineConfigResponse{ Config: nil, }, getForwardingPipelineConfigResponseError: nil, }, { - getForwardingPipelineConfigResponse: &v1.GetForwardingPipelineConfigResponse{ - Config: &v1.ForwardingPipelineConfig{P4Info: nil}, + getForwardingPipelineConfigResponse: &p4.GetForwardingPipelineConfigResponse{ + Config: &p4.ForwardingPipelineConfig{P4Info: nil}, }, getForwardingPipelineConfigResponseError: nil, }, @@ -94,7 +94,7 @@ func TestErrorGetP4Info(t *testing.T) { for _, response := range responses { p4RtClient := &fakeP4RuntimeClient{ - getForwardingPipelineConfigFn: func() (*v1.GetForwardingPipelineConfigResponse, error) { + getForwardingPipelineConfigFn: func() (*p4.GetForwardingPipelineConfigResponse, error) { return response.getForwardingPipelineConfigResponse, response.getForwardingPipelineConfigResponseError }, } @@ -111,12 +111,12 @@ func TestErrorGetP4Info(t *testing.T) { func TestOneCounterRead(t *testing.T) { tests := []struct { - forwardingPipelineConfig *v1.ForwardingPipelineConfig - EntityCounterEntry *v1.Entity_CounterEntry + forwardingPipelineConfig *p4.ForwardingPipelineConfig + EntityCounterEntry *p4.Entity_CounterEntry expected []telegraf.Metric }{ { - forwardingPipelineConfig: &v1.ForwardingPipelineConfig{ + forwardingPipelineConfig: &p4.ForwardingPipelineConfig{ P4Info: &p4_config.P4Info{ Counters: []*p4_config.Counter{ createCounter("foo", 1111, p4_config.CounterSpec_BOTH), @@ -127,7 +127,7 @@ func TestOneCounterRead(t *testing.T) { EntityCounterEntry: createEntityCounterEntry( 1111, 5, - &v1.CounterData{ByteCount: 5, PacketCount: 1}, + &p4.CounterData{ByteCount: 5, PacketCount: 1}, ), expected: []telegraf.Metric{testutil.MustMetric( "p4_runtime", @@ -143,7 +143,7 @@ func TestOneCounterRead(t *testing.T) { time.Unix(0, 0)), }, }, { - forwardingPipelineConfig: &v1.ForwardingPipelineConfig{ + forwardingPipelineConfig: &p4.ForwardingPipelineConfig{ P4Info: &p4_config.P4Info{ Counters: []*p4_config.Counter{ createCounter( @@ -158,7 +158,7 @@ func TestOneCounterRead(t *testing.T) { EntityCounterEntry: createEntityCounterEntry( 2222, 5, - &v1.CounterData{ByteCount: 5}, + &p4.CounterData{ByteCount: 5}, ), expected: []telegraf.Metric{testutil.MustMetric( "p4_runtime", @@ -174,7 +174,7 @@ func TestOneCounterRead(t *testing.T) { time.Unix(0, 0)), }, }, { - forwardingPipelineConfig: &v1.ForwardingPipelineConfig{ + forwardingPipelineConfig: &p4.ForwardingPipelineConfig{ P4Info: &p4_config.P4Info{ Counters: []*p4_config.Counter{ createCounter( @@ -189,7 +189,7 @@ func TestOneCounterRead(t *testing.T) { EntityCounterEntry: createEntityCounterEntry( 3333, 5, - &v1.CounterData{PacketCount: 1}, + &p4.CounterData{PacketCount: 1}, ), expected: []telegraf.Metric{testutil.MustMetric( "p4_runtime", @@ -205,7 +205,7 @@ func TestOneCounterRead(t *testing.T) { time.Unix(0, 0)), }, }, { - forwardingPipelineConfig: &v1.ForwardingPipelineConfig{ + forwardingPipelineConfig: &p4.ForwardingPipelineConfig{ P4Info: &p4_config.P4Info{ Counters: []*p4_config.Counter{ createCounter("foo", 4444, p4_config.CounterSpec_BOTH), @@ -216,7 +216,7 @@ func TestOneCounterRead(t *testing.T) { EntityCounterEntry: createEntityCounterEntry( 4444, 5, - &v1.CounterData{}, + &p4.CounterData{}, ), expected: nil, }, @@ -224,19 +224,19 @@ func TestOneCounterRead(t *testing.T) { for _, tt := range tests { p4RtReadClient := &fakeP4RuntimeReadClient{ - recvFn: func() (*v1.ReadResponse, error) { - return &v1.ReadResponse{ - Entities: []*v1.Entity{{Entity: tt.EntityCounterEntry}}, + recvFn: func() (*p4.ReadResponse, error) { + return &p4.ReadResponse{ + Entities: []*p4.Entity{{Entity: tt.EntityCounterEntry}}, }, nil }, } p4RtClient := &fakeP4RuntimeClient{ - readFn: func(*v1.ReadRequest) (v1.P4Runtime_ReadClient, error) { + readFn: func(*p4.ReadRequest) (p4.P4Runtime_ReadClient, error) { return p4RtReadClient, nil }, - getForwardingPipelineConfigFn: func() (*v1.GetForwardingPipelineConfigResponse, error) { - return &v1.GetForwardingPipelineConfigResponse{ + getForwardingPipelineConfigFn: func() (*p4.GetForwardingPipelineConfigResponse, error) { + return &p4.GetForwardingPipelineConfigResponse{ Config: tt.forwardingPipelineConfig, }, nil }, @@ -270,7 +270,7 @@ func TestMultipleEntitiesSingleCounterRead(t *testing.T) { totalNumOfEntries, "totalNumOfCounters", ) - entities := make([]*v1.Entity, 0, totalNumOfEntries) + entities := make([]*p4.Entity, 0, totalNumOfEntries) p4InfoCounters := make([]*p4_config.Counter, 0, totalNumOfEntries) p4InfoCounters = append( p4InfoCounters, @@ -278,11 +278,11 @@ func TestMultipleEntitiesSingleCounterRead(t *testing.T) { ) for i := 0; i < totalNumOfEntries; i++ { - counterEntry := &v1.Entity{ + counterEntry := &p4.Entity{ Entity: createEntityCounterEntry( 0, int64(i), - &v1.CounterData{ + &p4.CounterData{ ByteCount: int64(10), PacketCount: int64(10), }, @@ -306,7 +306,7 @@ func TestMultipleEntitiesSingleCounterRead(t *testing.T) { )) } - forwardingPipelineConfig := &v1.ForwardingPipelineConfig{ + forwardingPipelineConfig := &p4.ForwardingPipelineConfig{ P4Info: &p4_config.P4Info{ Counters: p4InfoCounters, PkgInfo: &p4_config.PkgInfo{Name: "P4Program"}, @@ -314,17 +314,17 @@ func TestMultipleEntitiesSingleCounterRead(t *testing.T) { } p4RtReadClient := &fakeP4RuntimeReadClient{ - recvFn: func() (*v1.ReadResponse, error) { - return &v1.ReadResponse{Entities: entities}, nil + recvFn: func() (*p4.ReadResponse, error) { + return &p4.ReadResponse{Entities: entities}, nil }, } p4RtClient := &fakeP4RuntimeClient{ - readFn: func(*v1.ReadRequest) (v1.P4Runtime_ReadClient, error) { + readFn: func(*p4.ReadRequest) (p4.P4Runtime_ReadClient, error) { return p4RtReadClient, nil }, - getForwardingPipelineConfigFn: func() (*v1.GetForwardingPipelineConfigResponse, error) { - return &v1.GetForwardingPipelineConfigResponse{ + getForwardingPipelineConfigFn: func() (*p4.GetForwardingPipelineConfigResponse, error) { + return &p4.GetForwardingPipelineConfigResponse{ Config: forwardingPipelineConfig, }, nil }, @@ -388,7 +388,7 @@ func TestSingleEntitiesMultipleCounterRead(t *testing.T) { )) } - forwardingPipelineConfig := &v1.ForwardingPipelineConfig{ + forwardingPipelineConfig := &p4.ForwardingPipelineConfig{ P4Info: &p4_config.P4Info{ Counters: p4InfoCounters, PkgInfo: &p4_config.PkgInfo{Name: "P4Program"}, @@ -396,16 +396,16 @@ func TestSingleEntitiesMultipleCounterRead(t *testing.T) { } p4RtClient := &fakeP4RuntimeClient{ - readFn: func(in *v1.ReadRequest) (v1.P4Runtime_ReadClient, error) { + readFn: func(in *p4.ReadRequest) (p4.P4Runtime_ReadClient, error) { counterID := in.Entities[0].GetCounterEntry().CounterId return &fakeP4RuntimeReadClient{ - recvFn: func() (*v1.ReadResponse, error) { - return &v1.ReadResponse{ - Entities: []*v1.Entity{{ + recvFn: func() (*p4.ReadResponse, error) { + return &p4.ReadResponse{ + Entities: []*p4.Entity{{ Entity: createEntityCounterEntry( counterID, 1, - &v1.CounterData{ + &p4.CounterData{ ByteCount: 10, PacketCount: 10, }, @@ -415,8 +415,8 @@ func TestSingleEntitiesMultipleCounterRead(t *testing.T) { }, }, nil }, - getForwardingPipelineConfigFn: func() (*v1.GetForwardingPipelineConfigResponse, error) { - return &v1.GetForwardingPipelineConfigResponse{ + getForwardingPipelineConfigFn: func() (*p4.GetForwardingPipelineConfigResponse, error) { + return &p4.GetForwardingPipelineConfigResponse{ Config: forwardingPipelineConfig, }, nil }, @@ -442,13 +442,13 @@ func TestSingleEntitiesMultipleCounterRead(t *testing.T) { } func TestNoCountersAvailable(t *testing.T) { - forwardingPipelineConfig := &v1.ForwardingPipelineConfig{ + forwardingPipelineConfig := &p4.ForwardingPipelineConfig{ P4Info: &p4_config.P4Info{Counters: []*p4_config.Counter{}}, } p4RtClient := &fakeP4RuntimeClient{ - getForwardingPipelineConfigFn: func() (*v1.GetForwardingPipelineConfigResponse, error) { - return &v1.GetForwardingPipelineConfigResponse{ + getForwardingPipelineConfigFn: func() (*p4.GetForwardingPipelineConfigResponse, error) { + return &p4.GetForwardingPipelineConfigResponse{ Config: forwardingPipelineConfig, }, nil }, @@ -464,7 +464,7 @@ func TestNoCountersAvailable(t *testing.T) { } func TestFilterCounters(t *testing.T) { - forwardingPipelineConfig := &v1.ForwardingPipelineConfig{ + forwardingPipelineConfig := &p4.ForwardingPipelineConfig{ P4Info: &p4_config.P4Info{ Counters: []*p4_config.Counter{ createCounter("foo", 1, p4_config.CounterSpec_BOTH), @@ -474,8 +474,8 @@ func TestFilterCounters(t *testing.T) { } p4RtClient := &fakeP4RuntimeClient{ - getForwardingPipelineConfigFn: func() (*v1.GetForwardingPipelineConfigResponse, error) { - return &v1.GetForwardingPipelineConfigResponse{ + getForwardingPipelineConfigFn: func() (*p4.GetForwardingPipelineConfigResponse, error) { + return &p4.GetForwardingPipelineConfigResponse{ Config: forwardingPipelineConfig, }, nil }, @@ -500,22 +500,22 @@ func TestFilterCounters(t *testing.T) { func TestFailReadCounterEntryFromEntry(t *testing.T) { p4RtReadClient := &fakeP4RuntimeReadClient{ - recvFn: func() (*v1.ReadResponse, error) { - return &v1.ReadResponse{ - Entities: []*v1.Entity{{ - Entity: &v1.Entity_TableEntry{ - TableEntry: &v1.TableEntry{}, + recvFn: func() (*p4.ReadResponse, error) { + return &p4.ReadResponse{ + Entities: []*p4.Entity{{ + Entity: &p4.Entity_TableEntry{ + TableEntry: &p4.TableEntry{}, }}}}, nil }, } p4RtClient := &fakeP4RuntimeClient{ - readFn: func(*v1.ReadRequest) (v1.P4Runtime_ReadClient, error) { + readFn: func(*p4.ReadRequest) (p4.P4Runtime_ReadClient, error) { return p4RtReadClient, nil }, - getForwardingPipelineConfigFn: func() (*v1.GetForwardingPipelineConfigResponse, error) { - return &v1.GetForwardingPipelineConfigResponse{ - Config: &v1.ForwardingPipelineConfig{ + getForwardingPipelineConfigFn: func() (*p4.GetForwardingPipelineConfigResponse, error) { + return &p4.GetForwardingPipelineConfigResponse{ + Config: &p4.ForwardingPipelineConfig{ P4Info: &p4_config.P4Info{ Counters: []*p4_config.Counter{ createCounter( @@ -553,12 +553,12 @@ func TestFailReadCounterEntryFromEntry(t *testing.T) { func TestFailReadAllEntries(t *testing.T) { p4RtClient := &fakeP4RuntimeClient{ - readFn: func(*v1.ReadRequest) (v1.P4Runtime_ReadClient, error) { + readFn: func(*p4.ReadRequest) (p4.P4Runtime_ReadClient, error) { return nil, errors.New("connection error") }, - getForwardingPipelineConfigFn: func() (*v1.GetForwardingPipelineConfigResponse, error) { - return &v1.GetForwardingPipelineConfigResponse{ - Config: &v1.ForwardingPipelineConfig{ + getForwardingPipelineConfigFn: func() (*p4.GetForwardingPipelineConfigResponse, error) { + return &p4.GetForwardingPipelineConfigResponse{ + Config: &p4.ForwardingPipelineConfig{ P4Info: &p4_config.P4Info{ Counters: []*p4_config.Counter{ createCounter(