Skip to content

Commit

Permalink
yang: optimize generation of YANG textual representations
Browse files Browse the repository at this point in the history
Most of the time, the textual representation of YANG values consists
of static strings. To avoid unnecessary memory allocations in these
cases, change `ToYang::to_yang` to return the `Cow` type, which can
accommodate both owned and static strings.

The northbound infrastructure for operational data still expects an
owned string for each state leaf, but that should change as part of
a large refactoring in the future.

Signed-off-by: Renato Westphal <[email protected]>
  • Loading branch information
rwestphal committed Dec 21, 2023
1 parent 6490c38 commit 08a3f49
Show file tree
Hide file tree
Showing 18 changed files with 263 additions and 270 deletions.
12 changes: 6 additions & 6 deletions holo-bfd/src/northbound/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ fn state_change_singlehop(
remote_discr = remote.discr.to_string();
args.push((base::remote_discr::PATH, Some(remote_discr.as_str())));
}
args.push((base::new_state::PATH, Some(new_state.as_str())));
args.push((base::state_change_reason::PATH, Some(diag.as_str())));
args.push((base::new_state::PATH, Some(new_state.as_ref())));
args.push((base::state_change_reason::PATH, Some(diag.as_ref())));
if let Some(time) = &sess.statistics.last_state_change_time {
last_state_change_time = time.to_rfc3339();
args.push((
Expand All @@ -66,7 +66,7 @@ fn state_change_singlehop(
args.push((base::source_addr::PATH, Some(src_addr.as_str())));
}
args.push((base::session_index::PATH, Some(sess_index.as_str())));
args.push((base::path_type::PATH, Some(path_type.as_str())));
args.push((base::path_type::PATH, Some(path_type.as_ref())));
args.push((base::interface::PATH, Some(ifname)));
args.push((base::echo_enabled::PATH, Some("false")));
notification::send(nb_tx, base::PATH, &args);
Expand Down Expand Up @@ -96,8 +96,8 @@ fn state_change_multihop(
remote_discr = remote.discr.to_string();
args.push((base::remote_discr::PATH, Some(remote_discr.as_str())));
}
args.push((base::new_state::PATH, Some(new_state.as_str())));
args.push((base::state_change_reason::PATH, Some(diag.as_str())));
args.push((base::new_state::PATH, Some(new_state.as_ref())));
args.push((base::state_change_reason::PATH, Some(diag.as_ref())));
if let Some(time) = &sess.statistics.last_state_change_time {
last_state_change_time = time.to_rfc3339();
args.push((
Expand All @@ -108,6 +108,6 @@ fn state_change_multihop(
args.push((base::dest_addr::PATH, Some(dst.as_str())));
args.push((base::source_addr::PATH, Some(src_addr.as_str())));
args.push((base::session_index::PATH, Some(sess_index.as_str())));
args.push((base::path_type::PATH, Some(path_type.as_str())));
args.push((base::path_type::PATH, Some(path_type.as_ref())));
notification::send(nb_tx, base::PATH, &args);
}
20 changes: 10 additions & 10 deletions holo-bfd/src/northbound/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ fn load_callbacks() -> Callbacks<Master> {
.path(bfd::ip_sh::sessions::session::path_type::PATH)
.get_element_string(|_master, args| {
let sess = args.list_entry.as_session().unwrap();
Some(sess.key.path_type().to_yang())
Some(sess.key.path_type().to_yang().into())
})
.path(bfd::ip_sh::sessions::session::ip_encapsulation::PATH)
.get_element_bool(|_master, _args| {
Expand Down Expand Up @@ -153,25 +153,25 @@ fn load_callbacks() -> Callbacks<Master> {
.path(bfd::ip_sh::sessions::session::session_running::local_state::PATH)
.get_element_string(|_master, args| {
let sess = args.list_entry.as_session().unwrap();
Some(sess.state.local_state.to_yang())
Some(sess.state.local_state.to_yang().into())
})
.path(bfd::ip_sh::sessions::session::session_running::remote_state::PATH)
.get_element_string(|_master, args| {
let sess = args.list_entry.as_session().unwrap();
sess.state.remote.as_ref().map(|remote| remote.state.to_yang())
sess.state.remote.as_ref().map(|remote| remote.state.to_yang().into())
})
.path(bfd::ip_sh::sessions::session::session_running::local_diagnostic::PATH)
.get_element_string(|_master, args| {
let sess = args.list_entry.as_session().unwrap();
Some(sess.state.local_diag.to_yang())
Some(sess.state.local_diag.to_yang().into())
})
.path(bfd::ip_sh::sessions::session::session_running::remote_diagnostic::PATH)
.get_element_string(|_master, args| {
let sess = args.list_entry.as_session().unwrap();
sess.state.remote.as_ref()
.map(|remote| remote.diag)
.and_then(DiagnosticCode::from_u8)
.map(|diag| diag.to_yang())
.map(|diag| diag.to_yang().into())
})
.path(bfd::ip_sh::sessions::session::session_running::remote_authenticated::PATH)
.get_element_bool(|_master, _args| {
Expand Down Expand Up @@ -310,7 +310,7 @@ fn load_callbacks() -> Callbacks<Master> {
.path(bfd::ip_mh::session_groups::session_group::sessions::path_type::PATH)
.get_element_string(|_master, args| {
let sess = args.list_entry.as_session().unwrap();
Some(sess.key.path_type().to_yang())
Some(sess.key.path_type().to_yang().into())
})
.path(bfd::ip_mh::session_groups::session_group::sessions::ip_encapsulation::PATH)
.get_element_bool(|_master, _args| {
Expand Down Expand Up @@ -347,25 +347,25 @@ fn load_callbacks() -> Callbacks<Master> {
.path(bfd::ip_mh::session_groups::session_group::sessions::session_running::local_state::PATH)
.get_element_string(|_master, args| {
let sess = args.list_entry.as_session().unwrap();
Some(sess.state.local_state.to_yang())
Some(sess.state.local_state.to_yang().into())
})
.path(bfd::ip_mh::session_groups::session_group::sessions::session_running::remote_state::PATH)
.get_element_string(|_master, args| {
let sess = args.list_entry.as_session().unwrap();
sess.state.remote.as_ref().map(|remote| remote.state.to_yang())
sess.state.remote.as_ref().map(|remote| remote.state.to_yang().into())
})
.path(bfd::ip_mh::session_groups::session_group::sessions::session_running::local_diagnostic::PATH)
.get_element_string(|_master, args| {
let sess = args.list_entry.as_session().unwrap();
Some(sess.state.local_diag.to_yang())
Some(sess.state.local_diag.to_yang().into())
})
.path(bfd::ip_mh::session_groups::session_group::sessions::session_running::remote_diagnostic::PATH)
.get_element_string(|_master, args| {
let sess = args.list_entry.as_session().unwrap();
sess.state.remote.as_ref()
.map(|remote| remote.diag)
.and_then(DiagnosticCode::from_u8)
.map(|diag| diag.to_yang())
.map(|diag| diag.to_yang().into())
})
.path(bfd::ip_mh::session_groups::session_group::sessions::session_running::remote_authenticated::PATH)
.get_element_bool(|_master, _args| {
Expand Down
28 changes: 13 additions & 15 deletions holo-bfd/src/northbound/yang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,29 @@
// SPDX-License-Identifier: MIT
//

use std::borrow::Cow;

use holo_yang::ToYang;

use crate::packet::DiagnosticCode;

// ===== ToYang implementations =====

impl ToYang for DiagnosticCode {
fn to_yang(&self) -> String {
fn to_yang(&self) -> Cow<'static, str> {
match self {
DiagnosticCode::Nothing => "none".to_owned(),
DiagnosticCode::TimeExpired => "control-expiry".to_owned(),
DiagnosticCode::EchoFailed => "echo-failed".to_owned(),
DiagnosticCode::NbrDown => "neighbor-down".to_owned(),
DiagnosticCode::FwdPlaneReset => "forwarding-reset".to_owned(),
DiagnosticCode::PathDown => "path-down".to_owned(),
DiagnosticCode::ConcatPathDown => {
"concatenated-path-down".to_owned()
}
DiagnosticCode::AdminDown => "admin-down".to_owned(),
DiagnosticCode::Nothing => "none".into(),
DiagnosticCode::TimeExpired => "control-expiry".into(),
DiagnosticCode::EchoFailed => "echo-failed".into(),
DiagnosticCode::NbrDown => "neighbor-down".into(),
DiagnosticCode::FwdPlaneReset => "forwarding-reset".into(),
DiagnosticCode::PathDown => "path-down".into(),
DiagnosticCode::ConcatPathDown => "concatenated-path-down".into(),
DiagnosticCode::AdminDown => "admin-down".into(),
DiagnosticCode::RevConcatPathDown => {
"reverse-concatenated-path-down".to_owned()
}
DiagnosticCode::MisConnectivity => {
"mis-connectivity-defect".to_owned()
"reverse-concatenated-path-down".into()
}
DiagnosticCode::MisConnectivity => "mis-connectivity-defect".into(),
}
}
}
14 changes: 7 additions & 7 deletions holo-ldp/src/northbound/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ fn load_callbacks() -> Callbacks<Instance> {
CallbacksBuilder::default()
.path(mpls_ldp::global::address_families::ipv4::label_distribution_control_mode::PATH)
.get_element_string(|_instance, _args| {
let mode = LabelDistMode::Independent.to_yang();
let mode = LabelDistMode::Independent.to_yang().into();
Some(mode)
})
.path(mpls_ldp::global::address_families::ipv4::bindings::address::PATH)
Expand Down Expand Up @@ -126,7 +126,7 @@ fn load_callbacks() -> Callbacks<Instance> {
.path(mpls_ldp::global::address_families::ipv4::bindings::address::advertisement_type::PATH)
.get_element_string(|_instance, args| {
let binding = args.list_entry.as_addr_binding().unwrap();
Some(binding.adv_type.to_yang())
Some(binding.adv_type.to_yang().into())
})
.path(mpls_ldp::global::address_families::ipv4::bindings::address::peer::lsr_id::PATH)
.get_element_ipv4(|_instance, args| {
Expand Down Expand Up @@ -196,7 +196,7 @@ fn load_callbacks() -> Callbacks<Instance> {
.path(mpls_ldp::global::address_families::ipv4::bindings::fec_label::peer::label::PATH)
.get_element_string(|_instance, args| {
let binding = args.list_entry.as_label_binding().unwrap();
Some(binding.label.to_yang())
Some(binding.label.to_yang().into())
})
.path(mpls_ldp::global::address_families::ipv4::bindings::fec_label::peer::used_in_forwarding::PATH)
.get_element_bool(|_instance, args| {
Expand Down Expand Up @@ -440,21 +440,21 @@ fn load_callbacks() -> Callbacks<Instance> {
.get_element_string(|_instance, args| {
let nbr = args.list_entry.as_neighbor().unwrap();
if nbr.is_operational() {
Some(LabelAdvMode::DownstreamUnsolicited.to_yang())
Some(LabelAdvMode::DownstreamUnsolicited.to_yang().into())
} else {
None
}
})
.path(mpls_ldp::peers::peer::label_advertisement_mode::peer::PATH)
.get_element_string(|_instance, args| {
let nbr = args.list_entry.as_neighbor().unwrap();
nbr.rcvd_label_adv_mode.as_ref().map(|mode| mode.to_yang())
nbr.rcvd_label_adv_mode.as_ref().map(|mode| mode.to_yang().into())
})
.path(mpls_ldp::peers::peer::label_advertisement_mode::negotiated::PATH)
.get_element_string(|_instance, args| {
let nbr = args.list_entry.as_neighbor().unwrap();
if nbr.is_operational() {
Some(LabelAdvMode::DownstreamUnsolicited.to_yang())
Some(LabelAdvMode::DownstreamUnsolicited.to_yang().into())
} else {
None
}
Expand Down Expand Up @@ -498,7 +498,7 @@ fn load_callbacks() -> Callbacks<Instance> {
.path(mpls_ldp::peers::peer::session_state::PATH)
.get_element_string(|_instance, args| {
let nbr = args.list_entry.as_neighbor().unwrap();
Some(nbr.state.to_yang())
Some(nbr.state.to_yang().into())
})
.path(mpls_ldp::peers::peer::tcp_connection::local_address::PATH)
.get_element_ip(|_instance, args| {
Expand Down
34 changes: 17 additions & 17 deletions holo-ldp/src/northbound/yang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
// SPDX-License-Identifier: MIT
//

use std::borrow::Cow;

use holo_yang::ToYang;

use crate::neighbor::{self, LabelAdvMode, LabelDistMode};
Expand All @@ -12,44 +14,42 @@ use crate::northbound::state::AdvertisementType;
// ===== ToYang implementations =====

impl ToYang for AdvertisementType {
fn to_yang(&self) -> String {
fn to_yang(&self) -> Cow<'static, str> {
match self {
AdvertisementType::Advertised => "advertised".to_owned(),
AdvertisementType::Received => "received".to_owned(),
AdvertisementType::Advertised => "advertised".into(),
AdvertisementType::Received => "received".into(),
}
}
}

impl ToYang for neighbor::fsm::State {
fn to_yang(&self) -> String {
fn to_yang(&self) -> Cow<'static, str> {
match self {
neighbor::fsm::State::NonExistent => "non-existent".to_owned(),
neighbor::fsm::State::Initialized => "initialized".to_owned(),
neighbor::fsm::State::OpenRec => "openrec".to_owned(),
neighbor::fsm::State::OpenSent => "opensent".to_owned(),
neighbor::fsm::State::Operational => "operational".to_owned(),
neighbor::fsm::State::NonExistent => "non-existent".into(),
neighbor::fsm::State::Initialized => "initialized".into(),
neighbor::fsm::State::OpenRec => "openrec".into(),
neighbor::fsm::State::OpenSent => "opensent".into(),
neighbor::fsm::State::Operational => "operational".into(),
}
}
}

impl ToYang for LabelDistMode {
fn to_yang(&self) -> String {
fn to_yang(&self) -> Cow<'static, str> {
match self {
LabelDistMode::Independent => "independent".to_owned(),
LabelDistMode::Ordered => "ordered".to_owned(),
LabelDistMode::Independent => "independent".into(),
LabelDistMode::Ordered => "ordered".into(),
}
}
}

impl ToYang for LabelAdvMode {
fn to_yang(&self) -> String {
fn to_yang(&self) -> Cow<'static, str> {
match self {
LabelAdvMode::DownstreamUnsolicited => {
"downstream-unsolicited".to_owned()
}
LabelAdvMode::DownstreamOnDemand => {
"downstream-on-demand".to_owned()
"downstream-unsolicited".into()
}
LabelAdvMode::DownstreamOnDemand => "downstream-on-demand".into(),
}
}
}
Loading

0 comments on commit 08a3f49

Please sign in to comment.