Elia Battiston | 4750d3c | 2022-07-14 13:24:56 +0000 | [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 | |
| 19 | //#cgo LDFLAGS: -lsysrepo -lyang -Wl,--allow-multiple-definition |
| 20 | //#include "plugin.c" |
| 21 | import "C" |
| 22 | import ( |
| 23 | "context" |
| 24 | |
| 25 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
| 26 | "github.com/opencord/voltha-northbound-bbf-adapter/internal/core" |
| 27 | ) |
| 28 | |
| 29 | //export get_devices_cb |
| 30 | func get_devices_cb(session *C.sr_session_ctx_t, parent **C.lyd_node) C.sr_error_t { |
| 31 | //This function is a callback for the retrieval of devices from sysrepo |
| 32 | //The "export" comment instructs CGO to create a C function for it |
| 33 | |
| 34 | ctx := context.Background() |
| 35 | logger.Debug(ctx, "processing-get-devices-request") |
| 36 | |
| 37 | if session == nil { |
| 38 | logger.Error(ctx, "sysrepo-get-devices-null-session") |
| 39 | return C.SR_ERR_OPERATION_FAILED |
| 40 | } |
| 41 | |
| 42 | if parent == nil { |
| 43 | logger.Error(ctx, "sysrepo-get-devices-null-parent-node") |
| 44 | return C.SR_ERR_OPERATION_FAILED |
| 45 | } |
| 46 | |
| 47 | if core.AdapterInstance == nil { |
| 48 | logger.Error(ctx, "sysrepo-get-devices-nil-translator") |
| 49 | return C.SR_ERR_OPERATION_FAILED |
| 50 | } |
| 51 | |
| 52 | devices, err := core.AdapterInstance.GetDevices(ctx) |
| 53 | if err != nil { |
| 54 | logger.Errorw(ctx, "sysrepo-get-devices-translator-error", log.Fields{"err": err}) |
| 55 | return C.SR_ERR_OPERATION_FAILED |
| 56 | } |
| 57 | |
| 58 | err = updateYangTree(ctx, session, parent, devices) |
| 59 | if err != nil { |
| 60 | logger.Errorw(ctx, "sysrepo-get-devices-update-error", log.Fields{"err": err}) |
| 61 | return C.SR_ERR_OPERATION_FAILED |
| 62 | } |
| 63 | |
| 64 | logger.Info(ctx, "devices-information-request-served") |
| 65 | |
| 66 | return C.SR_ERR_OK |
| 67 | } |