From d30bde378a8bf3af5c6136f0ee696c7c41a6bbc7 Mon Sep 17 00:00:00 2001 From: Ed Warnicke Date: Sat, 17 Apr 2021 18:23:27 -0500 Subject: [PATCH] Retire supported chain element (#843) Since sdk-vpp has been reworked to shift server chain elements before connect chain elements supported is neither used nor needed. https://github.com/networkservicemesh/sdk-vpp/pull/156 Signed-off-by: Ed Warnicke --- .../common/mechanisms/supported/doc.go | 18 --- .../common/mechanisms/supported/options.go | 31 ----- .../common/mechanisms/supported/server.go | 70 ----------- .../mechanisms/supported/server_test.go | 112 ------------------ 4 files changed, 231 deletions(-) delete mode 100644 pkg/networkservice/common/mechanisms/supported/doc.go delete mode 100644 pkg/networkservice/common/mechanisms/supported/options.go delete mode 100644 pkg/networkservice/common/mechanisms/supported/server.go delete mode 100644 pkg/networkservice/common/mechanisms/supported/server_test.go diff --git a/pkg/networkservice/common/mechanisms/supported/doc.go b/pkg/networkservice/common/mechanisms/supported/doc.go deleted file mode 100644 index f419f033f..000000000 --- a/pkg/networkservice/common/mechanisms/supported/doc.go +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright (c) 2021 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 supported allows fast short circuiting of a request if the server cannot provide any of its mechanism preferences -package supported diff --git a/pkg/networkservice/common/mechanisms/supported/options.go b/pkg/networkservice/common/mechanisms/supported/options.go deleted file mode 100644 index ad955b29e..000000000 --- a/pkg/networkservice/common/mechanisms/supported/options.go +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright (c) 2021 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 supported - -type supportedOptions struct { - supportedMechanismTypes []string -} - -// Option for supported.NewServer -type Option func(o *supportedOptions) - -// WithSupportedMechanismTypes - mechanism types supported by Endpoint -func WithSupportedMechanismTypes(supportedMechanismTypes ...string) Option { - return func(o *supportedOptions) { - o.supportedMechanismTypes = append(o.supportedMechanismTypes, supportedMechanismTypes...) - } -} diff --git a/pkg/networkservice/common/mechanisms/supported/server.go b/pkg/networkservice/common/mechanisms/supported/server.go deleted file mode 100644 index 2e70f1685..000000000 --- a/pkg/networkservice/common/mechanisms/supported/server.go +++ /dev/null @@ -1,70 +0,0 @@ -// Copyright (c) 2021 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 supported - -import ( - "context" - - "github.com/golang/protobuf/ptypes/empty" - "github.com/networkservicemesh/api/pkg/api/networkservice" - "github.com/pkg/errors" - - "github.com/networkservicemesh/sdk/pkg/networkservice/core/next" -) - -type supportedMechanismServer struct { - supportedMechanismTypes []string -} - -// NewServer - chain element to check that we have support for the mechanisms requested -func NewServer(options ...Option) networkservice.NetworkServiceServer { - o := &supportedOptions{} - for _, opt := range options { - opt(o) - } - return &supportedMechanismServer{ - supportedMechanismTypes: o.supportedMechanismTypes, - } -} - -func (s *supportedMechanismServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*networkservice.Connection, error) { - // If we have a specified Mechanism... we really need to support it - if request.GetConnection().GetMechanism() != nil { - for _, supportedMechanismType := range s.supportedMechanismTypes { - if request.GetConnection().GetMechanism().GetType() == supportedMechanismType { - return next.Server(ctx).Request(ctx, request) - } - } - return nil, errors.Errorf("Request.Connection.Mechanism.Type (%q) unsupported. Supported types are %q", request.GetConnection().GetMechanism().GetType(), s.supportedMechanismTypes) - } - - // If we don't have any of the mechanism preferences fall through to return an error - mechanismPreferenceTypes := []string{} - for _, mechanism := range request.GetMechanismPreferences() { - for _, supportedMechanismType := range s.supportedMechanismTypes { - if mechanism.GetType() == supportedMechanismType { - return next.Server(ctx).Request(ctx, request) - } - } - mechanismPreferenceTypes = append(mechanismPreferenceTypes, mechanism.GetType()) - } - return nil, errors.Errorf("All Request.MechanismPreferences[*].Types (%q) unsupported. Supported types are %q", mechanismPreferenceTypes, s.supportedMechanismTypes) -} - -func (s *supportedMechanismServer) Close(ctx context.Context, connection *networkservice.Connection) (*empty.Empty, error) { - return next.Server(ctx).Close(ctx, connection) -} diff --git a/pkg/networkservice/common/mechanisms/supported/server_test.go b/pkg/networkservice/common/mechanisms/supported/server_test.go deleted file mode 100644 index e2a3da849..000000000 --- a/pkg/networkservice/common/mechanisms/supported/server_test.go +++ /dev/null @@ -1,112 +0,0 @@ -// Copyright (c) 2021 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 supported_test - -import ( - "context" - "testing" - - "github.com/networkservicemesh/api/pkg/api/networkservice" - "github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/kernel" - "github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/memif" - "github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/vxlan" - "github.com/stretchr/testify/assert" - - "github.com/networkservicemesh/sdk/pkg/networkservice/common/mechanisms/supported" -) - -var testInput = []struct { - supported []string - unsupported []string -}{ - { - supported: []string{ - kernel.MECHANISM, memif.MECHANISM, - }, - unsupported: []string{vxlan.MECHANISM}, - }, -} - -func TestSupportedPreferences(t *testing.T) { - for _, input := range testInput { - server := supported.NewServer(supported.WithSupportedMechanismTypes(input.supported...)) - request := &networkservice.NetworkServiceRequest{} - for _, supported := range input.supported { - request.MechanismPreferences = append(request.GetMechanismPreferences(), &networkservice.Mechanism{ - Type: supported, - }) - } - _, err := server.Request(context.Background(), request) - assert.NoError(t, err) - _, err = server.Close(context.Background(), request.Connection) - assert.NoError(t, err) - } -} - -func TestUnSupportedPreferences(t *testing.T) { - for _, input := range testInput { - server := supported.NewServer(supported.WithSupportedMechanismTypes(input.supported...)) - request := &networkservice.NetworkServiceRequest{} - for _, unsupported := range input.unsupported { - request.MechanismPreferences = append(request.GetMechanismPreferences(), &networkservice.Mechanism{ - Type: unsupported, - }) - } - _, err := server.Request(context.Background(), request) - assert.Error(t, err) - _, err = server.Close(context.Background(), request.Connection) - assert.NoError(t, err) - } -} - -func TestSupportedMechanism(t *testing.T) { - for _, input := range testInput { - server := supported.NewServer(supported.WithSupportedMechanismTypes(input.supported...)) - for _, supported := range input.supported { - request := &networkservice.NetworkServiceRequest{ - Connection: &networkservice.Connection{ - Mechanism: &networkservice.Mechanism{ - Type: supported, - }, - }, - } - _, err := server.Request(context.Background(), request) - assert.NoError(t, err) - _, err = server.Close(context.Background(), request.Connection) - assert.NoError(t, err) - } - } -} - -func TestUnSupportedMechanism(t *testing.T) { - for _, input := range testInput { - server := supported.NewServer(supported.WithSupportedMechanismTypes(input.supported...)) - for _, unsupported := range input.unsupported { - request := &networkservice.NetworkServiceRequest{ - Connection: &networkservice.Connection{ - Mechanism: &networkservice.Mechanism{ - Type: unsupported, - }, - }, - } - _, err := server.Request(context.Background(), request) - assert.Error(t, err) - _, err = server.Close(context.Background(), request.Connection) - assert.NoError(t, err) - } - } -}