aboutsummaryrefslogtreecommitdiffstats
path: root/ms/blueprintsprocessor/functions/restconf-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/restconf/executor/RestconfExecutorExtensions.kt
blob: 61ab4c11c4e86a3cfd717d759d5ecfd1d59c25cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 *  Copyright © 2019 IBM.
 *  Modifications Copyright © 2018-2019 IBM, Bell Canada
 *
 *  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.cds.blueprintsprocessor.functions.restconf.executor

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

/**
 * Register the Restconf module exposed dependency
 */

val log = LoggerFactory.logger(AbstractScriptComponentFunction::class.java)!!

fun AbstractScriptComponentFunction.restconfClientService(selector: String): BlueprintWebClientService {
    return BlueprintDependencyService.restClientService(selector)
}

/**
 * Generic Mount function
 */

suspend fun AbstractScriptComponentFunction.restconfMountDevice(
    webClientService: BlueprintWebClientService,
    deviceId: String,
    payload: Any,
    headers: Map<String, String> = mutableMapOf("Content-Type" to "application/xml")
) {

    val mountUrl = "/restconf/config/network-topology:network-topology/topology/topology-netconf/node/$deviceId"
    log.info("sending mount request, url: $mountUrl")
    webClientService.exchangeResource("PUT", mountUrl, payload as String, headers)

    /** Check device has mounted */
    val mountCheckUrl = "/restconf/operational/network-topology:network-topology/topology/topology-netconf/node/$deviceId"

    val expectedResult = """"netconf-node-topology:connection-status":"connected""""
    val mountCheckExecutionBlock: suspend (Int) -> String = { tryCount: Int ->
        val result = webClientService.exchangeResource("GET", mountCheckUrl, "")
        if (result.body.contains(expectedResult)) {
            log.info("NF was mounted successfully on ODL")
            result.body
        } else {
            throw BlueprintRetryException("Wait for device($deviceId) to mount")
        }
    }

    log.info("url for ODL status check: $mountCheckUrl")
    webClientService.retry<String>(10, 0, 1000, mountCheckExecutionBlock)
}

/**
 * Generic Configure function
 * @return The WebClientResponse from the request
 */
suspend fun AbstractScriptComponentFunction.restconfApplyDeviceConfig(
    webClientService: BlueprintWebClientService,
    deviceId: String,
    configletResourcePath: String,
    configletToApply: Any,
    additionalHeaders: Map<String, String> = mutableMapOf("Content-Type" to "application/yang.patch+xml")
): BlueprintWebClientService.WebClientResponse<String> {
    log.debug("headers: $additionalHeaders")
    log.info("configuring device: $deviceId, Configlet: $configletToApply")
    val applyConfigUrl = "/restconf/config/network-topology:network-topology/topology/topology-netconf/node/" +
        "$deviceId/$configletResourcePath"
    return webClientService.exchangeResource("PATCH", applyConfigUrl, configletToApply as String, additionalHeaders)
}

suspend fun AbstractScriptComponentFunction.restconfDeviceConfig(
    webClientService: BlueprintWebClientService,
    deviceId: String,
    configletResourcePath: String
):
    BlueprintWebClientService.WebClientResponse<String> {

        val configPathUrl = "/restconf/config/network-topology:network-topology/topology/topology-netconf/node/" +
            "$deviceId/$configletResourcePath"
        log.debug("sending GET request,  url: $configPathUrl")
        return webClientService.exchangeResource("GET", configPathUrl, "")
    }

/**
 * Generic UnMount function
 */
suspend fun AbstractScriptComponentFunction.restconfUnMountDevice(
    webClientService: BlueprintWebClientService,
    deviceId: String,
    payload: String
) {
    val unMountUrl = "/restconf/config/network-topology:network-topology/topology/topology-netconf/node/$deviceId"
    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)
}