blob: 780093a01ba50498945e0354bb0d241aeea1ba8f [file] [log] [blame]
Pragya Arya324337e2020-02-20 14:35:08 +05301/*
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 "encoding/json"
21 "strconv"
22 "time"
23
24 "github.com/Shopify/sarama"
25 log "github.com/sirupsen/logrus"
26)
27
28var producer sarama.AsyncProducer
29var topic string
30
31// Event defines structure for bbsim events
32type Event struct {
33 EventType string
34 OnuSerial string
35 OltID int
36 IntfID int32
37 OnuID int32
38 EpochTime int64
39 Timestamp string
40}
41
Shrey Baid64cda472020-04-24 18:58:18 +053042type saramaIf interface {
43 NewAsyncProducer(addrs []string, conf *sarama.Config) (sarama.AsyncProducer, error)
44}
45
Pragya Arya324337e2020-02-20 14:35:08 +053046// InitializePublisher initalizes kafka publisher
Shrey Baid64cda472020-04-24 18:58:18 +053047func InitializePublisher(NewAsyncProducer func([]string, *sarama.Config) (sarama.AsyncProducer, error), oltID int) error {
Pragya Arya324337e2020-02-20 14:35:08 +053048
49 var err error
50 sarama.Logger = log.New()
51 // producer config
52 config := sarama.NewConfig()
53 config.Producer.Retry.Max = 5
54 config.Metadata.Retry.Max = 10
55 config.Metadata.Retry.Backoff = 10 * time.Second
56 config.ClientID = "BBSim-OLT-" + strconv.Itoa(oltID)
Shrey Baid64cda472020-04-24 18:58:18 +053057 if len(Options.BBSim.KafkaEventTopic) > 0 {
58 topic = Options.BBSim.KafkaEventTopic
59 } else {
60 topic = "BBSim-OLT-" + strconv.Itoa(oltID) + "-Events"
61 }
Pragya Arya324337e2020-02-20 14:35:08 +053062
Shrey Baid64cda472020-04-24 18:58:18 +053063 producer, err = NewAsyncProducer([]string{Options.BBSim.KafkaAddress}, config)
Pragya Arya324337e2020-02-20 14:35:08 +053064 return err
65}
66
67// KafkaPublisher receives messages on eventChannel and publish them to kafka
68func KafkaPublisher(eventChannel chan Event) {
69 defer log.Debugf("KafkaPublisher stopped")
70 for {
71 select {
72 case event := <-eventChannel:
Matteo Scandolo446fc9e2020-03-13 15:48:13 -070073 log.WithFields(log.Fields{
74 "EventType": event.EventType,
75 "OnuSerial": event.OnuSerial,
76 "OltID": event.OltID,
77 "IntfID": event.IntfID,
78 "OnuID": event.OnuID,
79 "EpochTime": event.EpochTime,
80 "Timestamp": event.Timestamp,
81 }).Trace("Received event on channel")
Pragya Arya324337e2020-02-20 14:35:08 +053082 jsonEvent, err := json.Marshal(event)
83 if err != nil {
84 log.Errorf("Failed to get json event %v", err)
85 continue
86 }
87 producer.Input() <- &sarama.ProducerMessage{
88 Topic: topic,
89 Value: sarama.ByteEncoder(jsonEvent),
90 }
Matteo Scandolo446fc9e2020-03-13 15:48:13 -070091 log.WithFields(log.Fields{
92 "EventType": event.EventType,
93 "OnuSerial": event.OnuSerial,
94 "OltID": event.OltID,
95 "IntfID": event.IntfID,
96 "OnuID": event.OnuID,
97 "EpochTime": event.EpochTime,
98 "Timestamp": event.Timestamp,
99 }).Debug("Event sent on kafka")
Pragya Arya324337e2020-02-20 14:35:08 +0530100 }
101 }
102}