diff --git a/pkg/networkservice/chains/nsmgr/server.go b/pkg/networkservice/chains/nsmgr/server.go index b25e8d4cc..aecbad2d4 100644 --- a/pkg/networkservice/chains/nsmgr/server.go +++ b/pkg/networkservice/chains/nsmgr/server.go @@ -93,9 +93,11 @@ func NewServer(ctx context.Context, nsmRegistration *registryapi.NetworkServiceE } localBypassRegistryServer := localbypass.NewNetworkServiceEndpointRegistryServer(nsmRegistration.Url) + localBypassRegistryClient := localbypass.NewNetworkServiceEndpointRegistryClient(nsmRegistration.Url) nseClient := next.NewNetworkServiceEndpointRegistryClient( querycache.NewClient(ctx), + localBypassRegistryClient, registryadapter.NetworkServiceEndpointServerToClient(next.NewNetworkServiceEndpointRegistryServer( localBypassRegistryServer, nseRegistry)), diff --git a/pkg/registry/common/localbypass/client.go b/pkg/registry/common/localbypass/client.go new file mode 100644 index 000000000..abb8fdc04 --- /dev/null +++ b/pkg/registry/common/localbypass/client.go @@ -0,0 +1,61 @@ +// Copyright (c) 2020-2021 Doc.ai and/or its affiliates. +// +// SPDX-License-Identifier: Apache-2.0 +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Package localbypass implements a chain element to set NSMgr URL to endpoints on registration and set back endpoints +// URLs on find +package localbypass + +import ( + "context" + "github.com/golang/protobuf/ptypes/empty" + "github.com/networkservicemesh/api/pkg/api/registry" + "google.golang.org/grpc" + + "github.com/networkservicemesh/sdk/pkg/registry/core/next" + "github.com/networkservicemesh/sdk/pkg/tools/stringurl" +) + +type localBypassNSEClient struct { + url string + nseURLs stringurl.Map +} + +// NewNetworkServiceEndpointRegistryClient creates new instance of NetworkServiceEndpointRegistryClient which +// checks endpoints URLs on find +func NewNetworkServiceEndpointRegistryClient(u string) registry.NetworkServiceEndpointRegistryClient { + return &localBypassNSEClient{ + url: u, + } +} + +func (rc *localBypassNSEClient) Register(ctx context.Context, in *registry.NetworkServiceEndpoint, opts ...grpc.CallOption) (*registry.NetworkServiceEndpoint, error) { + return next.NetworkServiceEndpointRegistryClient(ctx).Register(ctx, in, opts...) +} + +func (rc *localBypassNSEClient) Find(ctx context.Context, in *registry.NetworkServiceEndpointQuery, opts ...grpc.CallOption) (registry.NetworkServiceEndpointRegistry_FindClient, error) { + client, err := next.NetworkServiceEndpointRegistryClient(ctx).Find(ctx, in, opts...) + if err != nil { + return client, err + } + return &localBypassNSEFindClient{ + url: rc.url, + NetworkServiceEndpointRegistry_FindClient: client, + }, nil +} + +func (rc *localBypassNSEClient) Unregister(ctx context.Context, in *registry.NetworkServiceEndpoint, opts ...grpc.CallOption) (*empty.Empty, error) { + return next.NetworkServiceEndpointRegistryClient(ctx).Unregister(ctx, in, opts...) +} diff --git a/pkg/registry/common/localbypass/client_test.go b/pkg/registry/common/localbypass/client_test.go new file mode 100644 index 000000000..03fcdbe2b --- /dev/null +++ b/pkg/registry/common/localbypass/client_test.go @@ -0,0 +1,89 @@ +// Copyright (c) 2021 Doc.ai and/or its affiliates. +// +// SPDX-License-Identifier: Apache-2.0 +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package localbypass_test + +import ( + "context" + "testing" + + "github.com/stretchr/testify/require" + "go.uber.org/goleak" + + "github.com/networkservicemesh/api/pkg/api/registry" + + "github.com/networkservicemesh/sdk/pkg/registry/common/localbypass" + "github.com/networkservicemesh/sdk/pkg/registry/common/memory" + "github.com/networkservicemesh/sdk/pkg/registry/core/adapters" + "github.com/networkservicemesh/sdk/pkg/registry/core/next" +) + +func TestLocalBypassNSEClient_NSEUrl(t *testing.T) { + defer goleak.VerifyNone(t, goleak.IgnoreCurrent()) + + server := next.NewNetworkServiceEndpointRegistryClient( + localbypass.NewNetworkServiceEndpointRegistryClient(nsmgrURL), + adapters.NetworkServiceEndpointServerToClient(next.NewNetworkServiceEndpointRegistryServer( + memory.NewNetworkServiceEndpointRegistryServer(), + )), + ) + + // 1. Register + nse, err := server.Register(context.Background(), ®istry.NetworkServiceEndpoint{ + Name: "nse-1", + Url: nseURL, + }) + require.NoError(t, err) + require.Equal(t, nseURL, nse.Url) + + // 2. Find + stream, err := server.Find(context.Background(), ®istry.NetworkServiceEndpointQuery{ + NetworkServiceEndpoint: new(registry.NetworkServiceEndpoint), + }) + require.NoError(t, err) + + findNSE, err := stream.Recv() + require.NoError(t, err) + require.Equal(t, nseURL, findNSE.Url) +} + +func TestLocalBypassNSEClient_NSMgrUrl(t *testing.T) { + defer goleak.VerifyNone(t, goleak.IgnoreCurrent()) + + server := next.NewNetworkServiceEndpointRegistryClient( + localbypass.NewNetworkServiceEndpointRegistryClient(nsmgrURL), + adapters.NetworkServiceEndpointServerToClient(next.NewNetworkServiceEndpointRegistryServer( + memory.NewNetworkServiceEndpointRegistryServer(), + )), + ) + + // 1. Register + nse, err := server.Register(context.Background(), ®istry.NetworkServiceEndpoint{ + Name: "nse-1", + Url: nsmgrURL, + }) + require.NoError(t, err) + require.Equal(t, nsmgrURL, nse.Url) + + // 2. Find + stream, err := server.Find(context.Background(), ®istry.NetworkServiceEndpointQuery{ + NetworkServiceEndpoint: new(registry.NetworkServiceEndpoint), + }) + require.NoError(t, err) + + _, err = stream.Recv() + require.Error(t, err) +} diff --git a/pkg/registry/common/localbypass/const_test.go b/pkg/registry/common/localbypass/const_test.go new file mode 100644 index 000000000..1b2945afa --- /dev/null +++ b/pkg/registry/common/localbypass/const_test.go @@ -0,0 +1,22 @@ +// Copyright (c) 2021 Doc.ai and/or its affiliates. +// +// SPDX-License-Identifier: Apache-2.0 +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package localbypass_test + +const ( + nsmgrURL = "tcp://0.0.0.0" + nseURL = "tcp://1.1.1.1" +) diff --git a/pkg/registry/common/localbypass/find_client.go b/pkg/registry/common/localbypass/find_client.go new file mode 100644 index 000000000..82c66c8ec --- /dev/null +++ b/pkg/registry/common/localbypass/find_client.go @@ -0,0 +1,38 @@ +// Copyright (c) 2020-2021 Doc.ai and/or its affiliates. +// +// SPDX-License-Identifier: Apache-2.0 +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package localbypass + +import ( + "errors" + "github.com/networkservicemesh/api/pkg/api/registry" +) + +type localBypassNSEFindClient struct { + url string + registry.NetworkServiceEndpointRegistry_FindClient +} + +func (s *localBypassNSEFindClient) Recv() (*registry.NetworkServiceEndpoint, error) { + nse, err := s.NetworkServiceEndpointRegistry_FindClient.Recv() + if err != nil { + return nse, err + } + if nse.Url == s.url { + return nil, errors.New("NSMgr found unregistered endpoint") + } + return nse, err +} diff --git a/pkg/registry/common/localbypass/server_test.go b/pkg/registry/common/localbypass/server_test.go index e865fb511..9150db32b 100644 --- a/pkg/registry/common/localbypass/server_test.go +++ b/pkg/registry/common/localbypass/server_test.go @@ -31,11 +31,6 @@ import ( "github.com/networkservicemesh/sdk/pkg/registry/core/next" ) -const ( - nsmgrURL = "tcp://0.0.0.0" - nseURL = "tcp://1.1.1.1" -) - func TestLocalBypassNSEServer(t *testing.T) { defer goleak.VerifyNone(t, goleak.IgnoreCurrent())