Skip to content

Commit

Permalink
chore: get netgen bin setup ready for cli handling
Browse files Browse the repository at this point in the history
Introduce clap to netgen for CLI args handling
add default configs for plugins
add startup config for holo

Signed-off-by: Paul Wekesa <paul1tw1@gmail.com>
  • Loading branch information
Paul-weqe committed Jan 3, 2025
1 parent e63a108 commit 4096ad2
Show file tree
Hide file tree
Showing 11 changed files with 375 additions and 59 deletions.
110 changes: 110 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ libc = "0.2.164"
enum-as-inner = "0.6.1"
ipnetwork = "0.20.0"
rand = "0.8.5"
clap = {version="4.5.23", features = ["default", "cargo"]}
19 changes: 19 additions & 0 deletions assets/rt1.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
!
interfaces interface eth-sw1
type iana-if-type:ethernetCsmacd
!
ipv4 vrrp vrrp-instance 1
log-state-change true
preempt enabled false
advertise-interval-sec 1
priority 10
!
virtual-ipv4-addresses virtual-ipv4-address 10.0.1.5
!
interfaces interface lo
type iana-if-type:softwareLoopback
ipv4
!
ipv4 address 1.1.1.1
prefix-length 32
!
19 changes: 19 additions & 0 deletions assets/rt2.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
!
interfaces interface eth-sw1
type iana-if-type:ethernetCsmacd
!
ipv4 vrrp vrrp-instance 1
log-state-change true
preempt enabled false
advertise-interval-sec 1
priority 20
!
virtual-ipv4-addresses virtual-ipv4-address 10.0.1.5
!
interfaces interface lo
type iana-if-type:softwareLoopback
ipv4
!
ipv4 address 2.2.2.2
prefix-length 32
!
19 changes: 19 additions & 0 deletions assets/rt3.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
!
interfaces interface eth-sw1
type iana-if-type:ethernetCsmacd
!
ipv4 vrrp vrrp-instance 1
log-state-change true
preempt enabled false
advertise-interval-sec 1
priority 30
!
virtual-ipv4-addresses virtual-ipv4-address 10.0.1.5
!
interfaces interface lo
type iana-if-type:softwareLoopback
ipv4
!
ipv4 address 3.3.3.3
prefix-length 32
!
4 changes: 3 additions & 1 deletion assets/sample-top.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
routers:
rt1:
plugin: holo
startup-config: ./assets/rt1.conf
interfaces:
lo:
ipv4:
- 1.1.1.1/32

eth-sw1:
ipv4:
- 10.0.1.1/24


rt2:
plugin: holo
startup-config: ./assets/rt2.conf
interfaces:
lo:
ipv4:
Expand All @@ -23,6 +24,7 @@ routers:

rt3:
plugin: holo
startup-config: ./assets/rt3.conf
interfaces:
lo:
ipv4:
Expand Down
62 changes: 47 additions & 15 deletions src/bin/netgen.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,56 @@
#![feature(let_chains)]
use netgen::plugins::Config;
use netgen::topology::Topology;
use netgen::{error::Error, Result};

use std::fs::File;

use clap::{command, Arg};

#[tokio::main]
async fn main() {
let mut topology: Topology = Topology::new();
if let Ok(mut config_file) = File::open("./assets/config.yml")
&& let Ok(mut topo_file) = File::open("./assets/sample-top.yml")
{
// load the base configuration
let config = match Config::from_yaml_file(&mut config_file) {
Ok(config) => Some(config),
Err(_err) => None,
};

// load the topology configuration
topology = Topology::from_yaml_file(&mut topo_file, config).unwrap();
}
async fn main() -> Result<()> {
let matches = command!("netgen")
.arg(
Arg::new("Config File")
.short('c')
.long("config")
.value_name("yaml-file")
.help("the file with plugin configs"),
)
.arg(
Arg::new("Topo File")
.short('t')
.long("topo")
.value_name("yaml-file")
.help("file with the topology"),
)
.get_matches();
let config = match matches.get_one::<String>("Config File") {
Some(config_file) => {
let mut config_file = File::open(config_file).unwrap();
Config::from_yaml_file(Some(&mut config_file))?
}
None => Config::from_yaml_file(None)?,
};

let topo_file = match matches.get_one::<String>("Topo File") {
Some(topo_file) => topo_file,
None => {
return Err(Error::GeneralError(String::from(
"topolofy file not configured",
)))
}
};

let mut topo_file = File::open(topo_file).unwrap();
let mut topology = Topology::from_yaml_file(&mut topo_file, config).unwrap();

// "powers on" all the devices and sets up all the required links
topology.power_on().await.unwrap();
topology.power_on().await?;

// runs the plugins in the routers.
// and initiates their startup-config (if present)
topology.run().await?;

Ok(())
}
Loading

0 comments on commit 4096ad2

Please sign in to comment.