Skip to content

Commit

Permalink
port over config improvements for migrated adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
wilyle committed Sep 29, 2023
1 parent 385d0dc commit fef3535
Show file tree
Hide file tree
Showing 14 changed files with 72 additions and 57 deletions.
31 changes: 18 additions & 13 deletions Cargo.lock

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

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ ibeji-adapter = { path = "freyja_adapters/digital_twin/ibeji_adapter" }

# ESDV dependencies
# Versioning is managed by the Cargo.lock file rather than copying rev properties to all of these

## Ibeji
core-protobuf-data-access = { git = "https://github.com/eclipse-ibeji/ibeji" }

## Freyja
freyja = { git = "https://github.com/eclipse-ibeji/freyja" }
freyja-build-common = { git = "https://github.com/eclipse-ibeji/freyja" }
freyja-common = { git = "https://github.com/eclipse-ibeji/freyja" }
Expand All @@ -31,6 +35,8 @@ in-memory-mock-cloud-adapter = { git = "https://github.com/eclipse-ibeji/freyja"
in-memory-mock-digital-twin-adapter = { git = "https://github.com/eclipse-ibeji/freyja" }
in-memory-mock-mapping-client = { git = "https://github.com/eclipse-ibeji/freyja" }
proc-macros = { git = "https://github.com/eclipse-ibeji/freyja" }

## Chariott
service_discovery_proto = { git = "https://github.com/eclipse-chariott/chariott" }

# Cargo dependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ tempfile = { workspace = true }
tonic = { workspace = true }
tokio = { workspace = true }
tokio-stream = { workspace = true }
tower = { workspace = true }
tower = { workspace = true }

[build-dependencies]
freyja-build-common = { workspace = true }
12 changes: 6 additions & 6 deletions freyja_adapters/cloud/azure_cloud_connector_adapter/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# Azure Cloud Connector Adapter

This is an example implementation of an adapter for the [Azure Cloud Connectors](../../../cloud_connectors/azure/README.md).

This adapter is used to communicate with an Azure Cloud Connector to synchronize in-vehicle signals with the cloud.
The Azure Cloud Connector Adapter is used to communicate with the [Azure Cloud Connectors](../../../cloud_connectors/azure/README.md) to synchronize in-vehicle signals with the cloud.

## Prerequisites

Expand All @@ -12,8 +10,10 @@ You will need to either have the [Azure Digital Twins Connector](../../../cloud_

## Configuration

This cloud adapter can be configured using the `res/azure_cloud_connector_adapter_config.json` file. This file is automatically copied to the build output and contains the following properties:
This adapter supports the following configuration settings:

- `cloud_connector_url`: The URL of the cloud connector. This should match the configuration for your cloud connector.
- `cloud_connector_uri`: The URI of the cloud connector. This should match the configuration for your cloud connector.
- `max_retries`: The maximum number of times to retry failed attempts to send data to the cloud connector.
- `retry_interval_ms`: The duration between retries.
- `retry_interval_ms`: The duration between retries in milliseconds.

This adapter supports the same [config override method](../../docs/config-overrides.md) as the Freyja mocks. The override filename is `azure_cloud_connector_adapter_config.json`, and the default config is located at `res/azure_cloud_connector_adapter_config.default.json`.
25 changes: 12 additions & 13 deletions freyja_adapters/cloud/azure_cloud_connector_adapter/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@
// Licensed under the MIT license.
// SPDX-License-Identifier: MIT

use std::{env, fs, path::Path};
use std::env;

const OUT_DIR: &str = "OUT_DIR";
const SAMPLE_CONFIG_FILE: &str = "res/azure_cloud_connector_adapter_config.sample.json";
const CONFIG_FILE: &str = "azure_cloud_connector_adapter_config.json";
use freyja_build_common::copy_to_build_out_dir;

fn main() {
// The current directory of the build script is the package's root directory
let config_path = env::current_dir().unwrap().join(SAMPLE_CONFIG_FILE);

let target_dir = env::var(OUT_DIR).unwrap();
let dest_path = Path::new(&target_dir).join(CONFIG_FILE);
const RES_DIR_NAME: &str = "res";
const DEFAULT_CONFIG_FILE: &str = "azure_cloud_connector_adapter_config.default.json";

fs::copy(&config_path, dest_path).unwrap();
fn main() {
// Current directory of the build script is the package's root directory
let config_path = env::current_dir()
.unwrap()
.join(RES_DIR_NAME)
.join(DEFAULT_CONFIG_FILE);

println!("cargo:rerun-if-changed={}", config_path.to_str().unwrap());
}
copy_to_build_out_dir(config_path, DEFAULT_CONFIG_FILE);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"cloud_connector_uri": "http://[::1]:8890",
"max_retries": 5,
"retry_interval_ms": 1000
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT license.
// SPDX-License-Identifier: MIT

use std::{env, fs, path::Path, time::Duration};
use std::{env, time::Duration};

use async_trait::async_trait;
use azure_cloud_connector_proto::azure_cloud_connector::{
Expand All @@ -12,12 +12,14 @@ use log::debug;
use serde::{Deserialize, Serialize};
use tonic::transport::Channel;

use crate::azure_cloud_connector_adapter_config::{Config, CONFIG_FILE};
use freyja_common::retry_utils::execute_with_retry;
use freyja_common::{retry_utils::execute_with_retry, config_utils, out_dir};
use freyja_contracts::cloud_adapter::{
CloudAdapter, CloudAdapterError, CloudMessageRequest, CloudMessageResponse,
};

use crate::config::Config;

const CONFIG_FILE_STEM: &str = "azure_cloud_connector_adapter_config";
const MODEL_ID_KEY: &str = "model_id";
const INSTANCE_ID_KEY: &str = "instance_id";
const INSTANCE_PROPERTY_PATH_KEY: &str = "instance_property_path";
Expand Down Expand Up @@ -82,16 +84,18 @@ impl CloudAdapter for AzureCloudConnectorAdapter {
/// Creates a new instance of a CloudAdapter with default settings
fn create_new() -> Result<Self, CloudAdapterError> {
let cloud_connector_client = futures::executor::block_on(async {
let config_file = fs::read_to_string(Path::new(env!("OUT_DIR")).join(CONFIG_FILE))
.map_err(CloudAdapterError::io)?;
// Load the config
let config: Config =
serde_json::from_str(&config_file).map_err(CloudAdapterError::deserialize)?;
let config: Config = config_utils::read_from_files(
CONFIG_FILE_STEM,
config_utils::JSON_EXT,
out_dir!(),
CloudAdapterError::io,
CloudAdapterError::deserialize,
)?;

execute_with_retry(
config.max_retries,
Duration::from_millis(config.retry_interval_ms),
|| AzureCloudConnectorClient::connect(config.cloud_connector_url.clone()),
|| AzureCloudConnectorClient::connect(config.cloud_connector_uri.clone()),
Some(String::from(
"Connection retry for connecting to Azure Cloud Connector",
)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

use serde::{Deserialize, Serialize};

pub(crate) const CONFIG_FILE: &str = "azure_cloud_connector_adapter_config.json";

/// A config entry for the Azure Cloud Connector Adapter
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Config {
Expand All @@ -15,6 +13,6 @@ pub struct Config {
/// Retry interval in milliseconds
pub retry_interval_ms: u64,

/// The url for the cloud connector server
pub cloud_connector_url: String,
/// The uri for the cloud connector server
pub cloud_connector_uri: String,
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
// SPDX-License-Identifier: MIT

pub mod azure_cloud_connector_adapter;
mod azure_cloud_connector_adapter_config;
mod config;
2 changes: 1 addition & 1 deletion freyja_adapters/digital_twin/ibeji_adapter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ An example of a configuration file that uses Chariott can be found at `res/ibeji

### Configuration Overrides

This adapter supports the same [config overrides](../../docs/config-overrides.md) as the Freyja mocks. The override filename is `ibeji_adapter_config.json`, and the default config is located at `res/ibeji_adapter_config.default.json`.
This adapter supports the same [config override method](../../docs/config-overrides.md) as the Freyja mocks. The override filename is `ibeji_adapter_config.json`, and the default config is located at `res/ibeji_adapter_config.default.json`.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"service_discovery_method": "Config",
"service_discovery_method": "FromConfig",
"uri": "http://0.0.0.0:5010",
"max_retries": 5,
"retry_interval_ms": 1000
Expand Down
2 changes: 1 addition & 1 deletion freyja_adapters/digital_twin/ibeji_adapter/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use serde::{Deserialize, Serialize};
#[serde(tag = "service_discovery_method")]
pub enum Config {
/// Use a URI from the config for the In-Vehicle Digital Twin Service
Config {
FromConfig {
/// The URI for the In-Vehicle Digital Twin Service
uri: String,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl DigitalTwinAdapter for IbejiAdapter {
)?;

let (invehicle_digital_twin_service_uri, max_retries, retry_interval_ms) = match config {
Config::Config {
Config::FromConfig {
uri,
max_retries,
retry_interval_ms,
Expand All @@ -102,7 +102,7 @@ impl DigitalTwinAdapter for IbejiAdapter {
|| {
Self::retrieve_ibeji_invehicle_digital_twin_uri_from_chariott(
&uri,
discover_request,
discover_request.clone(),
)
},
Some(String::from("Connection retry for connecting to Chariott")),
Expand Down

0 comments on commit fef3535

Please sign in to comment.