diff --git a/src/rest/openapi.yaml b/src/rest/openapi.yaml index 43747e2e01f..2ba2a4dd56f 100644 --- a/src/rest/openapi.yaml +++ b/src/rest/openapi.yaml @@ -43,6 +43,15 @@ paths: application/json: schema: type: object + delete: + tags: + - node + summary: Erase all persistent information, essentially factory reset the Border Router. + responses: + "200": + description: Successful operation + "409": + description: Thread interface is in wrong state. /node/ba-id: get: tags: diff --git a/src/rest/resource.cpp b/src/rest/resource.cpp index 1139a2d7886..a60e9d94836 100644 --- a/src/rest/resource.cpp +++ b/src/rest/resource.cpp @@ -257,16 +257,46 @@ void Resource::GetNodeInfo(Response &aResponse) const } } -void Resource::NodeInfo(const Request &aRequest, Response &aResponse) const +void Resource::DeleteNodeInfo(Response &aResponse) const { + otbrError error = OTBR_ERROR_NONE; std::string errorCode; - if (aRequest.GetMethod() == HttpMethod::kGet) + + VerifyOrExit(mNcp->GetThreadHelper()->Detach() == OT_ERROR_NONE, error = OTBR_ERROR_INVALID_STATE); + VerifyOrExit(otInstanceErasePersistentInfo(mInstance) == OT_ERROR_NONE, error = OTBR_ERROR_REST); + mNcp->Reset(); + +exit: + if (error == OTBR_ERROR_NONE) { - GetNodeInfo(aResponse); + errorCode = GetHttpStatus(HttpStatusCode::kStatusOk); + aResponse.SetResponsCode(errorCode); } - else + else if (error == OTBR_ERROR_INVALID_STATE) + { + ErrorHandler(aResponse, HttpStatusCode::kStatusConflict); + } + else if (error != OTBR_ERROR_NONE) { + ErrorHandler(aResponse, HttpStatusCode::kStatusInternalServerError); + } +} + +void Resource::NodeInfo(const Request &aRequest, Response &aResponse) const +{ + std::string errorCode; + + switch (aRequest.GetMethod()) + { + case HttpMethod::kGet: + GetNodeInfo(aResponse); + break; + case HttpMethod::kDelete: + DeleteNodeInfo(aResponse); + break; + default: ErrorHandler(aResponse, HttpStatusCode::kStatusMethodNotAllowed); + break; } } diff --git a/src/rest/resource.hpp b/src/rest/resource.hpp index 0c089c7296f..d79085dbfcb 100644 --- a/src/rest/resource.hpp +++ b/src/rest/resource.hpp @@ -137,6 +137,7 @@ class Resource void HandleDiagnosticCallback(const Request &aRequest, Response &aResponse); void GetNodeInfo(Response &aResponse) const; + void DeleteNodeInfo(Response &aResponse) const; void GetDataBaId(Response &aResponse) const; void GetDataExtendedAddr(Response &aResponse) const; void GetDataState(Response &aResponse) const; diff --git a/src/rest/response.cpp b/src/rest/response.cpp index 93cbe0b6dfb..3460b90e1f8 100644 --- a/src/rest/response.cpp +++ b/src/rest/response.cpp @@ -34,7 +34,7 @@ #define OT_REST_RESPONSE_ACCESS_CONTROL_ALLOW_HEADERS \ "Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, " \ "Access-Control-Request-Headers" -#define OT_REST_RESPONSE_ACCESS_CONTROL_ALLOW_METHOD "GET, OPTIONS, PUT" +#define OT_REST_RESPONSE_ACCESS_CONTROL_ALLOW_METHOD "DELETE, GET, OPTIONS, PUT" #define OT_REST_RESPONSE_CONNECTION "close" namespace otbr {