1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#[macro_use]
extern crate wascc_codec as codec;
extern crate wasccgraph_common as common;
#[macro_use]
extern crate log;
use codec::capabilities::{
CapabilityDescriptor, CapabilityProvider, Dispatcher, NullDispatcher, OperationDirection,
OP_GET_CAPABILITY_DESCRIPTOR,
};
use codec::core::{CapabilityConfiguration, OP_BIND_ACTOR, OP_REMOVE_ACTOR};
use codec::{deserialize, serialize};
use std::error::Error;
use std::{
collections::HashMap,
sync::{Arc, RwLock},
};
use common::protocol::*;
use redis::Connection;
use redis::RedisResult;
use redisgraph::{Graph, RedisGraphResult, ResultSet};
mod rgraph;
const SYSTEM_ACTOR: &str = "system";
const VERSION: &str = env!("CARGO_PKG_VERSION");
const REVISION: u32 = 2;
#[cfg(not(feature = "static_plugin"))]
capability_provider!(WasccRedisgraphProvider, WasccRedisgraphProvider::new);
pub struct WasccRedisgraphProvider {
dispatcher: RwLock<Box<dyn Dispatcher>>,
clients: Arc<RwLock<HashMap<String, redis::Client>>>,
}
impl Default for WasccRedisgraphProvider {
fn default() -> Self {
let _ = env_logger::builder().format_module_path(false).try_init();
WasccRedisgraphProvider {
dispatcher: RwLock::new(Box::new(NullDispatcher::new())),
clients: Arc::new(RwLock::new(HashMap::new())),
}
}
}
impl WasccRedisgraphProvider {
pub fn new() -> Self {
Self::default()
}
fn query_graph(&self, actor: &str, query: QueryRequest) -> Result<Vec<u8>, Box<dyn Error>> {
trace!("Querying graph database: {:?}", query);
let mut g = self.open_graph(actor, &query.graph_name)?;
let rs: RedisGraphResult<ResultSet> = g.query(&query.query);
match rs {
Ok(rs) => Ok(serialize(&to_common_resultset(rs)?)?),
Err(e) => Err(format!("Graph query failure: {:?}", e).into()),
}
}
fn delete_graph(&self, actor: &str, delete: DeleteRequest) -> Result<Vec<u8>, Box<dyn Error>> {
let g = self.open_graph(actor, &delete.graph_name)?;
let rs: RedisGraphResult<()> = g.delete();
match rs {
Ok(_) => Ok(vec![]),
Err(e) => Err(format!("Failed to delete graph: {:?}", e).into()),
}
}
fn deconfigure(&self, actor: &str) -> Result<Vec<u8>, Box<dyn Error>> {
if self.clients.write().unwrap().remove(actor).is_none() {
warn!("Attempted to de-configure non-existent actor: {}", actor);
}
Ok(vec![])
}
fn configure(&self, config: CapabilityConfiguration) -> Result<Vec<u8>, Box<dyn Error>> {
trace!("Configuring provider for {}", &config.module);
let c = rgraph::initialize_client(config.clone())?;
self.clients.write().unwrap().insert(config.module, c);
Ok(vec![])
}
fn actor_con(&self, actor: &str) -> RedisResult<Connection> {
let lock = self.clients.read().unwrap();
if let Some(client) = lock.get(actor) {
client.get_connection()
} else {
Err(redis::RedisError::from((
redis::ErrorKind::InvalidClientConfig,
"No client for this actor. Did the host configure it?",
)))
}
}
fn open_graph(&self, actor: &str, graph: &str) -> Result<Graph, Box<dyn Error>> {
let conn = self.actor_con(actor)?;
let g = rgraph::open_graph(conn, &graph)?;
Ok(g)
}
fn get_descriptor(&self) -> Result<Vec<u8>, Box<dyn Error>> {
Ok(serialize(
CapabilityDescriptor::builder()
.id(common::CAPID_GRAPHDB)
.name("waSCC Graph Database Provider (RedisGraph)")
.long_description("A capability provider exposing Cypher-based RedisGraph database access to waSCC actors")
.version(VERSION)
.revision(REVISION)
.with_operation(
OP_QUERY,
OperationDirection::ToProvider,
"Executes a Cypher query against the database and returns the results"
)
.with_operation(
OP_DELETE,
OperationDirection::ToProvider,
"Deletes a graph database"
)
.build()
)?)
}
}
fn to_common_resultset(rs: redisgraph::ResultSet) -> Result<common::ResultSet, Box<dyn Error>> {
let input = serialize(&rs)?;
let output: common::ResultSet = deserialize(&input)?;
Ok(output)
}
impl CapabilityProvider for WasccRedisgraphProvider {
fn configure_dispatch(&self, dispatcher: Box<dyn Dispatcher>) -> Result<(), Box<dyn Error>> {
trace!("Dispatcher received.");
let mut lock = self.dispatcher.write().unwrap();
*lock = dispatcher;
Ok(())
}
fn handle_call(&self, actor: &str, op: &str, msg: &[u8]) -> Result<Vec<u8>, Box<dyn Error>> {
trace!("Received host call from {}, operation - {}", actor, op);
match op {
OP_BIND_ACTOR if actor == SYSTEM_ACTOR => self.configure(deserialize(msg)?),
OP_QUERY => self.query_graph(actor, deserialize(msg)?),
OP_DELETE => self.delete_graph(actor, deserialize(msg)?),
OP_REMOVE_ACTOR if actor == SYSTEM_ACTOR => self.deconfigure(actor),
OP_GET_CAPABILITY_DESCRIPTOR if actor == SYSTEM_ACTOR => self.get_descriptor(),
_ => Err("bad dispatch".into()),
}
}
}