Skip to content

Commit

Permalink
[cli][simulation] added 'add' parameter to load cmd; README updated w…
Browse files Browse the repository at this point in the history
…ith load/save commands.
  • Loading branch information
EskoDijk committed Feb 12, 2024
1 parent 44fd579 commit 53bd84b
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 3 deletions.
10 changes: 10 additions & 0 deletions cli/CmdRunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -1305,6 +1305,16 @@ func (rt *CmdRunner) executeLoad(cc *CommandContext, cmd *LoadCmd) {
cc.errorf("Error in YAML file: %v", err)
return
}
if len(cfgFile.NodesList) == 0 {
cc.errorf("No nodes defined in YAML file")
return
}

if cmd.Add != nil {
yamlMinNodeId := cfgFile.MinNodeId()
nodeIdOffset := sim.MaxNodeId() + 1 - yamlMinNodeId
cfgFile.NetworkConfig.BaseId = &nodeIdOffset
}

err = sim.ImportNodes(cfgFile.NetworkConfig, cfgFile.NodesList)
if err != nil {
Expand Down
32 changes: 31 additions & 1 deletion cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Python libraries use the CLI to manage simulations.
* [help](#help)
* [joins](#joins)
* [kpi](#kpi)
* [load](#load)
* [log](#log)
* [move](#move)
* [netinfo](#netinfo)
Expand All @@ -33,7 +34,8 @@ Python libraries use the CLI to manage simulations.
* [radio](#radio)
* [radiomodel](#radiomodel)
* [radioparam](#radioparam)
* [rxsens](#rxsens)
* [rfsim](#rfsim)
* [save](#save)
* [scan](#scan)
* [speed](#speed)
* [time](#time)
Expand Down Expand Up @@ -440,6 +442,22 @@ Done
NOTE: if any counters of nodes are reset using the OT CLI command `counters <type> reset` while KPI collection is ongoing,
the results of KPI collection will become incorrect.

### load

Load a network topology from a YAML file.

```shell
load "<filename.yaml>" [add]
```

If the optional `add` parameter is used, the node IDs as defined in the YAML file will be incremented as needed
to be higher than all current node IDs, and the new nodes will be added on top of nodes that are already there.
All nodes in the YAML file can also be position-shifted prior to loading by changing the `pos-shift` parameter
in the YAML file to a non-zero value. See [`save`](#save) for saving a network topology into a YAML file.

There is an example of the YAML format in the file `./pylibs/test_mesh_topology.yaml`.


### log
Inspect current log level, or set a new log level.

Expand Down Expand Up @@ -825,6 +843,18 @@ Done
>
```

### save

Save current network topology (nodes) into a YAML file.

```shell
save "<filename.yaml>"
```

Information about a node that will be saved in the file: type, position, and Thread version. Any
internal state like 802.15.4 addresses, IP addresses, routing information, flash, counters etc is not
saved. The saved YAML file can be loaded again with [`load`](#load)

### scan

Perform a network scan by the indicated node.
Expand Down
6 changes: 6 additions & 0 deletions cli/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,11 @@ type MaxSpeedFlag struct {
Dummy struct{} `( "max" | "inf")` //nolint
}

// noinspection GoVetStructTag
type AddFlag struct {
Dummy struct{} `"add"` //nolint
}

// noinspection GoVetStructTag
type CoapsCmd struct {
Cmd struct{} `"coaps"` //nolint
Expand Down Expand Up @@ -529,6 +534,7 @@ type KpiCmd struct {
type LoadCmd struct {
Cmd struct{} `"load"` //nolint
Filename string `@String` //nolint
Add *AddFlag `[ @@ ]` //nolint
}

// noinspection GoVetStructTag
Expand Down
11 changes: 11 additions & 0 deletions simulation/simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,17 @@ func (s *Simulation) GetNodes() []NodeId {
return keys
}

// MaxNodeId gets the largest Node Id of current nodes in the simulation.
func (s *Simulation) MaxNodeId() NodeId {
m := 0
for nid := range s.nodes {
if nid > m || m == 0 {
m = nid
}
}
return m
}

func (s *Simulation) AutoGo() bool {
return s.autoGo
}
Expand Down
8 changes: 6 additions & 2 deletions simulation/simulation_io.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,21 @@ func (s *Simulation) ExportNodes(nwConfig *YamlNetworkConfig) []YamlNodeConfig {

func (s *Simulation) ImportNodes(nwConfig YamlNetworkConfig, nodes []YamlNodeConfig) error {
allOk := true
rr := DefaultNodeConfig().RadioRange
rr := defaultRadioRange
if nwConfig.RadioRange != nil {
rr = *nwConfig.RadioRange
}
posOffset := nwConfig.Position
nodeIdOffset := 0
if nwConfig.BaseId != nil {
nodeIdOffset = *nwConfig.BaseId
}

for _, node := range nodes {
cfg := DefaultNodeConfig()

// fill config with entries from YAML 'node'
cfg.ID = node.ID
cfg.ID = node.ID + nodeIdOffset
if node.RadioRange != nil {
cfg.RadioRange = *node.RadioRange
} else {
Expand Down
13 changes: 13 additions & 0 deletions simulation/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import (
"fmt"
"io"
"regexp"

. "github.com/openthread/ot-ns/types"
)

var (
Expand Down Expand Up @@ -66,6 +68,7 @@ type YamlConfigFile struct {
type YamlNetworkConfig struct {
Position [3]int `yaml:"pos-shift,flow"` // provides an optional 3D position shift of all nodes.
RadioRange *int `yaml:"radio-range,omitempty"` // provides optional default radio-range.
BaseId *int `yaml:"base-id,omitempty"` // provides an optional node ID base (offset) for all nodes.
}

// YamlNodeConfig is a node config that can be loaded/saved in YAML.
Expand All @@ -76,3 +79,13 @@ type YamlNodeConfig struct {
Position [3]int `yaml:"pos,flow"`
RadioRange *int `yaml:"radio-range,omitempty"`
}

func (yc *YamlConfigFile) MinNodeId() NodeId {
var m NodeId = 0
for _, n := range yc.NodesList {
if n.ID < m || m == 0 {
m = n.ID
}
}
return m
}

0 comments on commit 53bd84b

Please sign in to comment.