aboutsummaryrefslogtreecommitdiffstats
path: root/sources/hv-collector-xnf-simulator/src
diff options
context:
space:
mode:
authorFilip Krzywka <filip.krzywka@nokia.com>2018-12-18 10:10:06 +0100
committerFilip Krzywka <filip.krzywka@nokia.com>2019-01-03 08:36:41 +0000
commit88871ce8d9e069c7967734163c4406d78f91823d (patch)
tree39e1f60210c18658e821117dbf653d9097a41b23 /sources/hv-collector-xnf-simulator/src
parent3d3eff47f0f94176f351d05f7dca39957a0c3c8b (diff)
Add script for sending messages from multiple xNFs
Change-Id: I9acd3278929c30a3036f97f403a31a8817723d27 Issue-ID: DCAEGEN2-1027 Signed-off-by: Filip Krzywka <filip.krzywka@nokia.com>
Diffstat (limited to 'sources/hv-collector-xnf-simulator/src')
-rw-r--r--sources/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/adapters/XnfApiServer.kt26
-rw-r--r--sources/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/simulations.kt4
-rw-r--r--sources/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/status.kt28
-rw-r--r--sources/hv-collector-xnf-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/main/OngoingSimulationsTest.kt51
4 files changed, 95 insertions, 14 deletions
diff --git a/sources/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/adapters/XnfApiServer.kt b/sources/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/adapters/XnfApiServer.kt
index cfd3a6e9..a0785620 100644
--- a/sources/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/adapters/XnfApiServer.kt
+++ b/sources/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/adapters/XnfApiServer.kt
@@ -20,10 +20,12 @@
package org.onap.dcae.collectors.veshv.simulators.xnf.impl.adapters
import arrow.core.Either
-import arrow.core.getOrElse
import arrow.effects.IO
import org.onap.dcae.collectors.veshv.simulators.xnf.impl.OngoingSimulations
import org.onap.dcae.collectors.veshv.simulators.xnf.impl.XnfSimulator
+import org.onap.dcae.collectors.veshv.simulators.xnf.impl.XnfStatus.BUSY
+import org.onap.dcae.collectors.veshv.simulators.xnf.impl.XnfStatus.DETAILED_STATUS_NODE
+import org.onap.dcae.collectors.veshv.simulators.xnf.impl.XnfStatus.IDLE
import org.onap.dcae.collectors.veshv.utils.http.HttpConstants
import org.onap.dcae.collectors.veshv.utils.http.Response
import org.onap.dcae.collectors.veshv.utils.http.Responses
@@ -37,6 +39,7 @@ import ratpack.http.TypedData
import ratpack.server.RatpackServer
import ratpack.server.ServerConfig
import java.util.*
+import javax.json.Json
/**
* @author Jakub Dudycz <jakub.dudycz@nokia.com>
@@ -58,10 +61,7 @@ internal class XnfApiServer(
.post("simulator", ::startSimulationHandler)
.post("simulator/async", ::startSimulationHandler)
.get("simulator/:id", ::simulatorStatusHandler)
- .get("healthcheck") { ctx ->
- logger.info { "Checking health" }
- ctx.response.status(HttpConstants.STATUS_OK).send()
- }
+ .get("healthcheck", ::healthcheckHandler)
}
private fun startSimulationHandler(ctx: Context) {
@@ -82,6 +82,7 @@ internal class XnfApiServer(
.map(Responses::acceptedResponse)
}
+
private fun simulatorStatusHandler(ctx: Context) {
logger.debug { "Checking task status" }
val id = UUID.fromString(ctx.pathTokens["id"])
@@ -92,6 +93,21 @@ internal class XnfApiServer(
ctx.response.sendAndHandleErrors(IO.just(response))
}
+ private fun healthcheckHandler(ctx: Context) {
+ val healthCheckDetailedMessage = createHealthCheckDetailedMessage()
+ val simulatorStatus = HttpConstants.STATUS_OK
+ logger.info { "Returning simulator status: ${simulatorStatus} ${healthCheckDetailedMessage}" }
+ ctx.response.status(simulatorStatus).send(healthCheckDetailedMessage)
+ }
+
+ private fun createHealthCheckDetailedMessage() =
+ Json.createObjectBuilder()
+ .add(DETAILED_STATUS_NODE, when {
+ ongoingSimulations.isAnySimulationPending() -> BUSY
+ else -> IDLE
+ })
+ .build().toString()
+
companion object {
private val logger = Logger(XnfApiServer::class)
}
diff --git a/sources/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/simulations.kt b/sources/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/simulations.kt
index d7d42d88..bd58dd9c 100644
--- a/sources/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/simulations.kt
+++ b/sources/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/simulations.kt
@@ -57,6 +57,10 @@ class OngoingSimulations(executor: Executor = Executors.newCachedThreadPool()) {
fun status(id: UUID) = simulations.getOrDefault(id, StatusNotFound)
+ fun isAnySimulationPending() = simulations.any {
+ status(it.key) is StatusOngoing
+ }
+
internal fun clear() {
simulations.clear()
}
diff --git a/sources/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/status.kt b/sources/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/status.kt
new file mode 100644
index 00000000..a86e3d50
--- /dev/null
+++ b/sources/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/status.kt
@@ -0,0 +1,28 @@
+/*
+ * ============LICENSE_START=======================================================
+ * dcaegen2-collectors-veshv
+ * ================================================================================
+ * Copyright (C) 2018 NOKIA
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+package org.onap.dcae.collectors.veshv.simulators.xnf.impl
+
+// TODO: probably should be merged with HealthDescription or made similiar to it
+internal object XnfStatus {
+
+ const val BUSY = "Busy"
+ const val IDLE = "Idle"
+ const val DETAILED_STATUS_NODE = "Detailed status"
+}
diff --git a/sources/hv-collector-xnf-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/main/OngoingSimulationsTest.kt b/sources/hv-collector-xnf-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/main/OngoingSimulationsTest.kt
index a04da7bf..113c3c42 100644
--- a/sources/hv-collector-xnf-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/main/OngoingSimulationsTest.kt
+++ b/sources/hv-collector-xnf-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/main/OngoingSimulationsTest.kt
@@ -47,7 +47,7 @@ internal class OngoingSimulationsTest : Spek({
given("not existing task task id") {
val id = UUID.randomUUID()
- on("status") {
+ on("asking for status") {
val result = cut.status(id)
it("should have 'not found' status") {
@@ -56,8 +56,16 @@ internal class OngoingSimulationsTest : Spek({
}
}
+ given("no tasks") {
+ on("quering about any pending task") {
+ it("should return false") {
+ assertThat(cut.isAnySimulationPending()).isFalse()
+ }
+ }
+ }
+
given("never ending task") {
- val task = IO.async<Unit> { }
+ val task = neverendingTask()
on("startAsynchronousSimulation") {
val result = cut.startAsynchronousSimulation(task)
@@ -65,33 +73,48 @@ internal class OngoingSimulationsTest : Spek({
it("should have ongoing status") {
assertThat(cut.status(result)).isEqualTo(StatusOngoing)
}
+
+ it("should return true when asked about any pending tasks") {
+ assertThat(cut.isAnySimulationPending()).isTrue()
+ }
}
}
given("failing task") {
- val cause = RuntimeException("facepalm")
- val task = IO.raiseError<Unit>(cause)
+ val (cause, task) = failingTask()
on("startAsynchronousSimulation") {
- val result = cut.startAsynchronousSimulation(task)
+ val taskID = cut.startAsynchronousSimulation(task)
it("should have failing status") {
waitUntilSucceeds {
- assertThat(cut.status(result)).isEqualTo(StatusFailure(cause))
+ assertThat(cut.status(taskID)).isEqualTo(StatusFailure(cause))
+ }
+ }
+
+ it("should return false when asked about any pending tasks") {
+ waitUntilSucceeds {
+ assertThat(cut.isAnySimulationPending()).isFalse()
}
}
}
}
given("successful task") {
- val task = IO { println("great success!") }
+ val task = succesfulTask()
on("startAsynchronousSimulation") {
- val result = cut.startAsynchronousSimulation(task)
+ val taskID = cut.startAsynchronousSimulation(task)
it("should have successful status") {
waitUntilSucceeds {
- assertThat(cut.status(result)).isEqualTo(StatusSuccess)
+ assertThat(cut.status(taskID)).isEqualTo(StatusSuccess)
+ }
+ }
+
+ it("should return false when asked about any pending tasks") {
+ waitUntilSucceeds {
+ assertThat(cut.isAnySimulationPending()).isFalse()
}
}
}
@@ -104,3 +127,13 @@ internal class OngoingSimulationsTest : Spek({
afterEachTest { cut.clear() }
})
+
+private fun neverendingTask() = IO.async<Unit> { }
+
+private fun succesfulTask(): IO<Unit> = IO { println("great success!") }
+
+private fun failingTask(): Pair<RuntimeException, IO<Unit>> {
+ val cause = RuntimeException("facepalm")
+ val task = IO.raiseError<Unit>(cause)
+ return Pair(cause, task)
+}