blob: 34fcde79eba8fc77691227465d3125cea2ff3c21 [file] [log] [blame]
Devmalya Paulf2e13782019-07-09 07:52:15 -04001/*
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"
Devmalya Paulf2e13782019-07-09 07:52:15 -040022 "strconv"
23 "strings"
24 "time"
Devmalya Paulf2e13782019-07-09 07:52:15 -040025
kdarapu283d1542019-07-31 17:21:11 +053026 "github.com/opencord/voltha-go/adapters/adapterif"
27 "github.com/opencord/voltha-go/common/log"
28 "github.com/opencord/voltha-go/kafka"
29 "github.com/opencord/voltha-protos/go/voltha"
Devmalya Paulf2e13782019-07-09 07:52:15 -040030)
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
kdarapu283d1542019-07-31 17:21:11 +053063func (ep *EventProxy) getEventHeader(eventName string, category adapterif.EventCategory, subCategory adapterif.EventSubCategory, eventType adapterif.EventType, raisedTs int64) *voltha.EventHeader {
Devmalya Paulf2e13782019-07-09 07:52:15 -040064 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
kdarapu283d1542019-07-31 17:21:11 +053075 header.TypeVersion = adapterif.EventTypeVersion
Devmalya Paulf2e13782019-07-09 07:52:15 -040076 header.RaisedTs = float32(raisedTs)
77 header.ReportedTs = float32(time.Now().UnixNano())
78 return &header
79}
80
81/* Send out device events*/
kdarapu283d1542019-07-31 17:21:11 +053082func (ep *EventProxy) SendDeviceEvent(deviceEvent *voltha.DeviceEvent, category adapterif.EventCategory, subCategory adapterif.EventSubCategory, raisedTs int64) error {
Devmalya Paulf2e13782019-07-09 07:52:15 -040083 if deviceEvent == nil {
84 log.Error("Recieved empty device event")
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 {
93 log.Errorw("Failed to send device event to KAFKA bus", log.Fields{"device-event": deviceEvent})
94 return err
95 }
96 log.Infow("Successfully sent device event KAFKA", log.Fields{"Id": event.Header.Id, "Category": event.Header.Category,
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
105/* TODO: Send out KPI events*/
106
107func (ep *EventProxy) sendEvent(event *voltha.Event) error {
108 if err := ep.kafkaClient.Send(event, &ep.eventTopic); err != nil {
109 return err
110 }
111 log.Debugw("Sent event to kafka", log.Fields{"event": event})
112
113 return nil
114}