summaryrefslogtreecommitdiffstats
path: root/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/BlueprintWebClientService.kt
blob: d6d02b3300640c9782d7cf400ef014a4977bb462 (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
 * Copyright © 2017-2019 AT&T, Bell Canada, Nordix Foundation
 * Modifications Copyright © 2018-2019 IBM.
 * Modifications Copyright © 2019 Huawei.
 *
 * 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.rest.service

import com.fasterxml.jackson.databind.JsonNode
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.apache.commons.io.IOUtils
import org.apache.http.client.methods.HttpDelete
import org.apache.http.client.methods.HttpGet
import org.apache.http.client.methods.HttpPatch
import org.apache.http.client.methods.HttpPost
import org.apache.http.client.methods.HttpPut
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.CloseableHttpClient
import org.apache.http.impl.client.HttpClients
import org.apache.http.message.BasicHeader
import org.onap.ccsdk.cds.blueprintsprocessor.rest.utils.WebClientUtils
import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
import org.springframework.http.HttpMethod
import java.io.InputStream
import java.nio.charset.Charset

interface BlueprintWebClientService {

    fun defaultHeaders(): Map<String, String>

    fun host(uri: String): String

    fun httpClient(): CloseableHttpClient {
        return HttpClients.custom()
                .addInterceptorFirst(WebClientUtils.logRequest())
                .addInterceptorLast(WebClientUtils.logResponse())
                .build()
    }

    fun exchangeResource(methodType: String, path: String, request: String):
            String {
        return this.exchangeResource(methodType, path, request,
                defaultHeaders())
    }

    fun exchangeResource(methodType: String, path: String, request: String,
                         headers: Map<String, String>): String {
        val convertedHeaders: Array<BasicHeader> = convertToBasicHeaders(
                headers)
        return when (HttpMethod.resolve(methodType)) {
            HttpMethod.DELETE -> delete(path, convertedHeaders)
            HttpMethod.GET -> get(path, convertedHeaders)
            HttpMethod.POST -> post(path, request, convertedHeaders)
            HttpMethod.PUT -> put(path, request, convertedHeaders)
            HttpMethod.PATCH -> patch(path, request, convertedHeaders)
            else -> throw BluePrintProcessorException("Unsupported met" +
                    "hodType($methodType)")
        }
    }

    fun convertToBasicHeaders(headers: Map<String, String>): Array<BasicHeader> {
        return headers.map{ BasicHeader(it.key, it.value)}.toTypedArray()
    }

    fun delete(path: String, headers: Array<BasicHeader>): String {
        val httpDelete = HttpDelete(host(path))
        httpDelete.setHeaders(headers)
        httpClient().execute(httpDelete).entity.content.use {
            return IOUtils.toString(it, Charset.defaultCharset())
        }
    }

    fun get(path: String, headers: Array<BasicHeader>): String {
        val httpGet = HttpGet(host(path))
        httpGet.setHeaders(headers)
        httpClient().execute(httpGet).entity.content.use {
            return IOUtils.toString(it, Charset.defaultCharset())
        }
    }

    fun post(path: String, request: String, headers: Array<BasicHeader>):
            String {
        val httpPost = HttpPost(host(path))
        val entity = StringEntity(request)
        httpPost.entity = entity
        httpPost.setHeaders(headers)
        httpClient().execute(httpPost).entity.content.use {
            return IOUtils.toString(it, Charset.defaultCharset())
        }
    }

    fun put(path: String, request: String, headers: Array<BasicHeader>):
            String {
        val httpPut = HttpPut(host(path))
        val entity = StringEntity(request)
        httpPut.entity = entity
        httpPut.setHeaders(headers)
        httpClient().execute(httpPut).entity.content.use {
            return IOUtils.toString(it, Charset.defaultCharset())
        }
    }

    fun patch(path: String, request: String, headers: Array<BasicHeader>):
            String {
        val httpPatch = HttpPatch(host(path))
        val entity = StringEntity(request)
        httpPatch.entity = entity
        httpPatch.setHeaders(headers)
        httpClient().execute(httpPatch).entity.content.use {
            return IOUtils.toString(it, Charset.defaultCharset())
        }
    }


    suspend fun getNB(path: String): String {
        return getNB(path, null, String::class.java)
    }

    suspend fun getNB(path: String, additionalHeaders: Map<String, String>?):
            String {
        return getNB(path, additionalHeaders, String::class.java)
    }

    suspend fun <T> getNB(path: String, additionalHeaders: Map<String, String>?,
                          responseType: Class<T>): T =
            withContext(Dispatchers.IO) {
        val httpGet = HttpGet(host(path))
        httpGet.setHeaders(basicHeaders(additionalHeaders))
        httpClientNB().execute(httpGet).entity.content.use {
            getResponse(it, responseType)
        }
    }

    suspend fun postNB(path: String, request: Any): String {
        return postNB(path, request, null, String::class.java)
    }

    suspend fun postNB(path: String, request: Any,
                       additionalHeaders: Map<String, String>?): String {
        return postNB(path, request, additionalHeaders, String::class.java)
    }

    suspend fun <T> postNB(path: String, request: Any,
                           additionalHeaders: Map<String, String>?,
                           responseType: Class<T>): T =
            withContext(Dispatchers.IO) {
                val httpPost = HttpPost(host(path))
                httpPost.entity = StringEntity(strRequest(request))
                httpPost.setHeaders(basicHeaders(additionalHeaders))
                httpClientNB().execute(httpPost).entity.content.use {
                    getResponse(it, responseType)
                }
            }

    suspend fun putNB(path: String, request: Any): String {
        return putNB(path, request, null, String::class.java)
    }

    suspend fun putNB(path: String, request: Any,
                      additionalHeaders: Map<String, String>?): String {
        return putNB(path, request, additionalHeaders, String::class.java)
    }

    suspend fun <T> putNB(path: String, request: Any,
                          additionalHeaders: Map<String, String>?,
                          responseType: Class<T>): T =
            withContext(Dispatchers.IO) {
        val httpPut = HttpPut(host(path))
        httpPut.entity = StringEntity(strRequest(request))
        httpPut.setHeaders(basicHeaders(additionalHeaders))
        httpClientNB().execute(httpPut).entity.content.use {
            getResponse(it, responseType)
        }
    }

    suspend fun <T> deleteNB(path: String): String {
        return deleteNB(path, null, String::class.java)
    }

    suspend fun <T> deleteNB(path: String,
                             additionalHeaders: Map<String, String>?): String {
        return deleteNB(path, additionalHeaders, String::class.java)
    }

    suspend fun <T> deleteNB(path: String,
                             additionalHeaders: Map<String, String>?,
                             responseType: Class<T>): T =
            withContext(Dispatchers.IO) {
        val httpDelete = HttpDelete(host(path))
        httpDelete.setHeaders(basicHeaders(additionalHeaders))
        httpClient().execute(httpDelete).entity.content.use {
            getResponse(it, responseType)
        }
    }

    suspend fun <T> patchNB(path: String, request: Any,
                            additionalHeaders: Map<String, String>?,
                            responseType: Class<T>): T =
            withContext(Dispatchers.IO) {
        val httpPatch = HttpPatch(host(path))
        httpPatch.entity = StringEntity(strRequest(request))
        httpPatch.setHeaders(basicHeaders(additionalHeaders))
        httpClient().execute(httpPatch).entity.content.use {
            getResponse(it, responseType)
        }
    }

    suspend fun exchangeNB(methodType: String, path: String, request: Any):
            String {
        return exchangeNB(methodType, path, request, hashMapOf(),
                String::class.java)
    }

    suspend fun exchangeNB(methodType: String, path: String, request: Any,
                           additionalHeaders: Map<String, String>?): String {
        return exchangeNB(methodType, path, request, additionalHeaders,
                String::class.java)
    }

    suspend fun <T> exchangeNB(methodType: String, path: String, request: Any,
                               additionalHeaders: Map<String, String>?,
                               responseType: Class<T>): T {

        return when (HttpMethod.resolve(methodType)) {
            HttpMethod.GET -> getNB(path, additionalHeaders, responseType)
            HttpMethod.POST -> postNB(path, request, additionalHeaders,
                    responseType)
            HttpMethod.DELETE -> deleteNB(path, additionalHeaders, responseType)
            HttpMethod.PUT -> putNB(path, request, additionalHeaders,
                    responseType)
            HttpMethod.PATCH -> patchNB(path, request, additionalHeaders,
                    responseType)
            else -> throw BluePrintProcessorException("Unsupported method" +
                    "Type($methodType)")
        }
    }

    private fun strRequest(request: Any): String {
        return when (request) {
            is String -> request.toString()
            is JsonNode -> request.toString()
            else -> JacksonUtils.getJson(request)
        }
    }

    private fun <T> getResponse(it: InputStream, responseType: Class<T>): T {
        return if (responseType == String::class.java) {
            IOUtils.toString(it, Charset.defaultCharset()) as T
        } else {
            JacksonUtils.readValue(it, responseType)!!
        }
    }

    private fun basicHeaders(headers: Map<String, String>?):
            Array<BasicHeader> {

        val basicHeaders = mutableListOf<BasicHeader>()
        defaultHeaders().forEach { name, value ->
            basicHeaders.add(BasicHeader(name, value))
        }
        headers?.forEach { name, value ->
            basicHeaders.add(BasicHeader(name, value))
        }
        return basicHeaders.toTypedArray()
    }

    // Non Blocking Rest Implementation
    suspend fun httpClientNB(): CloseableHttpClient {
        return HttpClients.custom()
                .addInterceptorFirst(WebClientUtils.logRequest())
                .addInterceptorLast(WebClientUtils.logResponse())
                .build()
    }
}