Skip to content

Commit

Permalink
northbound: use Cow for "date-and-time" leaves
Browse files Browse the repository at this point in the history
Update the "date-and-time" leaves to use `Cow`, similar to other
leaf types. This change allows for both owned and borrowed values,
providing greater flexibility.

Signed-off-by: Renato Westphal <[email protected]>
  • Loading branch information
rwestphal committed Jul 9, 2024
1 parent f54a0d7 commit f2b3bbd
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 30 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ unsafe_code = "forbid"
[workspace.lints.clippy]
borrowed_box = "allow"
manual_range_contains = "allow"
single_match = "allow"
too_many_arguments = "allow"

[profile.release]
Expand Down
6 changes: 4 additions & 2 deletions holo-bfd/src/northbound/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ fn state_change_singlehop(
time_of_last_state_change: sess
.statistics
.last_state_change_time
.as_ref(),
.as_ref()
.map(Cow::Borrowed),
dest_addr: Some(Cow::Borrowed(dst)),
source_addr: sess.config.src.as_ref().map(Cow::Borrowed),
session_index: Some(sess.id as u32),
Expand All @@ -71,7 +72,8 @@ fn state_change_multihop(
time_of_last_state_change: sess
.statistics
.last_state_change_time
.as_ref(),
.as_ref()
.map(Cow::Borrowed),
dest_addr: Some(Cow::Borrowed(dst)),
source_addr: Some(Cow::Borrowed(src)),
session_index: Some(sess.id as u32),
Expand Down
12 changes: 6 additions & 6 deletions holo-bfd/src/northbound/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ fn load_callbacks() -> Callbacks<Master> {
use bfd::ip_mh::session_groups::session_group::sessions::session_statistics::SessionStatistics;
let sess = args.list_entry.as_session().unwrap();
Box::new(SessionStatistics {
create_time: Some(&sess.statistics.create_time).ignore_in_testing(),
last_down_time: sess.statistics.last_down_time.as_ref().ignore_in_testing(),
last_up_time: sess.statistics.last_up_time.as_ref().ignore_in_testing(),
create_time: Some(Cow::Borrowed(&sess.statistics.create_time)).ignore_in_testing(),
last_down_time: sess.statistics.last_down_time.as_ref().map(Cow::Borrowed).ignore_in_testing(),
last_up_time: sess.statistics.last_up_time.as_ref().map(Cow::Borrowed).ignore_in_testing(),
down_count: Some(sess.statistics.down_count).ignore_in_testing(),
admin_down_count: Some(sess.statistics.admin_down_count).ignore_in_testing(),
receive_packet_count: Some(sess.statistics.rx_packet_count).ignore_in_testing(),
Expand Down Expand Up @@ -176,9 +176,9 @@ fn load_callbacks() -> Callbacks<Master> {
use bfd::ip_sh::sessions::session::session_statistics::SessionStatistics;
let sess = args.list_entry.as_session().unwrap();
Box::new(SessionStatistics {
create_time: Some(&sess.statistics.create_time).ignore_in_testing(),
last_down_time: sess.statistics.last_down_time.as_ref().ignore_in_testing(),
last_up_time: sess.statistics.last_up_time.as_ref().ignore_in_testing(),
create_time: Some(Cow::Borrowed(&sess.statistics.create_time)).ignore_in_testing(),
last_down_time: sess.statistics.last_down_time.as_ref().map(Cow::Borrowed).ignore_in_testing(),
last_up_time: sess.statistics.last_up_time.as_ref().map(Cow::Borrowed).ignore_in_testing(),
down_count: Some(sess.statistics.down_count).ignore_in_testing(),
admin_down_count: Some(sess.statistics.admin_down_count).ignore_in_testing(),
receive_packet_count: Some(sess.statistics.rx_packet_count).ignore_in_testing(),
Expand Down
4 changes: 2 additions & 2 deletions holo-bgp/src/northbound/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub(crate) fn backward_transition(
notification_received: nbr.notification_rcvd.as_ref().map(
|(time, notif)| {
Box::new(NotificationReceived {
last_notification: Some(time),
last_notification: Some(Cow::Borrowed(time)),
last_error: Some(notif.to_yang()),
last_error_code: Some(notif.error_code),
last_error_subcode: Some(notif.error_subcode),
Expand All @@ -52,7 +52,7 @@ pub(crate) fn backward_transition(
notification_sent: nbr.notification_sent.as_ref().map(
|(time, notif)| {
Box::new(NotificationSent {
last_notification: Some(time),
last_notification: Some(Cow::Borrowed(time)),
last_error: Some(notif.to_yang()),
last_error_code: Some(notif.error_code),
last_error_subcode: Some(notif.error_subcode),
Expand Down
6 changes: 3 additions & 3 deletions holo-bgp/src/northbound/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ fn load_callbacks() -> Callbacks<Instance> {
identifier: nbr.identifier.map(Cow::Owned),
dynamically_configured: None,
session_state: Some(nbr.state.to_yang()),
last_established: nbr.last_established.as_ref().ignore_in_testing(),
last_established: nbr.last_established.as_ref().map(Cow::Borrowed).ignore_in_testing(),
})
})
.path(bgp::neighbors::neighbor::timers::PATH)
Expand Down Expand Up @@ -298,7 +298,7 @@ fn load_callbacks() -> Callbacks<Instance> {
let mut last_error_subcode = None;
let mut last_error_data = None;
if let Some((time, notif)) = &nbr.notification_rcvd {
last_notification = Some(time);
last_notification = Some(Cow::Borrowed(time));
last_error = Some(notif.to_yang());
last_error_code = Some(notif.error_code);
last_error_subcode = Some(notif.error_subcode);
Expand All @@ -322,7 +322,7 @@ fn load_callbacks() -> Callbacks<Instance> {
let mut last_error_subcode = None;
let mut last_error_data = None;
if let Some((time, notif)) = &nbr.notification_sent {
last_notification = Some(time);
last_notification = Some(Cow::Borrowed(time));
last_error = Some(notif.to_yang());
last_error_code = Some(notif.error_code);
last_error_subcode = Some(notif.error_subcode);
Expand Down
2 changes: 0 additions & 2 deletions holo-interface/src/netlink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
// SPDX-License-Identifier: MIT
//

#![allow(clippy::single_match)]

use std::net::{Ipv4Addr, Ipv6Addr};

use capctl::caps::CapState;
Expand Down
6 changes: 5 additions & 1 deletion holo-keychain/src/northbound/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// SPDX-License-Identifier: MIT
//

use std::borrow::Cow;
use std::sync::LazyLock as Lazy;

use enum_as_inner::EnumAsInner;
Expand Down Expand Up @@ -40,7 +41,10 @@ fn load_callbacks() -> Callbacks<Master> {
let keychain = args.list_entry.as_keychain().unwrap();
Box::new(KeyChain {
name: keychain.name.as_str().into(),
last_modified_timestamp: keychain.last_modified.as_ref(),
last_modified_timestamp: keychain
.last_modified
.as_ref()
.map(Cow::Borrowed),
})
})
.path(key_chains::key_chain::key::PATH)
Expand Down
8 changes: 4 additions & 4 deletions holo-ldp/src/northbound/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ fn load_callbacks() -> Callbacks<Instance> {
use mpls_ldp::discovery::interfaces::interface::address_families::ipv4::hello_adjacencies::hello_adjacency::statistics::Statistics;
let adj = args.list_entry.as_interface_adj().unwrap();
Box::new(Statistics {
discontinuity_time: Some(&adj.discontinuity_time).ignore_in_testing(),
discontinuity_time: Some(Cow::Borrowed(&adj.discontinuity_time)).ignore_in_testing(),
hello_received: Some(adj.hello_rcvd).ignore_in_testing(),
hello_dropped: Some(adj.hello_dropped).ignore_in_testing(),
})
Expand Down Expand Up @@ -256,7 +256,7 @@ fn load_callbacks() -> Callbacks<Instance> {
use mpls_ldp::discovery::targeted::address_families::ipv4::hello_adjacencies::hello_adjacency::statistics::Statistics;
let adj = args.list_entry.as_targeted_nbr_adj().unwrap();
Box::new(Statistics {
discontinuity_time: Some(&adj.discontinuity_time).ignore_in_testing(),
discontinuity_time: Some(Cow::Borrowed(&adj.discontinuity_time)).ignore_in_testing(),
hello_received: Some(adj.hello_rcvd).ignore_in_testing(),
hello_dropped: Some(adj.hello_dropped).ignore_in_testing(),
})
Expand Down Expand Up @@ -318,7 +318,7 @@ fn load_callbacks() -> Callbacks<Instance> {
use mpls_ldp::peers::peer::address_families::ipv4::hello_adjacencies::hello_adjacency::statistics::Statistics;
let adj = args.list_entry.as_neighbor_adj().unwrap();
Box::new(Statistics {
discontinuity_time: Some(&adj.discontinuity_time).ignore_in_testing(),
discontinuity_time: Some(Cow::Borrowed(&adj.discontinuity_time)).ignore_in_testing(),
hello_received: Some(adj.hello_rcvd).ignore_in_testing(),
hello_dropped: Some(adj.hello_dropped).ignore_in_testing(),
})
Expand Down Expand Up @@ -388,7 +388,7 @@ fn load_callbacks() -> Callbacks<Instance> {
let total_labels = nbr.rcvd_mappings.len();
let total_fec_label_bindings = nbr.rcvd_mappings.keys().map(|prefix| instance.state.as_ref().unwrap().fecs.get(prefix).unwrap()).filter(|fec| fec.is_nbr_nexthop(nbr)).count();
Box::new(Statistics {
discontinuity_time: nbr.statistics.discontinuity_time.as_ref().ignore_in_testing(),
discontinuity_time: nbr.statistics.discontinuity_time.as_ref().map(Cow::Borrowed).ignore_in_testing(),
total_addresses: Some(total_addresses.saturating_into()),
total_labels: Some(total_labels.saturating_into()),
total_fec_label_bindings: Some(total_fec_label_bindings.saturating_into()),
Expand Down
2 changes: 1 addition & 1 deletion holo-northbound/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ fn leaf_typedef_map(leaf_type: &SchemaLeafType<'_>) -> Option<&'static str> {
Some("ip-prefix") => Some("Cow<'a, IpNetwork>"),
Some("ipv4-prefix") => Some("Cow<'a, Ipv4Network>"),
Some("ipv6-prefix") => Some("Cow<'a, Ipv6Network>"),
Some("date-and-time") => Some("&'a DateTime<Utc>"),
Some("date-and-time") => Some("Cow<'a, DateTime<Utc>>"),
Some("timer-value-seconds16") => Some("Cow<'a, Duration>"),
Some("timer-value-seconds32") => Some("Cow<'a, Duration>"),
Some("timer-value-milliseconds") => Some("Cow<'a, Duration>"),
Expand Down
8 changes: 4 additions & 4 deletions holo-ospf/src/northbound/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ where
let mut as_scope_lsa_count = None;
let mut as_scope_lsa_chksum_sum = None;
if let Some(state) = &instance.state {
discontinuity_time = Some(&state.discontinuity_time).ignore_in_testing();
discontinuity_time = Some(Cow::Borrowed(&state.discontinuity_time)).ignore_in_testing();
originate_new_lsa_count = Some(state.orig_lsa_count).ignore_in_testing();
rx_new_lsas_count = Some(state.rx_lsa_count).ignore_in_testing();
as_scope_lsa_count = Some(state.lsdb.lsa_count());
Expand Down Expand Up @@ -303,7 +303,7 @@ where
use ospf::areas::area::statistics::Statistics;
let area = args.list_entry.as_area().unwrap();
Box::new(Statistics {
discontinuity_time: Some(&area.state.discontinuity_time).ignore_in_testing(),
discontinuity_time: Some(Cow::Borrowed(&area.state.discontinuity_time)).ignore_in_testing(),
spf_runs_count: Some(area.state.spf_run_count).ignore_in_testing(),
abr_count: Some(area.abr_count() as _),
asbr_count: Some(area.asbr_count() as _),
Expand Down Expand Up @@ -407,7 +407,7 @@ where
use ospf::areas::area::interfaces::interface::statistics::Statistics;
let iface = args.list_entry.as_interface().unwrap();
Box::new(Statistics {
discontinuity_time: Some(&iface.state.discontinuity_time).ignore_in_testing(),
discontinuity_time: Some(Cow::Borrowed(&iface.state.discontinuity_time)).ignore_in_testing(),
if_event_count: Some(iface.state.event_count).ignore_in_testing(),
link_scope_lsa_count: Some(iface.state.lsdb.lsa_count()),
link_scope_lsa_cksum_sum: Some(iface.state.lsdb.cksum_sum()).ignore_in_testing(),
Expand Down Expand Up @@ -485,7 +485,7 @@ where
use ospf::areas::area::interfaces::interface::neighbors::neighbor::statistics::Statistics;
let (_, nbr) = args.list_entry.as_neighbor().unwrap();
Box::new(Statistics {
discontinuity_time: Some(&nbr.discontinuity_time).ignore_in_testing(),
discontinuity_time: Some(Cow::Borrowed(&nbr.discontinuity_time)).ignore_in_testing(),
nbr_event_count: Some(nbr.event_count).ignore_in_testing(),
nbr_retrans_qlen: Some(nbr.lists.ls_rxmt.len() as u32),
})
Expand Down
14 changes: 10 additions & 4 deletions holo-rip/src/northbound/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ where
updates_sent = Some(iface.state.statistics.updates_sent);
}
Box::new(Statistics {
discontinuity_time: discontinuity_time.ignore_in_testing(),
discontinuity_time: discontinuity_time
.map(Cow::Borrowed)
.ignore_in_testing(),
bad_packets_rcvd: bad_packets_rcvd.ignore_in_testing(),
bad_routes_rcvd: bad_routes_rcvd.ignore_in_testing(),
updates_sent: updates_sent.ignore_in_testing(),
Expand All @@ -137,7 +139,9 @@ where
responses_sent = Some(instance.state.statistics.responses_sent);
}
Box::new(Statistics {
discontinuity_time: discontinuity_time.ignore_in_testing(),
discontinuity_time: discontinuity_time
.map(Cow::Borrowed)
.ignore_in_testing(),
requests_rcvd: requests_rcvd.ignore_in_testing(),
requests_sent: requests_sent.ignore_in_testing(),
responses_rcvd: responses_rcvd.ignore_in_testing(),
Expand Down Expand Up @@ -167,7 +171,8 @@ fn load_callbacks_ripv2() -> Callbacks<Instance<Ripv2>> {
let nbr = args.list_entry.as_ipv4_neighbor().unwrap();
Box::new(Neighbor {
ipv4_address: Cow::Borrowed(&nbr.addr),
last_update: Some(&nbr.last_update).ignore_in_testing(),
last_update: Some(Cow::Borrowed(&nbr.last_update))
.ignore_in_testing(),
bad_packets_rcvd: Some(nbr.bad_packets_rcvd)
.ignore_in_testing(),
bad_routes_rcvd: Some(nbr.bad_routes_rcvd).ignore_in_testing(),
Expand Down Expand Up @@ -229,7 +234,8 @@ fn load_callbacks_ripng() -> Callbacks<Instance<Ripng>> {
let nbr = args.list_entry.as_ipv6_neighbor().unwrap();
Box::new(Neighbor {
ipv6_address: Cow::Borrowed(&nbr.addr),
last_update: Some(&nbr.last_update).ignore_in_testing(),
last_update: Some(Cow::Borrowed(&nbr.last_update))
.ignore_in_testing(),
bad_packets_rcvd: Some(nbr.bad_packets_rcvd)
.ignore_in_testing(),
bad_routes_rcvd: Some(nbr.bad_routes_rcvd).ignore_in_testing(),
Expand Down
2 changes: 1 addition & 1 deletion holo-routing/src/northbound/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ fn load_callbacks() -> Callbacks<Master> {
route_preference,
source_protocol,
active: route.flags.contains(RouteFlags::ACTIVE).then_some(()),
last_updated: Some(&route.last_updated),
last_updated: Some(Cow::Borrowed(&route.last_updated)),
ipv4_destination_prefix: dest.as_ipv4().copied().map(Cow::Borrowed),
ipv6_destination_prefix: dest.as_ipv6().copied().map(Cow::Borrowed),
mpls_enabled: None,
Expand Down

0 comments on commit f2b3bbd

Please sign in to comment.