Skip to content

Commit 2d6bbd9

Browse files
fix: keep only the from_strings method to create an AgentType (#288)
# Description Allow direct creation of agent type only via the from_strings method. Signed-off-by: Zsolt Kacsándi <[email protected]> Signed-off-by: Mauro Sardara <[email protected]> Co-authored-by: Mauro Sardara <[email protected]>
1 parent ffa5eed commit 2d6bbd9

File tree

5 files changed

+84
-116
lines changed

5 files changed

+84
-116
lines changed

data-plane/gateway/datapath/src/messages/encoder.rs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,6 @@ impl From<&ProtoAgent> for AgentType {
6464
}
6565

6666
impl AgentType {
67-
/// Create a new AgentType
68-
pub fn new(organization: u64, namespace: u64, agent_type: u64) -> Self {
69-
Self {
70-
organization,
71-
namespace,
72-
agent_type,
73-
strings: None,
74-
}
75-
}
76-
7767
pub fn from_strings(organization: &str, namespace: &str, agent_type: &str) -> Self {
7868
Self {
7969
organization: calculate_hash(organization),
@@ -87,21 +77,6 @@ impl AgentType {
8777
}
8878
}
8979

90-
pub fn with_organization(self, organization: u64) -> Self {
91-
Self {
92-
organization,
93-
..self
94-
}
95-
}
96-
97-
pub fn with_namespace(self, namespace: u64) -> Self {
98-
Self { namespace, ..self }
99-
}
100-
101-
pub fn with_agent_type(self, agent_type: u64) -> Self {
102-
Self { agent_type, ..self }
103-
}
104-
10580
pub fn organization(&self) -> u64 {
10681
self.organization
10782
}

data-plane/gateway/datapath/src/messages/utils.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,7 @@ mod tests {
942942

943943
#[test]
944944
fn test_conversions() {
945-
// ProtoAgent to Agent
945+
// Agent to ProtoAgent
946946
let agent = Agent::from_strings("org", "ns", "type", 1);
947947
let proto_agent = ProtoAgent::from(&agent);
948948

@@ -951,6 +951,22 @@ mod tests {
951951
assert_eq!(proto_agent.agent_type, agent.agent_type().agent_type());
952952
assert_eq!(proto_agent.agent_id.unwrap(), agent.agent_id());
953953

954+
// ProtoAgent to Agent
955+
let agent_from_proto = Agent::from(&proto_agent);
956+
assert_eq!(
957+
agent_from_proto.agent_type().organization(),
958+
proto_agent.organization
959+
);
960+
assert_eq!(
961+
agent_from_proto.agent_type().namespace(),
962+
proto_agent.namespace
963+
);
964+
assert_eq!(
965+
agent_from_proto.agent_type().agent_type(),
966+
proto_agent.agent_type
967+
);
968+
assert_eq!(agent_from_proto.agent_id(), proto_agent.agent_id.unwrap());
969+
954970
// AgentType to ProtoAgent
955971
let agent_type = AgentType::from_strings("org", "ns", "type");
956972
let proto_agent = ProtoAgent::from((&agent_type, Some(1)));

data-plane/testing/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ Options:
2222
#### Workload file structure
2323
The names used by the gateway are in the form
2424
```
25-
organinzation/namespace/agent_class/agent_id
25+
organization/namespace/agent_class/agent_id
2626
```
2727
These four components are strings decided by the application. For performance reason the gateway internally encodes these strings in u64 values. The workload generator application ignores the strings and creates random names by generating four random u64.
2828

2929
A subscription is represented as follow
3030
```
3131
SUB 24 7 9025227877545173655 4093485893050047688 12129358409937561971 3376091243880048110
3232
```
33-
- ```SUB``` indicates that the line is a subscription
33+
- ```SUB``` indicates that the line is a subscription
3434
- ```24``` is the index of the subscription
3535
- ```7``` is the ID of the subscriber that will generate the subscription. This is randomly generated and is a value between 0 and ```AGENTS``` that is provided as input. Be sure to spawn enough subscriber apps (see below) to cover all the IDs
3636
- the last for numbers are random generated and are the subscription name in the form ```organization/namespace/agent_class/agent_id```

data-plane/testing/src/bin/workload-gen.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,25 @@ fn main() {
157157
let bar = ProgressBar::new(max_subscriptions as u64);
158158
while subscriptions < max_subscriptions {
159159
// create a new type
160-
let agent_type = AgentType::new(rng.random(), rng.random(), rng.random());
160+
let org = rand::rng()
161+
.sample_iter(&rand::distr::Alphanumeric)
162+
.take(7)
163+
.map(char::from)
164+
.collect::<String>();
165+
166+
let ns = rand::rng()
167+
.sample_iter(&rand::distr::Alphanumeric)
168+
.take(7)
169+
.map(char::from)
170+
.collect::<String>();
171+
172+
let atype = rand::rng()
173+
.sample_iter(&rand::distr::Alphanumeric)
174+
.take(7)
175+
.map(char::from)
176+
.collect::<String>();
177+
178+
let agent_type = AgentType::from_strings(&org, &ns, &atype);
161179

162180
let mut type_state = TypeState::default();
163181

data-plane/testing/src/lib.rs

Lines changed: 46 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright AGNTCY Contributors (https://github.com/agntcy)
2+
// SPDX-License-Identifier: Apache-2.0
3+
14
use std::str::SplitWhitespace;
25

36
use agp_datapath::messages::{Agent, AgentType};
@@ -28,6 +31,35 @@ pub struct ParsedMessage {
2831
pub receivers: Vec<u64>,
2932
}
3033

34+
fn parse_ids(iter: &mut SplitWhitespace<'_>) -> Result<Agent, ParsingError> {
35+
let org = iter
36+
.next()
37+
.ok_or(ParsingError::ParsingError(
38+
"missing organization".to_string(),
39+
))?
40+
.parse::<String>()
41+
.map_err(|e| ParsingError::ParsingError(format!("failed to parse organization: {}", e)))?;
42+
let namespace = iter
43+
.next()
44+
.ok_or(ParsingError::ParsingError("missing namespace".to_string()))?
45+
.parse::<String>()
46+
.map_err(|e| ParsingError::ParsingError(format!("failed to parse namespace: {}", e)))?;
47+
let agent_type_val = iter
48+
.next()
49+
.ok_or(ParsingError::ParsingError("missing agent_type".to_string()))?
50+
.parse::<String>()
51+
.map_err(|e| ParsingError::ParsingError(format!("failed to parse agent type: {}", e)))?;
52+
let agent_id = iter
53+
.next()
54+
.ok_or(ParsingError::ParsingError("missing agent_id".to_string()))?
55+
.parse::<u64>()
56+
.map_err(|e| ParsingError::ParsingError(format!("failed to parse agent id: {}", e)))?;
57+
58+
let a_type = AgentType::from_strings(&org, &namespace, &agent_type_val);
59+
60+
Ok(Agent::new(a_type, agent_id))
61+
}
62+
3163
pub fn parse_sub(mut iter: SplitWhitespace<'_>) -> Result<ParsedMessage, ParsingError> {
3264
let mut subscription = ParsedMessage {
3365
msg_type: "SUB".to_string(),
@@ -54,44 +86,7 @@ pub fn parse_sub(mut iter: SplitWhitespace<'_>) -> Result<ParsedMessage, Parsing
5486
},
5587
}
5688

57-
let mut a_type = AgentType::default();
58-
let mut sub = Agent::default();
59-
match iter.next().unwrap().parse::<u64>() {
60-
Ok(x) => {
61-
a_type = a_type.with_organization(x);
62-
}
63-
Err(e) => {
64-
return Err(ParsingError::ParsingError(e.to_string()));
65-
}
66-
}
67-
68-
match iter.next().unwrap().parse::<u64>() {
69-
Ok(x) => {
70-
a_type = a_type.with_namespace(x);
71-
}
72-
Err(e) => {
73-
return Err(ParsingError::ParsingError(e.to_string()));
74-
}
75-
}
76-
77-
match iter.next().unwrap().parse::<u64>() {
78-
Ok(x) => {
79-
a_type = a_type.with_agent_type(x);
80-
}
81-
Err(e) => {
82-
return Err(ParsingError::ParsingError(e.to_string()));
83-
}
84-
}
85-
86-
match iter.next().unwrap().parse::<u64>() {
87-
Ok(x) => {
88-
sub = sub.with_agent_id(x);
89-
sub = sub.with_agent_type(a_type);
90-
}
91-
Err(e) => {
92-
return Err(ParsingError::ParsingError(e.to_string()));
93-
}
94-
}
89+
let sub = parse_ids(&mut iter)?;
9590

9691
subscription.name = sub;
9792
Ok(subscription)
@@ -120,44 +115,7 @@ pub fn parse_pub(mut iter: SplitWhitespace<'_>) -> Result<ParsedMessage, Parsing
120115
}
121116

122117
// get the publication name
123-
let mut a_type = AgentType::default();
124-
let mut pub_name = Agent::default();
125-
match iter.next().unwrap().parse::<u64>() {
126-
Ok(x) => {
127-
a_type = a_type.with_organization(x);
128-
}
129-
Err(e) => {
130-
return Err(ParsingError::ParsingError(e.to_string()));
131-
}
132-
}
133-
134-
match iter.next().unwrap().parse::<u64>() {
135-
Ok(x) => {
136-
a_type = a_type.with_namespace(x);
137-
}
138-
Err(e) => {
139-
return Err(ParsingError::ParsingError(e.to_string()));
140-
}
141-
}
142-
143-
match iter.next().unwrap().parse::<u64>() {
144-
Ok(x) => {
145-
a_type = a_type.with_agent_type(x);
146-
}
147-
Err(e) => {
148-
return Err(ParsingError::ParsingError(e.to_string()));
149-
}
150-
}
151-
152-
match iter.next().unwrap().parse::<u64>() {
153-
Ok(x) => {
154-
pub_name = pub_name.with_agent_id(x);
155-
pub_name = pub_name.with_agent_type(a_type);
156-
}
157-
Err(e) => {
158-
return Err(ParsingError::ParsingError(e.to_string()));
159-
}
160-
}
118+
let pub_name = parse_ids(&mut iter)?;
161119

162120
// get the len of the possible receivers
163121
let size = match iter.next().unwrap().parse::<u64>() {
@@ -191,16 +149,17 @@ pub fn parse_pub(mut iter: SplitWhitespace<'_>) -> Result<ParsedMessage, Parsing
191149

192150
pub fn parse_line(line: &str) -> Result<ParsedMessage, ParsingError> {
193151
let mut iter = line.split_whitespace();
194-
let prefix = iter.next();
195-
196-
if prefix == Some("SUB") {
197-
return parse_sub(iter);
152+
let msg_type = iter
153+
.next()
154+
.ok_or_else(|| ParsingError::ParsingError("missing type".to_string()))?
155+
.to_string();
156+
157+
match msg_type.as_str() {
158+
"SUB" => parse_sub(iter),
159+
"PUB" => parse_pub(iter),
160+
_ => Err(ParsingError::ParsingError(format!(
161+
"unknown type: {}",
162+
msg_type
163+
))),
198164
}
199-
200-
if prefix == Some("PUB") {
201-
return parse_pub(iter);
202-
}
203-
204-
// unable to parse this line
205-
Err(ParsingError::ParsingError("unknown prefix".to_string()))
206165
}

0 commit comments

Comments
 (0)