-
Notifications
You must be signed in to change notification settings - Fork 4
/
admin.go
232 lines (206 loc) · 8.22 KB
/
admin.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
* Copyright (c) 2017, 2020 ADLINK Technology Inc.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*
* Contributors:
* ADLINK zenoh team, <zenoh@adlink-labs.tech>
*/
package zenoh
import (
"fmt"
"strings"
)
// Admin is the zenoh administration class.
type Admin struct {
w *Workspace
zenohid string
}
//
// Backends management
//
// AddBackend adds a backend in the connected Zenoh router
// (i.e. the one you are directly connected to).
//
// The backend will use the properties for initialization and "beid" as identifier.
func (a *Admin) AddBackend(beid string, properties Properties) error {
return a.AddBackendAt(beid, properties, a.zenohid)
}
// AddBackendAt adds a backend in the specified Zenoh router,
// not necessarily the one you are connected to.
//
// The backend will use the properties for initialization and "beid" as identifier.
func (a *Admin) AddBackendAt(beid string, properties Properties, zenoh string) error {
path, err := NewPath(fmt.Sprintf("/@/router/%s/plugin/storages/backend/%s", zenoh, beid))
if err != nil {
return &ZError{Msg: "Invalid backend id: " + beid, Code: 0, Cause: err}
}
return a.w.Put(path, NewPropertiesValue(properties))
}
// GetBackend gets a backend's properties from the connected Zenoh router.
// (i.e. the one you are directly connected to).
func (a *Admin) GetBackend(beid string) (Properties, error) {
return a.GetBackendAt(beid, a.zenohid)
}
// GetBackendAt gets a backend's properties from the specified Zenoh router,
// not necessarily the one you are connected to.
func (a *Admin) GetBackendAt(beid string, zenoh string) (Properties, error) {
selector, err := NewSelector(fmt.Sprintf("/@/router/%s/plugin/storages/backend/%s", zenoh, beid))
if err != nil {
return nil, &ZError{Msg: "Invalid backend id: " + beid, Code: 0, Cause: err}
}
pvs := a.w.Get(selector)
if len(pvs) == 0 {
return nil, nil
}
return propertiesOfValue(pvs[0].Value()), nil
}
// GetBackends gets all the backends from the connected Zenoh router
// (i.e. the one you are directly connected to).
//
// It returns a map of the backends properties, indexed by the backends identifiers.
func (a *Admin) GetBackends() (map[string]Properties, error) {
return a.GetBackendsAt(a.zenohid)
}
// GetBackendsAt gets all the backends from the specified Zenoh router,
// not necessarily the one you are connected to.
//
// It returns a map of the backends properties, indexed by the backends identifiers.
func (a *Admin) GetBackendsAt(zenoh string) (map[string]Properties, error) {
sel := fmt.Sprintf("/@/router/%s/plugin/storages/backend/*", zenoh)
selector, _ := NewSelector(sel)
pvs := a.w.Get(selector)
result := make(map[string]Properties)
for _, pv := range pvs {
beid := pv.Path().ToString()[len(sel)-1:]
result[beid] = propertiesOfValue(pv.Value())
}
return result, nil
}
// RemoveBackend removes a backend from the connected Zenoh router
// (i.e. the one you are directly connected to).
func (a *Admin) RemoveBackend(beid string) error {
return a.RemoveBackendAt(beid, a.zenohid)
}
// RemoveBackendAt removes a backend from the specified Zenoh router,
// not necessarily the one you are connected to.
func (a *Admin) RemoveBackendAt(beid string, zenoh string) error {
path, err := NewPath(fmt.Sprintf("/@/router/%s/plugin/storages/backend/%s", zenoh, beid))
if err != nil {
return &ZError{Msg: "Invalid backend id: " + beid, Code: 0, Cause: err}
}
return a.w.Remove(path)
}
//
// Storages management
//
// AddStorage adds a storage in the connected Zenoh router, using an automatically chosen backend.
//
// The storage will use the properties for initialization and "stid" as identifier.
func (a *Admin) AddStorage(stid string, properties Properties) error {
return a.AddStorageOnBackendAt(stid, properties, "auto", a.zenohid)
}
// AddStorageAt adds a storage in the specified Zenoh router, using an automatically chosen backend.
//
// The storage will use the properties for initialization and "stid" as identifier.
func (a *Admin) AddStorageAt(stid string, properties Properties, zenoh string) error {
return a.AddStorageOnBackendAt(stid, properties, "auto", zenoh)
}
// AddStorageOnBackend adds a storage in the connected Zenoh router, using the specified backend.
func (a *Admin) AddStorageOnBackend(stid string, properties Properties, backend string) error {
return a.AddStorageOnBackendAt(stid, properties, backend, a.zenohid)
}
// AddStorageOnBackendAt adds a storage in the specified Zenoh router, using the specified backend.
func (a *Admin) AddStorageOnBackendAt(stid string, properties Properties, backend string, zenoh string) error {
path, err := NewPath(fmt.Sprintf("/@/router/%s/plugin/storages/backend/%s/storage/%s", zenoh, backend, stid))
if err != nil {
return &ZError{Msg: "Invalid backend or storage id in path: " + path.ToString(), Code: 0, Cause: err}
}
return a.w.Put(path, NewPropertiesValue(properties))
}
// GetStorage gets a storage's properties from the connected Zenoh router.
func (a *Admin) GetStorage(stid string) (Properties, error) {
return a.GetStorageAt(stid, a.zenohid)
}
// GetStorageAt gets a storage's properties from the specified Zenoh router.
func (a *Admin) GetStorageAt(stid string, zenoh string) (Properties, error) {
selector, err := NewSelector(fmt.Sprintf("/@/router/%s/plugin/storages/backend/*/storage/%s", zenoh, stid))
if err != nil {
return nil, &ZError{Msg: "Invalid storage id: " + stid, Code: 0, Cause: err}
}
pvs := a.w.Get(selector)
if len(pvs) == 0 {
return nil, nil
}
return propertiesOfValue(pvs[0].Value()), nil
}
// GetStorages gets all the storages from the connected Zenoh router.
//
// It returns a map of the stgorages properties, indexed by the storages identifiers.
func (a *Admin) GetStorages() (map[string]Properties, error) {
return a.GetStoragesFromBackendAt("*", a.zenohid)
}
// GetStoragesAt gets all the storages from the specified Zenoh router.
//
// It returns a map of the stgorages properties, indexed by the storages identifiers.
func (a *Admin) GetStoragesAt(zenoh string) (map[string]Properties, error) {
return a.GetStoragesFromBackendAt("*", a.zenohid)
}
// GetStoragesFromBackend gets all the storages from the specified backend within the connected Zenoh router.
//
// It returns a map of the stgorages properties, indexed by the storages identifiers.
func (a *Admin) GetStoragesFromBackend(backend string) (map[string]Properties, error) {
return a.GetStoragesFromBackendAt(backend, a.zenohid)
}
// GetStoragesFromBackendAt gets all the storages from the specified backend within the specified Zenoh router.
//
// It returns a map of the stgorages properties, indexed by the storages identifiers.
func (a *Admin) GetStoragesFromBackendAt(backend string, zenoh string) (map[string]Properties, error) {
sel := fmt.Sprintf("/@/router/%s/plugin/storages/backend/%s/storage/*", zenoh, backend)
selector, err := NewSelector(sel)
if err != nil {
return nil, &ZError{Msg: "Invalid backend id: " + backend, Code: 0, Cause: err}
}
pvs := a.w.Get(selector)
result := make(map[string]Properties)
for _, pv := range pvs {
stPath := pv.Path().ToString()
stid := pv.Path().ToString()[strings.LastIndex(stPath, "/")+1:]
result[stid] = propertiesOfValue(pv.Value())
}
return result, nil
}
// RemoveStorage removes a storage from the connected Zenoh router.
func (a *Admin) RemoveStorage(stid string) error {
return a.RemoveStorageAt(stid, a.zenohid)
}
// RemoveStorageAt removes a storage from the specified Zenoh router.
func (a *Admin) RemoveStorageAt(stid string, zenoh string) error {
selector, err := NewSelector(fmt.Sprintf("/@/router/%s/plugin/storages/backend/*/storage/%s", zenoh, stid))
if err != nil {
return &ZError{Msg: "Invalid storage id: " + stid, Code: 0, Cause: err}
}
pvs := a.w.Get(selector)
for _, pv := range pvs {
p := pv.Path()
err := a.w.Remove(p)
if err != nil {
return err
}
}
return nil
}
func propertiesOfValue(v Value) Properties {
pVal, ok := v.(*PropertiesValue)
if ok {
return pVal.p
}
p := make(Properties)
p["value"] = v.ToString()
return p
}