This repository has been archived by the owner on Feb 20, 2024. It is now read-only.
/
dcf_client.go
115 lines (99 loc) · 3.29 KB
/
dcf_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
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2021 Intel Corporation
package flowconfig
import (
"context"
"fmt"
"time"
flowapi "github.com/smart-edge-open/intel-ethernet-operator/pkg/flowconfig/rpc/v1/flow"
"google.golang.org/grpc"
)
const (
grpcUrl = "localhost:50051"
)
type dcfClient struct{}
// GetDCFClient return an instance of DCF FlowServiceClient
func GetDCFClient() flowapi.FlowServiceClient {
return &dcfClient{}
}
func (dc *dcfClient) Validate(ctx context.Context, in *flowapi.RequestFlowCreate, opts ...grpc.CallOption) (*flowapi.ResponseFlow, error) {
conn, err := getDCFFlowClientConn()
if err != nil {
return nil, err
}
defer conn.Close()
client := flowapi.NewFlowServiceClient(conn)
return client.Validate(ctx, in, opts...)
}
func (dc *dcfClient) Create(ctx context.Context, in *flowapi.RequestFlowCreate, opts ...grpc.CallOption) (*flowapi.ResponseFlowCreate, error) {
conn, err := getDCFFlowClientConn()
if err != nil {
return nil, err
}
defer conn.Close()
client := flowapi.NewFlowServiceClient(conn)
return client.Create(ctx, in, opts...)
}
func (dc *dcfClient) Destroy(ctx context.Context, in *flowapi.RequestFlowofPort, opts ...grpc.CallOption) (*flowapi.ResponseFlow, error) {
conn, err := getDCFFlowClientConn()
if err != nil {
return nil, err
}
defer conn.Close()
client := flowapi.NewFlowServiceClient(conn)
return client.Destroy(ctx, in, opts...)
}
func (dc *dcfClient) Query(ctx context.Context, in *flowapi.RequestFlowofPort, opts ...grpc.CallOption) (*flowapi.ResponseFlowQuery, error) {
conn, err := getDCFFlowClientConn()
if err != nil {
return nil, err
}
defer conn.Close()
client := flowapi.NewFlowServiceClient(conn)
return client.Query(ctx, in, opts...)
}
func (dc *dcfClient) List(ctx context.Context, in *flowapi.RequestofPort, opts ...grpc.CallOption) (*flowapi.ResponseFlowList, error) {
conn, err := getDCFFlowClientConn()
if err != nil {
return nil, err
}
defer conn.Close()
client := flowapi.NewFlowServiceClient(conn)
return client.List(ctx, in, opts...)
}
func (dc *dcfClient) Flush(ctx context.Context, in *flowapi.RequestofPort, opts ...grpc.CallOption) (*flowapi.ResponseFlow, error) {
conn, err := getDCFFlowClientConn()
if err != nil {
return nil, err
}
defer conn.Close()
client := flowapi.NewFlowServiceClient(conn)
return client.Flush(ctx, in, opts...)
}
func (dc *dcfClient) Isolate(ctx context.Context, in *flowapi.RequestIsolate, opts ...grpc.CallOption) (*flowapi.ResponseFlow, error) {
conn, err := getDCFFlowClientConn()
if err != nil {
return nil, err
}
defer conn.Close()
client := flowapi.NewFlowServiceClient(conn)
return client.Isolate(ctx, in, opts...)
}
func (dc *dcfClient) ListPorts(ctx context.Context, in *flowapi.RequestListPorts, opts ...grpc.CallOption) (*flowapi.ResponsePortList, error) {
conn, err := getDCFFlowClientConn()
if err != nil {
return nil, err
}
defer conn.Close()
client := flowapi.NewFlowServiceClient(conn)
return client.ListPorts(ctx, in, opts...)
}
func getDCFFlowClientConn() (*grpc.ClientConn, error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
conn, err := grpc.DialContext(ctx, grpcUrl, grpc.WithInsecure())
if err != nil {
return nil, fmt.Errorf("unable to connect to DCF grpc endpoint: %s", err)
}
return conn, nil
}