Add support for async REST
authorAlexis de Talhouët <adetalhouet89@gmail.com>
Tue, 15 Jan 2019 21:17:17 +0000 (16:17 -0500)
committerAlexis de Talhouët <adetalhouet89@gmail.com>
Fri, 18 Jan 2019 18:56:01 +0000 (13:56 -0500)
Change-Id: Ieb53cbd75c2e21355b153611f6490c1b2af6053b
Issue-ID: CCSDK-662
Signed-off-by: Alexis de Talhouët <adetalhouet89@gmail.com>
ms/blueprintsprocessor/modules/commons/core/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/core/api/data/BlueprintProcessorData.kt
ms/blueprintsprocessor/modules/inbounds/selfservice-api/pom.xml
ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/selfservice/api/ExecutionServiceController.kt
ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/selfservice/api/ExecutionServiceHandler.kt

index b0483dc..438e755 100644 (file)
@@ -1,99 +1,97 @@
-/*\r
- *  Copyright © 2017-2018 AT&T Intellectual Property.\r
- *  Modifications Copyright © 2018 IBM.\r
- *\r
- *  Licensed under the Apache License, Version 2.0 (the "License");\r
- *  you may not use this file except in compliance with the License.\r
- *  You may obtain a copy of the License at\r
- *\r
- *      http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- *  Unless required by applicable law or agreed to in writing, software\r
- *  distributed under the License is distributed on an "AS IS" BASIS,\r
- *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- *  See the License for the specific language governing permissions and\r
- *  limitations under the License.\r
- */\r
-\r
-package org.onap.ccsdk.apps.blueprintsprocessor.core.api.data\r
-\r
-import com.fasterxml.jackson.annotation.JsonFormat\r
-import com.fasterxml.jackson.databind.node.ObjectNode\r
-import io.swagger.annotations.ApiModelProperty\r
-import java.util.*\r
-\r
-/**\r
- * BlueprintProcessorData\r
- * @author Brinda Santh\r
- * DATE : 8/15/2018\r
- */\r
-\r
-open class ExecutionServiceInput {\r
-    @get:ApiModelProperty(required = true)\r
-    lateinit var commonHeader: CommonHeader\r
-    @get:ApiModelProperty(required = true)\r
-    lateinit var actionIdentifiers: ActionIdentifiers\r
-    @get:ApiModelProperty(required = true)\r
-    lateinit var payload: ObjectNode\r
-}\r
-\r
-open class ExecutionServiceOutput {\r
-    @get:ApiModelProperty(required = true)\r
-    lateinit var commonHeader: CommonHeader\r
-    @get:ApiModelProperty(required = true)\r
-    lateinit var actionIdentifiers: ActionIdentifiers\r
-    @get:ApiModelProperty(required = true)\r
-    var status: Status = Status()\r
-    @get:ApiModelProperty(required = true)\r
-    lateinit var payload: ObjectNode\r
-}\r
-\r
-open class ActionIdentifiers {\r
-    @get:ApiModelProperty(required = false)\r
-    lateinit var blueprintName: String\r
-    @get:ApiModelProperty(required = false)\r
-    lateinit var blueprintVersion: String\r
-    @get:ApiModelProperty(required = true)\r
-    lateinit var actionName: String\r
-    @get:ApiModelProperty(required = true, allowableValues = "sync, async")\r
-    lateinit var mode: String\r
-}\r
-\r
-open class CommonHeader {\r
-    @get:ApiModelProperty(required = true, example = "2012-04-23T18:25:43.511Z")\r
-    @get:JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")\r
-    var timestamp: Date = Date()\r
-    @get:ApiModelProperty(required = true)\r
-    lateinit var originatorId: String\r
-    @get:ApiModelProperty(required = true)\r
-    lateinit var requestId: String\r
-    @get:ApiModelProperty(required = true)\r
-    lateinit var subRequestId: String\r
-    @get:ApiModelProperty(required = false)\r
-    var flags: Flags? = null\r
-}\r
-\r
-open class Flags {\r
-    var isForce: Boolean = false\r
-    @get:ApiModelProperty(value = "3600")\r
-    var ttl: Int = 3600\r
-}\r
-\r
-open class Status {\r
-    @get:ApiModelProperty(required = true)\r
-    var code: Int = 200\r
-    @get:ApiModelProperty(required = true)\r
-    var eventType: String = "EVENT-ACTION-RESPONSE"\r
-    @get:ApiModelProperty(required = true, example = "2012-04-23T18:25:43.511Z")\r
-    @get:JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")\r
-    var timestamp: Date = Date()\r
-    @get:ApiModelProperty(required = false)\r
-    var errorMessage: String? = null\r
-    @get:ApiModelProperty(required = true)\r
-    var message: String = "success"\r
-}\r
-\r
-\r
-\r
-\r
-\r
+/*
+ *  Copyright © 2017-2018 AT&T Intellectual Property.
+ *  Modifications Copyright © 2018 IBM.
+ *
+ *  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.
+ */
+
+package org.onap.ccsdk.apps.blueprintsprocessor.core.api.data
+
+import com.fasterxml.jackson.annotation.JsonFormat
+import com.fasterxml.jackson.databind.node.ObjectNode
+import io.swagger.annotations.ApiModelProperty
+import java.util.*
+
+/**
+ * BlueprintProcessorData
+ * @author Brinda Santh
+ * DATE : 8/15/2018
+ */
+
+open class ExecutionServiceInput {
+    @get:ApiModelProperty(required = true)
+    lateinit var commonHeader: CommonHeader
+    @get:ApiModelProperty(required = true)
+    lateinit var actionIdentifiers: ActionIdentifiers
+    @get:ApiModelProperty(required = true)
+    lateinit var payload: ObjectNode
+}
+
+open class ExecutionServiceOutput {
+    @get:ApiModelProperty(required = true)
+    lateinit var commonHeader: CommonHeader
+    @get:ApiModelProperty(required = true)
+    lateinit var actionIdentifiers: ActionIdentifiers
+    @get:ApiModelProperty(required = true)
+    var status: Status = Status()
+    @get:ApiModelProperty(required = true)
+    lateinit var payload: ObjectNode
+}
+
+const val ACTION_MODE_ASYNC = "async"
+const val ACTION_MODE_SYNC = "sync"
+
+open class ActionIdentifiers {
+    @get:ApiModelProperty(required = false)
+    lateinit var blueprintName: String
+    @get:ApiModelProperty(required = false)
+    lateinit var blueprintVersion: String
+    @get:ApiModelProperty(required = true)
+    lateinit var actionName: String
+    @get:ApiModelProperty(required = true, allowableValues = "sync, async")
+    lateinit var mode: String
+}
+
+open class CommonHeader {
+    @get:ApiModelProperty(required = true, example = "2012-04-23T18:25:43.511Z")
+    @get:JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
+    var timestamp: Date = Date()
+    @get:ApiModelProperty(required = true)
+    lateinit var originatorId: String
+    @get:ApiModelProperty(required = true)
+    lateinit var requestId: String
+    @get:ApiModelProperty(required = true)
+    lateinit var subRequestId: String
+    @get:ApiModelProperty(required = false)
+    var flags: Flags? = null
+}
+
+open class Flags {
+    var isForce: Boolean = false
+    @get:ApiModelProperty(value = "3600")
+    var ttl: Int = 3600
+}
+
+open class Status {
+    @get:ApiModelProperty(required = true)
+    var code: Int = 200
+    @get:ApiModelProperty(required = true)
+    var eventType: String = "EVENT-ACTION-RESPONSE"
+    @get:ApiModelProperty(required = true, example = "2012-04-23T18:25:43.511Z")
+    @get:JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
+    var timestamp: Date = Date()
+    @get:ApiModelProperty(required = false)
+    var errorMessage: String? = null
+    @get:ApiModelProperty(required = true)
+    var message: String = "success"
+}
index 7041dab..5562df6 100644 (file)
     <description>Blueprints Processor Selfservice API</description>
 
     <dependencies>
