This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 279
/
announcement.go
253 lines (223 loc) · 6.44 KB
/
announcement.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package announcement
import (
"encoding"
"fmt"
"reflect"
"strings"
"time"
pb "github.com/TheThingsNetwork/api/discovery"
"github.com/TheThingsNetwork/ttn/core/types"
"github.com/fatih/structs"
)
const currentDBVersion = "2.4.1"
// Metadata represents metadata that is stored with an Announcement
type Metadata interface {
encoding.TextMarshaler
ToProto() *pb.Metadata
}
// AppEUIMetadata is used to store an AppEUI
type AppEUIMetadata struct {
AppEUI types.AppEUI
}
// ToProto implements the Metadata interface
func (m AppEUIMetadata) ToProto() *pb.Metadata {
return &pb.Metadata{
Metadata: &pb.Metadata_AppEUI{
AppEUI: m.AppEUI.Bytes(),
},
}
}
// MarshalText implements the encoding.TextMarshaler interface
func (m AppEUIMetadata) MarshalText() ([]byte, error) {
return []byte(fmt.Sprintf("AppEUI %s", m.AppEUI)), nil
}
// AppIDMetadata is used to store an AppID
type AppIDMetadata struct {
AppID string
}
// ToProto implements the Metadata interface
func (m AppIDMetadata) ToProto() *pb.Metadata {
return &pb.Metadata{
Metadata: &pb.Metadata_AppID{
AppID: m.AppID,
},
}
}
// MarshalText implements the encoding.TextMarshaler interface
func (m AppIDMetadata) MarshalText() ([]byte, error) {
return []byte(fmt.Sprintf("AppID %s", m.AppID)), nil
}
// GatewayIDMetadata is used to store a GatewayID
type GatewayIDMetadata struct {
GatewayID string
}
// ToProto implements the Metadata interface
func (m GatewayIDMetadata) ToProto() *pb.Metadata {
return &pb.Metadata{
Metadata: &pb.Metadata_GatewayID{
GatewayID: m.GatewayID,
},
}
}
// MarshalText implements the encoding.TextMarshaler interface
func (m GatewayIDMetadata) MarshalText() ([]byte, error) {
return []byte(fmt.Sprintf("GatewayID %s", m.GatewayID)), nil
}
// PrefixMetadata is used to store a DevAddr prefix
type PrefixMetadata struct {
Prefix types.DevAddrPrefix
}
// ToProto implements the Metadata interface
func (m PrefixMetadata) ToProto() *pb.Metadata {
return &pb.Metadata{
Metadata: &pb.Metadata_DevAddrPrefix{
DevAddrPrefix: m.Prefix.Bytes(),
},
}
}
// MarshalText implements the encoding.TextMarshaler interface
func (m PrefixMetadata) MarshalText() ([]byte, error) {
return []byte(fmt.Sprintf("Prefix %s", m.Prefix)), nil
}
// MetadataFromProto converts a protocol buffer metadata to a Metadata
func MetadataFromProto(proto *pb.Metadata) Metadata {
if euiBytes := proto.GetAppEUI(); euiBytes != nil {
eui := new(types.AppEUI)
if err := eui.Unmarshal(euiBytes); err != nil {
return nil
}
return AppEUIMetadata{*eui}
}
if appID := proto.GetAppID(); appID != "" {
return AppIDMetadata{appID}
}
if prefixBytes := proto.GetDevAddrPrefix(); prefixBytes != nil {
prefix := new(types.DevAddrPrefix)
if err := prefix.Unmarshal(prefixBytes); err != nil {
return nil
}
return PrefixMetadata{*prefix}
}
if gatewayID := proto.GetGatewayID(); gatewayID != "" {
return GatewayIDMetadata{gatewayID}
}
return nil
}
// MetadataFromString converts a string to a Metadata
func MetadataFromString(str string) Metadata {
meta := strings.SplitAfterN(str, " ", 2)
key := strings.TrimSpace(meta[0])
value := meta[1]
switch key {
case "AppEUI":
var appEUI types.AppEUI
appEUI.UnmarshalText([]byte(value))
return AppEUIMetadata{appEUI}
case "AppID":
return AppIDMetadata{value}
case "GatewayID":
return GatewayIDMetadata{value}
case "Prefix":
prefix := &types.DevAddrPrefix{
Length: 32,
}
prefix.UnmarshalText([]byte(value))
return PrefixMetadata{*prefix}
}
return nil
}
// Announcement of a network component
type Announcement struct {
old *Announcement
ID string `redis:"id"`
ServiceName string `redis:"service_name"`
ServiceVersion string `redis:"service_version"`
Description string `redis:"description"`
URL string `redis:"url"`
Public bool `redis:"public"`
NetAddress string `redis:"net_address"`
PublicKey string `redis:"public_key"`
Certificate string `redis:"certificate"`
APIAddress string `redis:"api_address"`
MQTTAddress string `redis:"mqtt_address"`
AMQPAddress string `redis:"amqp_address"`
Metadata []Metadata
CreatedAt time.Time `redis:"created_at"`
UpdatedAt time.Time `redis:"updated_at"`
}
// StartUpdate stores the state of the announcement
func (a *Announcement) StartUpdate() {
old := *a
a.old = &old
}
// DBVersion of the model
func (a *Announcement) DBVersion() string {
return currentDBVersion
}
// ChangedFields returns the names of the changed fields since the last call to StartUpdate
func (a Announcement) ChangedFields() (changed []string) {
new := structs.New(a)
fields := new.Names()
if a.old == nil {
return fields
}
old := structs.New(*a.old)
for _, field := range new.Fields() {
if !field.IsExported() || field.Name() == "old" {
continue
}
if !reflect.DeepEqual(field.Value(), old.Field(field.Name()).Value()) {
changed = append(changed, field.Name())
}
}
if len(changed) == 1 && changed[0] == "UpdatedAt" {
return []string{}
}
return
}
// ToProto converts the Announcement to a protobuf Announcement
func (a Announcement) ToProto() *pb.Announcement {
metadata := make([]*pb.Metadata, 0, len(a.Metadata))
for _, meta := range a.Metadata {
metadata = append(metadata, meta.ToProto())
}
return &pb.Announcement{
ID: a.ID,
ServiceName: a.ServiceName,
ServiceVersion: a.ServiceVersion,
Description: a.Description,
Url: a.URL,
Public: a.Public,
NetAddress: a.NetAddress,
PublicKey: a.PublicKey,
Certificate: a.Certificate,
ApiAddress: a.APIAddress,
MqttAddress: a.MQTTAddress,
AmqpAddress: a.AMQPAddress,
Metadata: metadata,
}
}
// FromProto converts an Announcement protobuf to an Announcement
func FromProto(a *pb.Announcement) Announcement {
metadata := make([]Metadata, 0, len(a.Metadata))
for _, meta := range a.Metadata {
metadata = append(metadata, MetadataFromProto(meta))
}
return Announcement{
ID: a.ID,
ServiceName: a.ServiceName,
ServiceVersion: a.ServiceVersion,
Description: a.Description,
URL: a.Url,
Public: a.Public,
NetAddress: a.NetAddress,
PublicKey: a.PublicKey,
Certificate: a.Certificate,
APIAddress: a.ApiAddress,
MQTTAddress: a.MqttAddress,
AMQPAddress: a.AmqpAddress,
Metadata: metadata,
}
}