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.BluePrintRetryException
25 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintDependencyService
28 * Register the Restconf module exposed dependency
31 val log = LoggerFactory.logger(AbstractScriptComponentFunction::class.java)!!
33 fun AbstractScriptComponentFunction.restconfClientService(selector: String): BlueprintWebClientService {
34 return BluePrintDependencyService.restClientService(selector)
38 * Generic Mount function
41 suspend fun AbstractScriptComponentFunction.restconfMountDevice(
42 webClientService: BlueprintWebClientService,
45 headers: Map<String, String> = mutableMapOf("Content-Type" to "application/xml")
48 val mountUrl = "restconf/config/network-topology:network-topology/topology/topology-netconf/node/$deviceId"
49 log.info("sending mount request, url: $mountUrl")
50 webClientService.exchangeResource("PUT", mountUrl, payload as String, headers)
52 /** Check device has mounted */
53 val mountCheckUrl = "restconf/operational/network-topology:network-topology/topology/topology-netconf/node/$deviceId"
55 val expectedResult = """"netconf-node-topology:connection-status":"connected""""
56 val mountCheckExecutionBlock: suspend (Int) -> String = { tryCount: Int ->
57 val result = webClientService.exchangeResource("GET", mountCheckUrl, "")
58 if (result.body.contains(expectedResult)) {
59 log.info("NF was mounted successfully on ODL")
62 throw BluePrintRetryException("Wait for device($deviceId) to mount")
66 log.info("url for ODL status check: $mountCheckUrl")
67 webClientService.retry<String>(10, 0, 1000, mountCheckExecutionBlock)
71 * Generic Configure function
72 * @return The WebClientResponse from the request
74 suspend fun AbstractScriptComponentFunction.restconfApplyDeviceConfig(
75 webClientService: BlueprintWebClientService,
77 configletResourcePath: String,
78 configletToApply: Any,
79 additionalHeaders: Map<String, String> = mutableMapOf("Content-Type" to "application/yang.patch+xml")
80 ): BlueprintWebClientService.WebClientResponse<String> {
81 log.debug("headers: $additionalHeaders")
82 log.info("configuring device: $deviceId, Configlet: $configletToApply")
83 val applyConfigUrl = "restconf/config/network-topology:network-topology/topology/topology-netconf/node/" +
84 "$deviceId/$configletResourcePath"
85 return webClientService.exchangeResource("PATCH", applyConfigUrl, configletToApply as String, additionalHeaders)
88 suspend fun AbstractScriptComponentFunction.restconfDeviceConfig(
89 webClientService: BlueprintWebClientService,
91 configletResourcePath: String
93 BlueprintWebClientService.WebClientResponse<String> {
95 val configPathUrl = "restconf/config/network-topology:network-topology/topology/topology-netconf/node/" +
96 "$deviceId/$configletResourcePath"
97 log.debug("sending GET request, url: $configPathUrl")
98 return webClientService.exchangeResource("GET", configPathUrl, "")
102 * Generic UnMount function
104 suspend fun AbstractScriptComponentFunction.restconfUnMountDevice(
105 webClientService: BlueprintWebClientService,
109 val unMountUrl = "restconf/config/network-topology:network-topology/topology/topology-netconf/node/$deviceId"
110 log.info("sending unMount request, url: $unMountUrl")
111 webClientService.exchangeResource("DELETE", unMountUrl, "")