blob: a028aa9b67673e778ce5f49e2d16c9f8bda4b6c5 [file] [log] [blame]
npujar467fe752020-01-16 20:17:45 +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
Matteo Scandolod525ae32020-04-02 17:27:29 -070017package kafka
npujar467fe752020-01-16 20:17:45 +053018
19import (
20 "context"
David Bainbridge9ae13132020-06-22 17:28:01 -070021
npujar467fe752020-01-16 20:17:45 +053022 "github.com/gogo/protobuf/proto"
23 "github.com/golang/protobuf/ptypes"
24 "github.com/golang/protobuf/ptypes/any"
yasin sapli5458a1c2021-06-14 22:24:38 +000025 "github.com/opencord/voltha-lib-go/v5/pkg/kafka"
Maninderdfadc982020-10-28 14:04:33 +053026 ic "github.com/opencord/voltha-protos/v4/go/inter_container"
npujar467fe752020-01-16 20:17:45 +053027)
28
29type InvokeRpcArgs struct {
30 Rpc string
31 ToTopic *kafka.Topic
32 ReplyToTopic *kafka.Topic
33 WaitForResponse bool
34 Key string
35 ParentDeviceId string
36 KvArgs map[int]interface{}
37}
38
39type InvokeRpcSpy struct {
40 CallCount int
41 Calls map[int]InvokeRpcArgs
42 Timeout bool
43 Response proto.Message
44}
45
Scott Bakerb9635992020-03-11 21:11:28 -070046type InvokeAsyncRpcSpy struct {
47 CallCount int
48 Calls map[int]InvokeRpcArgs
49 Timeout bool
50 Response proto.Message
51}
52
npujar467fe752020-01-16 20:17:45 +053053type MockKafkaICProxy struct {
54 InvokeRpcSpy InvokeRpcSpy
55}
56
Rohan Agrawal31f21802020-06-12 05:38:46 +000057func (s *MockKafkaICProxy) Start(ctx context.Context) error { return nil }
npujar467fe752020-01-16 20:17:45 +053058func (s *MockKafkaICProxy) GetDefaultTopic() *kafka.Topic {
59 t := kafka.Topic{
60 Name: "test-topic",
61 }
62 return &t
63}
David Bainbridge9ae13132020-06-22 17:28:01 -070064
Rohan Agrawal31f21802020-06-12 05:38:46 +000065func (s *MockKafkaICProxy) DeleteTopic(ctx context.Context, topic kafka.Topic) error { return nil }
66
67func (s *MockKafkaICProxy) Stop(ctx context.Context) {}
Scott Bakerb9635992020-03-11 21:11:28 -070068
69func (s *MockKafkaICProxy) InvokeAsyncRPC(ctx context.Context, rpc string, toTopic *kafka.Topic, replyToTopic *kafka.Topic,
70 waitForResponse bool, key string, kvArgs ...*kafka.KVArg) chan *kafka.RpcResponse {
71
72 args := make(map[int]interface{}, 4)
73 for k, v := range kvArgs {
74 args[k] = v
75 }
76
77 s.InvokeRpcSpy.Calls[s.InvokeRpcSpy.CallCount] = InvokeRpcArgs{
78 Rpc: rpc,
79 ToTopic: toTopic,
80 ReplyToTopic: replyToTopic,
81 WaitForResponse: waitForResponse,
82 Key: key,
83 KvArgs: args,
84 }
85
86 chnl := make(chan *kafka.RpcResponse)
87
88 return chnl
89}
90
npujar467fe752020-01-16 20:17:45 +053091func (s *MockKafkaICProxy) InvokeRPC(ctx context.Context, rpc string, toTopic *kafka.Topic, replyToTopic *kafka.Topic, waitForResponse bool, key string, kvArgs ...*kafka.KVArg) (bool, *any.Any) {
92 s.InvokeRpcSpy.CallCount++
93
94 success := true
95
96 args := make(map[int]interface{}, 4)
97 for k, v := range kvArgs {
98 args[k] = v
99 }
100
101 s.InvokeRpcSpy.Calls[s.InvokeRpcSpy.CallCount] = InvokeRpcArgs{
102 Rpc: rpc,
103 ToTopic: toTopic,
104 ReplyToTopic: replyToTopic,
105 WaitForResponse: waitForResponse,
106 Key: key,
107 KvArgs: args,
108 }
109
110 var response any.Any
111 if s.InvokeRpcSpy.Timeout {
112
113 success = false
114
115 err := &ic.Error{Reason: "context deadline exceeded", Code: ic.ErrorCode_DEADLINE_EXCEEDED}
116 res, _ := ptypes.MarshalAny(err)
117 response = *res
118 } else {
119 res, _ := ptypes.MarshalAny(s.InvokeRpcSpy.Response)
120 response = *res
121 }
122
123 return success, &response
124}
Rohan Agrawal31f21802020-06-12 05:38:46 +0000125func (s *MockKafkaICProxy) SubscribeWithRequestHandlerInterface(ctx context.Context, topic kafka.Topic, handler interface{}) error {
npujar467fe752020-01-16 20:17:45 +0530126 return nil
127}
Rohan Agrawal31f21802020-06-12 05:38:46 +0000128func (s *MockKafkaICProxy) SubscribeWithDefaultRequestHandler(ctx context.Context, topic kafka.Topic, initialOffset int64) error {
npujar467fe752020-01-16 20:17:45 +0530129 return nil
130}
Rohan Agrawal31f21802020-06-12 05:38:46 +0000131func (s *MockKafkaICProxy) UnSubscribeFromRequestHandler(ctx context.Context, topic kafka.Topic) error {
132 return nil
133}
134func (s *MockKafkaICProxy) EnableLivenessChannel(ctx context.Context, enable bool) chan bool {
135 return nil
136}
137func (s *MockKafkaICProxy) SendLiveness(ctx context.Context) error { return nil }