Skip to content

Commit

Permalink
Localbypass Client
Browse files Browse the repository at this point in the history
Signed-off-by: Artem Belov <artem.belov@xored.com>
  • Loading branch information
Artem Belov committed Mar 9, 2021
1 parent 009fa1e commit 063706d
Show file tree
Hide file tree
Showing 6 changed files with 212 additions and 5 deletions.
2 changes: 2 additions & 0 deletions pkg/networkservice/chains/nsmgr/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down
61 changes: 61 additions & 0 deletions pkg/registry/common/localbypass/client.go
Original file line number Diff line number Diff line change
@@ -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...)
}
89 changes: 89 additions & 0 deletions pkg/registry/common/localbypass/client_test.go
Original file line number Diff line number Diff line change
@@ -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(), &registry.NetworkServiceEndpoint{
Name: "nse-1",
Url: nseURL,
})
require.NoError(t, err)
require.Equal(t, nseURL, nse.Url)

// 2. Find
stream, err := server.Find(context.Background(), &registry.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(), &registry.NetworkServiceEndpoint{
Name: "nse-1",
Url: nsmgrURL,
})
require.NoError(t, err)
require.Equal(t, nsmgrURL, nse.Url)

// 2. Find
stream, err := server.Find(context.Background(), &registry.NetworkServiceEndpointQuery{
NetworkServiceEndpoint: new(registry.NetworkServiceEndpoint),
})
require.NoError(t, err)

_, err = stream.Recv()
require.Error(t, err)
}
22 changes: 22 additions & 0 deletions pkg/registry/common/localbypass/const_test.go
Original file line number Diff line number Diff line change
@@ -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"
)
38 changes: 38 additions & 0 deletions pkg/registry/common/localbypass/find_client.go
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 0 additions & 5 deletions pkg/registry/common/localbypass/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down

0 comments on commit 063706d

Please sign in to comment.