blob: 57de757173f3f1e21dd11dae7ce4b0195c3f2a44 [file] [log] [blame]
Elia Battistonac8d23f2022-03-14 17:54:56 +01001/*
2* Copyright 2022-present Open Networking Foundation
3
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at
7
8* http://www.apache.org/licenses/LICENSE-2.0
9
10* Unless required by applicable law or agreed to in writing, software
11* distributed under the License is distributed on an "AS IS" BASIS,
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13* See the License for the specific language governing permissions and
14* limitations under the License.
15 */
16
17package sysrepo
18
19//#cgo CFLAGS: -I/usr/include
Elia Battistone1cecb22022-03-21 10:05:25 +010020//#cgo LDFLAGS: -lsysrepo -lyang -Wl,--allow-multiple-definition
Elia Battistonac8d23f2022-03-14 17:54:56 +010021//#include "plugin.c"
22import "C"
23import (
24 "context"
25 "fmt"
Elia Battistone1cecb22022-03-21 10:05:25 +010026 "unsafe"
Elia Battistonac8d23f2022-03-14 17:54:56 +010027
28 "github.com/opencord/voltha-lib-go/v7/pkg/log"
Elia Battistone1cecb22022-03-21 10:05:25 +010029 "github.com/opencord/voltha-northbound-bbf-adapter/internal/core"
Elia Battistonac8d23f2022-03-14 17:54:56 +010030)
31
32type SysrepoPlugin struct {
33 connection *C.sr_conn_ctx_t
34 session *C.sr_session_ctx_t
35 subscription *C.sr_subscription_ctx_t
36}
37
38func errorMsg(code C.int) string {
39 return C.GoString(C.sr_strerror(code))
40}
41
Elia Battistone1cecb22022-03-21 10:05:25 +010042func freeCString(str *C.char) {
43 if str != nil {
44 C.free(unsafe.Pointer(str))
45 str = nil
46 }
47}
48
49func updateYangItems(ctx context.Context, session *C.sr_session_ctx_t, parent **C.lyd_node, items []core.YangItem) error {
50 conn := C.sr_session_get_connection(session)
51 if conn == nil {
52 return fmt.Errorf("null-connection")
53 }
54
55 //libyang context
56 ly_ctx := C.sr_get_context(conn)
57 if ly_ctx == nil {
58 return fmt.Errorf("null-libyang-context")
59 }
60
61 for _, item := range items {
62 logger.Debugw(ctx, "updating-yang-item", log.Fields{"item": item})
63
64 path := C.CString(item.Path)
65 value := C.CString(item.Value)
66
67 lyErr := C.lyd_new_path(*parent, ly_ctx, path, value, 0, nil)
68 if lyErr != C.LY_SUCCESS {
69 freeCString(path)
70 freeCString(value)
71 return fmt.Errorf("libyang-new-path-failed: %d", lyErr)
72 }
73
74 freeCString(path)
75 freeCString(value)
76 }
77
78 return nil
79}
80
Elia Battistonac8d23f2022-03-14 17:54:56 +010081//createPluginState populates a SysrepoPlugin struct by establishing
82//a connection and a session
83func (p *SysrepoPlugin) createSession(ctx context.Context) error {
Elia Battistonac8d23f2022-03-14 17:54:56 +010084 var errCode C.int
85
86 //Populates connection
87 errCode = C.sr_connect(C.SR_CONN_DEFAULT, &p.connection)
88 if errCode != C.SR_ERR_OK {
89 err := fmt.Errorf("sysrepo-connect-error")
90 logger.Errorw(ctx, err.Error(), log.Fields{"errCode": errCode, "errMsg": errorMsg(errCode)})
91 return err
92 }
93
94 //Populates session
95 errCode = C.sr_session_start(p.connection, C.SR_DS_RUNNING, &p.session)
96 if errCode != C.SR_ERR_OK {
97 err := fmt.Errorf("sysrepo-session-error")
98 logger.Errorw(ctx, err.Error(), log.Fields{"errCode": errCode, "errMsg": errorMsg(errCode)})
99
100 _ = p.Stop(ctx)
101
102 return err
103 }
104
105 return nil
106}
107
Elia Battistone1cecb22022-03-21 10:05:25 +0100108//export get_devices_cb
109func get_devices_cb(session *C.sr_session_ctx_t, parent **C.lyd_node) C.sr_error_t {
110 //This function is a callback for the retrieval of devices from sysrepo
Elia Battistonac8d23f2022-03-14 17:54:56 +0100111 //The "export" comment instructs CGO to create a C function for it
112
Elia Battistonac8d23f2022-03-14 17:54:56 +0100113 ctx := context.Background()
Elia Battistone1cecb22022-03-21 10:05:25 +0100114 logger.Debug(ctx, "processing-get-data-request")
115
116 if session == nil {
117 logger.Error(ctx, "sysrepo-get-data-null-session")
118 return C.SR_ERR_OPERATION_FAILED
119 }
120
121 if parent == nil {
122 logger.Error(ctx, "sysrepo-get-data-null-parent-node")
123 return C.SR_ERR_OPERATION_FAILED
124 }
125
126 if core.AdapterInstance == nil {
127 logger.Error(ctx, "sysrepo-get-data-nil-translator")
128 return C.SR_ERR_OPERATION_FAILED
129 }
130
131 devices, err := core.AdapterInstance.GetDevices(ctx)
132 if err != nil {
133 logger.Errorw(ctx, "sysrepo-get-data-translator-error", log.Fields{"err": err})
134 return C.SR_ERR_OPERATION_FAILED
135 }
136
137 err = updateYangItems(ctx, session, parent, devices)
138 if err != nil {
139 logger.Errorw(ctx, "sysrepo-get-data-update-error", log.Fields{"err": err})
140 return C.SR_ERR_OPERATION_FAILED
141 }
142
143 return C.SR_ERR_OK
Elia Battistonac8d23f2022-03-14 17:54:56 +0100144}
145
146func StartNewPlugin(ctx context.Context) (*SysrepoPlugin, error) {
147 plugin := &SysrepoPlugin{}
148
149 //Open a session to sysrepo
150 err := plugin.createSession(ctx)
151 if err != nil {
152 return nil, err
153 }
154
155 //TODO: could be useful to set it according to the adapter log level
156 C.sr_log_stderr(C.SR_LL_WRN)
157
158 //Set callbacks for events
159
160 //Subscribe with a callback to the request of data on a certain path
Elia Battistone1cecb22022-03-21 10:05:25 +0100161 module := C.CString(core.DeviceAggregationModel)
162 defer freeCString(module)
163
164 path := C.CString(core.DevicesPath + "/*")
165 defer freeCString(path)
166
Elia Battistonac8d23f2022-03-14 17:54:56 +0100167 errCode := C.sr_oper_get_items_subscribe(
168 plugin.session,
Elia Battistone1cecb22022-03-21 10:05:25 +0100169 module,
170 path,
171 C.function(C.get_devices_cb_wrapper),
Elia Battistonac8d23f2022-03-14 17:54:56 +0100172 C.NULL,
173 C.SR_SUBSCR_CTX_REUSE,
174 &plugin.subscription,
175 )
176 if errCode != C.SR_ERR_OK {
177 err := fmt.Errorf("sysrepo-failed-subscription-to-get-events")
178 logger.Errorw(ctx, err.Error(), log.Fields{"errCode": errCode, "errMsg": errorMsg(errCode)})
179 return nil, err
180 }
181
182 logger.Debug(ctx, "sysrepo-plugin-started")
183
184 return plugin, nil
185}
186
187func (p *SysrepoPlugin) Stop(ctx context.Context) error {
188 var errCode C.int
189
190 //Frees subscription
191 if p.subscription != nil {
192 errCode = C.sr_unsubscribe(p.subscription)
193 if errCode != C.SR_ERR_OK {
194 err := fmt.Errorf("failed-to-close-sysrepo-subscription")
195 logger.Errorw(ctx, err.Error(), log.Fields{"errCode": errCode, "errMsg": errorMsg(errCode)})
196 return err
197 }
198 p.subscription = nil
199 }
200
201 //Frees session
202 if p.session != nil {
203 errCode = C.sr_session_stop(p.session)
204 if errCode != C.SR_ERR_OK {
205 err := fmt.Errorf("failed-to-close-sysrepo-session")
206 logger.Errorw(ctx, err.Error(), log.Fields{"errCode": errCode, "errMsg": errorMsg(errCode)})
207 return err
208 }
209 p.session = nil
210 }
211
212 //Frees connection
213 if p.connection != nil {
214 errCode = C.sr_disconnect(p.connection)
215 if errCode != C.SR_ERR_OK {
216 err := fmt.Errorf("failed-to-close-sysrepo-connection")
217 logger.Errorw(ctx, err.Error(), log.Fields{"errCode": errCode, "errMsg": errorMsg(errCode)})
218 return err
219 }
220 p.connection = nil
221 }
222
223 logger.Debug(ctx, "sysrepo-plugin-stopped")
224
225 return nil
226}