forked from ligato/vpp-agent
/
configurator.go
125 lines (102 loc) · 3.67 KB
/
configurator.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
// Copyright (c) 2019 Cisco and/or its affiliates.
//
// 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 configurator
import (
"runtime/trace"
"github.com/gogo/status"
"github.com/ligato/cn-infra/logging"
"golang.org/x/net/context"
"google.golang.org/grpc/codes"
rpc "github.com/ligato/vpp-agent/api/configurator"
"github.com/ligato/vpp-agent/api/models/linux"
"github.com/ligato/vpp-agent/api/models/netalloc"
"github.com/ligato/vpp-agent/api/models/vpp"
"github.com/ligato/vpp-agent/pkg/models"
"github.com/ligato/vpp-agent/pkg/util"
kvs "github.com/ligato/vpp-agent/plugins/kvscheduler/api"
"github.com/ligato/vpp-agent/plugins/orchestrator"
)
// configuratorServer implements DataSyncer service.
type configuratorServer struct {
dumpService
notifyService
log logging.Logger
dispatch orchestrator.Dispatcher
}
// Get retrieves actual configuration data.
func (svc *configuratorServer) Get(context.Context, *rpc.GetRequest) (*rpc.GetResponse, error) {
defer trackOperation("Get")()
config := newConfig()
util.PlaceProtos(svc.dispatch.ListData(), config.LinuxConfig, config.VppConfig)
return &rpc.GetResponse{Config: config}, nil
}
// Update adds configuration data present in data request to the VPP/Linux
func (svc *configuratorServer) Update(ctx context.Context, req *rpc.UpdateRequest) (*rpc.UpdateResponse, error) {
ctx, task := trace.NewTask(ctx, "grpc.Update")
defer task.End()
trace.Logf(ctx, "updateData", "%+v", req)
defer trackOperation("Update")()
protos := util.ExtractProtos(req.Update.VppConfig, req.Update.LinuxConfig)
var kvPairs []orchestrator.KeyVal
for _, p := range protos {
key, err := models.GetKey(p)
if err != nil {
svc.log.Debug("models.GetKey failed: %s", err)
return nil, status.Error(codes.InvalidArgument, err.Error())
}
kvPairs = append(kvPairs, orchestrator.KeyVal{
Key: key,
Val: p,
})
}
if req.FullResync {
ctx = kvs.WithResync(ctx, kvs.FullResync, true)
}
ctx = orchestrator.DataSrcContext(ctx, "grpc")
if _, err := svc.dispatch.PushData(ctx, kvPairs); err != nil {
st := status.New(codes.FailedPrecondition, err.Error())
return nil, st.Err()
}
return &rpc.UpdateResponse{}, nil
}
// Delete removes configuration data present in data request from the VPP/linux
func (svc *configuratorServer) Delete(ctx context.Context, req *rpc.DeleteRequest) (*rpc.DeleteResponse, error) {
defer trackOperation("Delete")()
protos := util.ExtractProtos(req.Delete.VppConfig, req.Delete.LinuxConfig)
var kvPairs []orchestrator.KeyVal
for _, p := range protos {
key, err := models.GetKey(p)
if err != nil {
svc.log.Debug("models.GetKey failed: %s", err)
return nil, status.Error(codes.InvalidArgument, err.Error())
}
kvPairs = append(kvPairs, orchestrator.KeyVal{
Key: key,
Val: nil,
})
}
ctx = orchestrator.DataSrcContext(ctx, "grpc")
if _, err := svc.dispatch.PushData(ctx, kvPairs); err != nil {
st := status.New(codes.FailedPrecondition, err.Error())
return nil, st.Err()
}
return &rpc.DeleteResponse{}, nil
}
func newConfig() *rpc.Config {
return &rpc.Config{
LinuxConfig: &linux.ConfigData{},
VppConfig: &vpp.ConfigData{},
NetallocConfig: &netalloc.ConfigData{},
}
}