From 0142c737c4df2021cd82ea3638c6dac42190721c Mon Sep 17 00:00:00 2001 From: Vladimir Popov Date: Wed, 25 Nov 2020 13:02:48 +0700 Subject: [PATCH] Set back nse@domain name on return from interdomainURLServer (#605) Signed-off-by: Vladimir Popov Signed-off-by: Sergey Ershov --- .../chains/nsmgrproxy/server_test.go | 4 +- .../common/interdomainurl/server.go | 57 ++++++++++++------- .../common/interdomainurl/server_test.go | 20 ++++++- 3 files changed, 54 insertions(+), 27 deletions(-) diff --git a/pkg/networkservice/chains/nsmgrproxy/server_test.go b/pkg/networkservice/chains/nsmgrproxy/server_test.go index a2b5d54995..1802d6edb2 100644 --- a/pkg/networkservice/chains/nsmgrproxy/server_test.go +++ b/pkg/networkservice/chains/nsmgrproxy/server_test.go @@ -86,9 +86,7 @@ func TestNSMGR_InterdomainUseCase(t *testing.T) { // Simulate refresh from client. refreshRequest := request.Clone() - refreshRequest.GetConnection().Context = conn.Context - refreshRequest.GetConnection().Mechanism = conn.Mechanism - refreshRequest.GetConnection().NetworkServiceEndpointName = conn.NetworkServiceEndpointName + refreshRequest.Connection = conn.Clone() conn, err = nsc.Request(ctx, refreshRequest) require.NoError(t, err) diff --git a/pkg/networkservice/common/interdomainurl/server.go b/pkg/networkservice/common/interdomainurl/server.go index 62191a126a..fd89cee75b 100644 --- a/pkg/networkservice/common/interdomainurl/server.go +++ b/pkg/networkservice/common/interdomainurl/server.go @@ -22,9 +22,10 @@ import ( "net/url" "github.com/golang/protobuf/ptypes/empty" - "github.com/networkservicemesh/api/pkg/api/networkservice" "github.com/pkg/errors" + "github.com/networkservicemesh/api/pkg/api/networkservice" + "github.com/networkservicemesh/sdk/pkg/networkservice/core/next" "github.com/networkservicemesh/sdk/pkg/tools/clienturlctx" "github.com/networkservicemesh/sdk/pkg/tools/interdomain" @@ -32,35 +33,49 @@ import ( type interdomainURLServer struct{} +// NewServer creates new interdomainurl chain element +func NewServer() networkservice.NetworkServiceServer { + return &interdomainURLServer{} +} + func (i *interdomainURLServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*networkservice.Connection, error) { - nseName := request.GetConnection().GetNetworkServiceEndpointName() - if nseName == "" { - return nil, errors.New("NSE is not selected") + interDomainNSEName := request.GetConnection().GetNetworkServiceEndpointName() + nseName, domainURL, err := parseInterDomainNSEName(interDomainNSEName) + if err != nil { + return nil, err } - remoteURL := interdomain.Domain(nseName) - u, err := url.Parse(remoteURL) + request.GetConnection().NetworkServiceEndpointName = nseName + + conn, err := next.Server(ctx).Request(clienturlctx.WithClientURL(ctx, domainURL), request) if err != nil { - return nil, errors.Wrap(err, "selected NSE has wrong name. Make sure that proxy-registry has handled NSE") + return nil, err } - request.GetConnection().NetworkServiceEndpointName = interdomain.Target(nseName) - return next.Server(ctx).Request(clienturlctx.WithClientURL(ctx, u), request) + conn.NetworkServiceEndpointName = interDomainNSEName + + return conn, nil +} + +func (i *interdomainURLServer) Close(ctx context.Context, conn *networkservice.Connection) (*empty.Empty, error) { + interDomainNSEName := conn.GetNetworkServiceEndpointName() + nseName, domainURL, err := parseInterDomainNSEName(interDomainNSEName) + if err != nil { + return nil, err + } + conn.NetworkServiceEndpointName = nseName + + return next.Server(ctx).Close(clienturlctx.WithClientURL(ctx, domainURL), conn) } -func (i *interdomainURLServer) Close(ctx context.Context, connection *networkservice.Connection) (*empty.Empty, error) { - nseName := connection.GetNetworkServiceEndpointName() - if nseName == "" { - return nil, errors.New("NSE is not selected") +func parseInterDomainNSEName(interDomainNSEName string) (string, *url.URL, error) { + if interDomainNSEName == "" { + return "", nil, errors.New("NSE is not selected") } - remoteURL := interdomain.Domain(nseName) + + remoteURL := interdomain.Domain(interDomainNSEName) u, err := url.Parse(remoteURL) if err != nil { - return nil, errors.Wrap(err, "selected NSE has wrong name. Make sure that proxy-registry has handled NSE") + return "", nil, errors.Wrap(err, "selected NSE has wrong name. Make sure that proxy-registry has handled NSE") } - connection.NetworkServiceEndpointName = interdomain.Target(nseName) - return next.Server(ctx).Close(clienturlctx.WithClientURL(ctx, u), connection) -} -// NewServer creates new interdomainurl chain element -func NewServer() networkservice.NetworkServiceServer { - return &interdomainURLServer{} + return interdomain.Target(interDomainNSEName), u, nil } diff --git a/pkg/networkservice/common/interdomainurl/server_test.go b/pkg/networkservice/common/interdomainurl/server_test.go index 3ebf2ed343..0209d7b282 100644 --- a/pkg/networkservice/common/interdomainurl/server_test.go +++ b/pkg/networkservice/common/interdomainurl/server_test.go @@ -18,6 +18,7 @@ package interdomainurl_test import ( "context" + "fmt" "net/url" "testing" @@ -27,11 +28,17 @@ import ( "github.com/networkservicemesh/sdk/pkg/networkservice/common/interdomainurl" "github.com/networkservicemesh/sdk/pkg/networkservice/core/next" "github.com/networkservicemesh/sdk/pkg/networkservice/utils/checks/checkcontext" + "github.com/networkservicemesh/sdk/pkg/networkservice/utils/checks/checkrequest" "github.com/networkservicemesh/sdk/pkg/tools/clienturlctx" ) +const ( + nseName = "nse1" + domainURL = "tcp://127.0.0.1:5000" +) + func TestInterdomainURLServer_Register(t *testing.T) { - expected, err := url.Parse("tcp://127.0.0.1:5000") + expected, err := url.Parse(domainURL) require.NoError(t, err) s := next.NewNetworkServiceServer( @@ -39,11 +46,18 @@ func TestInterdomainURLServer_Register(t *testing.T) { checkcontext.NewServer(t, func(t *testing.T, ctx context.Context) { require.Equal(t, *expected, *clienturlctx.ClientURL(ctx)) }), + checkrequest.NewServer(t, func(t *testing.T, request *networkservice.NetworkServiceRequest) { + require.Equal(t, nseName, request.Connection.NetworkServiceEndpointName) + }), ) - _, err = s.Request(context.Background(), &networkservice.NetworkServiceRequest{ + + conn, err := s.Request(context.Background(), &networkservice.NetworkServiceRequest{ Connection: &networkservice.Connection{ - NetworkServiceEndpointName: "nse1@" + expected.String(), + NetworkServiceEndpointName: fmt.Sprintf("%s@%s", nseName, domainURL), }, }) require.NoError(t, err) + require.NotNil(t, conn) + + require.Equal(t, fmt.Sprintf("%s@%s", nseName, domainURL), conn.NetworkServiceEndpointName) }