Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add replaceNSEName chain element #1328

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkg/networkservice/chains/nsmgr/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
"github.com/networkservicemesh/sdk/pkg/networkservice/common/mechanisms/kernel"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/mechanismtranslation"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/replacelabels"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/replacensename"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/chain"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/count"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/inject/injecterror"
Expand Down Expand Up @@ -795,6 +796,7 @@ func additionalFunctionalityChain(ctx context.Context, clientURL *url.URL, clien
client.WithAdditionalFunctionality(
mechanismtranslation.NewClient(),
replacelabels.NewClient(labels),
replacensename.NewClient(),
Copy link
Member

@denis-tingaikin denis-tingaikin Jul 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im mostly sure that we could face with the similar issue in future (when we need to replace something in pass through requests)

Should we create a pkg like passthrough and concatenate all needed replace elements?

Something like

package passthrough

func NewClient(labels map[string]string) networkservice.NetworkServiceClient {
return chain.NewClient(
          replacelabels.NewClient(labels),
          replacensename.NewClient(),
     )

}

And use passthrough chain in firewall nses?

/cc @edwarnicke

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a good idea, thanks!
Done

kernel.NewClient(),
),
client.WithDialOptions(sandbox.DialOptions()...),
Expand Down
41 changes: 13 additions & 28 deletions pkg/networkservice/common/replacelabels/client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021 Doc.ai and/or its affiliates.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we return this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, done

// Copyright (c) 2022 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -29,45 +29,30 @@ import (
)

type replaceLabelsClient struct {
labels map[string]string
oldConnLabels map[string]string
oldNseName string
labels map[string]string
}

func (s *replaceLabelsClient) Request(ctx context.Context, request *networkservice.NetworkServiceRequest, opts ...grpc.CallOption) (conn *networkservice.Connection, err error) {
s.oldConnLabels = request.Connection.Labels
s.oldNseName = request.Connection.NetworkServiceEndpointName
// NewClient creates new instance of NetworkServiceClient chain element, which replaces labels in the connection
func NewClient(labels map[string]string) networkservice.NetworkServiceClient {
return &replaceLabelsClient{
labels: labels,
}
}

func (s *replaceLabelsClient) Request(ctx context.Context, request *networkservice.NetworkServiceRequest, opts ...grpc.CallOption) (conn *networkservice.Connection, err error) {
prevConnLabels := request.Connection.Labels
request.Connection.Labels = s.labels
request.Connection.NetworkServiceEndpointName = ""

conn, err = next.Client(ctx).Request(ctx, request, opts...)
if err != nil {
return nil, err
}

conn.Labels = s.oldConnLabels
conn.NetworkServiceEndpointName = s.oldNseName
conn.Labels = prevConnLabels

return conn, nil
}

func (s *replaceLabelsClient) Close(ctx context.Context, conn *networkservice.Connection, opts ...grpc.CallOption) (*empty.Empty, error) {
s.oldConnLabels = conn.Labels
s.oldNseName = conn.NetworkServiceEndpointName

rv, err := next.Client(ctx).Close(ctx, conn, opts...)

conn.Labels = s.oldConnLabels
conn.NetworkServiceEndpointName = s.oldNseName

return rv, err
}

// NewClient creates new instance of NetworkServiceClient chain element, which replaces labels in the connection
func NewClient(labels map[string]string) networkservice.NetworkServiceClient {
return &replaceLabelsClient{
labels: labels,
oldConnLabels: make(map[string]string),
}
conn.Labels = s.labels
return next.Client(ctx).Close(ctx, conn, opts...)
}
61 changes: 61 additions & 0 deletions pkg/networkservice/common/replacelabels/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (c) 2022 Cisco 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 replacelabels_test

import (
"context"
"testing"

"go.uber.org/goleak"

"github.com/stretchr/testify/require"

"github.com/networkservicemesh/api/pkg/api/networkservice"

"github.com/networkservicemesh/sdk/pkg/networkservice/common/replacelabels"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/chain"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/checks/checkclose"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/checks/checkrequest"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata"
)

func TestClient(t *testing.T) {
t.Cleanup(func() { goleak.VerifyNone(t) })

chainLabels := map[string]string{"1": "A", "2": "B"}
client := chain.NewNetworkServiceClient(
metadata.NewClient(),
replacelabels.NewClient(chainLabels),
checkrequest.NewClient(t, func(t *testing.T, r *networkservice.NetworkServiceRequest) {
require.Equal(t, chainLabels, r.Connection.Labels)
}),
checkclose.NewClient(t, func(t *testing.T, c *networkservice.Connection) {
require.Equal(t, chainLabels, c.Labels)
}),
)

// Create the request with any labels
req := &networkservice.NetworkServiceRequest{
Connection: &networkservice.Connection{Id: "nsc-1", Labels: map[string]string{"3": "C"}},
}
conn, err := client.Request(context.Background(), req)
require.NoError(t, err)
require.Equal(t, map[string]string{"3": "C"}, conn.Labels)

_, err = client.Close(context.Background(), conn)
require.NoError(t, err)
}
56 changes: 56 additions & 0 deletions pkg/networkservice/common/replacensename/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) 2022 Cisco 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 replacensename replaces NetworkServiceEndpointName if it was discovered before
package replacensename

import (
"context"

"github.com/golang/protobuf/ptypes/empty"
"google.golang.org/grpc"

"github.com/networkservicemesh/api/pkg/api/networkservice"

"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
)

type replaceNSEClient struct{}

// NewClient creates new instance of NetworkServiceClient chain element, which replaces NetworkServiceEndpointName in the connection
func NewClient() networkservice.NetworkServiceClient {
return &replaceNSEClient{}
}

