Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve configuration for example Freyja adapters #7

Merged
merged 4 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions Cargo.lock

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

7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,21 @@ 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" }
freyja-contracts = { git = "https://github.com/eclipse-ibeji/freyja" }
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 @@ -20,5 +20,8 @@ serde_json = { workspace = true }
tempfile = { workspace = true }
tonic = { workspace = true }
tokio = { workspace = true }
tokio-stream = { workspace = true }
tower = { workspace = true }
tokio-stream = { workspace = true, features = ["net"] }
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](https://github.com/eclipse-ibeji/freyja/blob/main/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`.
23 changes: 11 additions & 12 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::{config_utils, out_dir, retry_utils::execute_with_retry};
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;
5 changes: 4 additions & 1 deletion freyja_adapters/digital_twin/ibeji_adapter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@ tempfile = { workspace = true }
tokio = { workspace = true }
tokio-stream = { workspace = true, features = ["net"] }
tonic = { workspace = true }
tower = { workspace = true }
tower = { workspace = true }

[build-dependencies]
freyja-build-common = { workspace = true }
34 changes: 19 additions & 15 deletions freyja_adapters/digital_twin/ibeji_adapter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,32 @@ The Ibeji Adapter is used to integrate with the [Ibeji In-Vehicle Digital Twin S

## Configuration

The `res` directory contains two sample config files. The `ibeji_adapter_config.sample.json` will be copied to the build output, so you can edit this file to configure the adapter.
This adapter supports two different configuration schemas depending on how you want to discover the In-Vehicle Digital Twin Service:

### Common Configuration Fields
### Without Chariott

Whether or not you are using Chariott, the following config values are required:
To bypass Chariott and use a configuration value to specify the In-Vehicle Digital Twin Service URI, you must specify the following configuration:

- `service_type`: Specifies whether to call Ibeji directly or use Chariott's Service Discovery system to find it. Valid values are `"ChariottDiscoveryService"` and `"InVehicleDigitalTwinService"`.
- `uri`: The URI for the selected service.
- `max_retries`: The maximum number of times to retry failed attempts to communicate with the digital twin service.
- `service_discovery_method`: Set this value to `"Config"`.
- `uri`: The URI for the In-Vehicle Digital Twin Service.
- `max_retries`: The maximum number of times to retry failed attempts to communicate with the In-Vehicle Digital Twin Service.
- `retry_interval_ms`: The duration between retries in milliseconds.

### Ibeji Without Chariott
### Using Chariott

To use Ibeji without Chariott, the `service_type` must be `"InVehicleDigitalTwinService"`. No additional configuration is needed to use Ibeji without Chariott.
To use Chariott to discover the In-Vehicle Digital Twin Service, you must specify the following configuration:

### Ibeji With Chariott
- `service_discovery_method`: Set this value to `"ChariottServiceDiscovery"` to use Chariott.
- `uri`: The URI for Chariott's Service Discovery system.
- `max_retries`: The maximum number of times to retry failed attempts to communicate with Chariott or the In-Vehicle Digital Twin Service.
- `retry_interval_ms`: The duration between retries in milliseconds.
- `metadata`: Metadata for the discovery operation:
- `namespace`: The namespace for the In-Vehicle Digital Twin Service.
- `name`: The service name for the In-Vehicle Digital Twin Service.
- `version`: The version of the In-Vehicle Digital Twin Service to query for.

To use Ibeji with [Chariott's Service Discovery system](https://github.com/eclipse-chariott/chariott/blob/main/service_discovery/README.md), the `service_type` must be `"ChariottDiscoveryService"` and you must specify the following additional config for the adapter:
An example of a configuration file that uses Chariott can be found at `res/ibeji_adapter_config.chariott_sample.json`.

- `metadata`: Information used to query Chariott's Service Discovery system. This is an object with the following properties:
- `namespace`: The service namespace for Ibeji.
- `name`: The service name for Ibeji.
- `version`: The service version for Ibeji.
### Configuration Overrides

Ibeji must also be configured to use Chariott to use this feature.
This adapter supports the same [config override method](https://github.com/eclipse-ibeji/freyja/blob/main/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`.
Loading