-
Notifications
You must be signed in to change notification settings - Fork 302
/
server.go
149 lines (132 loc) · 4.61 KB
/
server.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
// Copyright © 2019 The Things Network Foundation, The Things Industries B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mock
import (
"context"
"fmt"
"strings"
"sync"
"go.thethings.network/lorawan-stack/v3/pkg/auth/rights"
"go.thethings.network/lorawan-stack/v3/pkg/component"
"go.thethings.network/lorawan-stack/v3/pkg/errors"
"go.thethings.network/lorawan-stack/v3/pkg/frequencyplans"
"go.thethings.network/lorawan-stack/v3/pkg/gatewayserver/io"
"go.thethings.network/lorawan-stack/v3/pkg/ttnpb"
"go.thethings.network/lorawan-stack/v3/pkg/unique"
"go.thethings.network/lorawan-stack/v3/pkg/util/test"
)
type server struct {
*component.Component
store *frequencyplans.Store
gateways map[string]*ttnpb.Gateway
connections map[string]*io.Connection
connectionsCh chan *io.Connection
downlinkClaims sync.Map
}
// Server represents a testing io.Server.
type Server interface {
io.Server
HasDownlinkClaim(context.Context, ttnpb.GatewayIdentifiers) bool
RegisterGateway(ctx context.Context, ids ttnpb.GatewayIdentifiers, gateway *ttnpb.Gateway)
GetConnection(ctx context.Context, ids ttnpb.GatewayIdentifiers) *io.Connection
Connections() <-chan *io.Connection
}
// NewServer instantiates a new Server.
func NewServer(c *component.Component) Server {
return &server{
Component: c,
store: frequencyplans.NewStore(test.FrequencyPlansFetcher),
gateways: make(map[string]*ttnpb.Gateway),
connections: make(map[string]*io.Connection),
connectionsCh: make(chan *io.Connection, 10),
}
}
// FillContext implements io.Server.
func (s *server) FillGatewayContext(ctx context.Context, ids ttnpb.GatewayIdentifiers) (context.Context, ttnpb.GatewayIdentifiers, error) {
ctx = s.FillContext(ctx)
if ids.IsZero() {
return nil, ttnpb.GatewayIdentifiers{}, errors.New("the identifiers are zero")
}
if ids.GatewayID != "" {
return ctx, ids, nil
}
ids.GatewayID = fmt.Sprintf("eui-%v", strings.ToLower(ids.EUI.String()))
return ctx, ids, nil
}
// Connect implements io.Server.
func (s *server) Connect(ctx context.Context, frontend io.Frontend, ids ttnpb.GatewayIdentifiers) (*io.Connection, error) {
if err := rights.RequireGateway(ctx, ids, ttnpb.RIGHT_GATEWAY_LINK); err != nil {
return nil, err
}
gtw, ok := s.gateways[unique.ID(ctx, ids)]
if !ok {
gtw = &ttnpb.Gateway{
GatewayIdentifiers: ids,
FrequencyPlanID: test.EUFrequencyPlanID,
}
}
conn, err := io.NewConnection(ctx, frontend, gtw, s.FrequencyPlans, true, nil)
if err != nil {
return nil, err
}
s.connections[unique.ID(ctx, ids)] = conn
select {
case s.connectionsCh <- conn:
default:
}
return conn, nil
}
// GetFrequencyPlans implements io.Server.
func (s *server) GetFrequencyPlans(ctx context.Context, ids ttnpb.GatewayIdentifiers) (map[string]*frequencyplans.FrequencyPlan, error) {
var fpID string
if gtw, ok := s.gateways[unique.ID(ctx, ids)]; ok {
fpID = gtw.FrequencyPlanID
} else {
fpID = test.EUFrequencyPlanID
}
fp, err := s.store.GetByID(fpID)
if err != nil {
return nil, err
}
fps := make(map[string]*frequencyplans.FrequencyPlan)
fps[fpID] = fp
return fps, nil
}
// ClaimDownlink implements io.Server.
func (s *server) ClaimDownlink(ctx context.Context, ids ttnpb.GatewayIdentifiers) error {
s.downlinkClaims.Store(unique.ID(ctx, ids), true)
return nil
}
// UnclaimDownlink implements io.Server.
func (s *server) UnclaimDownlink(ctx context.Context, ids ttnpb.GatewayIdentifiers) error {
s.downlinkClaims.Delete(unique.ID(ctx, ids))
return nil
}
func (s *server) HasDownlinkClaim(ctx context.Context, ids ttnpb.GatewayIdentifiers) bool {
_, ok := s.downlinkClaims.Load(unique.ID(ctx, ids))
return ok
}
func (s *server) RegisterGateway(ctx context.Context, ids ttnpb.GatewayIdentifiers, gateway *ttnpb.Gateway) {
uid := unique.ID(ctx, ids)
if len(gateway.FrequencyPlanIDs) > 0 {
gateway.FrequencyPlanID = gateway.FrequencyPlanIDs[0]
}
s.gateways[uid] = gateway
}
func (s *server) GetConnection(ctx context.Context, ids ttnpb.GatewayIdentifiers) *io.Connection {
return s.connections[unique.ID(ctx, ids)]
}
func (s *server) Connections() <-chan *io.Connection {
return s.connectionsCh
}