Skip to content

Commit

Permalink
Add WithLoadSwIfIndex option to ipaddress
Browse files Browse the repository at this point in the history
Signed-off-by: Artem Glazychev <artem.glazychev@xored.com>
  • Loading branch information
glazychev-art committed Sep 15, 2021
1 parent 15df586 commit 3f25c02
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 9 deletions.
18 changes: 15 additions & 3 deletions pkg/networkservice/connectioncontext/ipcontext/ipaddress/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@ import (
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata"
"github.com/networkservicemesh/sdk/pkg/tools/postpone"

"github.com/networkservicemesh/sdk-vpp/pkg/tools/ifindex"
)

type ipaddressClient struct {
vppConn api.Connection

loadIfIndex ifIndexFunc
}

// NewClient creates a NetworkServiceClient chain element to set the ip address on a vpp interface
Expand All @@ -58,9 +62,17 @@ type ipaddressClient struct {
// | |
// +---------------------------+
//
func NewClient(vppConn api.Connection) networkservice.NetworkServiceClient {
func NewClient(vppConn api.Connection, opts ...Option) networkservice.NetworkServiceClient {
o := &options{
loadIfIndex: ifindex.Load,
}
for _, opt := range opts {
opt(o)
}

return &ipaddressClient{
vppConn: vppConn,
vppConn: vppConn,
loadIfIndex: o.loadIfIndex,
}
}

Expand All @@ -72,7 +84,7 @@ func (i *ipaddressClient) Request(ctx context.Context, request *networkservice.N
return nil, err
}

if err := addDel(ctx, conn, i.vppConn, metadata.IsClient(i), true); err != nil {
if err := addDel(ctx, conn, i.vppConn, i.loadIfIndex, metadata.IsClient(i), true); err != nil {
closeCtx, cancelClose := postponeCtxFunc()
defer cancelClose()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ import (
"github.com/networkservicemesh/sdk/pkg/tools/log"
"github.com/pkg/errors"

"github.com/networkservicemesh/sdk-vpp/pkg/tools/ifindex"
"github.com/networkservicemesh/sdk-vpp/pkg/tools/types"
)

func addDel(ctx context.Context, conn *networkservice.Connection, vppConn api.Connection, isClient, isAdd bool) error {
swIfIndex, ok := ifindex.Load(ctx, isClient)
func addDel(ctx context.Context, conn *networkservice.Connection, vppConn api.Connection, loadIfIndex ifIndexFunc, isClient, isAdd bool) error {
swIfIndex, ok := loadIfIndex(ctx, isClient)
if !ok {
return errors.New("no swIfIndex available")
}
Expand Down
40 changes: 40 additions & 0 deletions pkg/networkservice/connectioncontext/ipcontext/ipaddress/option.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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 ipaddress

import (
"context"

"github.com/edwarnicke/govpp/binapi/interface_types"
)

type options struct {
loadIfIndex ifIndexFunc
}

// Option is an option pattern for ipaddressClient/Server
type Option func(o *options)

// ifIndexFunc is a function to load the interface index
type ifIndexFunc func(ctx context.Context, isClient bool) (value interface_types.InterfaceIndex, ok bool)

// WithLoadSwIfIndex - sets function to load the interface index
func WithLoadSwIfIndex(f ifIndexFunc) Option {
return func(o *options) {
o.loadIfIndex = f
}
}
18 changes: 15 additions & 3 deletions pkg/networkservice/connectioncontext/ipcontext/ipaddress/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@ import (
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata"
"github.com/networkservicemesh/sdk/pkg/tools/postpone"

"github.com/networkservicemesh/sdk-vpp/pkg/tools/ifindex"
)

type ipaddressServer struct {
vppConn api.Connection

loadIfIndex ifIndexFunc
}

// NewServer creates a NetworkServiceServer chain element to set the ip address on a vpp interface
Expand All @@ -57,9 +61,17 @@ type ipaddressServer struct {
// | |
// +---------------------------+
//
func NewServer(vppConn api.Connection) networkservice.NetworkServiceServer {
func NewServer(vppConn api.Connection, opts ...Option) networkservice.NetworkServiceServer {
o := &options{
loadIfIndex: ifindex.Load,
}
for _, opt := range opts {
opt(o)
}

return &ipaddressServer{
vppConn: vppConn,
vppConn: vppConn,
loadIfIndex: o.loadIfIndex,
}
}

Expand All @@ -71,7 +83,7 @@ func (i *ipaddressServer) Request(ctx context.Context, request *networkservice.N
return nil, err
}

if err := addDel(ctx, conn, i.vppConn, metadata.IsClient(i), true); err != nil {
if err := addDel(ctx, conn, i.vppConn, i.loadIfIndex, metadata.IsClient(i), true); err != nil {
closeCtx, cancelClose := postponeCtxFunc()
defer cancelClose()

Expand Down

0 comments on commit 3f25c02

Please sign in to comment.