routing/
config.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright 2024 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use crate::bedrock::dict_ext::DictExt;
use crate::bedrock::request_metadata;
use crate::capability_source::{
    CapabilitySource, CapabilityToCapabilitySource, ComponentCapability, ComponentSource,
};
use crate::component_instance::ComponentInstanceInterface;
use crate::{RouteRequest, RouterResponse, RoutingError};
use cm_rust::FidlIntoNative;
use sandbox::Data;
use std::sync::Arc;

/// Get a specific configuration use declaration from the structured
/// config key value.
pub fn get_use_config_from_key<'a>(
    key: &str,
    decl: &'a cm_rust::ComponentDecl,
) -> Option<&'a cm_rust::UseConfigurationDecl> {
    decl.uses.iter().find_map(|use_| match use_ {
        cm_rust::UseDecl::Config(c) => (c.target_name == key).then_some(c),
        _ => None,
    })
}

fn source_to_value(
    default: &Option<cm_rust::ConfigValue>,
    source: CapabilitySource,
) -> Result<Option<cm_rust::ConfigValue>, RoutingError> {
    let moniker = source.source_moniker();
    let cap = match source {
        CapabilitySource::Void(_) => {
            return Ok(default.clone());
        }
        CapabilitySource::Capability(CapabilityToCapabilitySource {
            source_capability, ..
        }) => source_capability,
        CapabilitySource::Component(ComponentSource { capability, .. }) => capability,
        o => {
            return Err(RoutingError::unsupported_route_source(moniker, o.type_name().to_string()));
        }
    };

    let cap = match cap {
        ComponentCapability::Config(c) => c,
        c => {
            return Err(RoutingError::unsupported_capability_type(moniker, c.type_name()));
        }
    };
    Ok(Some(cap.value))
}

pub async fn route_config_value_with_bedrock<C>(
    use_config: &cm_rust::UseConfigurationDecl,
    component: &Arc<C>,
) -> Result<Option<cm_rust::ConfigValue>, router_error::RouterError>
where
    C: ComponentInstanceInterface + 'static,
{
    let component_sandbox =
        component.component_sandbox().await.map_err(|e| RoutingError::from(e))?;
    let capability =
        match component_sandbox.program_input.config().get_capability(&use_config.target_name) {
            Some(c) => c,
            None => {
                return Err(RoutingError::BedrockNotPresentInDictionary {
                    name: use_config.target_name.to_string(),
                    moniker: component.moniker().clone().into(),
                }
                .into());
            }
        };
    let sandbox::Capability::DataRouter(router) = capability else {
        return Err(RoutingError::BedrockWrongCapabilityType {
            actual: format!("{:?}", capability),
            expected: "Router".to_string(),
            moniker: component.moniker().clone().into(),
        }
        .into());
    };
    let request = sandbox::Request {
        target: component.as_weak().into(),
        metadata: request_metadata::config_metadata(use_config.availability),
    };
    let data = match router.route(Some(request), false).await? {
        RouterResponse::<Data>::Capability(d) => d,
        RouterResponse::<Data>::Unavailable => return Ok(use_config.default.clone()),
        RouterResponse::<Data>::Debug(_) => {
            return Err(RoutingError::RouteUnexpectedDebug {
                type_name: cm_rust::CapabilityTypeName::Config,
                moniker: component.moniker().clone().into(),
            }
            .into());
        }
    };
    let sandbox::Data::Bytes(bytes) = data else {
        return Err(RoutingError::BedrockWrongCapabilityType {
            actual: format!("{:?}", data),
            expected: "Data::bytes".to_string(),
            moniker: component.moniker().clone().into(),
        }
        .into());
    };
    let config_value: fidl_fuchsia_component_decl::ConfigValue = match fidl::unpersist(&bytes) {
        Ok(v) => v,
        Err(_) => {
            return Err(RoutingError::BedrockWrongCapabilityType {
                actual: "{unknown}".into(),
                expected: "fuchsia.component.decl.ConfigValue".into(),
                moniker: component.moniker().clone().into(),
            }
            .into())
        }
    };

    Ok(Some(config_value.fidl_into_native()))
}

/// Route the given `use_config` from a specific `component`.
/// This returns the configuration value as a result.
/// This will return Ok(None) if it was routed successfully, but it
/// was an optional capability.
pub async fn route_config_value<C>(
    use_config: &cm_rust::UseConfigurationDecl,
    component: &Arc<C>,
) -> Result<Option<cm_rust::ConfigValue>, router_error::RouterError>
where
    C: ComponentInstanceInterface + 'static,
{
    if let Ok(Some(value)) = route_config_value_with_bedrock(use_config, component).await {
        return Ok(Some(value));
    }
    let source = crate::route_capability(
        RouteRequest::UseConfig(use_config.clone()),
        component,
        &mut crate::mapper::NoopRouteMapper,
    )
    .await?;
    Ok(source_to_value(&use_config.default, source.source)?)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::capability_source::VoidSource;
    use moniker::Moniker;

    #[test]
    fn config_from_void() {
        let void_source = CapabilitySource::Void(VoidSource {
            capability: crate::capability_source::InternalCapability::Config(
                "test".parse().unwrap(),
            ),
            moniker: Moniker::root(),
        });
        assert_eq!(Ok(None), source_to_value(&None, void_source));
    }

    #[test]
    fn config_from_capability() {
        let test_value: cm_rust::ConfigValue = cm_rust::ConfigSingleValue::Uint8(5).into();
        let void_source = CapabilitySource::Capability(CapabilityToCapabilitySource {
            source_capability: crate::capability_source::ComponentCapability::Config(
                cm_rust::ConfigurationDecl {
                    name: "test".parse().unwrap(),
                    value: test_value.clone(),
                },
            ),
            moniker: Moniker::root(),
        });
        assert_eq!(Ok(Some(test_value)), source_to_value(&None, void_source));
    }

    #[test]
    fn config_from_component() {
        let test_value: cm_rust::ConfigValue = cm_rust::ConfigSingleValue::Uint8(5).into();
        let void_source = CapabilitySource::Component(ComponentSource {
            capability: crate::capability_source::ComponentCapability::Config(
                cm_rust::ConfigurationDecl {
                    name: "test".parse().unwrap(),
                    value: test_value.clone(),
                },
            ),
            moniker: Moniker::root(),
        });
        assert_eq!(Ok(Some(test_value)), source_to_value(&None, void_source));
    }
}