Support for generic URL for mount, Put, Get added 72/115172/5
authortragait <rahul.tyagi@est.tech>
Fri, 20 Nov 2020 13:25:45 +0000 (13:25 +0000)
committerRahul Tyagi <rahul.tyagi@est.tech>
Mon, 23 Nov 2020 13:54:35 +0000 (13:54 +0000)
Issue-ID: CCSDK-2993
Signed-off-by: tragait <rahul.tyagi@est.tech>
Change-Id: I4061d6ba5084806c0a14b137e169f73cdd68a588

ms/blueprintsprocessor/functions/restconf-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/restconf/executor/RestconfExecutorExtensions.kt

index 7aabb73..2d3eb3b 100644 (file)
@@ -21,6 +21,7 @@ import org.hibernate.annotations.common.util.impl.LoggerFactory
 import org.onap.ccsdk.cds.blueprintsprocessor.rest.restClientService
 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService
 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractScriptComponentFunction
+import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintRetryException
 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintDependencyService
 
@@ -110,3 +111,72 @@ suspend fun AbstractScriptComponentFunction.restconfUnMountDevice(
     log.info("sending unMount request, url: $unMountUrl")
     webClientService.exchangeResource("DELETE", unMountUrl, "")
 }
+
+/**
+ * Generic PUT/PATCH/POST request function
+ */
+suspend fun AbstractScriptComponentFunction.genericPutPatchPostRequest(
+    webClientService: BlueprintWebClientService,
+    requestUrl: String,
+    requestType: String,
+    payload: Any,
+    headers: Map<String, String> = mutableMapOf("Content-Type" to "application/xml")
+): BlueprintWebClientService.WebClientResponse<String> {
+    when (requestType.toUpperCase()) {
+        "PUT" -> log.info("sending PUT request, url: $requestUrl")
+        "PATCH" -> log.info("sending PATCH request, url: $requestUrl")
+        "POST" -> log.info("sending POST request, url: $requestUrl")
+        else -> throw BluePrintProcessorException("Illegal request type, only POST, PUT or PATCH allowed.")
+    }
+    return webClientService.exchangeResource(requestType, requestUrl, payload as String, headers)
+}
+
+/**
+ * Generic GET/DELETE request function
+ */
+
+suspend fun AbstractScriptComponentFunction.genericGetOrDeleteRequest(
+    webClientService: BlueprintWebClientService,
+    requestUrl: String,
+    requestType: String
+): BlueprintWebClientService.WebClientResponse<String> {
+    when (requestType.toUpperCase()) {
+        "GET" -> log.info("sending GET request, url: $requestUrl")
+        "DELETE" -> log.info("sending DELETE request, url: $requestUrl")
+        else -> throw BluePrintProcessorException("Illegal request type, only GET and DELETE allowed.")
+    }
+    return webClientService.exchangeResource(requestType, requestUrl, "")
+}
+
+/**
+ * Generic Mount function
+ * This function mount the given deviceId and verify if device mounted successfully.
+ * This function take mount url and mount verify url as parameters.
+ */
+
+suspend fun AbstractScriptComponentFunction.restconfMountDevice(
+    webClientService: BlueprintWebClientService,
+    payload: Any,
+    mountUrl: String,
+    mountVerifyUrl: String,
+    headers: Map<String, String> = mutableMapOf("Content-Type" to "application/xml"),
+    expectedMountResult: String = """"netconf-node-topology:connection-status":"connected""""
+) {
+
+    log.info("sending mount request, url: $mountUrl")
+    webClientService.exchangeResource("PUT", mountUrl, payload as String, headers)
+
+    /** Check device has mounted */
+    val mountCheckExecutionBlock: suspend (Int) -> String = { tryCount: Int ->
+        val result = webClientService.exchangeResource("GET", mountVerifyUrl, "")
+        if (result.body.contains(expectedMountResult)) {
+            log.info("NF was mounted successfully on ODL")
+            result.body
+        } else {
+            throw BluePrintRetryException("Wait for device with url($mountUrl) to mount")
+        }
+    }
+
+    log.info("url for ODL status check: $mountVerifyUrl")
+    webClientService.retry<String>(10, 0, 1000, mountCheckExecutionBlock)
+}