+        <dependency>
+            <groupId>org.jetbrains.kotlinx</groupId>
+            <artifactId>kotlinx-coroutines-core</artifactId>
+        </dependency>
         <dependency>
             <groupId>io.grpc</groupId>
             <artifactId>grpc-testing</artifactId>
index 35d4e67..e4734c4 100644 (file)
@@ -38,7 +38,6 @@ class ExecutionServiceController {
     @Autowired
     lateinit var executionServiceHandler: ExecutionServiceHandler
 
-
     @RequestMapping(path = ["/ping"], method = [RequestMethod.GET], produces = [MediaType.APPLICATION_JSON_VALUE])
     @ResponseBody
     fun ping(): Mono<String> {
@@ -58,8 +57,7 @@ class ExecutionServiceController {
     @RequestMapping(path = ["/process"], method = [RequestMethod.POST], produces = [MediaType.APPLICATION_JSON_VALUE])
     @ApiOperation(value = "Resolve Resource Mappings", notes = "Takes the blueprint information and process as per the payload")
     @ResponseBody
-    fun process(@RequestBody executionServiceInput: ExecutionServiceInput): Mono<ExecutionServiceOutput> {
-        val executionServiceOutput = executionServiceHandler.process(executionServiceInput)
-        return Mono.just(executionServiceOutput)
+    fun process(@RequestBody executionServiceInput: ExecutionServiceInput): ExecutionServiceOutput {
+        return executionServiceHandler.process(executionServiceInput)
     }
 }
index 5690374..0b361d8 100644 (file)
 
 package org.onap.ccsdk.apps.blueprintsprocessor.selfservice.api
 
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.GlobalScope
+import kotlinx.coroutines.launch
 import org.onap.ccsdk.apps.blueprintsprocessor.core.BluePrintCoreConfiguration
+import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ACTION_MODE_ASYNC
+import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ACTION_MODE_SYNC
 import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceInput
 import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceOutput
+import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.Status
 import org.onap.ccsdk.apps.blueprintsprocessor.selfservice.api.utils.saveCBAFile
 import org.onap.ccsdk.apps.blueprintsprocessor.services.workflow.BlueprintDGExecutionService
+import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants
 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintCatalogService
 import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintFileUtils
@@ -50,7 +57,20 @@ class ExecutionServiceHandler(private val bluePrintCoreConfiguration: BluePrintC
     }
 
     fun process(executionServiceInput: ExecutionServiceInput): ExecutionServiceOutput {
+        return when {
+            executionServiceInput.actionIdentifiers.mode == ACTION_MODE_ASYNC -> {
+                GlobalScope.launch(Dispatchers.Default) {
+                    // TODO post result in DMaaP
+                    val executionServiceOutput = doProcess(executionServiceInput)
+                }
+                response(executionServiceInput)
+            }
+            executionServiceInput.actionIdentifiers.mode == ACTION_MODE_SYNC -> doProcess(executionServiceInput)
+            else -> response(executionServiceInput, true)
+        }
+    }
 
+    fun doProcess(executionServiceInput: ExecutionServiceInput): ExecutionServiceOutput {
         val requestId = executionServiceInput.commonHeader.requestId
         log.info("processing request id $requestId")
 
@@ -66,4 +86,26 @@ class ExecutionServiceHandler(private val bluePrintCoreConfiguration: BluePrintC
 
         return blueprintDGExecutionService.executeDirectedGraph(blueprintRuntimeService, executionServiceInput)
     }
+
+    fun response(executionServiceInput: ExecutionServiceInput, failure: Boolean = false): ExecutionServiceOutput {
+        val executionServiceOutput = ExecutionServiceOutput()
+        executionServiceOutput.commonHeader = executionServiceInput.commonHeader
+        executionServiceOutput.actionIdentifiers = executionServiceInput.actionIdentifiers
+        executionServiceOutput.payload = executionServiceInput.payload
+
+        val status = Status()
+        if (failure) {
+            status.eventType = "EVENT-COMPONENT-FAILURE"
+            status.code = 500
+            status.message = BluePrintConstants.STATUS_FAILURE
+        } else {
+            status.eventType = "EVENT-COMPONENT-PROCESSING"
+            status.code = 200
+            status.message = BluePrintConstants.STATUS_PROCESSING
+        }
+
+        executionServiceOutput.status = status
+
+        return executionServiceOutput
+    }
 }
\ No newline at end of file