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