Renaming Files having BluePrint to have Blueprint
[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.BlueprintProcessorException
25 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintRetryException
26 import org.onap.ccsdk.cds.controllerblueprints.core.service.BlueprintDependencyService
27
28 /**
29  * Register the Restconf module exposed dependency
30  */
31
32 val log = LoggerFactory.logger(AbstractScriptComponentFunction::class.java)!!
33
34 fun AbstractScriptComponentFunction.restconfClientService(selector: String): BlueprintWebClientService {
35     return BlueprintDependencyService.restClientService(selector)
36 }
37
38 /**
39  * Generic Mount function
40  */
41
42 suspend fun AbstractScriptComponentFunction.restconfMountDevice(
43     webClientService: BlueprintWebClientService,
44     deviceId: String,
45     payload: Any,
46     headers: Map<String, String> = mutableMapOf("Content-Type" to "application/xml")
47 ) {
48
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)
52
53     /** Check device has mounted */
54     val mountCheckUrl = "/restconf/operational/network-topology:network-topology/topology/topology-netconf/node/$deviceId"
55
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")
61             result.body
62         } else {
63             throw BlueprintRetryException("Wait for device($deviceId) to mount")
64         }
65     }
66
67     log.info("url for ODL status check: $mountCheckUrl")
68     webClientService.retry<String>(10, 0, 1000, mountCheckExecutionBlock)
69 }
70
71 /**
72  * Generic Configure function
73  * @return The WebClientResponse from the request
74  */
75 suspend fun AbstractScriptComponentFunction.restconfApplyDeviceConfig(
76     webClientService: BlueprintWebClientService,
77     deviceId: String,
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)
87 }
88
89 suspend fun AbstractScriptComponentFunction.restconfDeviceConfig(
90     webClientService: BlueprintWebClientService,
91     deviceId: String,
92     configletResourcePath: String
93 ):
94     BlueprintWebClientService.WebClientResponse<String> {
95
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, "")
100     }
101
102 /**
103  * Generic UnMount function
104  */
105 suspend fun AbstractScriptComponentFunction.restconfUnMountDevice(
106     webClientService: BlueprintWebClientService,
107     deviceId: String,
108     payload: String
109 ) {
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, "")
113 }
114
115 /**
116  * Generic PUT/PATCH/POST request function
117  */
118 suspend fun AbstractScriptComponentFunction.genericPutPatchPostRequest(
119     webClientService: BlueprintWebClientService,
120     requestUrl: String,
121     requestType: String,
122     payload: Any,
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.")
130     }
131     return webClientService.exchangeResource(requestType, requestUrl, payload as String, headers)
132 }
133
134 /**
135  * Generic GET/DELETE request function
136  */
137
138 suspend fun AbstractScriptComponentFunction.genericGetOrDeleteRequest(
139     webClientService: BlueprintWebClientService,
140     requestUrl: String,
141     requestType: String
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.")
147     }
148     return webClientService.exchangeResource(requestType, requestUrl, "")
149 }
150
151 /**
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.
155  */
156
157 suspend fun AbstractScriptComponentFunction.restconfMountDevice(
158     webClientService: BlueprintWebClientService,
159     payload: Any,
160     mountUrl: String,
161     mountVerifyUrl: String,
162     headers: Map<String, String> = mutableMapOf("Content-Type" to "application/xml"),
163     expectedMountResult: String = """"netconf-node-topology:connection-status":"connected""""
164 ) {
165
166     log.info("sending mount request, url: $mountUrl")
167     webClientService.exchangeResource("PUT", mountUrl, payload as String, headers)
168
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")
174             result.body
175         } else {
176             throw BlueprintRetryException("Wait for device with url($mountUrl) to mount")
177         }
178     }
179
180     log.info("url for ODL status check: $mountVerifyUrl")
181     webClientService.retry<String>(10, 0, 1000, mountCheckExecutionBlock)
182 }