Skip to content

Commit

Permalink
Output integrationUISProutes.template.csv while building the tree.
Browse files Browse the repository at this point in the history
  • Loading branch information
thebracket committed Jan 14, 2025
1 parent 7022a9d commit 0dbf0ef
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/rust/uisp_integration/src/strategies/full/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ pub async fn build_full_network(
squash_squashed_sites(&mut sites, config.clone(), &root_site)?;

// Build Path Weights
walk_tree_for_routing(&mut sites, &root_site, &routing_overrides)?;
walk_tree_for_routing(config.clone(), &mut sites, &root_site, &routing_overrides)?;

// Print Sites
if let Some(root_idx) = sites.iter().position(|s| s.name == root_site) {
Expand Down
12 changes: 12 additions & 0 deletions src/rust/uisp_integration/src/strategies/full/routes_override.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use csv::ReaderBuilder;
use lqos_config::Config;
use serde::{Deserialize, Serialize};
use std::path::Path;
use std::sync::Arc;
use tracing::{error, info};

/// Represents a route override in the integrationUISProutes.csv file.
Expand Down Expand Up @@ -82,3 +83,14 @@ pub fn get_route_overrides(config: &Config) -> Result<Vec<RouteOverride>, UispIn
Ok(Vec::new())
}
}

pub fn write_routing_overrides_template(config: Arc<Config>, natural_routes: &[RouteOverride]) -> anyhow::Result<()> {
let file_path = Path::new(&config.lqos_directory).join("integrationUISProutes.template.csv");
let mut writer = csv::Writer::from_path(file_path)?;
writer.write_record(&["From Site", "To Site", "Cost"])?;
for route in natural_routes {
writer.write_record(&[&route.from_site, &route.to_site, &route.cost.to_string()])?;
}
writer.flush()?;
Ok(())
}
23 changes: 19 additions & 4 deletions src/rust/uisp_integration/src/strategies/full/tree_walk.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::io::Write;
use std::sync::Arc;
use lqos_config::Config;
use crate::errors::UispIntegrationError;
use crate::strategies::full::routes_override::RouteOverride;
use crate::strategies::full::routes_override::{write_routing_overrides_template, RouteOverride};
use crate::uisp_types::{UispSite, UispSiteType};

/// Walks the tree to determine the best route for each site
Expand All @@ -12,22 +14,29 @@ use crate::uisp_types::{UispSite, UispSiteType};
/// * `root_site` - The name of the root site
/// * `overrides` - The list of route overrides
pub fn walk_tree_for_routing(
config: Arc<Config>,
sites: &mut Vec<UispSite>,
root_site: &str,
overrides: &Vec<RouteOverride>,
) -> Result<(), UispIntegrationError> {
if let Some(root_idx) = sites.iter().position(|s| s.name == root_site) {
let mut visited = std::collections::HashSet::new();
let current_node = root_idx;
let mut natural_weights: Vec<RouteOverride> = Vec::new();
let mut dot_graph = "digraph G {\n graph [ ranksep=2.0 overlap=false ]".to_string();
walk_node(current_node, 10, sites, &mut visited, overrides, &mut dot_graph);
walk_node(current_node, 10, sites, &mut visited, overrides, &mut dot_graph, &mut natural_weights);
dot_graph.push_str("}\n");
{
let graph_file = std::fs::File::create("graph.dot");
if let Ok(mut file) = graph_file {
let _ = file.write_all(dot_graph.as_bytes());
}
}
if let Err(e) = write_routing_overrides_template(config, &natural_weights) {
tracing::error!("Unable to write routing overrides template: {:?}", e);
} else {
tracing::info!("Wrote routing overrides template");
}
} else {
tracing::error!("Unable to build a path-weights graph because I can't find the root node");
return Err(UispIntegrationError::NoRootSite);
Expand All @@ -52,6 +61,7 @@ fn walk_node(
visited: &mut std::collections::HashSet<usize>,
overrides: &Vec<RouteOverride>,
dot_graph: &mut String,
natural_weights: &mut Vec<RouteOverride>,
) {
if visited.contains(&idx) {
return;
Expand All @@ -61,9 +71,14 @@ fn walk_node(
if sites[i].parent_indices.contains(&idx) {
let from = sites[i].name.clone();
let to = sites[idx].name.clone();
if sites[idx].site_type != UispSiteType::Client
if sites[idx].site_type != UispSiteType::Client && sites[i].site_type != UispSiteType::Client
{
dot_graph.push_str(&format!("\"{}\" [label=\"{}\"];\n", to, to));
natural_weights.push(RouteOverride {
from_site: from.clone(),
to_site: to.clone(),
cost: weight,
});
}
if let Some(route_override) = overrides
.iter()
Expand All @@ -74,7 +89,7 @@ fn walk_node(
} else {
sites[i].route_weights.push((idx, weight));
}
walk_node(i, weight + 10, sites, visited, overrides, dot_graph);
walk_node(i, weight + 10, sites, visited, overrides, dot_graph, natural_weights);
}
}
}

0 comments on commit 0dbf0ef

Please sign in to comment.