blob: 25b1be4bc0e9d9421c4f90c94da3f09fe227d532 [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
26 "github.com/opencord/voltha-lib-go/pkg/adapters/adapterif"
Scott Baker2c1c4822019-10-16 11:02:41 -070027 "github.com/opencord/voltha-lib-go/pkg/kafka"
Scott Bakere73f91e2019-10-17 12:58:11 -070028 "github.com/opencord/voltha-lib-go/pkg/log"
Scott Baker2c1c4822019-10-16 11:02:41 -070029 "github.com/opencord/voltha-protos/go/voltha"
30)
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 {
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}