Skip to content

Commit

Permalink
ibus: add derive_more crate
Browse files Browse the repository at this point in the history
Add derive_more crate.
Simplifies the calling of the
IbusMsg messages.

Signed-off-by: Paul Wekesa <[email protected]>
  • Loading branch information
Paul-weqe committed Nov 7, 2024
1 parent db0417c commit 625e839
Show file tree
Hide file tree
Showing 22 changed files with 166 additions and 191 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ chrono = { version = "0.4", features = ["serde"] }
convert_case = "0.6"
criterion = "0.4"
crossbeam-channel = "0.5"
derive_more = { version="1.0.0", features = ["from"] }
derive-new = "0.5"
enum-as-inner = "0.6"
fletcher = "1.0"
Expand Down
2 changes: 1 addition & 1 deletion holo-bfd/src/master.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ impl ProtocolInstance for Master {

async fn init(&mut self) {
// Request information about all interfaces.
let _ = self.tx.ibus.send(IbusMsg::Interface(InterfaceMsg::Dump));
let _ = self.tx.ibus.send(InterfaceMsg::Dump.into());
}

async fn process_ibus_msg(&mut self, msg: IbusMsg) {
Expand Down
8 changes: 4 additions & 4 deletions holo-bfd/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use generational_arena::{Arena, Index};
use holo_northbound::yang::control_plane_protocol::bfd;
use holo_protocol::InstanceChannelsTx;
use holo_utils::bfd::{ClientCfg, ClientId, SessionKey, State};
use holo_utils::ibus::{BfdSessionMsg, IbusMsg};
use holo_utils::ibus::BfdSessionMsg;
use holo_utils::ip::{IpAddrExt, IpAddrKind};
use holo_utils::socket::{UdpSocket, TTL_MAX};
use holo_utils::task::{IntervalTask, TimeoutTask};
Expand Down Expand Up @@ -141,11 +141,11 @@ impl Session {

// Notify protocol clients about the state transition if necessary.
if self.should_notify_clients(old_state) && !self.clients.is_empty() {
let msg = IbusMsg::BfdSession(BfdSessionMsg::Update {
let msg = BfdSessionMsg::Update {
sess_key: self.key.clone(),
state,
});
let _ = tx.ibus.send(msg);
};
let _ = tx.ibus.send(msg.into());
}

// Send YANG notification.
Expand Down
9 changes: 5 additions & 4 deletions holo-bgp/src/northbound/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use holo_northbound::configuration::{
};
use holo_northbound::yang::control_plane_protocol::bgp;
use holo_utils::bgp::AfiSafi;
use holo_utils::ibus::{IbusMsg, RouteRedistributeMsg};
use holo_utils::ibus::RouteRedistributeMsg;
use holo_utils::ip::{AddressFamily, IpAddrKind};
use holo_utils::policy::{ApplyPolicyCfg, DefaultPolicyType};
use holo_utils::protocol::Protocol;
Expand Down Expand Up @@ -1363,12 +1363,13 @@ impl Provider for Instance {
}
}
Event::RedistributeRequest(protocol, af) => {
let _ = self.tx.ibus.send(IbusMsg::RouteRedistribute(
let _ = self.tx.ibus.send(
RouteRedistributeMsg::Dump {
protocol,
af: Some(af),
},
));
}
.into(),
);
}
Event::RedistributeDelete(protocol, afi_safi) => {
let Some((mut instance, _)) = self.as_up() else {
Expand Down
28 changes: 12 additions & 16 deletions holo-bgp/src/southbound/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
use std::collections::BTreeSet;
use std::net::IpAddr;

use holo_utils::ibus::{
IbusMsg, IbusSender, NexthopMsg, RouteIpMsg, RouterIdMsg,
};
use holo_utils::ibus::{IbusSender, NexthopMsg, RouteIpMsg, RouterIdMsg};
use holo_utils::protocol::Protocol;
use holo_utils::southbound::{
Nexthop, RouteKeyMsg, RouteMsg, RouteOpaqueAttrs,
Expand All @@ -21,7 +19,7 @@ use crate::rib::LocalRoute;
// ===== global functions =====

pub(crate) fn router_id_query(ibus_tx: &IbusSender) {
let _ = ibus_tx.send(IbusMsg::RouterId(RouterIdMsg::Query));
let _ = ibus_tx.send(RouterIdMsg::Query.into());
}

pub(crate) fn route_install(
Expand All @@ -43,38 +41,36 @@ pub(crate) fn route_install(
.collect::<BTreeSet<_>>();

// Install route.
let msg = RouteMsg {
let msg = RouteIpMsg::Add(RouteMsg {
protocol: Protocol::BGP,
prefix: prefix.into(),
distance: distance.into(),
metric: route.attrs.base.value.med.unwrap_or(0),
tag: None,
opaque_attrs: RouteOpaqueAttrs::None,
nexthops: nexthops.clone(),
};
let msg = IbusMsg::RouteIp(RouteIpMsg::Add(msg));
let _ = ibus_tx.send(msg);
});
let _ = ibus_tx.send(msg.into());
}

pub(crate) fn route_uninstall(
ibus_tx: &IbusSender,
prefix: impl Into<IpNetwork>,
) {
// Uninstall route.
let msg = RouteKeyMsg {
let msg = RouteIpMsg::Delete(RouteKeyMsg {
protocol: Protocol::BGP,
prefix: prefix.into(),
};
let msg = IbusMsg::RouteIp(RouteIpMsg::Delete(msg));
let _ = ibus_tx.send(msg);
});
let _ = ibus_tx.send(msg.into());
}

pub(crate) fn nexthop_track(ibus_tx: &IbusSender, addr: IpAddr) {
let msg = IbusMsg::Nexthop(NexthopMsg::Track(addr));
let _ = ibus_tx.send(msg);
let msg = NexthopMsg::Track(addr);
let _ = ibus_tx.send(msg.into());
}

pub(crate) fn nexthop_untrack(ibus_tx: &IbusSender, addr: IpAddr) {
let msg = IbusMsg::Nexthop(NexthopMsg::Untrack(addr));
let _ = ibus_tx.send(msg);
let msg = NexthopMsg::Untrack(addr);
let _ = ibus_tx.send(msg.into());
}
29 changes: 14 additions & 15 deletions holo-interface/src/ibus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ pub(crate) fn notify_router_id_update(
ibus_tx: &IbusSender,
router_id: Option<Ipv4Addr>,
) {
let msg = IbusMsg::RouterId(RouterIdMsg::Update(router_id));
notify(ibus_tx, msg);
let msg = RouterIdMsg::Update(router_id);
notify(ibus_tx, msg.into());
}

pub(crate) fn notify_interface_update(ibus_tx: &IbusSender, iface: &Interface) {
Expand All @@ -86,14 +86,14 @@ pub(crate) fn notify_interface_update(ibus_tx: &IbusSender, iface: &Interface) {
flags: iface.flags,
mac_address: iface.mac_address,
};
let msg = IbusMsg::Interface(InterfaceMsg::Update(update_msg));
let msg = InterfaceMsg::Update(update_msg);

notify(ibus_tx, msg);
notify(ibus_tx, msg.into());
}

pub(crate) fn notify_interface_del(ibus_tx: &IbusSender, ifname: String) {
let msg = IbusMsg::Interface(InterfaceMsg::Delete(ifname));
notify(ibus_tx, msg);
let msg = InterfaceMsg::Delete(ifname);
notify(ibus_tx, msg.into());
}

pub(crate) fn notify_addr_add(
Expand All @@ -107,8 +107,8 @@ pub(crate) fn notify_addr_add(
addr,
flags,
};
let msg = IbusMsg::InterfaceAddress(InterfaceAddressMsg::Add(addr_msg));
notify(ibus_tx, msg);
let msg = InterfaceAddressMsg::Add(addr_msg);
notify(ibus_tx, msg.into());
}

pub(crate) fn notify_addr_del(
Expand All @@ -117,13 +117,12 @@ pub(crate) fn notify_addr_del(
addr: IpNetwork,
flags: AddressFlags,
) {
let msg =
IbusMsg::InterfaceAddress(InterfaceAddressMsg::Delete(AddressMsg {
ifname,
addr,
flags,
}));
notify(ibus_tx, msg);
let msg = InterfaceAddressMsg::Delete(AddressMsg {
ifname,
addr,
flags,
});
notify(ibus_tx, msg.into());
}

// ===== helper functions =====
Expand Down
7 changes: 4 additions & 3 deletions holo-isis/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::sync::Arc;

use chrono::{DateTime, Utc};
use holo_protocol::InstanceChannelsTx;
use holo_utils::ibus::{IbusMsg, IbusSender, InterfaceMsg};
use holo_utils::ibus::{IbusSender, InterfaceMsg};
use holo_utils::ip::AddressFamily;
use holo_utils::socket::{AsyncFd, Socket, SocketExt};
use holo_utils::southbound::InterfaceFlags;
Expand Down Expand Up @@ -643,10 +643,11 @@ impl Interface {
// Sends a southbound request for interface system information, such as
// operational status and IP addresses.
pub(crate) fn query_southbound(&self, ibus_tx: &IbusSender) {
let _ = ibus_tx.send(IbusMsg::Interface(InterfaceMsg::Query {
let msg = InterfaceMsg::Query {
ifname: self.name.clone(),
af: None,
}));
};
let _ = ibus_tx.send(msg.into());
}
}

Expand Down
4 changes: 2 additions & 2 deletions holo-isis/src/southbound/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
// See: https://nlnet.nl/NGI0
//

use holo_utils::ibus::{IbusMsg, IbusSender, RouterIdMsg};
use holo_utils::ibus::{IbusSender, RouterIdMsg};

// ===== global functions =====

pub(crate) fn router_id_query(ibus_tx: &IbusSender) {
let _ = ibus_tx.send(IbusMsg::RouterId(RouterIdMsg::Query));
let _ = ibus_tx.send(RouterIdMsg::Query.into());
}
10 changes: 5 additions & 5 deletions holo-keychain/src/northbound/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use holo_northbound::configuration::{
};
use holo_northbound::yang::key_chains;
use holo_utils::crypto::CryptoAlgo;
use holo_utils::ibus::{IbusMsg, KeychainMsg};
use holo_utils::ibus::KeychainMsg;
use holo_utils::keychain::{Key, Keychain, KeychainKey};
use holo_utils::yang::DataNodeRefExt;
use holo_yang::TryFromYang;
Expand Down Expand Up @@ -443,13 +443,13 @@ impl Provider for Master {
let keychain = Arc::new(keychain.clone());

// Notify protocols that the keychain has been updated.
let msg = IbusMsg::Keychain(KeychainMsg::Update(keychain));
let _ = self.ibus_tx.send(msg);
let msg = KeychainMsg::Update(keychain);
let _ = self.ibus_tx.send(msg.into());
}
Event::KeychainDelete(name) => {
// Notify protocols that the keychain has been deleted.
let msg = IbusMsg::Keychain(KeychainMsg::Delete(name));
let _ = self.ibus_tx.send(msg);
let msg = KeychainMsg::Delete(name);
let _ = self.ibus_tx.send(msg.into());
}
}
}
Expand Down
13 changes: 6 additions & 7 deletions holo-ldp/src/northbound/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use holo_northbound::configuration::{
ValidationCallbacksBuilder,
};
use holo_northbound::yang::control_plane_protocol::mpls_ldp;
use holo_utils::ibus::{IbusMsg, InterfaceMsg};
use holo_utils::ibus::InterfaceMsg;
use holo_utils::ip::AddressFamily;
use holo_utils::yang::DataNodeRefExt;

Expand Down Expand Up @@ -471,12 +471,11 @@ impl Provider for Instance {
}
Event::InterfaceQuerySouthbound(ifname) => {
if let Some((instance, _, _)) = self.as_up() {
let _ = instance.tx.ibus.send(IbusMsg::Interface(
InterfaceMsg::Query {
ifname,
af: Some(AddressFamily::Ipv4),
},
));
let msg = InterfaceMsg::Query {
ifname,
af: Some(AddressFamily::Ipv4),
};
let _ = instance.tx.ibus.send(msg.into());
}
}
Event::TargetedNbrUpdate(tnbr_idx) => {
Expand Down
18 changes: 8 additions & 10 deletions holo-ldp/src/southbound/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// SPDX-License-Identifier: MIT
//

use holo_utils::ibus::{IbusMsg, IbusSender, RouteMplsMsg, RouterIdMsg};
use holo_utils::ibus::{IbusSender, RouteMplsMsg, RouterIdMsg};
use holo_utils::protocol::Protocol;
use holo_utils::southbound::{self, LabelInstallMsg, LabelUninstallMsg};

Expand All @@ -13,7 +13,7 @@ use crate::fec::{FecInner, Nexthop};
// ===== global functions =====

pub(crate) fn router_id_query(ibus_tx: &IbusSender) {
let _ = ibus_tx.send(IbusMsg::RouterId(RouterIdMsg::Query));
let _ = ibus_tx.send(RouterIdMsg::Query.into());
}

pub(crate) fn label_install(
Expand All @@ -35,7 +35,7 @@ pub(crate) fn label_install(
let protocol = fec.protocol.unwrap();

// Fill-in message.
let msg = LabelInstallMsg {
let msg = RouteMplsMsg::Add(LabelInstallMsg {
protocol: Protocol::LDP,
label: local_label,
nexthops: [southbound::Nexthop::Address {
Expand All @@ -46,11 +46,10 @@ pub(crate) fn label_install(
.into(),
route: Some((protocol, *fec.prefix)),
replace: false,
};
});

// Send message.
let msg = IbusMsg::RouteMpls(RouteMplsMsg::Add(msg));
let _ = ibus_tx.send(msg);
let _ = ibus_tx.send(msg.into());
}

pub(crate) fn label_uninstall(
Expand All @@ -72,7 +71,7 @@ pub(crate) fn label_uninstall(
let protocol = fec.protocol.unwrap();

// Fill-in message.
let msg = LabelUninstallMsg {
let msg = RouteMplsMsg::Delete(LabelUninstallMsg {
protocol: Protocol::LDP,
label: local_label,
nexthops: [southbound::Nexthop::Address {
Expand All @@ -82,9 +81,8 @@ pub(crate) fn label_uninstall(
}]
.into(),
route: Some((protocol, *fec.prefix)),
};
});

// Send message.
let msg = IbusMsg::RouteMpls(RouteMplsMsg::Delete(msg));
let _ = ibus_tx.send(msg);
let _ = ibus_tx.send(msg.into());
}
28 changes: 12 additions & 16 deletions holo-ospf/src/neighbor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::sync::Arc;

use chrono::{DateTime, Utc};
use holo_utils::bfd;
use holo_utils::ibus::IbusMsg;
use holo_utils::ibus::BfdSessionMsg;
use holo_utils::task::{IntervalTask, TimeoutTask};
use nsm::{Event, State};
use rand::RngCore;
Expand Down Expand Up @@ -553,14 +553,12 @@ where
) {
Debug::<V>::NeighborBfdReg(self.router_id).log();

let msg = IbusMsg::BfdSession(
holo_utils::ibus::BfdSessionMsg::Registration {
sess_key: self.bfd_session_key(iface),
client_id: self.bfd_client_id(instance),
client_config: Some(iface.config.bfd_params),
},
);
let _ = instance.tx.ibus.send(msg);
let msg = BfdSessionMsg::Registration {
sess_key: self.bfd_session_key(iface),
client_id: self.bfd_client_id(instance),
client_config: Some(iface.config.bfd_params),
};
let _ = instance.tx.ibus.send(msg.into());
}

pub(crate) fn bfd_unregister(
Expand All @@ -570,13 +568,11 @@ where
) {
Debug::<V>::NeighborBfdUnreg(self.router_id).log();

let msg = IbusMsg::BfdSession(
holo_utils::ibus::BfdSessionMsg::Unregistration {
sess_key: self.bfd_session_key(iface),
client_id: self.bfd_client_id(instance),
},
);
let _ = instance.tx.ibus.send(msg);
let msg = BfdSessionMsg::Unregistration {
sess_key: self.bfd_session_key(iface),
client_id: self.bfd_client_id(instance),
};
let _ = instance.tx.ibus.send(msg.into());
}

fn bfd_session_key(&self, iface: &Interface<V>) -> bfd::SessionKey {
Expand Down
Loading

0 comments on commit 625e839

Please sign in to comment.