khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [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 | |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 18 | import "strings" |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 19 | |
| 20 | const ( |
| 21 | TopicSeparator = "_" |
| 22 | DeviceIdLength = 24 |
| 23 | ) |
| 24 | |
| 25 | // A Topic definition - may be augmented with additional attributes eventually |
| 26 | type Topic struct { |
| 27 | // The name of the topic. It must start with a letter, |
| 28 | // and contain only letters (`[A-Za-z]`), numbers (`[0-9]`), dashes (`-`), |
| 29 | // underscores (`_`), periods (`.`), tildes (`~`), plus (`+`) or percent |
| 30 | // signs (`%`). |
| 31 | Name string |
| 32 | } |
| 33 | |
| 34 | type KVArg struct { |
| 35 | Key string |
| 36 | Value interface{} |
| 37 | } |
| 38 | |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 39 | // TODO: Remove and provide better may to get the device id |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 40 | // GetDeviceIdFromTopic extract the deviceId from the topic name. The topic name is formatted either as: |
| 41 | // <any string> or <any string>_<deviceId>. The device Id is 24 characters long. |
| 42 | func GetDeviceIdFromTopic(topic Topic) string { |
| 43 | pos := strings.LastIndex(topic.Name, TopicSeparator) |
| 44 | if pos == -1 { |
| 45 | return "" |
| 46 | } |
| 47 | adjustedPos := pos + len(TopicSeparator) |
| 48 | if adjustedPos >= len(topic.Name) { |
| 49 | return "" |
| 50 | } |
| 51 | deviceId := topic.Name[adjustedPos:len(topic.Name)] |
| 52 | if len(deviceId) != DeviceIdLength { |
| 53 | return "" |
| 54 | } |
| 55 | return deviceId |
| 56 | } |