blob: ebc9423b65bc4a2ebfbab6eb6d50439d128a985e [file] [log] [blame]
Scott Baker2c1c4822019-10-16 11:02:41 -07001/*
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
17package common
18
19import (
20 "errors"
21 "fmt"
22 "strconv"
23 "strings"
24 "time"
25
Scott Bakerce767002019-10-23 13:30:24 -070026 "github.com/opencord/voltha-lib-go/v2/pkg/adapters/adapterif"
27 "github.com/opencord/voltha-lib-go/v2/pkg/kafka"
28 "github.com/opencord/voltha-lib-go/v2/pkg/log"
Scott Bakerf1b096c2019-11-01 12:36:30 -070029 "github.com/opencord/voltha-protos/v2/go/voltha"
Scott Baker2c1c4822019-10-16 11:02:41 -070030)
31
32type EventProxy struct {
33 kafkaClient kafka.Client
34 eventTopic kafka.Topic
35}
36
37func NewEventProxy(opts ...EventProxyOption) *EventProxy {
38 var proxy EventProxy
39 for _, option := range opts {
40 option(&proxy)
41 }
42 return &proxy
43}
44
45type EventProxyOption func(*EventProxy)
46
47func MsgClient(client kafka.Client) EventProxyOption {
48 return func(args *EventProxy) {
49 args.kafkaClient = client
50 }
51}
52
53func MsgTopic(topic kafka.Topic) EventProxyOption {
54 return func(args *EventProxy) {
55 args.eventTopic = topic
56 }
57}
58
59func (ep *EventProxy) formatId(eventName string) string {
60 return fmt.Sprintf("Voltha.openolt.%s.%s", eventName, strconv.FormatInt(time.Now().UnixNano(), 10))
61}
62
63func (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*/
82func (ep *EventProxy) SendDeviceEvent(deviceEvent *voltha.DeviceEvent, category adapterif.EventCategory, subCategory adapterif.EventSubCategory, raisedTs int64) error {
83 if deviceEvent == nil {
khenaidoob332f9b2020-01-16 16:25:26 -050084 logger.Error("Recieved empty device event")
Scott Baker2c1c4822019-10-16 11:02:41 -070085 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 {
khenaidoob332f9b2020-01-16 16:25:26 -050093 logger.Errorw("Failed to send device event to KAFKA bus", log.Fields{"device-event": deviceEvent})
Scott Baker2c1c4822019-10-16 11:02:41 -070094 return err
95 }
khenaidoob332f9b2020-01-16 16:25:26 -050096 logger.Infow("Successfully sent device event KAFKA", log.Fields{"Id": event.Header.Id, "Category": event.Header.Category,
Scott Baker2c1c4822019-10-16 11:02:41 -070097 "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 Manjunatha59c9152019-10-30 12:48:49 +0530105// SendKpiEvent is to send kpi events to voltha.event topic
Naga Manjunath86e9d2e2019-10-25 15:21:49 +0530106func (ep *EventProxy) SendKpiEvent(id string, kpiEvent *voltha.KpiEvent2, category adapterif.EventCategory, subCategory adapterif.EventSubCategory, raisedTs int64) error {
107 if kpiEvent == nil {
khenaidoob332f9b2020-01-16 16:25:26 -0500108 logger.Error("Recieved empty kpi event")
Naga Manjunath86e9d2e2019-10-25 15:21:49 +0530109 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 {
khenaidoob332f9b2020-01-16 16:25:26 -0500117 logger.Errorw("Failed to send kpi event to KAFKA bus", log.Fields{"device-event": kpiEvent})
Naga Manjunath86e9d2e2019-10-25 15:21:49 +0530118 return err
119 }
khenaidoob332f9b2020-01-16 16:25:26 -0500120 logger.Infow("Successfully sent kpi event to KAFKA", log.Fields{"Id": event.Header.Id, "Category": event.Header.Category,
Naga Manjunath86e9d2e2019-10-25 15:21:49 +0530121 "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 Baker2c1c4822019-10-16 11:02:41 -0700128/* TODO: Send out KPI events*/
129
130func (ep *EventProxy) sendEvent(event *voltha.Event) error {
131 if err := ep.kafkaClient.Send(event, &ep.eventTopic); err != nil {
132 return err
133 }
khenaidoob332f9b2020-01-16 16:25:26 -0500134 logger.Debugw("Sent event to kafka", log.Fields{"event": event})
Scott Baker2c1c4822019-10-16 11:02:41 -0700135
136 return nil
137}