Pragya Arya | 324337e | 2020-02-20 14:35:08 +0530 | [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 ( |
| 20 | "encoding/json" |
| 21 | "strconv" |
| 22 | "time" |
| 23 | |
| 24 | "github.com/Shopify/sarama" |
| 25 | log "github.com/sirupsen/logrus" |
| 26 | ) |
| 27 | |
| 28 | var producer sarama.AsyncProducer |
| 29 | var topic string |
| 30 | |
| 31 | // Event defines structure for bbsim events |
| 32 | type 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 Baid | 64cda47 | 2020-04-24 18:58:18 +0530 | [diff] [blame] | 42 | type saramaIf interface { |
| 43 | NewAsyncProducer(addrs []string, conf *sarama.Config) (sarama.AsyncProducer, error) |
| 44 | } |
| 45 | |
Pragya Arya | 324337e | 2020-02-20 14:35:08 +0530 | [diff] [blame] | 46 | // InitializePublisher initalizes kafka publisher |
Shrey Baid | 64cda47 | 2020-04-24 18:58:18 +0530 | [diff] [blame] | 47 | func InitializePublisher(NewAsyncProducer func([]string, *sarama.Config) (sarama.AsyncProducer, error), oltID int) error { |
Pragya Arya | 324337e | 2020-02-20 14:35:08 +0530 | [diff] [blame] | 48 | |
| 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 Baid | 64cda47 | 2020-04-24 18:58:18 +0530 | [diff] [blame] | 57 | if len(Options.BBSim.KafkaEventTopic) > 0 { |
| 58 | topic = Options.BBSim.KafkaEventTopic |
| 59 | } else { |
| 60 | topic = "BBSim-OLT-" + strconv.Itoa(oltID) + "-Events" |
| 61 | } |
Pragya Arya | 324337e | 2020-02-20 14:35:08 +0530 | [diff] [blame] | 62 | |
Shrey Baid | 64cda47 | 2020-04-24 18:58:18 +0530 | [diff] [blame] | 63 | producer, err = NewAsyncProducer([]string{Options.BBSim.KafkaAddress}, config) |
Pragya Arya | 324337e | 2020-02-20 14:35:08 +0530 | [diff] [blame] | 64 | return err |
| 65 | } |
| 66 | |
| 67 | // KafkaPublisher receives messages on eventChannel and publish them to kafka |
| 68 | func KafkaPublisher(eventChannel chan Event) { |
| 69 | defer log.Debugf("KafkaPublisher stopped") |
| 70 | for { |
| 71 | select { |
| 72 | case event := <-eventChannel: |
Matteo Scandolo | 446fc9e | 2020-03-13 15:48:13 -0700 | [diff] [blame] | 73 | 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 Arya | 324337e | 2020-02-20 14:35:08 +0530 | [diff] [blame] | 82 | 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 Scandolo | 446fc9e | 2020-03-13 15:48:13 -0700 | [diff] [blame] | 91 | 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 Arya | 324337e | 2020-02-20 14:35:08 +0530 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | } |