blob: bdc615fe2ddf60dfc45d1edc4b40a8b5c8a36c55 [file] [log] [blame]
Scott Baker2c1c4822019-10-16 11:02:41 -07001/*
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 */
16package kafka
17
Matteo Scandoloed128822020-02-10 15:52:35 -080018import (
19 "github.com/golang/protobuf/ptypes/any"
20 "strings"
21)
Scott Baker2c1c4822019-10-16 11:02:41 -070022
23const (
24 TopicSeparator = "_"
25 DeviceIdLength = 24
26)
27
28// A Topic definition - may be augmented with additional attributes eventually
29type Topic struct {
30 // The name of the topic. It must start with a letter,
31 // and contain only letters (`[A-Za-z]`), numbers (`[0-9]`), dashes (`-`),
32 // underscores (`_`), periods (`.`), tildes (`~`), plus (`+`) or percent
33 // signs (`%`).
34 Name string
35}
36
37type KVArg struct {
38 Key string
39 Value interface{}
40}
41
Matteo Scandoloed128822020-02-10 15:52:35 -080042type RpcMType int
43
44const (
45 RpcFormattingError RpcMType = iota
46 RpcSent
47 RpcReply
48 RpcTimeout
49 RpcTransportError
50 RpcSystemClosing
51)
52
53type RpcResponse struct {
54 MType RpcMType
55 Err error
56 Reply *any.Any
57}
58
59func NewResponse(messageType RpcMType, err error, body *any.Any) *RpcResponse {
60 return &RpcResponse{
61 MType: messageType,
62 Err: err,
63 Reply: body,
64 }
65}
66
Scott Baker2c1c4822019-10-16 11:02:41 -070067// TODO: Remove and provide better may to get the device id
68// GetDeviceIdFromTopic extract the deviceId from the topic name. The topic name is formatted either as:
69// <any string> or <any string>_<deviceId>. The device Id is 24 characters long.
70func GetDeviceIdFromTopic(topic Topic) string {
71 pos := strings.LastIndex(topic.Name, TopicSeparator)
72 if pos == -1 {
73 return ""
74 }
75 adjustedPos := pos + len(TopicSeparator)
76 if adjustedPos >= len(topic.Name) {
77 return ""
78 }
79 deviceId := topic.Name[adjustedPos:len(topic.Name)]
80 if len(deviceId) != DeviceIdLength {
81 return ""
82 }
83 return deviceId
84}