Humera Kouser | a444295 | 2020-11-23 23:51:19 +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 dmiserver |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "encoding/json" |
| 22 | "strconv" |
| 23 | "time" |
| 24 | |
| 25 | "github.com/Shopify/sarama" |
| 26 | log "github.com/sirupsen/logrus" |
| 27 | ) |
| 28 | |
hkouser | 24361d4 | 2020-12-14 19:21:47 +0530 | [diff] [blame] | 29 | var producer sarama.AsyncProducer |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 30 | |
| 31 | // InitializeDMKafkaPublishers initializes metrics kafka publisher |
| 32 | func InitializeDMKafkaPublishers(NewAsyncProducer func([]string, *sarama.Config) (sarama.AsyncProducer, error), oltID int, msgBusEndPoint string) error { |
| 33 | var err error |
| 34 | sarama.Logger = log.New() |
| 35 | // producer config |
| 36 | config := sarama.NewConfig() |
| 37 | config.Producer.Retry.Max = 5 |
| 38 | config.Metadata.Retry.Max = 10 |
| 39 | config.Metadata.Retry.Backoff = 10 * time.Second |
hkouser | 24361d4 | 2020-12-14 19:21:47 +0530 | [diff] [blame] | 40 | config.ClientID = "BBSim-OLT-DMIServer-" + strconv.Itoa(oltID) |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 41 | |
hkouser | 24361d4 | 2020-12-14 19:21:47 +0530 | [diff] [blame] | 42 | producer, err = NewAsyncProducer([]string{msgBusEndPoint}, config) |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 43 | return err |
| 44 | } |
| 45 | |
| 46 | // DMKafkaPublisher receives messages on ch and publish them to kafka on topic |
| 47 | func DMKafkaPublisher(ctx context.Context, ch chan interface{}, topic string) { |
| 48 | defer log.Debugf("DMKafkaPublisher stopped") |
| 49 | loop: |
| 50 | for { |
| 51 | select { |
hkouser | 24361d4 | 2020-12-14 19:21:47 +0530 | [diff] [blame] | 52 | case data := <-ch: |
| 53 | log.Tracef("Writing to kafka topic(%s): %v", topic, data) |
| 54 | jsonData, err := json.Marshal(data) |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 55 | if err != nil { |
hkouser | 24361d4 | 2020-12-14 19:21:47 +0530 | [diff] [blame] | 56 | log.Errorf("Failed to get json %v", err) |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 57 | continue |
| 58 | } |
hkouser | 24361d4 | 2020-12-14 19:21:47 +0530 | [diff] [blame] | 59 | producer.Input() <- &sarama.ProducerMessage{ |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 60 | Topic: topic, |
hkouser | 24361d4 | 2020-12-14 19:21:47 +0530 | [diff] [blame] | 61 | Value: sarama.ByteEncoder(jsonData), |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 62 | } |
| 63 | case <-ctx.Done(): |
| 64 | log.Infof("Stopping DM Kafka Publisher for topic %s", topic) |
| 65 | break loop |
| 66 | } |
| 67 | } |
| 68 | } |