blob: c527619cb218bb4ecbd8008dd9ea742758c2180c [file] [log] [blame]
khenaidoobf6e7bb2018-08-14 22:27:29 -04001/*
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 */
khenaidooabad44c2018-08-03 16:58:35 -040016package kafka
17
18import (
19 "context"
20 "errors"
21 "fmt"
khenaidooabad44c2018-08-03 16:58:35 -040022 "github.com/golang/protobuf/proto"
23 "github.com/golang/protobuf/ptypes"
24 "github.com/golang/protobuf/ptypes/any"
25 "github.com/google/uuid"
26 "github.com/opencord/voltha-go/common/log"
khenaidoo79232702018-12-04 11:00:41 -050027 ic "github.com/opencord/voltha-go/protos/inter_container"
khenaidooabad44c2018-08-03 16:58:35 -040028 "reflect"
khenaidoo19374072018-12-11 11:05:15 -050029 "strings"
khenaidooabad44c2018-08-03 16:58:35 -040030 "sync"
31 "time"
32)
33
34// Initialize the logger - gets the default until the main function setup the logger
35func init() {
khenaidooca301322019-01-09 23:06:32 -050036 log.AddPackage(log.JSON, log.DebugLevel, nil)
khenaidooabad44c2018-08-03 16:58:35 -040037}
38
39const (
khenaidoo43c82122018-11-22 18:38:28 -050040 DefaultMaxRetries = 3
khenaidooca301322019-01-09 23:06:32 -050041 DefaultRequestTimeout = 3000 // 3000 milliseconds - to handle a wider latency range
khenaidooabad44c2018-08-03 16:58:35 -040042)
43
khenaidoo43c82122018-11-22 18:38:28 -050044// requestHandlerChannel represents an interface associated with a channel. Whenever, an event is
45// obtained from that channel, this interface is invoked. This is used to handle
46// async requests into the Core via the kafka messaging bus
47type requestHandlerChannel struct {
48 requesthandlerInterface interface{}
khenaidoo79232702018-12-04 11:00:41 -050049 ch <-chan *ic.InterContainerMessage
khenaidooabad44c2018-08-03 16:58:35 -040050}
51
khenaidoo43c82122018-11-22 18:38:28 -050052// transactionChannel represents a combination of a topic and a channel onto which a response received
53// on the kafka bus will be sent to
54type transactionChannel struct {
55 topic *Topic
khenaidoo79232702018-12-04 11:00:41 -050056 ch chan *ic.InterContainerMessage
khenaidoo43c82122018-11-22 18:38:28 -050057}
58
59// InterContainerProxy represents the messaging proxy
60type InterContainerProxy struct {
61 kafkaHost string
62 kafkaPort int
63 DefaultTopic *Topic
64 defaultRequestHandlerInterface interface{}
khenaidoo79232702018-12-04 11:00:41 -050065 deviceDiscoveryTopic *Topic
khenaidoo43c82122018-11-22 18:38:28 -050066 kafkaClient Client
67 doneCh chan int
68
69 // This map is used to map a topic to an interface and channel. When a request is received
70 // on that channel (registered to the topic) then that interface is invoked.
71 topicToRequestHandlerChannelMap map[string]*requestHandlerChannel
72 lockTopicRequestHandlerChannelMap sync.RWMutex
73
74 // This map is used to map a channel to a response topic. This channel handles all responses on that
khenaidoo4c1a5bf2018-11-29 15:53:42 -050075 // channel for that topic and forward them to the appropriate consumers channel, using the
khenaidoo43c82122018-11-22 18:38:28 -050076 // transactionIdToChannelMap.
khenaidoo79232702018-12-04 11:00:41 -050077 topicToResponseChannelMap map[string]<-chan *ic.InterContainerMessage
khenaidoo43c82122018-11-22 18:38:28 -050078 lockTopicResponseChannelMap sync.RWMutex
79
khenaidoo4c1a5bf2018-11-29 15:53:42 -050080 // This map is used to map a transaction to a consumers channel. This is used whenever a request has been
khenaidoo43c82122018-11-22 18:38:28 -050081 // sent out and we are waiting for a response.
82 transactionIdToChannelMap map[string]*transactionChannel
khenaidooabad44c2018-08-03 16:58:35 -040083 lockTransactionIdToChannelMap sync.RWMutex
84}
85
khenaidoo43c82122018-11-22 18:38:28 -050086type InterContainerProxyOption func(*InterContainerProxy)
khenaidooabad44c2018-08-03 16:58:35 -040087
khenaidoo43c82122018-11-22 18:38:28 -050088func InterContainerHost(host string) InterContainerProxyOption {
89 return func(args *InterContainerProxy) {
90 args.kafkaHost = host
khenaidooabad44c2018-08-03 16:58:35 -040091 }
92}
93
khenaidoo43c82122018-11-22 18:38:28 -050094func InterContainerPort(port int) InterContainerProxyOption {
95 return func(args *InterContainerProxy) {
96 args.kafkaPort = port
khenaidooabad44c2018-08-03 16:58:35 -040097 }
98}
99
khenaidoo43c82122018-11-22 18:38:28 -0500100func DefaultTopic(topic *Topic) InterContainerProxyOption {
101 return func(args *InterContainerProxy) {
khenaidooabad44c2018-08-03 16:58:35 -0400102 args.DefaultTopic = topic
103 }
104}
105
khenaidoo79232702018-12-04 11:00:41 -0500106func DeviceDiscoveryTopic(topic *Topic) InterContainerProxyOption {
107 return func(args *InterContainerProxy) {
108 args.deviceDiscoveryTopic = topic
109 }
110}
111
khenaidoo43c82122018-11-22 18:38:28 -0500112func RequestHandlerInterface(handler interface{}) InterContainerProxyOption {
113 return func(args *InterContainerProxy) {
114 args.defaultRequestHandlerInterface = handler
khenaidooabad44c2018-08-03 16:58:35 -0400115 }
116}
117
khenaidoo43c82122018-11-22 18:38:28 -0500118func MsgClient(client Client) InterContainerProxyOption {
119 return func(args *InterContainerProxy) {
120 args.kafkaClient = client
121 }
122}
123
124func NewInterContainerProxy(opts ...InterContainerProxyOption) (*InterContainerProxy, error) {
125 proxy := &InterContainerProxy{
126 kafkaHost: DefaultKafkaHost,
127 kafkaPort: DefaultKafkaPort,
khenaidooabad44c2018-08-03 16:58:35 -0400128 }
129
130 for _, option := range opts {
131 option(proxy)
132 }
133
134 // Create the locks for all the maps
khenaidoo43c82122018-11-22 18:38:28 -0500135 proxy.lockTopicRequestHandlerChannelMap = sync.RWMutex{}
khenaidooabad44c2018-08-03 16:58:35 -0400136 proxy.lockTransactionIdToChannelMap = sync.RWMutex{}
khenaidoo43c82122018-11-22 18:38:28 -0500137 proxy.lockTopicResponseChannelMap = sync.RWMutex{}
khenaidooabad44c2018-08-03 16:58:35 -0400138
139 return proxy, nil
140}
141
khenaidoo43c82122018-11-22 18:38:28 -0500142func (kp *InterContainerProxy) Start() error {
khenaidooabad44c2018-08-03 16:58:35 -0400143 log.Info("Starting-Proxy")
144
khenaidoo43c82122018-11-22 18:38:28 -0500145 // Kafka MsgClient should already have been created. If not, output fatal error
146 if kp.kafkaClient == nil {
147 log.Fatal("kafka-client-not-set")
148 }
149
khenaidooabad44c2018-08-03 16:58:35 -0400150 // Create the Done channel
151 kp.doneCh = make(chan int, 1)
152
khenaidoo43c82122018-11-22 18:38:28 -0500153 // Start the kafka client
khenaidoo4c1a5bf2018-11-29 15:53:42 -0500154 if err := kp.kafkaClient.Start(); err != nil {
khenaidoo43c82122018-11-22 18:38:28 -0500155 log.Errorw("Cannot-create-kafka-proxy", log.Fields{"error": err})
khenaidooabad44c2018-08-03 16:58:35 -0400156 return err
157 }
158
khenaidoo43c82122018-11-22 18:38:28 -0500159 // Create the topic to response channel map
khenaidoo79232702018-12-04 11:00:41 -0500160 kp.topicToResponseChannelMap = make(map[string]<-chan *ic.InterContainerMessage)
khenaidoo43c82122018-11-22 18:38:28 -0500161 //
khenaidooabad44c2018-08-03 16:58:35 -0400162 // Create the transactionId to Channel Map
khenaidoo43c82122018-11-22 18:38:28 -0500163 kp.transactionIdToChannelMap = make(map[string]*transactionChannel)
164
165 // Create the topic to request channel map
166 kp.topicToRequestHandlerChannelMap = make(map[string]*requestHandlerChannel)
khenaidooabad44c2018-08-03 16:58:35 -0400167
168 return nil
169}
170
khenaidoo43c82122018-11-22 18:38:28 -0500171func (kp *InterContainerProxy) Stop() {
172 log.Info("stopping-intercontainer-proxy")
173 kp.doneCh <- 1
174 // TODO : Perform cleanup
khenaidooca301322019-01-09 23:06:32 -0500175 kp.kafkaClient.Stop()
khenaidoo43c82122018-11-22 18:38:28 -0500176 //kp.deleteAllTopicRequestHandlerChannelMap()
177 //kp.deleteAllTopicResponseChannelMap()
178 //kp.deleteAllTransactionIdToChannelMap()
khenaidooabad44c2018-08-03 16:58:35 -0400179}
180
khenaidoo79232702018-12-04 11:00:41 -0500181// DeviceDiscovered publish the discovered device onto the kafka messaging bus
khenaidoo19374072018-12-11 11:05:15 -0500182func (kp *InterContainerProxy) DeviceDiscovered(deviceId string, deviceType string, parentId string, publisher string) error {
khenaidoo79232702018-12-04 11:00:41 -0500183 log.Debugw("sending-device-discovery-msg", log.Fields{"deviceId": deviceId})
184 // Simple validation
185 if deviceId == "" || deviceType == "" {
186 log.Errorw("invalid-parameters", log.Fields{"id": deviceId, "type": deviceType})
187 return errors.New("invalid-parameters")
188 }
189 // Create the device discovery message
190 header := &ic.Header{
191 Id: uuid.New().String(),
192 Type: ic.MessageType_DEVICE_DISCOVERED,
193 FromTopic: kp.DefaultTopic.Name,
194 ToTopic: kp.deviceDiscoveryTopic.Name,
195 Timestamp: time.Now().UnixNano(),
196 }
197 body := &ic.DeviceDiscovered{
198 Id: deviceId,
199 DeviceType: deviceType,
200 ParentId: parentId,
khenaidood2b6df92018-12-13 16:37:20 -0500201 Publisher: publisher,
khenaidoo79232702018-12-04 11:00:41 -0500202 }
203
204 var marshalledData *any.Any
205 var err error
206 if marshalledData, err = ptypes.MarshalAny(body); err != nil {
207 log.Errorw("cannot-marshal-request", log.Fields{"error": err})
208 return err
209 }
210 msg := &ic.InterContainerMessage{
211 Header: header,
212 Body: marshalledData,
213 }
214
215 // Send the message
216 if err := kp.kafkaClient.Send(msg, kp.deviceDiscoveryTopic); err != nil {
217 log.Errorw("cannot-send-device-discovery-message", log.Fields{"error": err})
218 return err
219 }
220 return nil
221}
222
khenaidoo43c82122018-11-22 18:38:28 -0500223// InvokeRPC is used to send a request to a given topic
224func (kp *InterContainerProxy) InvokeRPC(ctx context.Context, rpc string, toTopic *Topic, replyToTopic *Topic,
225 waitForResponse bool, kvArgs ...*KVArg) (bool, *any.Any) {
226
227 // If a replyToTopic is provided then we use it, otherwise just use the default toTopic. The replyToTopic is
228 // typically the device ID.
229 responseTopic := replyToTopic
230 if responseTopic == nil {
231 responseTopic = kp.DefaultTopic
232 }
233
khenaidooabad44c2018-08-03 16:58:35 -0400234 // Encode the request
khenaidoo43c82122018-11-22 18:38:28 -0500235 protoRequest, err := encodeRequest(rpc, toTopic, responseTopic, kvArgs...)
khenaidooabad44c2018-08-03 16:58:35 -0400236 if err != nil {
237 log.Warnw("cannot-format-request", log.Fields{"rpc": rpc, "error": err})
238 return false, nil
239 }
240
241 // Subscribe for response, if needed, before sending request
khenaidoo79232702018-12-04 11:00:41 -0500242 var ch <-chan *ic.InterContainerMessage
khenaidooabad44c2018-08-03 16:58:35 -0400243 if waitForResponse {
244 var err error
khenaidoo43c82122018-11-22 18:38:28 -0500245 if ch, err = kp.subscribeForResponse(*responseTopic, protoRequest.Header.Id); err != nil {
246 log.Errorw("failed-to-subscribe-for-response", log.Fields{"error": err, "toTopic": toTopic.Name})
khenaidooabad44c2018-08-03 16:58:35 -0400247 }
248 }
249
khenaidoo43c82122018-11-22 18:38:28 -0500250 // Send request - if the topic is formatted with a device Id then we will send the request using a
251 // specific key, hence ensuring a single partition is used to publish the request. This ensures that the
252 // subscriber on that topic will receive the request in the order it was sent. The key used is the deviceId.
253 key := GetDeviceIdFromTopic(*toTopic)
254 log.Debugw("sending-msg", log.Fields{"rpc": rpc, "toTopic": toTopic, "replyTopic": responseTopic, "key": key})
255 go kp.kafkaClient.Send(protoRequest, toTopic, key)
khenaidooabad44c2018-08-03 16:58:35 -0400256
257 if waitForResponse {
khenaidoob9203542018-09-17 22:56:37 -0400258 // Create a child context based on the parent context, if any
khenaidooabad44c2018-08-03 16:58:35 -0400259 var cancel context.CancelFunc
khenaidoob9203542018-09-17 22:56:37 -0400260 childCtx := context.Background()
khenaidooabad44c2018-08-03 16:58:35 -0400261 if ctx == nil {
262 ctx, cancel = context.WithTimeout(context.Background(), DefaultRequestTimeout*time.Millisecond)
khenaidoob9203542018-09-17 22:56:37 -0400263 } else {
264 childCtx, cancel = context.WithTimeout(ctx, DefaultRequestTimeout*time.Millisecond)
khenaidooabad44c2018-08-03 16:58:35 -0400265 }
khenaidoob9203542018-09-17 22:56:37 -0400266 defer cancel()
khenaidooabad44c2018-08-03 16:58:35 -0400267
268 // Wait for response as well as timeout or cancellation
269 // Remove the subscription for a response on return
270 defer kp.unSubscribeForResponse(protoRequest.Header.Id)
271 select {
272 case msg := <-ch:
khenaidoo43c82122018-11-22 18:38:28 -0500273 log.Debugw("received-response", log.Fields{"rpc": rpc, "msgHeader": msg.Header})
khenaidoo79232702018-12-04 11:00:41 -0500274 var responseBody *ic.InterContainerResponseBody
khenaidooabad44c2018-08-03 16:58:35 -0400275 var err error
276 if responseBody, err = decodeResponse(msg); err != nil {
277 log.Errorw("decode-response-error", log.Fields{"error": err})
278 }
279 return responseBody.Success, responseBody.Result
280 case <-ctx.Done():
281 log.Debugw("context-cancelled", log.Fields{"rpc": rpc, "ctx": ctx.Err()})
282 // pack the error as proto any type
khenaidoo79232702018-12-04 11:00:41 -0500283 protoError := &ic.Error{Reason: ctx.Err().Error()}
khenaidooabad44c2018-08-03 16:58:35 -0400284 var marshalledArg *any.Any
285 if marshalledArg, err = ptypes.MarshalAny(protoError); err != nil {
286 return false, nil // Should never happen
287 }
288 return false, marshalledArg
khenaidoob9203542018-09-17 22:56:37 -0400289 case <-childCtx.Done():
290 log.Debugw("context-cancelled", log.Fields{"rpc": rpc, "ctx": childCtx.Err()})
291 // pack the error as proto any type
khenaidoo79232702018-12-04 11:00:41 -0500292 protoError := &ic.Error{Reason: childCtx.Err().Error()}
khenaidoob9203542018-09-17 22:56:37 -0400293 var marshalledArg *any.Any
294 if marshalledArg, err = ptypes.MarshalAny(protoError); err != nil {
295 return false, nil // Should never happen
296 }
297 return false, marshalledArg
khenaidooabad44c2018-08-03 16:58:35 -0400298 case <-kp.doneCh:
khenaidoo43c82122018-11-22 18:38:28 -0500299 log.Infow("received-exit-signal", log.Fields{"toTopic": toTopic.Name, "rpc": rpc})
khenaidooabad44c2018-08-03 16:58:35 -0400300 return true, nil
301 }
302 }
303 return true, nil
304}
305
khenaidoo43c82122018-11-22 18:38:28 -0500306// SubscribeWithRequestHandlerInterface allows a caller to assign a target object to be invoked automatically
khenaidooabad44c2018-08-03 16:58:35 -0400307// when a message is received on a given topic
khenaidoo43c82122018-11-22 18:38:28 -0500308func (kp *InterContainerProxy) SubscribeWithRequestHandlerInterface(topic Topic, handler interface{}) error {
khenaidooabad44c2018-08-03 16:58:35 -0400309
310 // Subscribe to receive messages for that topic
khenaidoo79232702018-12-04 11:00:41 -0500311 var ch <-chan *ic.InterContainerMessage
khenaidooabad44c2018-08-03 16:58:35 -0400312 var err error
khenaidoo4c1a5bf2018-11-29 15:53:42 -0500313 if ch, err = kp.kafkaClient.Subscribe(&topic); err != nil {
khenaidoo43c82122018-11-22 18:38:28 -0500314 //if ch, err = kp.Subscribe(topic); err != nil {
khenaidooabad44c2018-08-03 16:58:35 -0400315 log.Errorw("failed-to-subscribe", log.Fields{"error": err, "topic": topic.Name})
316 }
khenaidoo43c82122018-11-22 18:38:28 -0500317
318 kp.defaultRequestHandlerInterface = handler
319 kp.addToTopicRequestHandlerChannelMap(topic.Name, &requestHandlerChannel{requesthandlerInterface: handler, ch: ch})
khenaidooabad44c2018-08-03 16:58:35 -0400320 // Launch a go routine to receive and process kafka messages
khenaidoo43c82122018-11-22 18:38:28 -0500321 go kp.waitForRequest(ch, topic, handler)
khenaidooabad44c2018-08-03 16:58:35 -0400322
323 return nil
324}
325
khenaidoo43c82122018-11-22 18:38:28 -0500326// SubscribeWithDefaultRequestHandler allows a caller to add a topic to an existing target object to be invoked automatically
327// when a message is received on a given topic. So far there is only 1 target registered per microservice
328func (kp *InterContainerProxy) SubscribeWithDefaultRequestHandler(topic Topic) error {
329 // Subscribe to receive messages for that topic
khenaidoo79232702018-12-04 11:00:41 -0500330 var ch <-chan *ic.InterContainerMessage
khenaidoo43c82122018-11-22 18:38:28 -0500331 var err error
khenaidoo4c1a5bf2018-11-29 15:53:42 -0500332 if ch, err = kp.kafkaClient.Subscribe(&topic); err != nil {
khenaidoo43c82122018-11-22 18:38:28 -0500333 log.Errorw("failed-to-subscribe", log.Fields{"error": err, "topic": topic.Name})
khenaidooca301322019-01-09 23:06:32 -0500334 return err
khenaidoo43c82122018-11-22 18:38:28 -0500335 }
336 kp.addToTopicRequestHandlerChannelMap(topic.Name, &requestHandlerChannel{requesthandlerInterface: kp.defaultRequestHandlerInterface, ch: ch})
337
338 // Launch a go routine to receive and process kafka messages
339 go kp.waitForRequest(ch, topic, kp.defaultRequestHandlerInterface)
340
khenaidooabad44c2018-08-03 16:58:35 -0400341 return nil
342}
343
khenaidoo43c82122018-11-22 18:38:28 -0500344func (kp *InterContainerProxy) UnSubscribeFromRequestHandler(topic Topic) error {
345 return kp.deleteFromTopicRequestHandlerChannelMap(topic.Name)
346}
347
khenaidoo4c1a5bf2018-11-29 15:53:42 -0500348// setupTopicResponseChannelMap sets up single consumers channel that will act as a broadcast channel for all
khenaidoo43c82122018-11-22 18:38:28 -0500349// responses from that topic.
khenaidoo79232702018-12-04 11:00:41 -0500350func (kp *InterContainerProxy) setupTopicResponseChannelMap(topic string, arg <-chan *ic.InterContainerMessage) {
khenaidoo43c82122018-11-22 18:38:28 -0500351 kp.lockTopicResponseChannelMap.Lock()
352 defer kp.lockTopicResponseChannelMap.Unlock()
353 if _, exist := kp.topicToResponseChannelMap[topic]; !exist {
354 kp.topicToResponseChannelMap[topic] = arg
khenaidooabad44c2018-08-03 16:58:35 -0400355 }
356}
357
khenaidoo43c82122018-11-22 18:38:28 -0500358func (kp *InterContainerProxy) isTopicSubscribedForResponse(topic string) bool {
359 kp.lockTopicResponseChannelMap.Lock()
360 defer kp.lockTopicResponseChannelMap.Unlock()
361 _, exist := kp.topicToResponseChannelMap[topic]
362 return exist
363}
364
365func (kp *InterContainerProxy) deleteFromTopicResponseChannelMap(topic string) error {
366 kp.lockTopicResponseChannelMap.Lock()
367 defer kp.lockTopicResponseChannelMap.Unlock()
368 if _, exist := kp.topicToResponseChannelMap[topic]; exist {
369 // Unsubscribe to this topic first - this will close the subscribed channel
370 var err error
371 if err = kp.kafkaClient.UnSubscribe(&Topic{Name: topic}, kp.topicToResponseChannelMap[topic]); err != nil {
372 log.Errorw("unsubscribing-error", log.Fields{"topic": topic})
373 }
374 delete(kp.topicToResponseChannelMap, topic)
375 return err
376 } else {
377 return errors.New(fmt.Sprintf("%s-Topic-not-found", topic))
khenaidooabad44c2018-08-03 16:58:35 -0400378 }
379}
380
khenaidoo43c82122018-11-22 18:38:28 -0500381func (kp *InterContainerProxy) deleteAllTopicResponseChannelMap() error {
382 kp.lockTopicResponseChannelMap.Lock()
383 defer kp.lockTopicResponseChannelMap.Unlock()
384 var err error
385 for topic, _ := range kp.topicToResponseChannelMap {
386 // Unsubscribe to this topic first - this will close the subscribed channel
387 if err = kp.kafkaClient.UnSubscribe(&Topic{Name: topic}, kp.topicToResponseChannelMap[topic]); err != nil {
388 log.Errorw("unsubscribing-error", log.Fields{"topic": topic, "error": err})
389 }
390 delete(kp.topicToResponseChannelMap, topic)
khenaidooabad44c2018-08-03 16:58:35 -0400391 }
khenaidoo43c82122018-11-22 18:38:28 -0500392 return err
khenaidooabad44c2018-08-03 16:58:35 -0400393}
394
khenaidoo43c82122018-11-22 18:38:28 -0500395func (kp *InterContainerProxy) addToTopicRequestHandlerChannelMap(topic string, arg *requestHandlerChannel) {
396 kp.lockTopicRequestHandlerChannelMap.Lock()
397 defer kp.lockTopicRequestHandlerChannelMap.Unlock()
398 if _, exist := kp.topicToRequestHandlerChannelMap[topic]; !exist {
399 kp.topicToRequestHandlerChannelMap[topic] = arg
khenaidooabad44c2018-08-03 16:58:35 -0400400 }
khenaidooabad44c2018-08-03 16:58:35 -0400401}
402
khenaidoo43c82122018-11-22 18:38:28 -0500403func (kp *InterContainerProxy) deleteFromTopicRequestHandlerChannelMap(topic string) error {
404 kp.lockTopicRequestHandlerChannelMap.Lock()
405 defer kp.lockTopicRequestHandlerChannelMap.Unlock()
406 if _, exist := kp.topicToRequestHandlerChannelMap[topic]; exist {
407 // Close the kafka client client first by unsubscribing to this topic
408 kp.kafkaClient.UnSubscribe(&Topic{Name: topic}, kp.topicToRequestHandlerChannelMap[topic].ch)
409 delete(kp.topicToRequestHandlerChannelMap, topic)
khenaidooabad44c2018-08-03 16:58:35 -0400410 return nil
khenaidoo43c82122018-11-22 18:38:28 -0500411 } else {
412 return errors.New(fmt.Sprintf("%s-Topic-not-found", topic))
khenaidooabad44c2018-08-03 16:58:35 -0400413 }
khenaidooabad44c2018-08-03 16:58:35 -0400414}
415
khenaidoo43c82122018-11-22 18:38:28 -0500416func (kp *InterContainerProxy) deleteAllTopicRequestHandlerChannelMap() error {
417 kp.lockTopicRequestHandlerChannelMap.Lock()
418 defer kp.lockTopicRequestHandlerChannelMap.Unlock()
419 var err error
420 for topic, _ := range kp.topicToRequestHandlerChannelMap {
421 // Close the kafka client client first by unsubscribing to this topic
422 if err = kp.kafkaClient.UnSubscribe(&Topic{Name: topic}, kp.topicToRequestHandlerChannelMap[topic].ch); err != nil {
423 log.Errorw("unsubscribing-error", log.Fields{"topic": topic, "error": err})
424 }
425 delete(kp.topicToRequestHandlerChannelMap, topic)
426 }
427 return err
428}
429
khenaidoo79232702018-12-04 11:00:41 -0500430func (kp *InterContainerProxy) addToTransactionIdToChannelMap(id string, topic *Topic, arg chan *ic.InterContainerMessage) {
khenaidooabad44c2018-08-03 16:58:35 -0400431 kp.lockTransactionIdToChannelMap.Lock()
432 defer kp.lockTransactionIdToChannelMap.Unlock()
433 if _, exist := kp.transactionIdToChannelMap[id]; !exist {
khenaidoo43c82122018-11-22 18:38:28 -0500434 kp.transactionIdToChannelMap[id] = &transactionChannel{topic: topic, ch: arg}
khenaidooabad44c2018-08-03 16:58:35 -0400435 }
436}
437
khenaidoo43c82122018-11-22 18:38:28 -0500438func (kp *InterContainerProxy) deleteFromTransactionIdToChannelMap(id string) {
khenaidooabad44c2018-08-03 16:58:35 -0400439 kp.lockTransactionIdToChannelMap.Lock()
440 defer kp.lockTransactionIdToChannelMap.Unlock()
khenaidoo43c82122018-11-22 18:38:28 -0500441 if transChannel, exist := kp.transactionIdToChannelMap[id]; exist {
442 // Close the channel first
443 close(transChannel.ch)
khenaidooabad44c2018-08-03 16:58:35 -0400444 delete(kp.transactionIdToChannelMap, id)
445 }
446}
447
khenaidoo43c82122018-11-22 18:38:28 -0500448func (kp *InterContainerProxy) deleteTopicTransactionIdToChannelMap(id string) {
449 kp.lockTransactionIdToChannelMap.Lock()
450 defer kp.lockTransactionIdToChannelMap.Unlock()
451 for key, value := range kp.transactionIdToChannelMap {
452 if value.topic.Name == id {
453 close(value.ch)
454 delete(kp.transactionIdToChannelMap, key)
khenaidooabad44c2018-08-03 16:58:35 -0400455 }
456 }
khenaidooabad44c2018-08-03 16:58:35 -0400457}
458
khenaidoo43c82122018-11-22 18:38:28 -0500459func (kp *InterContainerProxy) deleteAllTransactionIdToChannelMap() {
460 kp.lockTransactionIdToChannelMap.Lock()
461 defer kp.lockTransactionIdToChannelMap.Unlock()
462 for key, value := range kp.transactionIdToChannelMap {
463 close(value.ch)
464 delete(kp.transactionIdToChannelMap, key)
khenaidooabad44c2018-08-03 16:58:35 -0400465 }
khenaidooabad44c2018-08-03 16:58:35 -0400466}
467
khenaidoo43c82122018-11-22 18:38:28 -0500468func (kp *InterContainerProxy) DeleteTopic(topic Topic) error {
469 // If we have any consumers on that topic we need to close them
470 kp.deleteFromTopicResponseChannelMap(topic.Name)
471 kp.deleteFromTopicRequestHandlerChannelMap(topic.Name)
472 kp.deleteTopicTransactionIdToChannelMap(topic.Name)
473 return kp.kafkaClient.DeleteTopic(&topic)
474}
475
476func encodeReturnedValue(returnedVal interface{}) (*any.Any, error) {
khenaidooabad44c2018-08-03 16:58:35 -0400477 // Encode the response argument - needs to be a proto message
478 if returnedVal == nil {
479 return nil, nil
480 }
481 protoValue, ok := returnedVal.(proto.Message)
482 if !ok {
483 log.Warnw("response-value-not-proto-message", log.Fields{"error": ok, "returnVal": returnedVal})
484 err := errors.New("response-value-not-proto-message")
485 return nil, err
486 }
487
488 // Marshal the returned value, if any
489 var marshalledReturnedVal *any.Any
490 var err error
491 if marshalledReturnedVal, err = ptypes.MarshalAny(protoValue); err != nil {
492 log.Warnw("cannot-marshal-returned-val", log.Fields{"error": err})
493 return nil, err
494 }
495 return marshalledReturnedVal, nil
496}
497
khenaidoo79232702018-12-04 11:00:41 -0500498func encodeDefaultFailedResponse(request *ic.InterContainerMessage) *ic.InterContainerMessage {
499 responseHeader := &ic.Header{
khenaidooabad44c2018-08-03 16:58:35 -0400500 Id: request.Header.Id,
khenaidoo79232702018-12-04 11:00:41 -0500501 Type: ic.MessageType_RESPONSE,
khenaidooabad44c2018-08-03 16:58:35 -0400502 FromTopic: request.Header.ToTopic,
503 ToTopic: request.Header.FromTopic,
504 Timestamp: time.Now().Unix(),
505 }
khenaidoo79232702018-12-04 11:00:41 -0500506 responseBody := &ic.InterContainerResponseBody{
khenaidooabad44c2018-08-03 16:58:35 -0400507 Success: false,
508 Result: nil,
509 }
510 var marshalledResponseBody *any.Any
511 var err error
512 // Error should never happen here
513 if marshalledResponseBody, err = ptypes.MarshalAny(responseBody); err != nil {
514 log.Warnw("cannot-marshal-failed-response-body", log.Fields{"error": err})
515 }
516
khenaidoo79232702018-12-04 11:00:41 -0500517 return &ic.InterContainerMessage{
khenaidooabad44c2018-08-03 16:58:35 -0400518 Header: responseHeader,
519 Body: marshalledResponseBody,
520 }
521
522}
523
524//formatRequest formats a request to send over kafka and returns an InterContainerMessage message on success
525//or an error on failure
khenaidoo79232702018-12-04 11:00:41 -0500526func encodeResponse(request *ic.InterContainerMessage, success bool, returnedValues ...interface{}) (*ic.InterContainerMessage, error) {
khenaidoo43c82122018-11-22 18:38:28 -0500527 //log.Debugw("encodeResponse", log.Fields{"success": success, "returnedValues": returnedValues})
khenaidoo79232702018-12-04 11:00:41 -0500528 responseHeader := &ic.Header{
khenaidooabad44c2018-08-03 16:58:35 -0400529 Id: request.Header.Id,
khenaidoo79232702018-12-04 11:00:41 -0500530 Type: ic.MessageType_RESPONSE,
khenaidooabad44c2018-08-03 16:58:35 -0400531 FromTopic: request.Header.ToTopic,
532 ToTopic: request.Header.FromTopic,
533 Timestamp: time.Now().Unix(),
534 }
535
536 // Go over all returned values
537 var marshalledReturnedVal *any.Any
538 var err error
539 for _, returnVal := range returnedValues {
khenaidoo43c82122018-11-22 18:38:28 -0500540 if marshalledReturnedVal, err = encodeReturnedValue(returnVal); err != nil {
khenaidooabad44c2018-08-03 16:58:35 -0400541 log.Warnw("cannot-marshal-response-body", log.Fields{"error": err})
542 }
543 break // for now we support only 1 returned value - (excluding the error)
544 }
545
khenaidoo79232702018-12-04 11:00:41 -0500546 responseBody := &ic.InterContainerResponseBody{
khenaidooabad44c2018-08-03 16:58:35 -0400547 Success: success,
548 Result: marshalledReturnedVal,
549 }
550
551 // Marshal the response body
552 var marshalledResponseBody *any.Any
553 if marshalledResponseBody, err = ptypes.MarshalAny(responseBody); err != nil {
554 log.Warnw("cannot-marshal-response-body", log.Fields{"error": err})
555 return nil, err
556 }
557
khenaidoo79232702018-12-04 11:00:41 -0500558 return &ic.InterContainerMessage{
khenaidooabad44c2018-08-03 16:58:35 -0400559 Header: responseHeader,
560 Body: marshalledResponseBody,
561 }, nil
562}
563
564func CallFuncByName(myClass interface{}, funcName string, params ...interface{}) (out []reflect.Value, err error) {
565 myClassValue := reflect.ValueOf(myClass)
khenaidoo19374072018-12-11 11:05:15 -0500566 // Capitalize the first letter in the funcName to workaround the first capital letters required to
567 // invoke a function from a different package
568 funcName = strings.Title(funcName)
khenaidooabad44c2018-08-03 16:58:35 -0400569 m := myClassValue.MethodByName(funcName)
570 if !m.IsValid() {
khenaidoo43c82122018-11-22 18:38:28 -0500571 return make([]reflect.Value, 0), fmt.Errorf("method-not-found \"%s\"", funcName)
khenaidooabad44c2018-08-03 16:58:35 -0400572 }
573 in := make([]reflect.Value, len(params))
574 for i, param := range params {
575 in[i] = reflect.ValueOf(param)
576 }
577 out = m.Call(in)
578 return
579}
580
khenaidoo79232702018-12-04 11:00:41 -0500581func (kp *InterContainerProxy) handleRequest(msg *ic.InterContainerMessage, targetInterface interface{}) {
khenaidooabad44c2018-08-03 16:58:35 -0400582
khenaidoo43c82122018-11-22 18:38:28 -0500583 // First extract the header to know whether this is a request - responses are handled by a different handler
khenaidoo79232702018-12-04 11:00:41 -0500584 if msg.Header.Type == ic.MessageType_REQUEST {
khenaidooabad44c2018-08-03 16:58:35 -0400585
586 var out []reflect.Value
587 var err error
588
589 // Get the request body
khenaidoo79232702018-12-04 11:00:41 -0500590 requestBody := &ic.InterContainerRequestBody{}
khenaidooabad44c2018-08-03 16:58:35 -0400591 if err = ptypes.UnmarshalAny(msg.Body, requestBody); err != nil {
592 log.Warnw("cannot-unmarshal-request", log.Fields{"error": err})
593 } else {
khenaidoo43c82122018-11-22 18:38:28 -0500594 log.Debugw("received-request", log.Fields{"rpc": requestBody.Rpc, "header": msg.Header})
khenaidooabad44c2018-08-03 16:58:35 -0400595 // let the callee unpack the arguments as its the only one that knows the real proto type
596 out, err = CallFuncByName(targetInterface, requestBody.Rpc, requestBody.Args)
597 if err != nil {
598 log.Warn(err)
599 }
600 }
601 // Response required?
602 if requestBody.ResponseRequired {
603 // If we already have an error before then just return that
khenaidoo79232702018-12-04 11:00:41 -0500604 var returnError *ic.Error
khenaidooabad44c2018-08-03 16:58:35 -0400605 var returnedValues []interface{}
606 var success bool
607 if err != nil {
khenaidoo79232702018-12-04 11:00:41 -0500608 returnError = &ic.Error{Reason: err.Error()}
khenaidooabad44c2018-08-03 16:58:35 -0400609 returnedValues = make([]interface{}, 1)
610 returnedValues[0] = returnError
611 } else {
khenaidoo43c82122018-11-22 18:38:28 -0500612 //log.Debugw("returned-api-response", log.Fields{"len": len(out), "err": err})
khenaidoob9203542018-09-17 22:56:37 -0400613 returnedValues = make([]interface{}, 0)
614 // Check for errors first
615 lastIndex := len(out) - 1
616 if out[lastIndex].Interface() != nil { // Error
617 if goError, ok := out[lastIndex].Interface().(error); ok {
khenaidoo79232702018-12-04 11:00:41 -0500618 returnError = &ic.Error{Reason: goError.Error()}
khenaidoob9203542018-09-17 22:56:37 -0400619 returnedValues = append(returnedValues, returnError)
620 } else { // Should never happen
khenaidoo79232702018-12-04 11:00:41 -0500621 returnError = &ic.Error{Reason: "incorrect-error-returns"}
khenaidoob9203542018-09-17 22:56:37 -0400622 returnedValues = append(returnedValues, returnError)
623 }
624 } else { // Non-error case
625 success = true
626 for idx, val := range out {
khenaidoo43c82122018-11-22 18:38:28 -0500627 //log.Debugw("returned-api-response-loop", log.Fields{"idx": idx, "val": val.Interface()})
khenaidoob9203542018-09-17 22:56:37 -0400628 if idx != lastIndex {
629 returnedValues = append(returnedValues, val.Interface())
khenaidooabad44c2018-08-03 16:58:35 -0400630 }
khenaidooabad44c2018-08-03 16:58:35 -0400631 }
632 }
633 }
634
khenaidoo79232702018-12-04 11:00:41 -0500635 var icm *ic.InterContainerMessage
khenaidooabad44c2018-08-03 16:58:35 -0400636 if icm, err = encodeResponse(msg, success, returnedValues...); err != nil {
khenaidoob9203542018-09-17 22:56:37 -0400637 log.Warnw("error-encoding-response-returning-failure-result", log.Fields{"error": err})
khenaidooabad44c2018-08-03 16:58:35 -0400638 icm = encodeDefaultFailedResponse(msg)
639 }
khenaidoo43c82122018-11-22 18:38:28 -0500640 // To preserve ordering of messages, all messages to a given topic are sent to the same partition
641 // by providing a message key. The key is encoded in the topic name. If the deviceId is not
642 // present then the key will be empty, hence all messages for a given topic will be sent to all
643 // partitions.
644 replyTopic := &Topic{Name: msg.Header.FromTopic}
645 key := GetDeviceIdFromTopic(*replyTopic)
khenaidoo90847922018-12-03 14:47:51 -0500646 log.Debugw("sending-response-to-kafka", log.Fields{"rpc": requestBody.Rpc, "header": icm.Header, "key": key})
khenaidoo43c82122018-11-22 18:38:28 -0500647 // TODO: handle error response.
648 kp.kafkaClient.Send(icm, replyTopic, key)
khenaidooabad44c2018-08-03 16:58:35 -0400649 }
650
khenaidooabad44c2018-08-03 16:58:35 -0400651 }
652}
653
khenaidoo79232702018-12-04 11:00:41 -0500654func (kp *InterContainerProxy) waitForRequest(ch <-chan *ic.InterContainerMessage, topic Topic, targetInterface interface{}) {
khenaidooabad44c2018-08-03 16:58:35 -0400655 // Wait for messages
656 for msg := range ch {
khenaidoo43c82122018-11-22 18:38:28 -0500657 //log.Debugw("request-received", log.Fields{"msg": msg, "topic": topic.Name, "target": targetInterface})
khenaidooabad44c2018-08-03 16:58:35 -0400658 go kp.handleRequest(msg, targetInterface)
659 }
660}
661
khenaidoo79232702018-12-04 11:00:41 -0500662func (kp *InterContainerProxy) dispatchResponse(msg *ic.InterContainerMessage) {
khenaidooabad44c2018-08-03 16:58:35 -0400663 kp.lockTransactionIdToChannelMap.Lock()
664 defer kp.lockTransactionIdToChannelMap.Unlock()
665 if _, exist := kp.transactionIdToChannelMap[msg.Header.Id]; !exist {
666 log.Debugw("no-waiting-channel", log.Fields{"transaction": msg.Header.Id})
667 return
668 }
khenaidoo43c82122018-11-22 18:38:28 -0500669 kp.transactionIdToChannelMap[msg.Header.Id].ch <- msg
khenaidooabad44c2018-08-03 16:58:35 -0400670}
671
khenaidoo43c82122018-11-22 18:38:28 -0500672// waitForResponse listens for messages on the subscribedCh, ensure we get a response with the transaction ID,
khenaidoo4c1a5bf2018-11-29 15:53:42 -0500673// and then dispatches to the consumers
khenaidoo79232702018-12-04 11:00:41 -0500674func (kp *InterContainerProxy) waitForResponseLoop(subscribedCh <-chan *ic.InterContainerMessage, topic *Topic) {
khenaidoo43c82122018-11-22 18:38:28 -0500675 log.Debugw("starting-response-loop-for-topic", log.Fields{"topic": topic.Name})
khenaidooabad44c2018-08-03 16:58:35 -0400676startloop:
677 for {
678 select {
khenaidoo43c82122018-11-22 18:38:28 -0500679 case msg := <-subscribedCh:
khenaidooca301322019-01-09 23:06:32 -0500680 log.Debugw("message-received", log.Fields{"msg": msg, "fromTopic": msg.Header.FromTopic})
khenaidoo79232702018-12-04 11:00:41 -0500681 if msg.Header.Type == ic.MessageType_RESPONSE {
khenaidoo43c82122018-11-22 18:38:28 -0500682 go kp.dispatchResponse(msg)
683 }
khenaidooabad44c2018-08-03 16:58:35 -0400684 case <-kp.doneCh:
685 log.Infow("received-exit-signal", log.Fields{"topic": topic.Name})
686 break startloop
687 }
688 }
khenaidoo43c82122018-11-22 18:38:28 -0500689 //log.Infow("received-exit-signal-out-of-for-loop", log.Fields{"topic": topic.Name})
690 // We got an exit signal. Unsubscribe to the channel
691 //kp.kafkaClient.UnSubscribe(topic, subscribedCh)
khenaidooabad44c2018-08-03 16:58:35 -0400692}
693
694// subscribeForResponse allows a caller to subscribe to a given topic when waiting for a response.
695// This method is built to prevent all subscribers to receive all messages as is the case of the Subscribe
696// API. There is one response channel waiting for kafka messages before dispatching the message to the
697// corresponding waiting channel
khenaidoo79232702018-12-04 11:00:41 -0500698func (kp *InterContainerProxy) subscribeForResponse(topic Topic, trnsId string) (chan *ic.InterContainerMessage, error) {
khenaidoob9203542018-09-17 22:56:37 -0400699 log.Debugw("subscribeForResponse", log.Fields{"topic": topic.Name, "trnsid": trnsId})
khenaidooabad44c2018-08-03 16:58:35 -0400700
khenaidoo43c82122018-11-22 18:38:28 -0500701 // First check whether we already have a channel listening for response on that topic. If there is
702 // already one then it will be reused. If not, it will be created.
703 if !kp.isTopicSubscribedForResponse(topic.Name) {
khenaidooca301322019-01-09 23:06:32 -0500704 log.Debugw("not-subscribed-for-response", log.Fields{"topic": topic.Name, "trnsid": trnsId})
khenaidoo79232702018-12-04 11:00:41 -0500705 var subscribedCh <-chan *ic.InterContainerMessage
khenaidooabad44c2018-08-03 16:58:35 -0400706 var err error
khenaidoo4c1a5bf2018-11-29 15:53:42 -0500707 if subscribedCh, err = kp.kafkaClient.Subscribe(&topic); err != nil {
khenaidoo43c82122018-11-22 18:38:28 -0500708 log.Debugw("subscribe-failure", log.Fields{"topic": topic.Name})
khenaidooabad44c2018-08-03 16:58:35 -0400709 return nil, err
710 }
khenaidoo43c82122018-11-22 18:38:28 -0500711 kp.setupTopicResponseChannelMap(topic.Name, subscribedCh)
712 go kp.waitForResponseLoop(subscribedCh, &topic)
khenaidooabad44c2018-08-03 16:58:35 -0400713 }
714
khenaidoo4c1a5bf2018-11-29 15:53:42 -0500715 // Create a specific channel for this consumers. We cannot use the channel from the kafkaclient as it will
khenaidoo43c82122018-11-22 18:38:28 -0500716 // broadcast any message for this topic to all channels waiting on it.
khenaidoo79232702018-12-04 11:00:41 -0500717 ch := make(chan *ic.InterContainerMessage)
khenaidoo43c82122018-11-22 18:38:28 -0500718 kp.addToTransactionIdToChannelMap(trnsId, &topic, ch)
khenaidooabad44c2018-08-03 16:58:35 -0400719
720 return ch, nil
721}
722
khenaidoo43c82122018-11-22 18:38:28 -0500723func (kp *InterContainerProxy) unSubscribeForResponse(trnsId string) error {
khenaidooabad44c2018-08-03 16:58:35 -0400724 log.Debugw("unsubscribe-for-response", log.Fields{"trnsId": trnsId})
khenaidoo43c82122018-11-22 18:38:28 -0500725 if _, exist := kp.transactionIdToChannelMap[trnsId]; exist {
726 // The delete operation will close the channel
727 kp.deleteFromTransactionIdToChannelMap(trnsId)
728 }
khenaidooabad44c2018-08-03 16:58:35 -0400729 return nil
730}
731
732//formatRequest formats a request to send over kafka and returns an InterContainerMessage message on success
733//or an error on failure
khenaidoo79232702018-12-04 11:00:41 -0500734func encodeRequest(rpc string, toTopic *Topic, replyTopic *Topic, kvArgs ...*KVArg) (*ic.InterContainerMessage, error) {
735 requestHeader := &ic.Header{
khenaidooabad44c2018-08-03 16:58:35 -0400736 Id: uuid.New().String(),
khenaidoo79232702018-12-04 11:00:41 -0500737 Type: ic.MessageType_REQUEST,
khenaidooabad44c2018-08-03 16:58:35 -0400738 FromTopic: replyTopic.Name,
739 ToTopic: toTopic.Name,
740 Timestamp: time.Now().Unix(),
741 }
khenaidoo79232702018-12-04 11:00:41 -0500742 requestBody := &ic.InterContainerRequestBody{
khenaidooabad44c2018-08-03 16:58:35 -0400743 Rpc: rpc,
744 ResponseRequired: true,
745 ReplyToTopic: replyTopic.Name,
746 }
747
748 for _, arg := range kvArgs {
khenaidoo2c6f1672018-09-20 23:14:41 -0400749 if arg == nil {
750 // In case the caller sends an array with empty args
751 continue
752 }
khenaidooabad44c2018-08-03 16:58:35 -0400753 var marshalledArg *any.Any
754 var err error
755 // ascertain the value interface type is a proto.Message
756 protoValue, ok := arg.Value.(proto.Message)
757 if !ok {
758 log.Warnw("argument-value-not-proto-message", log.Fields{"error": ok, "Value": arg.Value})
759 err := errors.New("argument-value-not-proto-message")
760 return nil, err
761 }
762 if marshalledArg, err = ptypes.MarshalAny(protoValue); err != nil {
763 log.Warnw("cannot-marshal-request", log.Fields{"error": err})
764 return nil, err
765 }
khenaidoo79232702018-12-04 11:00:41 -0500766 protoArg := &ic.Argument{
khenaidooabad44c2018-08-03 16:58:35 -0400767 Key: arg.Key,
768 Value: marshalledArg,
769 }
770 requestBody.Args = append(requestBody.Args, protoArg)
771 }
772
773 var marshalledData *any.Any
774 var err error
775 if marshalledData, err = ptypes.MarshalAny(requestBody); err != nil {
776 log.Warnw("cannot-marshal-request", log.Fields{"error": err})
777 return nil, err
778 }
khenaidoo79232702018-12-04 11:00:41 -0500779 request := &ic.InterContainerMessage{
khenaidooabad44c2018-08-03 16:58:35 -0400780 Header: requestHeader,
781 Body: marshalledData,
782 }
783 return request, nil
784}
785
khenaidoo79232702018-12-04 11:00:41 -0500786func decodeResponse(response *ic.InterContainerMessage) (*ic.InterContainerResponseBody, error) {
khenaidooabad44c2018-08-03 16:58:35 -0400787 // Extract the message body
khenaidoo79232702018-12-04 11:00:41 -0500788 responseBody := ic.InterContainerResponseBody{}
khenaidooabad44c2018-08-03 16:58:35 -0400789 if err := ptypes.UnmarshalAny(response.Body, &responseBody); err != nil {
790 log.Warnw("cannot-unmarshal-response", log.Fields{"error": err})
791 return nil, err
792 }
khenaidoo43c82122018-11-22 18:38:28 -0500793 //log.Debugw("response-decoded-successfully", log.Fields{"response-status": &responseBody.Success})
khenaidooabad44c2018-08-03 16:58:35 -0400794
795 return &responseBody, nil
796
797}