slim_controller/
config.rs

1// Copyright AGNTCY Contributors (https://github.com/agntcy)
2// SPDX-License-Identifier: Apache-2.0
3
4use std::sync::Arc;
5
6use serde::Deserialize;
7
8use slim_config::component::configuration::{Configuration, ConfigurationError};
9use slim_config::component::id::ID;
10use slim_config::grpc::client::ClientConfig;
11use slim_config::grpc::server::ServerConfig;
12use slim_datapath::message_processing::MessageProcessor;
13
14use crate::service::ControlPlane;
15
16/// Configuration for the Control-Plane / Data-Plane component
17#[derive(Debug, Clone, Deserialize, Default)]
18pub struct Config {
19    /// Controller GRPC server settings
20    #[serde(default)]
21    pub servers: Vec<ServerConfig>,
22
23    /// Controller client config to connect to control plane
24    #[serde(default)]
25    pub clients: Vec<ClientConfig>,
26}
27
28impl Config {
29    /// Create a new Config instance with default values
30    pub fn new() -> Self {
31        Self::default()
32    }
33
34    /// Create a new Config instance with the given servers
35    pub fn with_servers(self, servers: Vec<ServerConfig>) -> Self {
36        Self { servers, ..self }
37    }
38
39    /// Create a new Config instance with the given clients
40    pub fn with_clients(self, clients: Vec<ClientConfig>) -> Self {
41        Self { clients, ..self }
42    }
43
44    /// Get the list of server configurations
45    pub fn servers(&self) -> &[ServerConfig] {
46        &self.servers
47    }
48    /// Get the list of client configurations
49    pub fn clients(&self) -> &[ClientConfig] {
50        &self.clients
51    }
52
53    /// Create a ControlPlane service instance from this configuration
54    pub fn into_service(
55        &self,
56        id: ID,
57        rx_drain: drain::Watch,
58        message_processor: Arc<MessageProcessor>,
59    ) -> ControlPlane {
60        ControlPlane::new(
61            id,
62            self.servers.clone(),
63            self.clients.clone(),
64            rx_drain,
65            message_processor,
66        )
67    }
68}
69
70impl Configuration for Config {
71    fn validate(&self) -> Result<(), ConfigurationError> {
72        // Validate client and server configurations
73        for server in self.servers.iter() {
74            server.validate()?;
75        }
76
77        for client in &self.clients {
78            client.validate()?;
79        }
80
81        Ok(())
82    }
83}