2 * Copyright © 2019 IBM.
3 * Modifications Copyright © 2018-2019 IBM, Bell Canada
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.onap.ccsdk.cds.blueprintsprocessor.functions.restconf.executor
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.BluePrintProcessorException
25 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintRetryException
26 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintDependencyService
29 * Register the Restconf module exposed dependency
32 val log = LoggerFactory.logger(AbstractScriptComponentFunction::class.java)!!
34 fun AbstractScriptComponentFunction.restconfClientService(selector: String): BlueprintWebClientService {
35 return BluePrintDependencyService.restClientService(selector)
39 * Generic Mount function
42 suspend fun AbstractScriptComponentFunction.restconfMountDevice(
43 webClientService: BlueprintWebClientService,
46 headers: Map<String, String> = mutableMapOf("Content-Type" to "application/xml")
49 val mountUrl = "/restconf/config/network-topology:network-topology/topology/topology-netconf/node/$deviceId"
50 log.info("sending mount request, url: $mountUrl")
51 webClientService.exchangeResource("PUT", mountUrl, payload as String, headers)
53 /** Check device has mounted */
54 val mountCheckUrl = "/restconf/operational/network-topology:network-topology/topology/topology-netconf/node/$deviceId"
56 val expectedResult = """"netconf-node-topology:connection-status":"connected""""
57 val mountCheckExecutionBlock: suspend (Int) -> String = { tryCount: Int ->
58 val result = webClientService.exchangeResource("GET", mountCheckUrl, "")
59 if (result.body.contains(expectedResult)) {
60 log.info("NF was mounted successfully on ODL")
63 throw BluePrintRetryException("Wait for device($deviceId) to mount")
67 log.info("url for ODL status check: $mountCheckUrl")
68 webClientService.retry<String>(10, 0, 1000, mountCheckExecutionBlock)
72 * Generic Configure function
73 * @return The WebClientResponse from the request
75 suspend fun AbstractScriptComponentFunction.restconfApplyDeviceConfig(
76 webClientService: BlueprintWebClientService,
78 configletResourcePath: String,
79 configletToApply: Any,
80 additionalHeaders: Map<String, String> = mutableMapOf("Content-Type" to "application/yang.patch+xml")
81 ): BlueprintWebClientService.WebClientResponse<String> {
82 log.debug("headers: $additionalHeaders")
83 log.info("configuring device: $deviceId, Configlet: $configletToApply")
84 val applyConfigUrl = "/restconf/config/network-topology:network-topology/topology/topology-netconf/node/" +
85 "$deviceId/$configletResourcePath"
86 return webClientService.exchangeResource("PATCH", applyConfigUrl, configletToApply as String, additionalHeaders)
89 suspend fun AbstractScriptComponentFunction.restconfDeviceConfig(
90 webClientService: BlueprintWebClientService,
92 configletResourcePath: String
94 BlueprintWebClientService.WebClientResponse<String> {
96 val configPathUrl = "/restconf/config/network-topology:network-topology/topology/topology-netconf/node/" +
97 "$deviceId/$configletResourcePath"
98 log.debug("sending GET request, url: $configPathUrl")
99 return webClientService.exchangeResource("GET", configPathUrl, "")
103 * Generic UnMount function
105 suspend fun AbstractScriptComponentFunction.restconfUnMountDevice(
106 webClientService: BlueprintWebClientService,
110 val unMountUrl = "/restconf/config/network-topology:network-topology/topology/topology-netconf/node/$deviceId"
111 log.info("sending unMount request, url: $unMountUrl")
112 webClientService.exchangeResource("DELETE", unMountUrl, "")
116 * Generic PUT/PATCH/POST request function
118 suspend fun AbstractScriptComponentFunction.genericPutPatchPostRequest(
119 webClientService: BlueprintWebClientService,
123 headers: Map<String, String> = mutableMapOf("Content-Type" to "application/xml")
124 ): BlueprintWebClientService.WebClientResponse<String> {
125 when (requestType.toUpperCase()) {
126 "PUT" -> log.info("sending PUT request, url: $requestUrl")
127 "PATCH" -> log.info("sending PATCH request, url: $requestUrl")
128 "POST" -> log.info("sending POST request, url: $requestUrl")
129 else -> throw BluePrintProcessorException("Illegal request type, only POST, PUT or PATCH allowed.")
131 return webClientService.exchangeResource(requestType, requestUrl, payload as String, headers)
135 * Generic GET/DELETE request function
138 suspend fun AbstractScriptComponentFunction.genericGetOrDeleteRequest(
139 webClientService: BlueprintWebClientService,
142 ): BlueprintWebClientService.WebClientResponse<String> {
143 when (requestType.toUpperCase()) {
144 "GET" -> log.info("sending GET request, url: $requestUrl")
145 "DELETE" -> log.info("sending DELETE request, url: $requestUrl")
146 else -> throw BluePrintProcessorException("Illegal request type, only GET and DELETE allowed.")
148 return webClientService.exchangeResource(requestType, requestUrl, "")
152 * Generic Mount function
153 * This function mount the given deviceId and verify if device mounted successfully.
154 * This function take mount url and mount verify url as parameters.
157 suspend fun AbstractScriptComponentFunction.restconfMountDevice(
158 webClientService: BlueprintWebClientService,
161 mountVerifyUrl: String,
162 headers: Map<String, String> = mutableMapOf("Content-Type" to "application/xml"),
163 expectedMountResult: String = """"netconf-node-topology:connection-status":"connected""""
166 log.info("sending mount request, url: $mountUrl")
167 webClientService.exchangeResource("PUT", mountUrl, payload as String, headers)
169 /** Check device has mounted */
170 val mountCheckExecutionBlock: suspend (Int) -> String = { tryCount: Int ->
171 val result = webClientService.exchangeResource("GET", mountVerifyUrl, "")
172 if (result.body.contains(expectedMountResult)) {
173 log.info("NF was mounted successfully on ODL")
176 throw BluePrintRetryException("Wait for device with url($mountUrl) to mount")
180 log.info("url for ODL status check: $mountVerifyUrl")
181 webClientService.retry<String>(10, 0, 1000, mountCheckExecutionBlock)