Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package sysrepo |
| 18 | |
Elia Battiston | e1cecb2 | 2022-03-21 10:05:25 +0100 | [diff] [blame] | 19 | //#cgo LDFLAGS: -lsysrepo -lyang -Wl,--allow-multiple-definition |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame] | 20 | //#include "plugin.c" |
| 21 | import "C" |
| 22 | import ( |
| 23 | "context" |
| 24 | "fmt" |
Elia Battiston | 589addb | 2022-04-04 16:40:01 +0200 | [diff] [blame] | 25 | "io/ioutil" |
| 26 | "os" |
Elia Battiston | e1cecb2 | 2022-03-21 10:05:25 +0100 | [diff] [blame] | 27 | "unsafe" |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame] | 28 | |
| 29 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
Elia Battiston | e1cecb2 | 2022-03-21 10:05:25 +0100 | [diff] [blame] | 30 | "github.com/opencord/voltha-northbound-bbf-adapter/internal/core" |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame] | 31 | ) |
| 32 | |
| 33 | type SysrepoPlugin struct { |
Elia Battiston | 589addb | 2022-04-04 16:40:01 +0200 | [diff] [blame] | 34 | connection *C.sr_conn_ctx_t |
| 35 | session *C.sr_session_ctx_t |
| 36 | subscription *C.sr_subscription_ctx_t |
| 37 | schemaMountData *C.lyd_node |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame] | 38 | } |
| 39 | |
Elia Battiston | 589addb | 2022-04-04 16:40:01 +0200 | [diff] [blame] | 40 | func srErrorMsg(code C.int) string { |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame] | 41 | return C.GoString(C.sr_strerror(code)) |
| 42 | } |
| 43 | |
Elia Battiston | 589addb | 2022-04-04 16:40:01 +0200 | [diff] [blame] | 44 | func lyErrorMsg(ly_ctx *C.ly_ctx) string { |
| 45 | lyErrString := C.ly_errmsg(ly_ctx) |
| 46 | defer freeCString(lyErrString) |
| 47 | |
| 48 | return C.GoString(lyErrString) |
| 49 | } |
| 50 | |
Elia Battiston | e1cecb2 | 2022-03-21 10:05:25 +0100 | [diff] [blame] | 51 | func freeCString(str *C.char) { |
| 52 | if str != nil { |
| 53 | C.free(unsafe.Pointer(str)) |
| 54 | str = nil |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | func updateYangItems(ctx context.Context, session *C.sr_session_ctx_t, parent **C.lyd_node, items []core.YangItem) error { |
| 59 | conn := C.sr_session_get_connection(session) |
| 60 | if conn == nil { |
| 61 | return fmt.Errorf("null-connection") |
| 62 | } |
| 63 | |
| 64 | //libyang context |
Elia Battiston | b244bb5 | 2022-03-24 15:47:16 +0100 | [diff] [blame] | 65 | ly_ctx := C.sr_acquire_context(conn) |
Elia Battiston | 589addb | 2022-04-04 16:40:01 +0200 | [diff] [blame] | 66 | defer C.sr_release_context(conn) |
Elia Battiston | e1cecb2 | 2022-03-21 10:05:25 +0100 | [diff] [blame] | 67 | if ly_ctx == nil { |
| 68 | return fmt.Errorf("null-libyang-context") |
| 69 | } |
| 70 | |
| 71 | for _, item := range items { |
Elia Battiston | 589addb | 2022-04-04 16:40:01 +0200 | [diff] [blame] | 72 | if item.Value == "" { |
| 73 | continue |
| 74 | } |
| 75 | |
Elia Battiston | e1cecb2 | 2022-03-21 10:05:25 +0100 | [diff] [blame] | 76 | logger.Debugw(ctx, "updating-yang-item", log.Fields{"item": item}) |
| 77 | |
| 78 | path := C.CString(item.Path) |
| 79 | value := C.CString(item.Value) |
| 80 | |
| 81 | lyErr := C.lyd_new_path(*parent, ly_ctx, path, value, 0, nil) |
| 82 | if lyErr != C.LY_SUCCESS { |
| 83 | freeCString(path) |
| 84 | freeCString(value) |
Elia Battiston | b244bb5 | 2022-03-24 15:47:16 +0100 | [diff] [blame] | 85 | |
Elia Battiston | 589addb | 2022-04-04 16:40:01 +0200 | [diff] [blame] | 86 | err := fmt.Errorf("libyang-new-path-failed: %d %s", lyErr, lyErrorMsg(ly_ctx)) |
Elia Battiston | b244bb5 | 2022-03-24 15:47:16 +0100 | [diff] [blame] | 87 | |
| 88 | return err |
Elia Battiston | e1cecb2 | 2022-03-21 10:05:25 +0100 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | freeCString(path) |
| 92 | freeCString(value) |
| 93 | } |
| 94 | |
| 95 | return nil |
| 96 | } |
| 97 | |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame] | 98 | //createPluginState populates a SysrepoPlugin struct by establishing |
| 99 | //a connection and a session |
| 100 | func (p *SysrepoPlugin) createSession(ctx context.Context) error { |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame] | 101 | var errCode C.int |
| 102 | |
| 103 | //Populates connection |
| 104 | errCode = C.sr_connect(C.SR_CONN_DEFAULT, &p.connection) |
| 105 | if errCode != C.SR_ERR_OK { |
| 106 | err := fmt.Errorf("sysrepo-connect-error") |
Elia Battiston | 589addb | 2022-04-04 16:40:01 +0200 | [diff] [blame] | 107 | logger.Errorw(ctx, err.Error(), log.Fields{"errCode": errCode, "errMsg": srErrorMsg(errCode)}) |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame] | 108 | return err |
| 109 | } |
| 110 | |
| 111 | //Populates session |
| 112 | errCode = C.sr_session_start(p.connection, C.SR_DS_RUNNING, &p.session) |
| 113 | if errCode != C.SR_ERR_OK { |
| 114 | err := fmt.Errorf("sysrepo-session-error") |
Elia Battiston | 589addb | 2022-04-04 16:40:01 +0200 | [diff] [blame] | 115 | logger.Errorw(ctx, err.Error(), log.Fields{"errCode": errCode, "errMsg": srErrorMsg(errCode)}) |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame] | 116 | |
| 117 | _ = p.Stop(ctx) |
| 118 | |
| 119 | return err |
| 120 | } |
| 121 | |
| 122 | return nil |
| 123 | } |
| 124 | |
Elia Battiston | e1cecb2 | 2022-03-21 10:05:25 +0100 | [diff] [blame] | 125 | //export get_devices_cb |
| 126 | func get_devices_cb(session *C.sr_session_ctx_t, parent **C.lyd_node) C.sr_error_t { |
| 127 | //This function is a callback for the retrieval of devices from sysrepo |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame] | 128 | //The "export" comment instructs CGO to create a C function for it |
| 129 | |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame] | 130 | ctx := context.Background() |
Elia Battiston | e1cecb2 | 2022-03-21 10:05:25 +0100 | [diff] [blame] | 131 | logger.Debug(ctx, "processing-get-data-request") |
| 132 | |
| 133 | if session == nil { |
| 134 | logger.Error(ctx, "sysrepo-get-data-null-session") |
| 135 | return C.SR_ERR_OPERATION_FAILED |
| 136 | } |
| 137 | |
| 138 | if parent == nil { |
| 139 | logger.Error(ctx, "sysrepo-get-data-null-parent-node") |
| 140 | return C.SR_ERR_OPERATION_FAILED |
| 141 | } |
| 142 | |
| 143 | if core.AdapterInstance == nil { |
| 144 | logger.Error(ctx, "sysrepo-get-data-nil-translator") |
| 145 | return C.SR_ERR_OPERATION_FAILED |
| 146 | } |
| 147 | |
| 148 | devices, err := core.AdapterInstance.GetDevices(ctx) |
| 149 | if err != nil { |
| 150 | logger.Errorw(ctx, "sysrepo-get-data-translator-error", log.Fields{"err": err}) |
| 151 | return C.SR_ERR_OPERATION_FAILED |
| 152 | } |
| 153 | |
| 154 | err = updateYangItems(ctx, session, parent, devices) |
| 155 | if err != nil { |
| 156 | logger.Errorw(ctx, "sysrepo-get-data-update-error", log.Fields{"err": err}) |
| 157 | return C.SR_ERR_OPERATION_FAILED |
| 158 | } |
| 159 | |
| 160 | return C.SR_ERR_OK |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame] | 161 | } |
| 162 | |
Elia Battiston | 589addb | 2022-04-04 16:40:01 +0200 | [diff] [blame] | 163 | func StartNewPlugin(ctx context.Context, schemaMountFilePath string) (*SysrepoPlugin, error) { |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame] | 164 | plugin := &SysrepoPlugin{} |
| 165 | |
| 166 | //Open a session to sysrepo |
| 167 | err := plugin.createSession(ctx) |
| 168 | if err != nil { |
| 169 | return nil, err |
| 170 | } |
| 171 | |
Elia Battiston | 589addb | 2022-04-04 16:40:01 +0200 | [diff] [blame] | 172 | //Read the schema-mount file |
| 173 | if _, err := os.Stat(schemaMountFilePath); err != nil { |
| 174 | //The file cannot be found |
| 175 | return nil, fmt.Errorf("plugin-startup-schema-mount-file-not-found: %v", err) |
| 176 | } |
| 177 | |
| 178 | smBuffer, err := ioutil.ReadFile(schemaMountFilePath) |
| 179 | if err != nil { |
| 180 | return nil, fmt.Errorf("plugin-startup-cannot-read-schema-mount-file: %v", err) |
| 181 | } |
| 182 | |
| 183 | smString := C.CString(string(smBuffer)) |
| 184 | defer freeCString(smString) |
| 185 | |
| 186 | ly_ctx := C.sr_acquire_context(plugin.connection) |
| 187 | defer C.sr_release_context(plugin.connection) |
| 188 | if ly_ctx == nil { |
| 189 | return nil, fmt.Errorf("plugin-startup-null-libyang-context") |
| 190 | } |
| 191 | |
| 192 | //Parse the schema-mount file into libyang nodes, and save them into the plugin data |
| 193 | lyErrCode := C.lyd_parse_data_mem(ly_ctx, smString, C.LYD_XML, C.LYD_PARSE_STRICT, C.LYD_VALIDATE_PRESENT, &plugin.schemaMountData) |
| 194 | if lyErrCode != C.LY_SUCCESS { |
| 195 | return nil, fmt.Errorf("plugin-startup-cannot-parse-schema-mount: %v", lyErrorMsg(ly_ctx)) |
| 196 | } |
| 197 | |
| 198 | //Bind the callback needed to support schema-mount |
| 199 | C.sr_set_ext_data_cb(plugin.connection, C.function(C.mountpoint_ext_data_clb), unsafe.Pointer(plugin.schemaMountData)) |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame] | 200 | |
| 201 | //Set callbacks for events |
| 202 | |
| 203 | //Subscribe with a callback to the request of data on a certain path |
Elia Battiston | e1cecb2 | 2022-03-21 10:05:25 +0100 | [diff] [blame] | 204 | module := C.CString(core.DeviceAggregationModel) |
| 205 | defer freeCString(module) |
| 206 | |
| 207 | path := C.CString(core.DevicesPath + "/*") |
| 208 | defer freeCString(path) |
| 209 | |
Elia Battiston | b244bb5 | 2022-03-24 15:47:16 +0100 | [diff] [blame] | 210 | errCode := C.sr_oper_get_subscribe( |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame] | 211 | plugin.session, |
Elia Battiston | e1cecb2 | 2022-03-21 10:05:25 +0100 | [diff] [blame] | 212 | module, |
| 213 | path, |
| 214 | C.function(C.get_devices_cb_wrapper), |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame] | 215 | C.NULL, |
Elia Battiston | b244bb5 | 2022-03-24 15:47:16 +0100 | [diff] [blame] | 216 | C.SR_SUBSCR_DEFAULT, |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame] | 217 | &plugin.subscription, |
| 218 | ) |
| 219 | if errCode != C.SR_ERR_OK { |
| 220 | err := fmt.Errorf("sysrepo-failed-subscription-to-get-events") |
Elia Battiston | 589addb | 2022-04-04 16:40:01 +0200 | [diff] [blame] | 221 | logger.Errorw(ctx, err.Error(), log.Fields{"errCode": errCode, "errMsg": srErrorMsg(errCode)}) |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame] | 222 | return nil, err |
| 223 | } |
| 224 | |
| 225 | logger.Debug(ctx, "sysrepo-plugin-started") |
| 226 | |
| 227 | return plugin, nil |
| 228 | } |
| 229 | |
| 230 | func (p *SysrepoPlugin) Stop(ctx context.Context) error { |
| 231 | var errCode C.int |
| 232 | |
Elia Battiston | 589addb | 2022-04-04 16:40:01 +0200 | [diff] [blame] | 233 | //Free the libyang nodes for external schema-mount data |
| 234 | C.lyd_free_all(p.schemaMountData) |
| 235 | |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame] | 236 | //Frees subscription |
| 237 | if p.subscription != nil { |
| 238 | errCode = C.sr_unsubscribe(p.subscription) |
| 239 | if errCode != C.SR_ERR_OK { |
| 240 | err := fmt.Errorf("failed-to-close-sysrepo-subscription") |
Elia Battiston | 589addb | 2022-04-04 16:40:01 +0200 | [diff] [blame] | 241 | logger.Errorw(ctx, err.Error(), log.Fields{"errCode": errCode, "errMsg": srErrorMsg(errCode)}) |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame] | 242 | return err |
| 243 | } |
| 244 | p.subscription = nil |
| 245 | } |
| 246 | |
| 247 | //Frees session |
| 248 | if p.session != nil { |
| 249 | errCode = C.sr_session_stop(p.session) |
| 250 | if errCode != C.SR_ERR_OK { |
| 251 | err := fmt.Errorf("failed-to-close-sysrepo-session") |
Elia Battiston | 589addb | 2022-04-04 16:40:01 +0200 | [diff] [blame] | 252 | logger.Errorw(ctx, err.Error(), log.Fields{"errCode": errCode, "errMsg": srErrorMsg(errCode)}) |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame] | 253 | return err |
| 254 | } |
| 255 | p.session = nil |
| 256 | } |
| 257 | |
| 258 | //Frees connection |
| 259 | if p.connection != nil { |
| 260 | errCode = C.sr_disconnect(p.connection) |
| 261 | if errCode != C.SR_ERR_OK { |
| 262 | err := fmt.Errorf("failed-to-close-sysrepo-connection") |
Elia Battiston | 589addb | 2022-04-04 16:40:01 +0200 | [diff] [blame] | 263 | logger.Errorw(ctx, err.Error(), log.Fields{"errCode": errCode, "errMsg": srErrorMsg(errCode)}) |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame] | 264 | return err |
| 265 | } |
| 266 | p.connection = nil |
| 267 | } |
| 268 | |
| 269 | logger.Debug(ctx, "sysrepo-plugin-stopped") |
| 270 | |
| 271 | return nil |
| 272 | } |