Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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 common |
| 18 | |
| 19 | import ( |
| 20 | "errors" |
| 21 | "fmt" |
| 22 | "strconv" |
| 23 | "strings" |
| 24 | "time" |
| 25 | |
serkant.uluderya | b38671c | 2019-11-01 09:35:38 -0700 | [diff] [blame] | 26 | "github.com/opencord/voltha-lib-go/v3/pkg/adapters/adapterif" |
| 27 | "github.com/opencord/voltha-lib-go/v3/pkg/kafka" |
| 28 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 29 | "github.com/opencord/voltha-protos/v3/go/voltha" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 30 | ) |
| 31 | |
| 32 | type EventProxy struct { |
| 33 | kafkaClient kafka.Client |
| 34 | eventTopic kafka.Topic |
| 35 | } |
| 36 | |
| 37 | func NewEventProxy(opts ...EventProxyOption) *EventProxy { |
| 38 | var proxy EventProxy |
| 39 | for _, option := range opts { |
| 40 | option(&proxy) |
| 41 | } |
| 42 | return &proxy |
| 43 | } |
| 44 | |
| 45 | type EventProxyOption func(*EventProxy) |
| 46 | |
| 47 | func MsgClient(client kafka.Client) EventProxyOption { |
| 48 | return func(args *EventProxy) { |
| 49 | args.kafkaClient = client |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | func MsgTopic(topic kafka.Topic) EventProxyOption { |
| 54 | return func(args *EventProxy) { |
| 55 | args.eventTopic = topic |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | func (ep *EventProxy) formatId(eventName string) string { |
| 60 | return fmt.Sprintf("Voltha.openolt.%s.%s", eventName, strconv.FormatInt(time.Now().UnixNano(), 10)) |
| 61 | } |
| 62 | |
| 63 | func (ep *EventProxy) getEventHeader(eventName string, category adapterif.EventCategory, subCategory adapterif.EventSubCategory, eventType adapterif.EventType, raisedTs int64) *voltha.EventHeader { |
| 64 | var header voltha.EventHeader |
| 65 | if strings.Contains(eventName, "_") { |
| 66 | eventName = strings.Join(strings.Split(eventName, "_")[:len(strings.Split(eventName, "_"))-2], "_") |
| 67 | } else { |
| 68 | eventName = "UNKNOWN_EVENT" |
| 69 | } |
| 70 | /* Populating event header */ |
| 71 | header.Id = ep.formatId(eventName) |
| 72 | header.Category = category |
| 73 | header.SubCategory = subCategory |
| 74 | header.Type = eventType |
| 75 | header.TypeVersion = adapterif.EventTypeVersion |
| 76 | header.RaisedTs = float32(raisedTs) |
| 77 | header.ReportedTs = float32(time.Now().UnixNano()) |
| 78 | return &header |
| 79 | } |
| 80 | |
| 81 | /* Send out device events*/ |
| 82 | func (ep *EventProxy) SendDeviceEvent(deviceEvent *voltha.DeviceEvent, category adapterif.EventCategory, subCategory adapterif.EventSubCategory, raisedTs int64) error { |
| 83 | if deviceEvent == nil { |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 84 | logger.Error("Recieved empty device event") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 85 | return errors.New("Device event nil") |
| 86 | } |
| 87 | var event voltha.Event |
| 88 | var de voltha.Event_DeviceEvent |
| 89 | de.DeviceEvent = deviceEvent |
| 90 | event.Header = ep.getEventHeader(deviceEvent.DeviceEventName, category, subCategory, voltha.EventType_DEVICE_EVENT, raisedTs) |
| 91 | event.EventType = &de |
| 92 | if err := ep.sendEvent(&event); err != nil { |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 93 | logger.Errorw("Failed to send device event to KAFKA bus", log.Fields{"device-event": deviceEvent}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 94 | return err |
| 95 | } |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 96 | logger.Infow("Successfully sent device event KAFKA", log.Fields{"Id": event.Header.Id, "Category": event.Header.Category, |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 97 | "SubCategory": event.Header.SubCategory, "Type": event.Header.Type, "TypeVersion": event.Header.TypeVersion, |
| 98 | "ReportedTs": event.Header.ReportedTs, "ResourceId": deviceEvent.ResourceId, "Context": deviceEvent.Context, |
| 99 | "DeviceEventName": deviceEvent.DeviceEventName}) |
| 100 | |
| 101 | return nil |
| 102 | |
| 103 | } |
| 104 | |
Naga Manjunath | a59c915 | 2019-10-30 12:48:49 +0530 | [diff] [blame] | 105 | // SendKpiEvent is to send kpi events to voltha.event topic |
Naga Manjunath | 86e9d2e | 2019-10-25 15:21:49 +0530 | [diff] [blame] | 106 | func (ep *EventProxy) SendKpiEvent(id string, kpiEvent *voltha.KpiEvent2, category adapterif.EventCategory, subCategory adapterif.EventSubCategory, raisedTs int64) error { |
| 107 | if kpiEvent == nil { |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 108 | logger.Error("Recieved empty kpi event") |
Naga Manjunath | 86e9d2e | 2019-10-25 15:21:49 +0530 | [diff] [blame] | 109 | return errors.New("KPI event nil") |
| 110 | } |
| 111 | var event voltha.Event |
| 112 | var de voltha.Event_KpiEvent2 |
| 113 | de.KpiEvent2 = kpiEvent |
| 114 | event.Header = ep.getEventHeader(id, category, subCategory, voltha.EventType_KPI_EVENT2, raisedTs) |
| 115 | event.EventType = &de |
| 116 | if err := ep.sendEvent(&event); err != nil { |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 117 | logger.Errorw("Failed to send kpi event to KAFKA bus", log.Fields{"device-event": kpiEvent}) |
Naga Manjunath | 86e9d2e | 2019-10-25 15:21:49 +0530 | [diff] [blame] | 118 | return err |
| 119 | } |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 120 | logger.Infow("Successfully sent kpi event to KAFKA", log.Fields{"Id": event.Header.Id, "Category": event.Header.Category, |
Naga Manjunath | 86e9d2e | 2019-10-25 15:21:49 +0530 | [diff] [blame] | 121 | "SubCategory": event.Header.SubCategory, "Type": event.Header.Type, "TypeVersion": event.Header.TypeVersion, |
| 122 | "ReportedTs": event.Header.ReportedTs, "KpiEventName": "STATS_EVENT"}) |
| 123 | |
| 124 | return nil |
| 125 | |
| 126 | } |
| 127 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 128 | /* TODO: Send out KPI events*/ |
| 129 | |
| 130 | func (ep *EventProxy) sendEvent(event *voltha.Event) error { |
| 131 | if err := ep.kafkaClient.Send(event, &ep.eventTopic); err != nil { |
| 132 | return err |
| 133 | } |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 134 | logger.Debugw("Sent event to kafka", log.Fields{"event": event}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 135 | |
| 136 | return nil |
| 137 | } |