use std::sync::Arc;
use axum::Json;
use axum::extract::State;
use axum::response::{IntoResponse, Response};
use axum::routing::post;
use crate::cpo::{
AuthorizeRemoteReservationStartRequest, AuthorizeRemoteReservationStopRequest,
AuthorizeRemoteStartRequest, AuthorizeRemoteStopRequest,
};
use crate::transport::Operation;
use crate::types::{Acknowledgement, Code, Validate};
pub trait CpoService: Send + Sync + 'static {
fn authorize_remote_start(
&self,
request: AuthorizeRemoteStartRequest,
) -> impl core::future::Future<Output = Acknowledgement> + Send;
fn authorize_remote_stop(
&self,
request: AuthorizeRemoteStopRequest,
) -> impl core::future::Future<Output = Acknowledgement> + Send;
fn reservation_start(
&self,
request: AuthorizeRemoteReservationStartRequest,
) -> impl core::future::Future<Output = Acknowledgement> + Send;
fn reservation_stop(
&self,
request: AuthorizeRemoteReservationStopRequest,
) -> impl core::future::Future<Output = Acknowledgement> + Send;
fn on_invalid_request(&self, violations: &crate::types::Violations) -> Option<Acknowledgement> {
Some(Acknowledgement::failure_with(Code::DataError, violations.to_string()))
}
}
struct AckResponse(Acknowledgement);
impl IntoResponse for AckResponse {
fn into_response(self) -> Response {
(axum::http::StatusCode::OK, Json(self.0)).into_response()
}
}
pub fn cpo_router<S: CpoService>(service: Arc<S>) -> axum::Router {
axum::Router::new()
.route(
Operation::AuthorizeRemoteStart.path_template(),
post(|State(s): State<Arc<S>>, Json(body): Json<serde_json::Value>| async move {
match accept(s.as_ref(), body) {
Err(response) => response,
Ok(request) => AckResponse(s.authorize_remote_start(request).await).into_response(),
}
}),
)
.route(
Operation::AuthorizeRemoteStop.path_template(),
post(|State(s): State<Arc<S>>, Json(body): Json<serde_json::Value>| async move {
match accept(s.as_ref(), body) {
Err(response) => response,
Ok(request) => AckResponse(s.authorize_remote_stop(request).await).into_response(),
}
}),
)
.route(
Operation::AuthorizeRemoteReservationStart.path_template(),
post(|State(s): State<Arc<S>>, Json(body): Json<serde_json::Value>| async move {
match accept(s.as_ref(), body) {
Err(response) => response,
Ok(request) => AckResponse(s.reservation_start(request).await).into_response(),
}
}),
)
.route(
Operation::AuthorizeRemoteReservationStop.path_template(),
post(|State(s): State<Arc<S>>, Json(body): Json<serde_json::Value>| async move {
match accept(s.as_ref(), body) {
Err(response) => response,
Ok(request) => AckResponse(s.reservation_stop(request).await).into_response(),
}
}),
)
.with_state(service)
}
fn accept<S: CpoService, T: serde::de::DeserializeOwned + Validate>(
service: &S,
body: serde_json::Value,
) -> Result<T, Response> {
let request: T = deserialize(body).map_err(|message| {
tracing::warn!(target: "oicp_kit::server", %message, "a request could not be decoded");
AckResponse(Acknowledgement::failure_with(Code::DataError, message)).into_response()
})?;
if let Err(violations) = request.validate() {
tracing::warn!(target: "oicp_kit::server", %violations, "a request is not conformant");
if let Some(refusal) = service.on_invalid_request(&violations) {
return Err(AckResponse(refusal).into_response());
}
}
Ok(request)
}
fn deserialize<T: serde::de::DeserializeOwned>(body: serde_json::Value) -> Result<T, String> {
serde_path_to_error::deserialize(body).map_err(|e| format!("{} at {}", e.inner(), e.path()))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::types::{Extensions, Identification, RemoteIdentification};
struct Recording {
started: std::sync::Mutex<Vec<String>>,
}
impl CpoService for Recording {
async fn authorize_remote_start(&self, request: AuthorizeRemoteStartRequest) -> Acknowledgement {
self.started.lock().unwrap().push(request.evse_id.to_string());
Acknowledgement::success().with_session(request.session_id)
}
async fn authorize_remote_stop(&self, request: AuthorizeRemoteStopRequest) -> Acknowledgement {
Acknowledgement::success().with_session(request.session_id)
}
async fn reservation_start(&self, _: AuthorizeRemoteReservationStartRequest) -> Acknowledgement {
Acknowledgement::failure(Code::ServiceNotAvailable)
}
async fn reservation_stop(&self, _: AuthorizeRemoteReservationStopRequest) -> Acknowledgement {
Acknowledgement::failure(Code::ServiceNotAvailable)
}
}
fn remote_start() -> AuthorizeRemoteStartRequest {
AuthorizeRemoteStartRequest {
session_id: "f98efba4-02d8-4fa0-b810-9a9d50d2c527".parse().unwrap(),
cpo_partner_session_id: None,
emp_partner_session_id: None,
provider_id: "DE-DCB".parse().unwrap(),
evse_id: "DE*ABC*E1".parse().unwrap(),
identification: Identification::Remote(RemoteIdentification {
evco_id: "DE-DCB-C12345678-X".parse().unwrap(),
}),
partner_product_id: None,
extensions: Extensions::new(),
}
}
#[tokio::test]
async fn a_conformant_request_reaches_the_service() {
let service = Recording { started: std::sync::Mutex::new(vec![]) };
let body = serde_json::to_value(remote_start()).unwrap();
let request = accept::<_, AuthorizeRemoteStartRequest>(&service, body).expect("a conformant request");
let ack = service.authorize_remote_start(request).await;
assert!(ack.is_success());
assert_eq!(service.started.lock().unwrap().as_slice(), ["DE*ABC*E1"]);
}
#[tokio::test]
async fn a_malformed_request_is_refused_without_reaching_the_service() {
let service = Recording { started: std::sync::Mutex::new(vec![]) };
let body = serde_json::json!({"not": "a remote start"});
let response = accept::<_, AuthorizeRemoteStartRequest>(&service, body)
.expect_err("a malformed body is refused");
assert_eq!(response.status(), axum::http::StatusCode::OK);
assert!(service.started.lock().unwrap().is_empty());
}
#[tokio::test]
async fn a_non_conformant_request_is_refused_by_default() {
let service = Recording { started: std::sync::Mutex::new(vec![]) };
let mut request = remote_start();
request.identification =
Identification::RfidMifareFamily(crate::types::RfidMifareFamilyIdentification {
uid: "7568290FFF765F".parse().unwrap(),
});
let body = serde_json::to_value(&request).unwrap();
let response = accept::<_, AuthorizeRemoteStartRequest>(&service, body)
.expect_err("an RFID identification is illegal in a remote start");
assert_eq!(response.status(), axum::http::StatusCode::OK);
assert!(service.started.lock().unwrap().is_empty(), "the default refuses it");
}
#[tokio::test]
async fn a_service_may_choose_to_accept_non_conformant_requests() {
struct Tolerant;
impl CpoService for Tolerant {
async fn authorize_remote_start(&self, request: AuthorizeRemoteStartRequest) -> Acknowledgement {
Acknowledgement::success().with_session(request.session_id)
}
async fn authorize_remote_stop(&self, r: AuthorizeRemoteStopRequest) -> Acknowledgement {
Acknowledgement::success().with_session(r.session_id)
}
async fn reservation_start(&self, _: AuthorizeRemoteReservationStartRequest) -> Acknowledgement {
Acknowledgement::failure(Code::ServiceNotAvailable)
}
async fn reservation_stop(&self, _: AuthorizeRemoteReservationStopRequest) -> Acknowledgement {
Acknowledgement::failure(Code::ServiceNotAvailable)
}
fn on_invalid_request(&self, _: &crate::types::Violations) -> Option<Acknowledgement> {
None }
}
let mut request = remote_start();
request.identification =
Identification::RfidMifareFamily(crate::types::RfidMifareFamilyIdentification {
uid: "7568290FFF765F".parse().unwrap(),
});
let body = serde_json::to_value(&request).unwrap();
let request = accept::<_, AuthorizeRemoteStartRequest>(&Tolerant, body)
.expect("a tolerant service accepts it anyway");
assert!(Tolerant.authorize_remote_start(request).await.is_success());
}
#[test]
fn the_router_uses_the_paths_from_the_endpoint_table() {
let _router = cpo_router(Arc::new(Recording { started: std::sync::Mutex::new(vec![]) }));
assert_eq!(
Operation::AuthorizeRemoteStart.path_template(),
"/charging/v21/providers/{providerID}/authorize-remote/start"
);
}
#[test]
fn a_cpo_serves_exactly_what_the_table_says_a_cpo_serves() {
use crate::transport::Role;
let served: Vec<Operation> =
Operation::ALL.iter().copied().filter(|op| op.is_served_by(Role::Cpo)).collect();
assert_eq!(
served,
vec![
Operation::AuthorizeRemoteStart,
Operation::AuthorizeRemoteStop,
Operation::AuthorizeRemoteReservationStart,
Operation::AuthorizeRemoteReservationStop,
],
"the four methods on CpoService, and nothing else"
);
for operation in Operation::for_role(Role::Cpo) {
assert!(
served.contains(&operation) || !operation.is_served_by(Role::Cpo),
"{operation:?} is neither called nor served"
);
}
assert!(Operation::PullEvseData.involvement(Role::Cpo).is_none());
assert!(Operation::GetChargeDetailRecords.involvement(Role::Cpo).is_none());
}
}