Clean restconf duplicate models and Implementation.
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / restconf-executor / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / restconf / executor / RestconfExecutorExtensions.kt
1 /*
2  *  Copyright © 2019 IBM.
3  *  Modifications Copyright © 2018-2019 IBM, Bell Canada
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17
18 package org.onap.ccsdk.cds.blueprintsprocessor.functions.restconf.executor
19
20 import org.hibernate.annotations.common.util.impl.LoggerFactory
21 import org.onap.ccsdk.cds.blueprintsprocessor.rest.restClientService
22 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService
23 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractScriptComponentFunction
24 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintRetryException
25 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintDependencyService
26
27 /**
28  * Register the Restconf module exposed dependency
29  */
30
31 val log = LoggerFactory.logger(AbstractScriptComponentFunction::class.java)!!
32
33 fun AbstractScriptComponentFunction.restconfClientService(selector: String): BlueprintWebClientService {
34     return BluePrintDependencyService.restClientService(selector)
35 }
36
37 /**
38  * Generic Mount function
39  */
40
41 suspend fun AbstractScriptComponentFunction.restconfMountDevice(webClientService: BlueprintWebClientService,
42                                                                 deviceId: String, payload: Any) {
43     val headers: MutableMap<String, String> = hashMapOf()
44     headers["Content-Type"] = "application/xml"
45     val mountUrl = "restconf/config/network-topology:network-topology/topology/topology-netconf/node/$deviceId"
46     log.info("sending mount request, url: $mountUrl")
47     webClientService.exchangeNB("PUT", mountUrl, payload, headers)
48
49     /** Check device has mounted */
50     val mountCheckUrl = "restconf/operational/network-topology:network-topology/topology/topology-netconf/node/$deviceId"
51
52     val expectedResult = """"netconf-node-topology:connection-status":"connected""""
53     val mountCheckExecutionBlock: suspend (Int) -> String = { tryCount: Int ->
54         val result = webClientService.exchangeNB("GET", mountCheckUrl, "")
55         if (result.body.contains(expectedResult)) {
56             log.info("NF was mounted successfully on ODL")
57             result.body
58         } else {
59             throw BluePrintRetryException("Wait for device($deviceId) to mount")
60         }
61     }
62
63     log.info("url for ODL status check: $mountCheckUrl")
64     webClientService.retry<String>(10, 0, 1000, mountCheckExecutionBlock)
65 }
66
67 /**
68  * Generic Configure function
69  */
70 suspend fun AbstractScriptComponentFunction.restconfApplyDeviceConfig(webClientService: BlueprintWebClientService,
71                                                                       deviceId: String, configletResourcePath: String,
72                                                                       configletToApply: Any,
73                                                                       additionalHeaders: Map<String, String>?) {
74
75     log.debug("headers: $additionalHeaders")
76     log.info("configuring device: $deviceId, Configlet: $configletToApply")
77     val applyConfigUrl = "restconf/config/network-topology:network-topology/topology/topology-netconf/node/" +
78             "$deviceId/$configletResourcePath"
79     val result = webClientService.exchangeNB("PATCH", applyConfigUrl, configletToApply, additionalHeaders)
80     log.info("Configuration application result: $result")
81 }
82
83
84 suspend fun AbstractScriptComponentFunction.restconfDeviceConfig(webClientService: BlueprintWebClientService,
85                                                                  deviceId: String, configletResourcePath: String)
86         : BlueprintWebClientService.WebClientResponse<String> {
87
88     val configPathUrl = "restconf/config/network-topology:network-topology/topology/topology-netconf/node/" +
89             "$deviceId/$configletResourcePath"
90     log.debug("sending GET request,  url: $configPathUrl")
91     return webClientService.exchangeNB("GET", configPathUrl, "")
92 }
93
94 /**
95  * Generic UnMount function
96  */
97 suspend fun AbstractScriptComponentFunction.restconfUnMountDevice(webClientService: BlueprintWebClientService,
98                                                                   deviceId: String, payload: String) {
99     val unMountUrl = "restconf/config/network-topology:network-topology/topology/topology-netconf/node/$deviceId"
100     log.info("sending unMount request, url: $unMountUrl")
101     webClientService.exchangeNB("DELETE", unMountUrl, "")
102 }