Skip to content

Commit

Permalink
Use HumanReadableCommunity
Browse files Browse the repository at this point in the history
  • Loading branch information
density215 committed Dec 21, 2023
1 parent 1962471 commit 708775f
Show file tree
Hide file tree
Showing 16 changed files with 90 additions and 74 deletions.
4 changes: 3 additions & 1 deletion examples/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use roto::types::typevalue::TypeValue;
use roto::vm;
use roto::blocks::Scope::{self, FilterMap};

use routecore::bgp::communities::HumanReadableCommunity as Community;

fn test_data(
name: Scope,
source_code: &'static str,
Expand All @@ -30,7 +32,7 @@ fn test_data(
let asn = Asn::from_u32(211321).into();
let comms =
TypeValue::List(List::new(vec![ElementTypeValue::Primitive(
routecore::bgp::communities::Community::from([127, 12, 13, 12]).into())
Community::from([127, 12, 13, 12]).into())
]));
let my_comms_type =
TypeDef::List(Box::new(TypeDef::List(Box::new(TypeDef::Community))));
Expand Down
2 changes: 1 addition & 1 deletion examples/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn test_data(
.0
.announcements()
.unwrap()
.filter_map(|p| if let Ok(Nlri::Unicast(BasicNlri { prefix, .. })) = p { Some(Prefix::from(prefix)) } else { None })
.filter_map(|p| if let Ok(Nlri::Unicast(BasicNlri { prefix, .. })) = p { Some(prefix) } else { None })
.collect();
let msg_id = (RotondaId(0), 0);

Expand Down
4 changes: 3 additions & 1 deletion examples/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use roto::types::collections::{ElementTypeValue, List, Record};
use roto::types::typedef::TypeDef;
use roto::types::typevalue::TypeValue;

use routecore::bgp::communities::HumanReadableCommunity as Community;

fn main() {
// let count = RotoType::create_primitive_var(
// RotoType::Asn,
Expand Down Expand Up @@ -45,7 +47,7 @@ fn main() {

let comms =
TypeValue::List(List::new(vec![ElementTypeValue::Primitive(
routecore::bgp::communities::Community::from([127, 12, 13, 12]).into())
Community::from([127, 12, 13, 12]).into())
]));

let my_comms_type =
Expand Down
31 changes: 16 additions & 15 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ use nom::{AsChar, Finish};
use serde::{Serialize, Serializer};
use smallvec::SmallVec;

use routecore::bgp::communities::HumanReadableCommunity as Community;
use routecore::bgp::communities::{ExtendedCommunity, LargeCommunity, StandardCommunity};

use crate::compiler::error::CompileError;
use crate::types::builtin::{Asn, Boolean};
use crate::types::typevalue::TypeValue;
Expand Down Expand Up @@ -2147,23 +2150,23 @@ impl From<&'_ StandardCommunityLiteral> for ShortString {
}
}

impl TryFrom<&'_ StandardCommunityLiteral> for routecore::bgp::communities::Community {
impl TryFrom<&'_ StandardCommunityLiteral> for Community {
type Error = CompileError;

// The aforementioned heavy lifting is here.
fn try_from(
literal: &StandardCommunityLiteral,
) -> Result<Self, Self::Error> {
let comm = <routecore::bgp::communities::StandardCommunity as str::FromStr>::from_str(
let comm = <StandardCommunity as str::FromStr>::from_str(
&literal.0
).map_err(
|e| CompileError::from(format!(
"Cannot convert literal '{}' into Extended Community: {e}", literal.0,
)))?;

Ok(routecore::bgp::communities::Community::Standard(
comm,
))
Ok(
comm.into(),
)
}
}

Expand Down Expand Up @@ -2252,23 +2255,23 @@ impl From<&'_ ExtendedCommunityLiteral> for ShortString {
}
}

impl<'a> TryFrom<&'a ExtendedCommunityLiteral> for routecore::bgp::communities::Community {
impl<'a> TryFrom<&'a ExtendedCommunityLiteral> for Community {
type Error = CompileError;

// The aforementioned heavy lifting is here.
fn try_from(
literal: &ExtendedCommunityLiteral,
) -> Result<Self, Self::Error> {
let comm = <routecore::bgp::communities::ExtendedCommunity as str::FromStr>::from_str(
let comm = <ExtendedCommunity as str::FromStr>::from_str(
&literal.0
).map_err(
|e| CompileError::from(format!(
"Cannot convert literal '{}' into Extended Community: {e}", literal.0,
)))?;

Ok(routecore::bgp::communities::Community::Extended(
comm,
))
Ok(
comm.into()
)
}
}

Expand Down Expand Up @@ -2378,23 +2381,21 @@ impl From<&'_ LargeCommunityLiteral> for ShortString {
}
}

impl TryFrom<&'_ LargeCommunityLiteral> for routecore::bgp::communities::Community {
impl TryFrom<&'_ LargeCommunityLiteral> for Community {
type Error = CompileError;

