forked from cisco-app-networking/networkservicemesh
/
client.go
130 lines (112 loc) · 4.04 KB
/
client.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2018, 2019 VMware, Inc.
// 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 endpoint
import (
"context"
"github.com/golang/protobuf/ptypes/empty"
"github.com/sirupsen/logrus"
"github.com/hashicorp/go-multierror"
"cisco-app-networking.github.io/networkservicemesh/controlplane/api/connection"
"cisco-app-networking.github.io/networkservicemesh/controlplane/api/networkservice"
"cisco-app-networking.github.io/networkservicemesh/sdk/client"
"cisco-app-networking.github.io/networkservicemesh/sdk/common"
)
// ClientEndpoint - opens a Client connection to another Network Service
type ClientEndpoint struct {
mechanismType string
ioConnMap map[string]*connection.Connection
configuration *common.NSConfiguration
}
// Request implements the request handler
// Consumes from ctx context.Context:
// Next
func (cce *ClientEndpoint) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*connection.Connection, error) {
name := request.GetConnection().GetId()
nsmClient, err := client.NewNSMClient(ctx, cce.configuration)
if err != nil {
logrus.Fatalf("Unable to create the NSM client %v", err)
return nil, err
}
defer func() {
if deleteErr := nsmClient.Destroy(ctx); deleteErr != nil {
logrus.Errorf("error destroying nsm client %v", deleteErr)
}
}()
outgoingConnection, err := nsmClient.Connect(ctx, name, cce.mechanismType, "Describe "+name)
if err != nil {
logrus.Errorf("Error when creating the connection %v", err)
return nil, err
}
//TODO: Do we need this ?
outgoingConnection.GetMechanism().GetParameters()[connection.Workspace] = ""
ctx = WithClientConnection(ctx, outgoingConnection)
incomingConnection := request.GetConnection()
if Next(ctx) != nil {
incomingConnection, err = Next(ctx).Request(ctx, request)
if err != nil {
return nil, err
}
}
cce.ioConnMap[request.GetConnection().GetId()] = outgoingConnection
logrus.Infof("outgoingConnection: %v", outgoingConnection)
return incomingConnection, nil
}
// Close implements the close handler
// Consumes from ctx context.Context:
// Next
func (cce *ClientEndpoint) Close(ctx context.Context, connection *connection.Connection) (*empty.Empty, error) {
var result error
nsmClient, err := client.NewNSMClient(ctx, cce.configuration)
if err != nil {
logrus.Fatalf("Unable to create the NSM client %v", err)
return nil, err
}
defer func() {
if err := nsmClient.Destroy(ctx); err != nil {
logrus.Errorf("error destroy nsm client %v", err)
}
}()
if outgoingConnection, ok := cce.ioConnMap[connection.GetId()]; ok {
if err := nsmClient.Close(ctx, outgoingConnection); err != nil {
result = multierror.Append(result, err)
}
ctx = WithClientConnection(ctx, outgoingConnection)
}
// Remove collection from map, after all child items are passed
defer delete(cce.ioConnMap, connection.GetId())
if Next(ctx) != nil {
if _, err := Next(ctx).Close(ctx, connection); err != nil {
return &empty.Empty{}, multierror.Append(result, err)
}
}
return &empty.Empty{}, nil
}
// Name returns the composite name
func (cce *ClientEndpoint) Name() string {
return "client"
}
// NewClientEndpoint creates a ClientEndpoint
func NewClientEndpoint(configuration *common.NSConfiguration) *ClientEndpoint {
// ensure the env variables are processed
if configuration == nil {
configuration = &common.NSConfiguration{}
}
self := &ClientEndpoint{
ioConnMap: map[string]*connection.Connection{},
mechanismType: configuration.MechanismType,
configuration: configuration,
}
return self
}