Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [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 | package kafka |
| 17 | |
Matteo Scandolo | ed12882 | 2020-02-10 15:52:35 -0800 | [diff] [blame] | 18 | import ( |
| 19 | "github.com/golang/protobuf/ptypes/any" |
| 20 | "strings" |
| 21 | ) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 22 | |
| 23 | const ( |
| 24 | TopicSeparator = "_" |
| 25 | DeviceIdLength = 24 |
| 26 | ) |
| 27 | |
| 28 | // A Topic definition - may be augmented with additional attributes eventually |
| 29 | type 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 | |
| 37 | type KVArg struct { |
| 38 | Key string |
| 39 | Value interface{} |
| 40 | } |
| 41 | |
Matteo Scandolo | ed12882 | 2020-02-10 15:52:35 -0800 | [diff] [blame] | 42 | type RpcMType int |
| 43 | |
| 44 | const ( |
| 45 | RpcFormattingError RpcMType = iota |
| 46 | RpcSent |
| 47 | RpcReply |
| 48 | RpcTimeout |
| 49 | RpcTransportError |
| 50 | RpcSystemClosing |
| 51 | ) |
| 52 | |
| 53 | type RpcResponse struct { |
| 54 | MType RpcMType |
| 55 | Err error |
| 56 | Reply *any.Any |
| 57 | } |
| 58 | |
| 59 | func 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 Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 67 | // 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. |
| 70 | func 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 | } |