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 | "fmt" |
| 25 | |
| 26 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
| 27 | "github.com/opencord/voltha-northbound-bbf-adapter/internal/core" |
| 28 | "github.com/opencord/voltha-protos/v5/go/voltha" |
| 29 | ) |
| 30 | |
| 31 | const ( |
| 32 | eventNameOnuActivated = "ONU_ACTIVATED_RAISE_EVENT" |
| 33 | ) |
| 34 | |
| 35 | //Performs the necessary operations on a new voltha event received from Kafka |
| 36 | func (p *SysrepoPlugin) ManageVolthaEvent(ctx context.Context, event *voltha.Event) { |
| 37 | if event.Header.Type == voltha.EventType_DEVICE_EVENT { |
| 38 | devEvent, ok := event.EventType.(*voltha.Event_DeviceEvent) |
| 39 | if !ok { |
| 40 | logger.Errorw(ctx, "unexpected-event-type", log.Fields{ |
| 41 | "headerType": event.Header.Type, |
| 42 | "actualType": fmt.Sprintf("%T", event.EventType), |
| 43 | }) |
| 44 | return |
| 45 | } |
| 46 | |
| 47 | //TODO: map other events to ONU state changes |
| 48 | switch devEvent.DeviceEvent.DeviceEventName { |
| 49 | case eventNameOnuActivated: |
| 50 | logger.Debugw(ctx, "onu-activated-event-received", log.Fields{ |
| 51 | "header": event.Header, |
| 52 | "deviceEvent": devEvent.DeviceEvent, |
| 53 | }) |
| 54 | |
| 55 | if err := p.sendOnuActivatedNotification(ctx, event.Header, devEvent.DeviceEvent); err != nil { |
| 56 | logger.Errorw(ctx, "failed-to-send-onu-activated-notification", log.Fields{"err": err}) |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | //Sends a notification based on the content of the received device event |
| 63 | func (p *SysrepoPlugin) sendOnuActivatedNotification(ctx context.Context, eventHeader *voltha.EventHeader, deviceEvent *voltha.DeviceEvent) error { |
| 64 | //Prepare the content of the notification |
| 65 | notificationItems, channelTermItems, err := core.TranslateOnuActivatedEvent(eventHeader, deviceEvent) |
| 66 | if err != nil { |
| 67 | return fmt.Errorf("failed-to-translate-onu-activated-event: %v", err) |
| 68 | } |
| 69 | |
| 70 | //Create the channel termination in the datastore to make the notification leafref valid |
| 71 | channelTermTree, err := createYangTree(ctx, p.operationalSession, channelTermItems) |
| 72 | if err != nil { |
| 73 | return fmt.Errorf("failed-to-create-channel-termination-tree: %v", err) |
| 74 | } |
| 75 | defer C.lyd_free_all(channelTermTree) |
| 76 | |
| 77 | err = editDatastore(ctx, p.operationalSession, channelTermTree) |
| 78 | if err != nil { |
| 79 | return fmt.Errorf("failed-to-apply-channel-termination-to-datastore: %v", err) |
| 80 | } |
| 81 | |
| 82 | //Create the notification tree |
| 83 | notificationTree, err := createYangTree(ctx, p.operationalSession, notificationItems) |
| 84 | if err != nil { |
| 85 | return fmt.Errorf("failed-to-create-onu-activated-notification-tree: %v", err) |
| 86 | } |
| 87 | |
| 88 | //Let sysrepo manage the notification tree to properly free it after its delivery |
| 89 | var notificationData *C.sr_data_t |
| 90 | errCode := C.sr_acquire_data(p.connection, notificationTree, ¬ificationData) |
| 91 | if errCode != C.SR_ERR_OK { |
| 92 | err := fmt.Errorf("cannot-acquire-notification-data") |
| 93 | logger.Errorw(ctx, err.Error(), log.Fields{"errCode": errCode, "errMsg": srErrorMsg(errCode)}) |
| 94 | return err |
| 95 | } |
| 96 | defer C.sr_release_data(notificationData) |
| 97 | |
| 98 | //Send the notification |
| 99 | logger.Infow(ctx, "sending-onu-activated-notification", log.Fields{ |
| 100 | "onuSn": deviceEvent.Context["serial-number"], |
| 101 | }) |
| 102 | errCode = C.sr_notif_send_tree(p.operationalSession, notificationData.tree, 0, 0) |
| 103 | if errCode != C.SR_ERR_OK { |
| 104 | err := fmt.Errorf("cannot-send-notification") |
| 105 | logger.Errorw(ctx, err.Error(), log.Fields{"errCode": errCode, "errMsg": srErrorMsg(errCode)}) |
| 106 | return err |
| 107 | } |
| 108 | |
| 109 | return nil |
| 110 | } |