func (s *replaceNSEClient) Request(ctx context.Context, request *networkservice.NetworkServiceRequest, opts ...grpc.CallOption) (conn *networkservice.Connection, err error) {
prevNseName := request.Connection.NetworkServiceEndpointName
request.Connection.NetworkServiceEndpointName, _ = load(ctx)

conn, err = next.Client(ctx).Request(ctx, request, opts...)
if err != nil {
return nil, err
}

store(ctx, conn.NetworkServiceEndpointName)
conn.NetworkServiceEndpointName = prevNseName

return conn, nil
}

func (s *replaceNSEClient) Close(ctx context.Context, conn *networkservice.Connection, opts ...grpc.CallOption) (*empty.Empty, error) {
conn.NetworkServiceEndpointName, _ = loadAndDelete(ctx)
return next.Client(ctx).Close(ctx, conn, opts...)
}
83 changes: 83 additions & 0 deletions pkg/networkservice/common/replacensename/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright (c) 2022 Cisco 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 replacensename_test

import (
"context"
"testing"

"go.uber.org/goleak"
"google.golang.org/grpc"

"github.com/golang/protobuf/ptypes/empty"
"github.com/stretchr/testify/require"

"github.com/networkservicemesh/api/pkg/api/networkservice"

"github.com/networkservicemesh/sdk/pkg/networkservice/common/replacensename"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/chain"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/checks/checkclose"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/checks/checkrequest"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata"
)

func TestClient(t *testing.T) {
t.Cleanup(func() { goleak.VerifyNone(t) })

client := chain.NewNetworkServiceClient(
metadata.NewClient(),
replacensename.NewClient(),
&setNSENameClient{name: "nse-name1"},
checkrequest.NewClient(t, func(t *testing.T, r *networkservice.NetworkServiceRequest) {
require.Equal(t, "nse-name1", r.Connection.NetworkServiceEndpointName)
}),
checkclose.NewClient(t, func(t *testing.T, c *networkservice.Connection) {
require.Equal(t, "nse-name1", c.NetworkServiceEndpointName)
}),
)
req := &networkservice.NetworkServiceRequest{
Connection: &networkservice.Connection{Id: "nsc-1"},
}
conn, err := client.Request(context.Background(), req)
require.NoError(t, err)

// Change NetworkServiceEndpointName to another name
conn.NetworkServiceEndpointName = "nse-name2"
conn, err = client.Request(context.Background(), req)
require.Equal(t, "nse-name2", conn.NetworkServiceEndpointName)
require.NoError(t, err)

_, err = client.Close(context.Background(), conn)
require.NoError(t, err)
}

// setNSENameClient sets NetworkServiceEndpointName only if it is empty
type setNSENameClient struct {
name string
}

func (s *setNSENameClient) Request(ctx context.Context, request *networkservice.NetworkServiceRequest, opts ...grpc.CallOption) (*networkservice.Connection, error) {
if request.GetConnection().NetworkServiceEndpointName == "" {
request.GetConnection().NetworkServiceEndpointName = s.name
}
return next.Client(ctx).Request(ctx, request, opts...)
}

func (s *setNSENameClient) Close(ctx context.Context, conn *networkservice.Connection, opts ...grpc.CallOption) (*empty.Empty, error) {
return next.Client(ctx).Close(ctx, conn, opts...)
}
50 changes: 50 additions & 0 deletions pkg/networkservice/common/replacensename/metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) 2022 Cisco 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 replacensename

import (
"context"

"github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata"
)

type keyType struct{}

// store - stores the next network service endpoint name
func store(ctx context.Context, nseName string) {
metadata.Map(ctx, true).Store(keyType{}, nseName)
}

// load - returns the next network service endpoint name
func load(ctx context.Context) (value string, ok bool) {
rawValue, ok := metadata.Map(ctx, true).Load(keyType{})
if !ok {
return
}
value, ok = rawValue.(string)
return value, ok
}

// loadAndDelete - returns the next network service endpoint name and deletes it from the metadata
func loadAndDelete(ctx context.Context) (value string, ok bool) {
rawValue, ok := metadata.Map(ctx, true).LoadAndDelete(keyType{})
if !ok {
return
}
value, ok = rawValue.(string)
return value, ok
}
53 changes: 53 additions & 0 deletions pkg/networkservice/utils/checks/checkclose/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) 2022 Cisco 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 checkclose - provides networkservice chain elements to check the Close received from the previous element in the chain
package checkclose

import (
"context"
"testing"

"github.com/golang/protobuf/ptypes/empty"
"github.com/networkservicemesh/api/pkg/api/networkservice"
"google.golang.org/grpc"

"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
)

type checkCloseClient struct {
*testing.T
check func(*testing.T, *networkservice.Connection)
}

// NewClient - returns NetworkServiceClient chain elements to check the Close received from the previous element in the chain
// t - *testing.T for checks
// check - function to check the Connection
func NewClient(t *testing.T, check func(*testing.T, *networkservice.Connection)) networkservice.NetworkServiceClient {
return &checkCloseClient{
T: t,
check: check,
}
}

func (c *checkCloseClient) Request(ctx context.Context, request *networkservice.NetworkServiceRequest, opts ...grpc.CallOption) (*networkservice.Connection, error) {
return next.Client(ctx).Request(ctx, request, opts...)
}

func (c *checkCloseClient) Close(ctx context.Context, conn *networkservice.Connection, opts ...grpc.CallOption) (*empty.Empty, error) {
c.check(c.T, conn)
return next.Client(ctx).Close(ctx, conn, opts...)
}