-
Notifications
You must be signed in to change notification settings - Fork 311
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ns: Add RelayConfigurationService WIP
- Loading branch information
1 parent
3d20e63
commit a844491
Showing
5 changed files
with
318 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,287 @@ | ||
// Copyright © 2023 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 networkserver | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"go.thethings.network/lorawan-stack/v3/pkg/auth/rights" | ||
"go.thethings.network/lorawan-stack/v3/pkg/band" | ||
"go.thethings.network/lorawan-stack/v3/pkg/errors" | ||
"go.thethings.network/lorawan-stack/v3/pkg/frequencyplans" | ||
. "go.thethings.network/lorawan-stack/v3/pkg/networkserver/internal" | ||
"go.thethings.network/lorawan-stack/v3/pkg/ttnpb" | ||
) | ||
|
||
var ( | ||
errRelayAlreadyExists = errors.DefineAlreadyExists("relay_already_exists", "relay already exists") | ||
errRelayNotFound = errors.DefineNotFound("relay_not_found", "relay not found") | ||
) | ||
|
||
func validateRelaySecondChannel(secondCh *ttnpb.RelaySecondChannel, phy *band.Band, path ...string) error { | ||
if secondCh == nil { | ||
return nil | ||
} | ||
if _, ok := phy.DataRates[secondCh.DataRateIndex]; !ok { | ||
return newInvalidFieldValueError(strings.Join(append(path, "data_rate_index"), ".")) | ||
} | ||
inSubBand := false | ||
for _, sb := range phy.SubBands { | ||
if sb.MinFrequency >= secondCh.Frequency && secondCh.Frequency <= sb.MaxFrequency { | ||
inSubBand = true | ||
break | ||
} | ||
} | ||
if !inSubBand { | ||
return newInvalidFieldValueError(strings.Join(append(path, "frequency"), ".")) | ||
} | ||
return nil | ||
} | ||
|
||
func validateRelayConfigurationServed(served *ttnpb.RelayConfiguration_Served, phy *band.Band, path ...string) error { | ||
if served == nil { | ||
return nil | ||
} | ||
if err := validateRelaySecondChannel(served.SecondChannel, phy, append(path, "second_channel")...); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func validateRelayConfigurationServing(serving *ttnpb.RelayConfiguration_Serving, phy *band.Band, path ...string) error { | ||
if serving == nil { | ||
return nil | ||
} | ||
if err := validateRelaySecondChannel(serving.SecondChannel, phy, append(path, "second_channel")...); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func validateRelayConfiguration(conf *ttnpb.RelayConfiguration, phy *band.Band, path ...string) error { | ||
if conf == nil { | ||
return nil | ||
} | ||
switch mode := conf.Mode.(type) { | ||
case *ttnpb.RelayConfiguration_Served_: | ||
return validateRelayConfigurationServed(mode.Served, phy, append(path, "mode", "served")...) | ||
case *ttnpb.RelayConfiguration_Serving_: | ||
return validateRelayConfigurationServing(mode.Serving, phy, append(path, "mode", "serving")...) | ||
case nil: | ||
return nil | ||
default: | ||
panic(fmt.Sprintf("unknown mode %T", mode)) | ||
} | ||
} | ||
|
||
func relayParametersFromConfiguration(conf *ttnpb.RelayConfiguration) *ttnpb.RelayParameters { | ||
if conf == nil { | ||
return nil | ||
} | ||
switch mode := conf.Mode.(type) { | ||
case *ttnpb.RelayConfiguration_Served_: | ||
served := &ttnpb.ServedRelayParameters{ | ||
Backoff: mode.Served.Backoff, | ||
SecondChannel: mode.Served.SecondChannel, | ||
ServingDeviceId: mode.Served.ServingDeviceId, | ||
} | ||
switch mode := mode.Served.Mode.(type) { | ||
case *ttnpb.RelayConfiguration_Served_Always: | ||
served.Mode = &ttnpb.ServedRelayParameters_Always{ | ||
Always: mode.Always, | ||
} | ||
case *ttnpb.RelayConfiguration_Served_Dynamic: | ||
served.Mode = &ttnpb.ServedRelayParameters_Dynamic{ | ||
Dynamic: mode.Dynamic, | ||
} | ||
case *ttnpb.RelayConfiguration_Served_EndDeviceControlled: | ||
served.Mode = &ttnpb.ServedRelayParameters_EndDeviceControlled{ | ||
EndDeviceControlled: mode.EndDeviceControlled, | ||
} | ||
case nil: | ||
default: | ||
panic(fmt.Sprintf("unknown mode %T", mode)) | ||
} | ||
return &ttnpb.RelayParameters{ | ||
Mode: &ttnpb.RelayParameters_Served{ | ||
Served: served, | ||
}, | ||
} | ||
case *ttnpb.RelayConfiguration_Serving_: | ||
return &ttnpb.RelayParameters{ | ||
Mode: &ttnpb.RelayParameters_Serving{ | ||
Serving: &ttnpb.ServingRelayParameters{ | ||
SecondChannel: mode.Serving.SecondChannel, | ||
DefaultChannelIndex: mode.Serving.DefaultChannelIndex, | ||
CadPeriodicity: mode.Serving.CadPeriodicity, | ||
Limits: mode.Serving.Limits, | ||
}, | ||
}, | ||
} | ||
case nil: | ||
return &ttnpb.RelayParameters{} | ||
default: | ||
panic(fmt.Sprintf("unknown mode %T", mode)) | ||
} | ||
} | ||
|
||
func relayConfigurationFromParameters(params *ttnpb.RelayParameters) *ttnpb.RelayConfiguration { | ||
if params == nil { | ||
return nil | ||
} | ||
switch mode := params.Mode.(type) { | ||
case *ttnpb.RelayParameters_Served: | ||
served := &ttnpb.RelayConfiguration_Served{ | ||
Backoff: mode.Served.Backoff, | ||
SecondChannel: mode.Served.SecondChannel, | ||
ServingDeviceId: mode.Served.ServingDeviceId, | ||
} | ||
switch mode := mode.Served.Mode.(type) { | ||
case *ttnpb.ServedRelayParameters_Always: | ||
served.Mode = &ttnpb.RelayConfiguration_Served_Always{ | ||
Always: mode.Always, | ||
} | ||
case *ttnpb.ServedRelayParameters_Dynamic: | ||
served.Mode = &ttnpb.RelayConfiguration_Served_Dynamic{ | ||
Dynamic: mode.Dynamic, | ||
} | ||
case *ttnpb.ServedRelayParameters_EndDeviceControlled: | ||
served.Mode = &ttnpb.RelayConfiguration_Served_EndDeviceControlled{ | ||
EndDeviceControlled: mode.EndDeviceControlled, | ||
} | ||
case nil: | ||
default: | ||
panic(fmt.Sprintf("unknown mode %T", mode)) | ||
} | ||
return &ttnpb.RelayConfiguration{ | ||
Mode: &ttnpb.RelayConfiguration_Served_{ | ||
Served: served, | ||
}, | ||
} | ||
case *ttnpb.RelayParameters_Serving: | ||
return &ttnpb.RelayConfiguration{ | ||
Mode: &ttnpb.RelayConfiguration_Serving_{ | ||
Serving: &ttnpb.RelayConfiguration_Serving{ | ||
SecondChannel: mode.Serving.SecondChannel, | ||
DefaultChannelIndex: mode.Serving.DefaultChannelIndex, | ||
CadPeriodicity: mode.Serving.CadPeriodicity, | ||
Limits: mode.Serving.Limits, | ||
}, | ||
}, | ||
} | ||
case nil: | ||
return &ttnpb.RelayConfiguration{} | ||
default: | ||
panic(fmt.Sprintf("unknown mode %T", mode)) | ||
} | ||
} | ||
|
||
type nsRelayConfigurationService struct { | ||
ttnpb.UnimplementedNsRelayConfigurationServiceServer | ||
|
||
devices DeviceRegistry | ||
frequencyPlans func(context.Context) (*frequencyplans.Store, error) | ||
} | ||
|
||
// CreateRelay implements ttnpb.NsRelayConfigurationServiceServer. | ||
func (s *nsRelayConfigurationService) CreateRelay( | ||
ctx context.Context, req *ttnpb.CreateRelayRequest, | ||
) (*ttnpb.CreateRelayResponse, error) { | ||
if err := rights.RequireApplication( | ||
ctx, req.EndDeviceIds.ApplicationIds, ttnpb.Right_RIGHT_APPLICATION_DEVICES_WRITE, | ||
); err != nil { | ||
return nil, err | ||
} | ||
fps, err := s.frequencyPlans(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if _, ctx, err := s.devices.SetByID( | ||
ctx, | ||
req.EndDeviceIds.ApplicationIds, | ||
req.EndDeviceIds.DeviceId, | ||
[]string{ | ||
"frequency_plan_id", | ||
"lorawan_phy_version", | ||
"mac_settings.desired_relay", | ||
"mac_state.desired_parameters", | ||
"pending_mac_state.desired_parameters", | ||
}, | ||
func(ctx context.Context, dev *ttnpb.EndDevice) (*ttnpb.EndDevice, []string, error) { | ||
if dev == nil { | ||
return nil, nil, errDeviceNotFound.New() | ||
} | ||
if dev.MacSettings.GetDesiredRelay() != nil { | ||
return nil, nil, errRelayAlreadyExists.New() | ||
} | ||
phy, err := DeviceBand(dev, fps) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
if err := validateRelayConfiguration(req.Configuration, phy, "configuration"); err != nil { | ||
return nil, nil, err | ||
} | ||
parameters := relayParametersFromConfiguration(req.Configuration) | ||
dev.MacSettings = &ttnpb.MACSettings{DesiredRelay: parameters} | ||
paths := []string{"mac_settings.desired_relay"} | ||
for path, desiredParameters := range map[string]*ttnpb.MACParameters{ | ||
"mac_state.desired_parameters.relay": dev.MacState.GetDesiredParameters(), | ||
"pending_mac_state.desired_parameters.relay": dev.PendingMacState.GetDesiredParameters(), | ||
} { | ||
if desiredParameters == nil { | ||
continue | ||
} | ||
desiredParameters.Relay = parameters | ||
paths = ttnpb.AddFields(paths, path) | ||
} | ||
return dev, paths, nil | ||
}, | ||
); err != nil { | ||
logRegistryRPCError(ctx, err, "Failed to create relay") | ||
return nil, err | ||
} | ||
return &ttnpb.CreateRelayResponse{ | ||
Configuration: req.Configuration, | ||
}, nil | ||
} | ||
|
||
// GetRelay implements ttnpb.NsRelayConfigurationServiceServer. | ||
func (s *nsRelayConfigurationService) GetRelay( | ||
ctx context.Context, req *ttnpb.GetRelayRequest, | ||
) (*ttnpb.GetRelayResponse, error) { | ||
if err := rights.RequireApplication( | ||
ctx, req.EndDeviceIds.ApplicationIds, ttnpb.Right_RIGHT_APPLICATION_DEVICES_READ, | ||
); err != nil { | ||
return nil, err | ||
} | ||
dev, ctx, err := s.devices.GetByID( | ||
ctx, | ||
req.EndDeviceIds.ApplicationIds, | ||
req.EndDeviceIds.DeviceId, | ||
ttnpb.FieldsWithPrefix("mac_settings.desired_relay", req.FieldMask.GetPaths()...), | ||
) | ||
if err != nil { | ||
logRegistryRPCError(ctx, err, "Failed to get relay") | ||
return nil, err | ||
} | ||
if dev.MacSettings.GetDesiredRelay() == nil { | ||
return nil, errRelayNotFound.New() | ||
} | ||
return &ttnpb.GetRelayResponse{ | ||
Configuration: relayConfigurationFromParameters(dev.MacSettings.DesiredRelay), | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.