blob: ea410ac08feb64721562deb7d20da95294434bd8 [file] [log] [blame]
khenaidood948f772021-08-11 17:49:24 -04001/*
2 * Copyright 2019-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 */
16package kafka
17
18import (
19 "context"
20 "fmt"
21 "sync"
22 "time"
23
24 "github.com/golang/protobuf/proto"
25 "github.com/opencord/voltha-lib-go/v7/pkg/kafka"
26 "github.com/opencord/voltha-lib-go/v7/pkg/log"
27 "google.golang.org/grpc/codes"
28 "google.golang.org/grpc/status"
29)
30
31const (
32 maxConcurrentMessage = 100
33)
34
35// static check to ensure KafkaClient implements kafka.Client
36var _ kafka.Client = &KafkaClient{}
37
38type KafkaClient struct {
39 topicsChannelMap map[string][]chan proto.Message
40 lock sync.RWMutex
41 alive bool
42 livenessMutex sync.Mutex
43 liveness chan bool
44}
45
46func NewKafkaClient() *KafkaClient {
47 return &KafkaClient{
48 topicsChannelMap: make(map[string][]chan proto.Message),
49 lock: sync.RWMutex{},
50 }
51}
52
53func (kc *KafkaClient) Start(ctx context.Context) error {
54 logger.Debug(ctx, "kafka-client-started")
55 return nil
56}
57
58func (kc *KafkaClient) Stop(ctx context.Context) {
59 kc.lock.Lock()
60 defer kc.lock.Unlock()
61 for topic, chnls := range kc.topicsChannelMap {
62 for _, c := range chnls {
63 close(c)
64 }
65 delete(kc.topicsChannelMap, topic)
66 }
67 logger.Debug(ctx, "kafka-client-stopped")
68}
69
70func (kc *KafkaClient) CreateTopic(ctx context.Context, topic *kafka.Topic, numPartition int, repFactor int) error {
71 logger.Debugw(ctx, "CreatingTopic", log.Fields{"topic": topic.Name, "numPartition": numPartition, "replicationFactor": repFactor})
72 kc.lock.Lock()
73 defer kc.lock.Unlock()
74 if _, ok := kc.topicsChannelMap[topic.Name]; ok {
75 return fmt.Errorf("Topic %s already exist", topic.Name)
76 }
77 ch := make(chan proto.Message)
78 kc.topicsChannelMap[topic.Name] = append(kc.topicsChannelMap[topic.Name], ch)
79 return nil
80}
81
82func (kc *KafkaClient) DeleteTopic(ctx context.Context, topic *kafka.Topic) error {
83 logger.Debugw(ctx, "DeleteTopic", log.Fields{"topic": topic.Name})
84 kc.lock.Lock()
85 defer kc.lock.Unlock()
86 delete(kc.topicsChannelMap, topic.Name)
87 return nil
88}
89
90func (kc *KafkaClient) Subscribe(ctx context.Context, topic *kafka.Topic, kvArgs ...*kafka.KVArg) (<-chan proto.Message, error) {
91 logger.Debugw(ctx, "Subscribe", log.Fields{"topic": topic.Name, "args": kvArgs})
92 kc.lock.Lock()
93 defer kc.lock.Unlock()
94 ch := make(chan proto.Message, maxConcurrentMessage)
95 kc.topicsChannelMap[topic.Name] = append(kc.topicsChannelMap[topic.Name], ch)
96 return ch, nil
97}
98
99func removeChannel(s []chan proto.Message, i int) []chan proto.Message {
100 s[i] = s[len(s)-1]
101 return s[:len(s)-1]
102}
103
104func (kc *KafkaClient) UnSubscribe(ctx context.Context, topic *kafka.Topic, ch <-chan proto.Message) error {
105 logger.Debugw(ctx, "UnSubscribe", log.Fields{"topic": topic.Name})
106 kc.lock.Lock()
107 defer kc.lock.Unlock()
108 if chnls, ok := kc.topicsChannelMap[topic.Name]; ok {
109 idx := -1
110 for i, c := range chnls {
111 if c == ch {
112 close(c)
113 idx = i
114 }
115 }
116 if idx >= 0 {
117 kc.topicsChannelMap[topic.Name] = removeChannel(kc.topicsChannelMap[topic.Name], idx)
118 }
119 }
120 return nil
121}
122
123func (kc *KafkaClient) SubscribeForMetadata(ctx context.Context, _ func(fromTopic string, timestamp time.Time)) {
124 logger.Debug(ctx, "SubscribeForMetadata - unimplemented")
125}
126
127func (kc *KafkaClient) Send(ctx context.Context, msg interface{}, topic *kafka.Topic, keys ...string) error {
128 // Assert message is a proto message
129 protoMsg, ok := msg.(proto.Message)
130 if !ok {
131 logger.Warnw(ctx, "message-not-a-proto-message", log.Fields{"msg": msg})
132 return status.Error(codes.InvalidArgument, "msg-not-a-proto-msg")
133 }
134 kc.lock.RLock()
135 defer kc.lock.RUnlock()
136 for _, ch := range kc.topicsChannelMap[topic.Name] {
137 select {
138 case ch <- protoMsg:
139 logger.Debugw(ctx, "publishing", log.Fields{"toTopic": topic.Name, "msg": protoMsg})
140 default:
141 logger.Debugw(ctx, "ignoring-event-channel-busy", log.Fields{"toTopic": topic.Name, "msg": protoMsg})
142 }
143 }
144 return nil
145}
146
147func (kc *KafkaClient) SendLiveness(ctx context.Context) error {
148 kc.livenessMutex.Lock()
149 defer kc.livenessMutex.Unlock()
150 if kc.liveness != nil {
151 kc.liveness <- true // I am a mock
152 }
153 return nil
154}
155
156func (kc *KafkaClient) EnableLivenessChannel(ctx context.Context, enable bool) chan bool {
157 logger.Infow(ctx, "kafka-enable-liveness-channel", log.Fields{"enable": enable})
158 if enable {
159 kc.livenessMutex.Lock()
160 defer kc.livenessMutex.Unlock()
161 if kc.liveness == nil {
162 logger.Info(ctx, "kafka-create-liveness-channel")
163 kc.liveness = make(chan bool, 10)
164 // post intial state to the channel
165 kc.liveness <- kc.alive
166 }
167 } else {
168 panic("Turning off liveness reporting is not supported")
169 }
170 return kc.liveness
171}
172
173func (kc *KafkaClient) EnableHealthinessChannel(ctx context.Context, enable bool) chan bool {
174 logger.Debug(ctx, "EnableHealthinessChannel - unimplemented")
175 return nil
176}