This repository has been archived by the owner on Aug 3, 2020. It is now read-only.
forked from ligato/vpp-agent
/
punt_exception.go
172 lines (145 loc) · 5.29 KB
/
punt_exception.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// 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 descriptor
import (
"errors"
"strings"
"github.com/ligato/cn-infra/logging"
punt "github.com/ligato/vpp-agent/api/models/vpp/punt"
"github.com/ligato/vpp-agent/pkg/models"
kvs "github.com/ligato/vpp-agent/plugins/kvscheduler/api"
"github.com/ligato/vpp-agent/plugins/vpp/puntplugin/descriptor/adapter"
"github.com/ligato/vpp-agent/plugins/vpp/puntplugin/vppcalls"
)
const (
// PuntExceptionDescriptorName is the name of the descriptor for the VPP punt exception
PuntExceptionDescriptorName = "vpp-punt-exception"
)
// A list of non-retriable errors:
var (
// ErrPuntExceptionWithoutReason is returned when VPP punt exception has undefined reason.
ErrPuntExceptionWithoutReason = errors.New("VPP punt exception defined without reason")
)
// PuntExceptionDescriptor teaches KVScheduler how to configure VPP putn exception.
type PuntExceptionDescriptor struct {
RegisterSocketFn func(register bool, toHost *punt.Exception, socketPath string)
// dependencies
log logging.Logger
puntHandler vppcalls.PuntVppAPI
}
// NewPuntExceptionDescriptor creates a new instance of the punt exception.
func NewPuntExceptionDescriptor(puntHandler vppcalls.PuntVppAPI, log logging.LoggerFactory) *PuntExceptionDescriptor {
return &PuntExceptionDescriptor{
log: log.NewLogger("punt-exception-descriptor"),
puntHandler: puntHandler,
}
}
// GetDescriptor returns descriptor suitable for registration (via adapter) with
// the KVScheduler.
func (d *PuntExceptionDescriptor) GetDescriptor() *adapter.PuntExceptionDescriptor {
return &adapter.PuntExceptionDescriptor{
Name: PuntExceptionDescriptorName,
NBKeyPrefix: punt.ModelException.KeyPrefix(),
ValueTypeName: punt.ModelException.ProtoName(),
KeySelector: punt.ModelException.IsKeyValid,
KeyLabel: punt.ModelException.StripKeyPrefix,
ValueComparator: d.EquivalentPuntException,
Validate: d.Validate,
Create: d.Create,
Delete: d.Delete,
Retrieve: d.Retrieve,
}
}
// EquivalentPuntException is case-insensitive comparison function for punt.Exception.
func (d *PuntExceptionDescriptor) EquivalentPuntException(key string, oldPunt, newPunt *punt.Exception) bool {
if oldPunt.Reason != newPunt.Reason {
return false
}
// if the socket path contains '!' as prefix we return false
// to force scheduler to recreate (register) punt socket
if strings.HasPrefix(oldPunt.SocketPath, "!") {
return false
}
return true
}
// Validate validates VPP punt configuration.
func (d *PuntExceptionDescriptor) Validate(key string, puntCfg *punt.Exception) error {
// validate reason
if puntCfg.GetReason() == "" {
return ErrPuntExceptionWithoutReason
}
if puntCfg.SocketPath == "" {
return kvs.NewInvalidValueError(ErrPuntWithoutSocketPath, "socket_path")
}
return nil
}
// Create adds new punt to host entry or registers new punt to unix domain socket.
func (d *PuntExceptionDescriptor) Create(key string, punt *punt.Exception) (interface{}, error) {
// register punt exception
pathname, err := d.puntHandler.AddPuntException(punt)
if err != nil {
d.log.Error(err)
return nil, err
}
if d.RegisterSocketFn != nil {
d.RegisterSocketFn(true, punt, pathname)
}
return nil, nil
}
// Delete removes VPP punt configuration.
func (d *PuntExceptionDescriptor) Delete(key string, punt *punt.Exception, metadata interface{}) error {
// check if the socketpath contains '!' as prefix from retrieve
p := punt
if strings.HasPrefix(p.SocketPath, "!") {
p = &(*punt)
p.SocketPath = strings.TrimPrefix(p.SocketPath, "!")
}
// delete punt exception
err := d.puntHandler.DeletePuntException(punt)
if err != nil {
d.log.Error(err)
return err
}
if d.RegisterSocketFn != nil {
d.RegisterSocketFn(false, punt, "")
}
return nil
}
// Retrieve returns all configured VPP punt exception entries.
func (d *PuntExceptionDescriptor) Retrieve(correlate []adapter.PuntExceptionKVWithMetadata) (retrieved []adapter.PuntExceptionKVWithMetadata, err error) {
// Dump punt exceptions
punts, err := d.puntHandler.DumpExceptions()
if err == vppcalls.ErrUnsupported {
return nil, nil
} else if err != nil {
return nil, err
}
// for all dumped punts that were not yet registered and for which
// the VPP socket is unknown we prepend '!' as prefix
// to allow descriptor to recognize this in equivalent
// and force recreation or make it possible to delete it
for _, p := range punts {
if p.Exception.SocketPath == "" && p.SocketPath != "" {
p.Exception.SocketPath = "!" + p.SocketPath
}
}
for _, p := range punts {
retrieved = append(retrieved, adapter.PuntExceptionKVWithMetadata{
Key: models.Key(p.Exception),
Value: p.Exception,
Origin: kvs.FromNB,
})
}
return retrieved, nil
}