blob: 0cb9535d274df0f6f5b46ba38ccd2080abdce934 [file] [log] [blame]
khenaidoo43c82122018-11-22 18:38:28 -05001/*
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
khenaidoo54e0ddf2019-02-27 16:21:33 -050018import "strings"
khenaidoo43c82122018-11-22 18:38:28 -050019
20const (
21 TopicSeparator = "_"
22 DeviceIdLength = 24
23)
24
25// A Topic definition - may be augmented with additional attributes eventually
26type 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
34type KVArg struct {
35 Key string
36 Value interface{}
37}
38
khenaidoo54e0ddf2019-02-27 16:21:33 -050039// TODO: Remove and provide better may to get the device id
khenaidoo43c82122018-11-22 18:38:28 -050040// 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.
42func 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}