// The aforementioned heavy lifting is here.
fn try_from(
literal: &LargeCommunityLiteral,
) -> Result<Self, Self::Error> {
let comm = <routecore::bgp::communities::LargeCommunity as str::FromStr>::from_str(
let comm = <LargeCommunity as str::FromStr>::from_str(
&literal.0
).map_err(
|e| CompileError::from(format!(
"Cannot convert literal '{}' into Large Community: {e}", literal.0,
)))?;

Ok(routecore::bgp::communities::Community::Large(
comm,
))
Ok(comm.into())
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/attr_change_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use routecore::bgp::aspath::HopPath;
use routecore::bgp::message::nlri::PathId;
use routecore::bgp::types::AfiSafi;
use routecore::addr::Prefix;
use routecore::bgp::communities::Community;
use routecore::bgp::communities::HumanReadableCommunity as Community;
use serde::Serialize;
use std::marker::PhantomData;
use std::ops::Index;
Expand Down
2 changes: 1 addition & 1 deletion src/types/builtin/builtin_type_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::fmt::Display;
use routecore::bgp::message::nlri::PathId;
use routecore::bgp::types::{AfiSafi, NextHop};
use routecore::addr::Prefix;
use routecore::bgp::communities::Community;
use routecore::bgp::communities::HumanReadableCommunity as Community;
use serde::Serialize;

use crate::compiler::compile::CompileError;
Expand Down
24 changes: 10 additions & 14 deletions src/types/builtin/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ use super::super::typedef::TypeDef;
use super::super::typevalue::TypeValue;
use super::builtin_type_value::BuiltinTypeValue;

use routecore::bgp::communities::HumanReadableCommunity as Community;

//------------ U16 Type -----------------------------------------------------

// #[derive(Debug, Eq, Copy, Clone, Serialize)]
Expand Down Expand Up @@ -2244,7 +2246,7 @@ impl RotoType for routecore::bgp::message::nlri::PathId {
// }
// }

impl RotoType for routecore::bgp::communities::Community {
impl RotoType for Community {
fn get_props_for_method(
_ty: TypeDef,
method_name: &crate::ast::Identifier,
Expand Down Expand Up @@ -2385,28 +2387,23 @@ impl RotoType for routecore::bgp::communities::Community {
}
}

impl ScalarValue for routecore::bgp::communities::Community {}
impl ScalarValue for Community {}

impl From<routecore::bgp::communities::Community> for TypeValue {
fn from(val: routecore::bgp::communities::Community) -> Self {
impl From<Community> for TypeValue {
fn from(val: Community) -> Self {
TypeValue::Builtin(BuiltinTypeValue::Community(val))
}
}

// impl From<routecore::bgp::communities::Community> for TypeValue {
// fn from(val: routecore::bgp::communities::Community) -> Self {
// TypeValue::Builtin(BuiltinTypeValue::Community(val))
// }
// }

impl From<routecore::bgp::communities::Community> for BuiltinTypeValue {
fn from(value: routecore::bgp::communities::Community) -> Self {
impl From<Community> for BuiltinTypeValue {
fn from(value: Community) -> Self {
BuiltinTypeValue::Community(value)
}
}

impl From<Vec<routecore::bgp::communities::Community>> for TypeValue {
fn from(value: Vec<routecore::bgp::communities::Community>) -> Self {
impl From<Vec<Community>> for TypeValue {
fn from(value: Vec<Community>) -> Self {
let list: Vec<ElementTypeValue> = value
.iter()
.map(|c| ElementTypeValue::Primitive(TypeValue::from(*c)))
Expand Down Expand Up @@ -3009,7 +3006,6 @@ impl VectorValue for routecore::bgp::aspath::HopPath {
&mut self,
vector: Vec<Self::WriteItem>,
) -> Result<(), LongSegmentError> {

for asn in vector {
self.append(OwnedHop::Asn(asn.0));
}
Expand Down
43 changes: 27 additions & 16 deletions src/types/builtin/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@
// └─────────────┘ status

use log::{debug, error};
use routecore::bgp::{message::{SessionConfig, nlri::PathId}, types::{AfiSafi, NextHop}, aspath::HopPath};
use routecore::addr::Prefix;
use routecore::bgp::{
aspath::HopPath,
communities::HumanReadableCommunity as Community,
message::{nlri::PathId, SessionConfig},
types::{AfiSafi, NextHop},
};
use serde::{Deserialize, Serialize};
use smallvec::SmallVec;
use std::{net::IpAddr, sync::Arc};
Expand All @@ -35,10 +40,7 @@ use crate::{
vm::{StackValue, VmError},
};

use super::{
Asn, BuiltinTypeValue, IpAddress, OriginType,
RouteStatus,
};
use super::{Asn, BuiltinTypeValue, IpAddress, OriginType, RouteStatus};
use crate::attr_change_set::{
AttrChangeSet, ScalarOption, ScalarValue, VectorOption, VectorValue,
};
Expand Down Expand Up @@ -161,7 +163,7 @@ impl RawRouteWithDeltas {
peer_asn,
router_id.clone(),
afi_safi,
path_id
path_id,
)?,
))?;

Expand Down Expand Up @@ -280,7 +282,7 @@ impl RawRouteWithDeltas {
self.peer_asn,
self.router_id.clone(),
self.afi_safi,
self.path_id
self.path_id,
)?)
}
}
Expand Down Expand Up @@ -311,7 +313,7 @@ impl RawRouteWithDeltas {
self.peer_asn,
self.router_id,
self.afi_safi,
self.path_id
self.path_id,
);
}

Expand All @@ -337,7 +339,7 @@ impl RawRouteWithDeltas {
self.peer_asn,
self.router_id.clone(),
self.afi_safi,
self.path_id
self.path_id,
)?,
))?;
}
Expand Down Expand Up @@ -451,7 +453,9 @@ impl RawRouteWithDeltas {
&self,
field_token: usize,
) -> Result<Option<&TypeValue>, VmError> {
let current_set = if let Some(atrd) = self.attribute_deltas.get_latest_change_set() {
let current_set = if let Some(atrd) =
self.attribute_deltas.get_latest_change_set()
{
atrd
} else {
return Err(VmError::InvalidRecord);
Expand Down Expand Up @@ -481,7 +485,6 @@ impl RawRouteWithDeltas {
&self,
field_token: usize,
) -> Result<TypeValue, VmError> {

match field_token.try_into()? {
RouteToken::AsPath => self
.raw_message
Expand Down Expand Up @@ -548,15 +551,21 @@ impl RawRouteWithDeltas {
.raw_message
.raw_message
.0
.all_communities()
.all_communities::<Community>()
.ok()
.flatten()
.map(TypeValue::from)
.ok_or(VmError::InvalidFieldAccess),
RouteToken::Prefix => Ok(self.prefix.into()),
RouteToken::Status => Ok(self.status_deltas.current()),
RouteToken::PeerIp => self.peer_ip.map(TypeValue::from).ok_or(VmError::InvalidFieldAccess),
RouteToken::PeerAsn => self.peer_asn.map(TypeValue::from).ok_or(VmError::InvalidFieldAccess),
RouteToken::PeerIp => self
.peer_ip
.map(TypeValue::from)
.ok_or(VmError::InvalidFieldAccess),
RouteToken::PeerAsn => self
.peer_asn
.map(TypeValue::from)
.ok_or(VmError::InvalidFieldAccess),
// _ => None,
// originator_id: ChangedOption {
// value: None,
Expand Down Expand Up @@ -1192,7 +1201,7 @@ impl TryFrom<usize> for RouteToken {
9 => Ok(RouteToken::Status),
10 => Ok(RouteToken::PeerIp),
11 => Ok(RouteToken::PeerAsn),
_ => {
_ => {
debug!("Unknown RouteToken value: {}", value);
Err(VmError::InvalidMethodCall)
}
Expand Down Expand Up @@ -1386,7 +1395,9 @@ impl UpdateMessage {
self.0.aggregator().map_err(VmError::from)?,
),
communities: VectorOption::from(
self.0.all_communities().map_err(VmError::from)?,
self.0
.all_communities::<Community>()
.map_err(VmError::from)?,
),
peer_ip: peer_ip.into(),
peer_asn: peer_asn.into(),
Expand Down
12 changes: 5 additions & 7 deletions src/types/builtin/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ mod route {
_Consume,
}
use routecore::bgp::aspath::HopPath;
use routecore::bgp::communities::{Community, StandardCommunity, Tag, ExtendedCommunity, LargeCommunity};
use routecore::bgp::communities::{HumanReadableCommunity as Community, StandardCommunity, Tag, ExtendedCommunity, LargeCommunity};
use routecore::bgp::{
message::{
nlri::{BasicNlri, Nlri},
Expand Down Expand Up @@ -932,12 +932,11 @@ src_ty.clone().test_type_conversion(arg_ty)"]

#[test]
fn test_standard_community_2() {
let test_value = Community::Standard(
let test_value: Community =
StandardCommunity::new(
routecore::asn::Asn16::from(12500),
Tag::new(7890),
),
);
).into();
let res = StringLiteral::new("AS12500:7890".to_string());

mk_converted_type_value(test_value, res).unwrap();
Expand Down Expand Up @@ -986,12 +985,11 @@ src_ty.clone().test_type_conversion(arg_ty)"]
LargeCommunity::from_str(
"234:123:456"
).unwrap().into();
let res = Community::Standard(
let res: Community =
StandardCommunity::new(
routecore::asn::Asn16::from(7500),
Tag::new(3000),
)
);
).into();

test_consume_method_on_type_value(test_value, "set", res)
}
Expand Down
Loading

0 comments on commit 708775f

Please sign in to comment.