forked from cisco-app-networking/networkservicemesh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_memif_connect.go
84 lines (69 loc) · 2.65 KB
/
client_memif_connect.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package vppagent
import (
"context"
"github.com/golang/protobuf/ptypes/empty"
"github.com/pkg/errors"
"github.com/adodon2go/networkservicemesh/controlplane/api/connection"
"github.com/adodon2go/networkservicemesh/controlplane/api/networkservice"
"github.com/adodon2go/networkservicemesh/sdk/common"
"github.com/adodon2go/networkservicemesh/sdk/endpoint"
)
// ClientMemifConnect is a VPP Agent Client Memif Connect composite
type ClientMemifConnect struct {
Workspace string
}
// Request implements the request handler
// Provides/Consumes from ctx context.Context:
// VppAgentConfig
// ClientConnection
// Next
func (cmc *ClientMemifConnect) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*connection.Connection, error) {
ctx = WithConfig(ctx) // Guarantees we will retrieve a non-nil VppAgentConfig from context.Context
vppAgentConfig := Config(ctx)
incomingConnection := request.GetConnection()
outgoingConnection := endpoint.ClientConnection(ctx)
if outgoingConnection == nil {
return nil, errors.New("endpoint.ClientConnection(ctx) - returned nil value")
}
// Copy context to incoming, since it should match.
incomingConnection.Context = outgoingConnection.Context
// Socket is constructed from outgoing name
if err := appendMemifInterface(vppAgentConfig, outgoingConnection, cmc.Workspace, false); err != nil {
return nil, err
}
if endpoint.Next(ctx) != nil {
return endpoint.Next(ctx).Request(ctx, request)
}
return request.GetConnection(), nil
}
// Close implements the close handler
// Request implements the request handler
// Provides/Consumes from ctx context.Context:
// VppAgentConfig
// ClientConnection
// Next
func (cmc *ClientMemifConnect) Close(ctx context.Context, connection *connection.Connection) (*empty.Empty, error) {
ctx = WithConfig(ctx) // Guarantees we will retrieve a non-nil VppAgentConfig from context.Context
vppAgentConfig := Config(ctx)
outgoingConnection := endpoint.ClientConnection(ctx)
if outgoingConnection == nil {
return nil, errors.Errorf("endpoint.ClientConnection(ctx) - returned nil value")
}
if err := appendMemifInterface(vppAgentConfig, outgoingConnection, cmc.Workspace, false); err != nil {
return nil, err
}
if endpoint.Next(ctx) != nil {
return endpoint.Next(ctx).Close(ctx, connection)
}
return &empty.Empty{}, nil
}
// Name returns the composite name
func (cmc *ClientMemifConnect) Name() string {
return "client-memif-connect"
}
// NewClientMemifConnect creates a ClientMemifConnect
func NewClientMemifConnect(configuration *common.NSConfiguration) *ClientMemifConnect {
return &ClientMemifConnect{
Workspace: configuration.Workspace,
}
}