diff --git a/dva-api/api/src/main/kotlin/hu/bme/mit/ftsrg/dva/api/Application.kt b/dva-api/api/src/main/kotlin/hu/bme/mit/ftsrg/dva/api/Application.kt index 0426d21..4a31918 100644 --- a/dva-api/api/src/main/kotlin/hu/bme/mit/ftsrg/dva/api/Application.kt +++ b/dva-api/api/src/main/kotlin/hu/bme/mit/ftsrg/dva/api/Application.kt @@ -1,15 +1,10 @@ package hu.bme.mit.ftsrg.dva.api -import com.rabbitmq.client.Connection -import com.rabbitmq.client.ConnectionFactory import hu.bme.mit.ftsrg.dva.api.db.* import hu.bme.mit.ftsrg.dva.api.err.addHandlers -import hu.bme.mit.ftsrg.dva.api.rabbit.connectWithRetry import hu.bme.mit.ftsrg.dva.api.route.* import hu.bme.mit.ftsrg.dva.log.ReqestLogRepo import hu.bme.mit.ftsrg.dva.log.VerifRequestLogRepo -import hu.bme.mit.ftsrg.dva.vla.TemplateRepo -import hu.bme.mit.ftsrg.dva.vla.VLARepo import io.ktor.client.* import io.ktor.client.engine.cio.CIO import io.ktor.http.* @@ -61,15 +56,7 @@ fun Application.installPlugins() { } fun Application.configureKoin() { - val rabbitHost = environment.config.property("rabbitmq.host").getString() - val appModule = module { - single { - ConnectionFactory().run { - host = rabbitHost - connectWithRetry(logger = log) - } - } single { HttpClient(CIO) { install(ClientContentNegotiation) { @@ -80,10 +67,8 @@ fun Application.configureKoin() { } } } - single { PgTemplateRepo() } single { PgRequestLogRepo() } single { PgVerifRequestLogRepo() } - single { PgVLARepo() } } serverInstall(Koin) { modules(appModule) } @@ -91,9 +76,6 @@ fun Application.configureKoin() { fun Application.addRoutes() { docRoutes(openapiPath = environment.config.property("swagger.openapiFile").getString()) - templateRoutes() aovRoutes() - vlaRoutes() - evaluationRoutes() infoRoutes() } diff --git a/dva-api/api/src/main/kotlin/hu/bme/mit/ftsrg/dva/api/resource/Evaluation.kt b/dva-api/api/src/main/kotlin/hu/bme/mit/ftsrg/dva/api/resource/Evaluation.kt deleted file mode 100644 index 18e2d1e..0000000 --- a/dva-api/api/src/main/kotlin/hu/bme/mit/ftsrg/dva/api/resource/Evaluation.kt +++ /dev/null @@ -1,11 +0,0 @@ -package hu.bme.mit.ftsrg.dva.api.resource - -import io.ktor.resources.* - -@Suppress("unused") -@Resource("/evaluate") -class Evaluation { - - @Resource(path = "from-template") - class FromTemplate(val parent: Evaluation = Evaluation()) -} \ No newline at end of file diff --git a/dva-api/api/src/main/kotlin/hu/bme/mit/ftsrg/dva/api/resource/Templates.kt b/dva-api/api/src/main/kotlin/hu/bme/mit/ftsrg/dva/api/resource/Templates.kt deleted file mode 100644 index 5058902..0000000 --- a/dva-api/api/src/main/kotlin/hu/bme/mit/ftsrg/dva/api/resource/Templates.kt +++ /dev/null @@ -1,18 +0,0 @@ -package hu.bme.mit.ftsrg.dva.api.resource - -import io.ktor.resources.* -import kotlin.uuid.ExperimentalUuidApi -import kotlin.uuid.Uuid - -@Suppress("unused") -@Resource("/template") -class Templates { - - @OptIn(ExperimentalUuidApi::class) - @Resource("{id}") - class Id(val parent: Templates = Templates(), val id: Uuid) { - - @Resource("render") - class Render(val parent: Id) - } -} \ No newline at end of file diff --git a/dva-api/api/src/main/kotlin/hu/bme/mit/ftsrg/dva/api/resource/VLAs.kt b/dva-api/api/src/main/kotlin/hu/bme/mit/ftsrg/dva/api/resource/VLAs.kt deleted file mode 100644 index 925a95d..0000000 --- a/dva-api/api/src/main/kotlin/hu/bme/mit/ftsrg/dva/api/resource/VLAs.kt +++ /dev/null @@ -1,17 +0,0 @@ -package hu.bme.mit.ftsrg.dva.api.resource - -import io.ktor.resources.* -import kotlin.uuid.ExperimentalUuidApi -import kotlin.uuid.Uuid - -@Suppress("unused") -@Resource("/vla") -class VLAs { - - @OptIn(ExperimentalUuidApi::class) - @Resource("{id}") - class Id(val parent: VLAs = VLAs(), val id: Uuid) - - @Resource("from-templates") - class FromTemplates(val parent: VLAs = VLAs()) -} \ No newline at end of file diff --git a/dva-api/api/src/main/kotlin/hu/bme/mit/ftsrg/dva/api/route/aovRoutes.kt b/dva-api/api/src/main/kotlin/hu/bme/mit/ftsrg/dva/api/route/aovRoutes.kt index 1c87ac9..f129528 100644 --- a/dva-api/api/src/main/kotlin/hu/bme/mit/ftsrg/dva/api/route/aovRoutes.kt +++ b/dva-api/api/src/main/kotlin/hu/bme/mit/ftsrg/dva/api/route/aovRoutes.kt @@ -3,6 +3,7 @@ package hu.bme.mit.ftsrg.dva.api.route import com.rabbitmq.client.Connection import com.rabbitmq.client.MessageProperties import hu.bme.mit.ftsrg.dva.api.resource.Attestations +import hu.bme.mit.ftsrg.dva.dto.ErrDTO import hu.bme.mit.ftsrg.dva.dto.IDDTO import hu.bme.mit.ftsrg.dva.dto.aov.ACAPyPresentationRequestDTO import hu.bme.mit.ftsrg.dva.dto.aov.ACAPyPresentationResponseDTO @@ -103,18 +104,25 @@ fun Application.aovRoutes() { ) ) } - val acaPyResp: ACAPyPresentationResponseDTO = resp.body() + if (!resp.status.isSuccess()) { + call.respond( + resp.status, + ErrDTO(type = "ACAPY_${resp.status.value}", title = resp.bodyAsText()), + ) + } else { + val acaPyResp: ACAPyPresentationResponseDTO = resp.body() - if (verifLogEntity != null) { - verifsRepo.update( - VerifRequestLogPatch( - id = verifLogEntity.id, - presentationRequestData = acaPyResp.aov, + if (verifLogEntity != null) { + verifsRepo.update( + VerifRequestLogPatch( + id = verifLogEntity.id, + presentationRequestData = acaPyResp.aov, + ) ) - ) - } + } - call.respond(status = resp.status, message = acaPyResp) + call.respond(status = resp.status, message = acaPyResp) + } } } } diff --git a/dva-api/api/src/main/kotlin/hu/bme/mit/ftsrg/dva/api/route/evaluationRoutes.kt b/dva-api/api/src/main/kotlin/hu/bme/mit/ftsrg/dva/api/route/evaluationRoutes.kt deleted file mode 100644 index 6f27e63..0000000 --- a/dva-api/api/src/main/kotlin/hu/bme/mit/ftsrg/dva/api/route/evaluationRoutes.kt +++ /dev/null @@ -1,73 +0,0 @@ -package hu.bme.mit.ftsrg.dva.api.route - -import hu.bme.mit.ftsrg.dva.api.resource.Evaluation -import hu.bme.mit.ftsrg.dva.api.service.render -import hu.bme.mit.ftsrg.dva.dto.ErrDTO -import hu.bme.mit.ftsrg.dva.evaluation.Evaluate -import hu.bme.mit.ftsrg.dva.evaluation.EvaluateFromTemplate -import hu.bme.mit.ftsrg.dva.vla.Template -import hu.bme.mit.ftsrg.dva.vla.TemplateRepo -import hu.bme.mit.ftsrg.odcs.DataQuality -import io.ktor.client.HttpClient -import io.ktor.client.request.* -import io.ktor.client.statement.bodyAsText -import io.ktor.http.ContentType -import io.ktor.http.HttpStatusCode.Companion.InternalServerError -import io.ktor.http.HttpStatusCode.Companion.NotFound -import io.ktor.http.contentType -import io.ktor.server.application.Application -import io.ktor.server.request.receive -import io.ktor.server.resources.post -import io.ktor.server.response.respond -import io.ktor.server.response.respondText -import io.ktor.server.routing.routing -import org.koin.ktor.ext.inject -import kotlin.uuid.ExperimentalUuidApi - -@OptIn(ExperimentalUuidApi::class) -fun Application.evaluationRoutes() { - val httpClient by inject() - val templateRepo by inject() - - val processingURL = environment.config.property("processing.url").getString() - - routing { - post { - val request = call.receive() - val response = httpClient.post("${processingURL}/evaluate") { - setBody(request) - contentType(ContentType.Application.Json) - } - - call.respondText( - text = response.bodyAsText(), - contentType = ContentType.Application.Json, - status = response.status - ) - } - - post { - val evaluationReq = call.receive() - val template: Template = templateRepo.byID(evaluationReq.templateID) ?: run { - call.respond(NotFound) - return@post - } - val quality: DataQuality = template.render(evaluationReq.templateModel) ?: run { - call.respond(InternalServerError, ErrDTO(type = "UNKNOWN", title = "Failed to render template")) - return@post - } - - val request = Evaluate(requirement = quality, data = evaluationReq.data) - val response = httpClient.post("${processingURL}/evaluate") { - setBody(request) - contentType(ContentType.Application.Json) - } - - call.respondText( - text = response.bodyAsText(), - contentType = ContentType.Application.Json, - status = response.status - ) - } - } -} diff --git a/dva-api/api/src/main/kotlin/hu/bme/mit/ftsrg/dva/api/route/templateRoutes.kt b/dva-api/api/src/main/kotlin/hu/bme/mit/ftsrg/dva/api/route/templateRoutes.kt deleted file mode 100644 index c29205f..0000000 --- a/dva-api/api/src/main/kotlin/hu/bme/mit/ftsrg/dva/api/route/templateRoutes.kt +++ /dev/null @@ -1,93 +0,0 @@ -package hu.bme.mit.ftsrg.dva.api.route - -import hu.bme.mit.ftsrg.dva.api.resource.Templates -import hu.bme.mit.ftsrg.dva.api.service.render -import hu.bme.mit.ftsrg.dva.dto.ErrDTO -import hu.bme.mit.ftsrg.dva.dto.IDDTO -import hu.bme.mit.ftsrg.dva.vla.TemplateNew -import hu.bme.mit.ftsrg.dva.vla.TemplatePatch -import hu.bme.mit.ftsrg.dva.vla.TemplateRepo -import io.ktor.http.HttpStatusCode.Companion.BadRequest -import io.ktor.http.HttpStatusCode.Companion.Created -import io.ktor.http.HttpStatusCode.Companion.InternalServerError -import io.ktor.http.HttpStatusCode.Companion.NoContent -import io.ktor.http.HttpStatusCode.Companion.NotFound -import io.ktor.server.application.* -import io.ktor.server.request.* -import io.ktor.server.resources.* -import io.ktor.server.resources.patch -import io.ktor.server.resources.post -import io.ktor.server.response.* -import io.ktor.server.routing.* -import kotlinx.serialization.json.JsonObject -import org.koin.ktor.ext.inject -import kotlin.uuid.ExperimentalUuidApi - -@OptIn(ExperimentalUuidApi::class) -fun Application.templateRoutes() { - val repo by inject() - - routing { - get { - call.respond(repo.all()) - } - - get { req -> - val template = repo.byID(req.id) ?: run { - call.respond(NotFound) - return@get - } - call.respond(template) - } - - post { - val templateReq = call.receive() - - val template = repo.add(templateReq) ?: run { - call.respond(InternalServerError, ErrDTO(type = "UNKNOWN", title = "Failed to create template")) - return@post - } - call.respond(Created, IDDTO(template.id.toString())) - } - - patch { req -> - val patch = call.receive() - if (req.id != patch.id) { - call.respond( - status = BadRequest, - ErrDTO(type = "BAD_REQUEST", title = "ID path parameter does not match ID in body") - ) - } - val updatedTemplate = repo.update(patch) ?: run { - call.respond(NotFound) - return@patch - } - call.respond(updatedTemplate) - } - - delete { req -> - if (repo.remove(req.id)) { - call.respond(NoContent) - } else { - call.respond(NotFound) - } - } - - delete { - repo.removeAll() - call.respond(NoContent) - } - - post { req -> - val template = repo.byID(req.parent.id) ?: run { - call.respond(NotFound) - return@post - } - val model = call.receive() - val renderedQuality = template.render(model) ?: run { - call.respond(BadRequest, ErrDTO(type = "BAD_REQUEST", title = "Failed to render template")) - } - call.respond(renderedQuality) - } - } -} \ No newline at end of file diff --git a/dva-api/api/src/main/kotlin/hu/bme/mit/ftsrg/dva/api/route/vlaRoutes.kt b/dva-api/api/src/main/kotlin/hu/bme/mit/ftsrg/dva/api/route/vlaRoutes.kt deleted file mode 100644 index 15c762f..0000000 --- a/dva-api/api/src/main/kotlin/hu/bme/mit/ftsrg/dva/api/route/vlaRoutes.kt +++ /dev/null @@ -1,128 +0,0 @@ -package hu.bme.mit.ftsrg.dva.api.route - -import hu.bme.mit.ftsrg.dva.api.resource.VLAs -import hu.bme.mit.ftsrg.dva.api.service.render -import hu.bme.mit.ftsrg.dva.dto.ErrDTO -import hu.bme.mit.ftsrg.dva.dto.IDDTO -import hu.bme.mit.ftsrg.dva.vla.TemplateRepo -import hu.bme.mit.ftsrg.dva.vla.VLANew -import hu.bme.mit.ftsrg.dva.vla.VLANewFromTemplates -import hu.bme.mit.ftsrg.dva.vla.VLARepo -import hu.bme.mit.ftsrg.odcs.DataQuality -import io.ktor.http.HttpStatusCode.Companion.Created -import io.ktor.http.HttpStatusCode.Companion.InternalServerError -import io.ktor.http.HttpStatusCode.Companion.NotFound -import io.ktor.server.application.Application -import io.ktor.server.request.* -import io.ktor.server.resources.* -import io.ktor.server.resources.post -import io.ktor.server.response.* -import io.ktor.server.routing.routing -import kotlinx.serialization.json.* -import org.koin.ktor.ext.inject -import kotlin.uuid.ExperimentalUuidApi - -@OptIn(ExperimentalUuidApi::class) -fun Application.vlaRoutes() { - val templateRepo by inject() - val vlaRepo by inject() - - routing { - get { - call.respond(vlaRepo.all()) - } - - get { req -> - val vla = vlaRepo.byID(req.id) ?: run { - call.respond(NotFound) - return@get - } - call.respond(vla) - } - - post { - val vlaReq = call.receive() - val vla = buildJsonObject { - put("apiVersion", "v3.0.2") - put("kind", "DataContract") - put("version", "0.1.0") - put("status", "active") - - vlaReq.apply { - description?.let { put("description", it) } - servers?.let { put("servers", it) } - schema?.let { put("schema", it) } - quality?.let { put("quality", Json.encodeToJsonElement(it)) } - price?.let { put("price", it) } - team?.let { put("team", it) } - roles?.let { put("roles", it) } - slaProperties?.let { put("slaProperties", it) } - support?.let { put("support", it) } - tags?.let { put("tags", it) } - } - } - val id = vlaRepo.add(vla) ?: run { - call.respond(InternalServerError, ErrDTO(type = "UNKNOWN", title = "Failed to create VLA")) - return@post - } - - call.respond(Created, IDDTO(id.toString())) - } - - post { - val vlaReq = call.receive() - val originalVLA = buildJsonObject { - put("apiVersion", "v3.0.2") - put("kind", "DataContract") - put("version", "0.1.0") - put("status", "active") - - vlaReq.apply { - description?.let { put("description", it) } - servers?.let { put("servers", it) } - schema?.let { put("schema", it) } - quality?.let { put("quality", Json.encodeToJsonElement(it)) } - price?.let { put("price", it) } - team?.let { put("team", it) } - roles?.let { put("roles", it) } - slaProperties?.let { put("slaProperties", it) } - support?.let { put("support", it) } - tags?.let { put("tags", it) } - } - } - - // Extend VLA with rendered templates - val qualityRequirements = mutableListOf() - vlaReq.qualityTemplates?.forEach { templateInstantiation -> - val template = templateRepo.byID(templateInstantiation.id) ?: run { - // TODO: respond with more detail? - call.respond(NotFound) - return@post - } - val quality = template.render(templateInstantiation.model) ?: run { - call.respond(InternalServerError, ErrDTO(type = "UNKNOWN", title = "Failed to render template")) - return@post - } - qualityRequirements.add(quality) - } - val extendedVLA = buildJsonObject { - originalVLA.forEach { k, v -> put(k, v) } - putJsonArray("quality") { - originalVLA["quality"]?.jsonArray?.forEach { add(it) } - qualityRequirements.forEach { add(Json.encodeToJsonElement(it)) } - } - } - - val id = vlaRepo.add(extendedVLA) ?: run { - call.respond(InternalServerError, ErrDTO(type = "UNKNOWN", title = "Failed to create VLA")) - return@post - } - - call.respond(Created, IDDTO(id.toString())) - } - - delete { - vlaRepo.removeAll() - } - } -} \ No newline at end of file diff --git a/dva-api/api/src/main/resources/application.yaml b/dva-api/api/src/main/resources/application.yaml index f2af4bc..4859bc7 100644 --- a/dva-api/api/src/main/resources/application.yaml +++ b/dva-api/api/src/main/resources/application.yaml @@ -13,9 +13,6 @@ postgres: user: "$DVA_POSTGRES_USER:postgres" password: "$DVA_POSTGRES_PASSWORD:postgres" -rabbitmq: - host: "$DVA_RABBITMQ_HOST:localhost" - processing: url: "$DVA_PROCESSING_URL:http://localhost:5000" diff --git a/dva-api/api/src/test/kotlin/hu/bme/mit/ftsrg/dva/api/route/TemplateRoutesTest.kt b/dva-api/api/src/test/kotlin/hu/bme/mit/ftsrg/dva/api/route/TemplateRoutesTest.kt deleted file mode 100644 index 5ea2206..0000000 --- a/dva-api/api/src/test/kotlin/hu/bme/mit/ftsrg/dva/api/route/TemplateRoutesTest.kt +++ /dev/null @@ -1,210 +0,0 @@ -package hu.bme.mit.ftsrg.dva.api.route - -import hu.bme.mit.ftsrg.dva.api.testutil.createTestClient -import hu.bme.mit.ftsrg.dva.api.testutil.setupTestApplication -import hu.bme.mit.ftsrg.dva.dto.IDDTO -import hu.bme.mit.ftsrg.dva.vla.* -import hu.bme.mit.ftsrg.odcs.DataQuality -import io.ktor.client.call.* -import io.ktor.client.request.* -import io.ktor.http.* -import io.ktor.http.ContentType.* -import io.ktor.http.HttpStatusCode.Companion.Created -import io.ktor.http.HttpStatusCode.Companion.NoContent -import io.ktor.http.HttpStatusCode.Companion.NotFound -import io.ktor.http.HttpStatusCode.Companion.OK -import io.ktor.server.application.install -import io.ktor.server.testing.* -import kotlinx.serialization.json.buildJsonObject -import kotlinx.serialization.json.put -import kotlinx.serialization.json.putJsonObject -import org.junit.jupiter.api.Assertions.assertEquals -import org.junit.jupiter.api.Assertions.assertNotNull -import org.junit.jupiter.api.Test -import org.koin.dsl.module -import org.koin.ktor.plugin.Koin -import kotlin.uuid.ExperimentalUuidApi -import kotlin.uuid.Uuid - -@OptIn(ExperimentalUuidApi::class) -class TemplateRoutesTest { - - @Test - fun `should return list of templates`() = testApplication { - setupApplication() - - val client = createTestClient() - client.get("/template").apply { - assertEquals(OK, status) - // Fake template repository seeds itself with 1 template - assertEquals(1, body>().size) - } - } - - @Test - fun `should return template by ID`() = testApplication { - setupApplication() - - val client = createTestClient() - client.get("/template/${Uuid.NIL}").apply { - assertEquals(OK, status) - assertNotNull(body