Merge "Wrap day2 api and create configuration-value component"
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / k8s-connection-plugin / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / k8s / instance / K8sPluginInstanceApi.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2019 IBM.
4  * Modifications Copyright © 2021 Orange.
5  * Modifications Copyright © 2020 Deutsche Telekom AG.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 package org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.instance
21
22 import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
23 import com.fasterxml.jackson.module.kotlin.readValue
24 import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.K8sConnectionPluginConfiguration
25 import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.instance.healthcheck.K8sRbInstanceHealthCheck
26 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService
27 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintProcessorException
28 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
29 import org.slf4j.LoggerFactory
30 import org.springframework.http.HttpMethod.DELETE
31 import org.springframework.http.HttpMethod.GET
32 import org.springframework.http.HttpMethod.POST
33 import org.springframework.http.HttpMethod.PUT
34
35 class K8sPluginInstanceApi(
36     private val k8sConfiguration: K8sConnectionPluginConfiguration
37 ) {
38     private val log = LoggerFactory.getLogger(K8sPluginInstanceApi::class.java)!!
39
40     fun getInstanceList(): List<K8sRbInstance>? {
41         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration)
42         try {
43             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
44                 GET.name,
45                 "",
46                 ""
47             )
48             log.debug(result.toString())
49             return if (result.status in 200..299) {
50                 val objectMapper = jacksonObjectMapper()
51                 val parsedObject: ArrayList<K8sRbInstance>? = objectMapper.readValue(result.body)
52                 parsedObject
53             } else if (result.status == 500 && result.body.contains("Did not find any objects with tag"))
54                 null
55             else
56                 throw BlueprintProcessorException(result.body)
57         } catch (e: Exception) {
58             log.error("Caught exception trying to get k8s rb instance")
59             throw BlueprintProcessorException("${e.message}")
60         }
61     }
62
63     fun getInstanceById(instanceId: String): K8sRbInstance? {
64         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
65         try {
66             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
67                 GET.name,
68                 "",
69                 ""
70             )
71             log.debug(result.toString())
72             return if (result.status in 200..299) {
73                 val parsedObject: K8sRbInstance? = JacksonUtils.readValue(result.body, K8sRbInstance::class.java)
74                 parsedObject
75             } else if (result.status == 500 && result.body.contains("Error finding master table"))
76                 null
77             else
78                 throw BlueprintProcessorException(result.body)
79         } catch (e: Exception) {
80             log.error("Caught exception trying to get k8s rb instance")
81             throw BlueprintProcessorException("${e.message}")
82         }
83     }
84
85     fun getInstanceByRequestProperties(
86         rbDefinitionName: String,
87         rbDefinitionVersion: String,
88         rbProfileName: String
89     ): K8sRbInstance? {
90         val instances: List<K8sRbInstance>? = this.getInstanceList()
91         instances?.forEach {
92             if (it.request?.rbName == rbDefinitionName && it.request?.rbVersion == rbDefinitionVersion &&
93                 it.request?.profileName == rbProfileName
94             )
95                 return it
96         }
97         return null
98     }
99
100     fun getInstanceStatus(instanceId: String): K8sRbInstanceStatus? {
101         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
102         try {
103             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
104                 GET.name,
105                 "/status",
106                 ""
107             )
108             log.debug(result.toString())
109             return if (result.status in 200..299) {
110                 val parsedObject: K8sRbInstanceStatus? = JacksonUtils.readValue(
111                     result.body, K8sRbInstanceStatus::class.java
112                 )
113                 parsedObject
114             } else if (result.status == 500 && result.body.contains("Error finding master table"))
115                 null
116             else
117                 throw BlueprintProcessorException(result.body)
118         } catch (e: Exception) {
119             log.error("Caught exception trying to get k8s rb instance")
120             throw BlueprintProcessorException("${e.message}")
121         }
122     }
123
124     fun queryInstanceStatus(instanceId: String, kind: String, apiVersion: String, name: String?, labels: String?): K8sRbInstanceStatus? {
125         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
126         try {
127             var path: String = "/query?ApiVersion=$apiVersion&Kind=$kind"
128             if (name != null)
129                 path = path.plus("&Name=$name")
130             if (labels != null)
131                 path = path.plus("&Labels=$labels")
132             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
133                 GET.name,
134                 path,
135                 ""
136             )
137             log.debug(result.toString())
138             return if (result.status in 200..299) {
139                 val parsedObject: K8sRbInstanceStatus? = JacksonUtils.readValue(
140                     result.body, K8sRbInstanceStatus::class.java
141                 )
142                 parsedObject
143             } else if (result.status == 500 && result.body.contains("Error finding master table"))
144                 null
145             else
146                 throw BlueprintProcessorException(result.body)
147         } catch (e: Exception) {
148             log.error("Caught exception trying to get k8s rb instance")
149             throw BlueprintProcessorException("${e.message}")
150         }
151     }
152
153     fun getInstanceHealthCheckList(instanceId: String): List<K8sRbInstanceHealthCheck>? {
154         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
155         try {
156             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
157                 GET.name,
158                 "/healthcheck",
159                 ""
160             )
161             log.debug(result.toString())
162             return if (result.status in 200..299) {
163                 val objectMapper = jacksonObjectMapper()
164                 val parsedObject: ArrayList<K8sRbInstanceHealthCheck>? = objectMapper.readValue(result.body)
165                 parsedObject
166             } else if (result.status == 500 && result.body.contains("Error finding master table"))
167                 null
168             else
169                 throw BlueprintProcessorException(result.body)
170         } catch (e: Exception) {
171             log.error("Caught exception trying to get k8s rb instance")
172             throw BlueprintProcessorException("${e.message}")
173         }
174     }
175
176     fun getInstanceHealthCheck(instanceId: String, healthCheckId: String): K8sRbInstanceHealthCheck? {
177         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
178         try {
179             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
180                 GET.name,
181                 "/healthcheck/$healthCheckId",
182                 ""
183             )
184             log.debug(result.toString())
185             return if (result.status in 200..299) {
186                 val parsedObject: K8sRbInstanceHealthCheck? = JacksonUtils.readValue(
187                     result.body,
188                     K8sRbInstanceHealthCheck::class.java
189                 )
190                 parsedObject
191             } else if (result.status == 500 && result.body.contains("Error finding master table"))
192                 null
193             else
194                 throw BlueprintProcessorException(result.body)
195         } catch (e: Exception) {
196             log.error("Caught exception trying to get k8s rb instance")
197             throw BlueprintProcessorException("${e.message}")
198         }
199     }
200
201     fun startInstanceHealthCheck(instanceId: String): K8sRbInstanceHealthCheck? {
202         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
203         try {
204             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
205                 POST.name,
206                 "/healthcheck",
207                 ""
208             )
209             log.debug(result.toString())
210             return if (result.status in 200..299) {
211                 val parsedObject: K8sRbInstanceHealthCheck? = JacksonUtils.readValue(
212                     result.body,
213                     K8sRbInstanceHealthCheck::class.java
214                 )
215                 parsedObject
216             } else if (result.status == 500 && result.body.contains("Error finding master table"))
217                 null
218             else
219                 throw BlueprintProcessorException(result.body)
220         } catch (e: Exception) {
221             log.error("Caught exception trying to get k8s rb instance")
222             throw BlueprintProcessorException("${e.message}")
223         }
224     }
225
226     fun createConfigurationValues(configValues: K8sConfigValueRequest, instanceId: String): K8sConfigValueResponse? {
227         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
228         try {
229             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
230                 POST.name,
231                 "/config",
232                 JacksonUtils.getJson(configValues)
233             )
234             log.debug(result.toString())
235             return if (result.status in 200..299) {
236                 val parsedObject: K8sConfigValueResponse? = JacksonUtils.readValue(
237                     result.body, K8sConfigValueResponse::class.java
238                 )
239                 parsedObject
240             } else
241                 throw BlueprintProcessorException(result.body)
242         } catch (e: Exception) {
243             log.error("Caught exception trying to get k8s rb instance")
244             throw BlueprintProcessorException("${e.message}")
245         }
246     }
247
248     fun editConfigurationValues(configValues: K8sConfigValueRequest, instanceId: String, configName: String): K8sConfigValueResponse? {
249         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
250         try {
251             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
252                 PUT.name,
253                 "/config/$configName",
254                 JacksonUtils.getJson(configValues)
255             )
256             log.debug(result.toString())
257             return if (result.status in 200..299) {
258                 val parsedObject: K8sConfigValueResponse? = JacksonUtils.readValue(
259                     result.body, K8sConfigValueResponse::class.java
260                 )
261                 parsedObject
262             } else
263                 throw BlueprintProcessorException(result.body)
264         } catch (e: Exception) {
265             log.error("Caught exception trying to get k8s rb instance")
266             throw BlueprintProcessorException("${e.message}")
267         }
268     }
269
270     fun getConfigurationValues(instanceId: String, configName: String): K8sConfigValueResponse? {
271         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
272         try {
273             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
274                 GET.name,
275                 "/config/$configName",
276                 ""
277             )
278             log.debug(result.toString())
279             return if (result.status in 200..299) {
280                 val parsedObject: K8sConfigValueResponse? = JacksonUtils.readValue(
281                     result.body, K8sConfigValueResponse::class.java
282                 )
283                 parsedObject
284             } else
285                 throw BlueprintProcessorException(result.body)
286         } catch (e: Exception) {
287             log.error("Caught exception trying to get k8s rb instance")
288             throw BlueprintProcessorException("${e.message}")
289         }
290     }
291
292     fun deleteConfigurationValues(instanceId: String, configName: String) {
293         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
294         try {
295             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
296                 DELETE.name,
297                 "/config/$configName",
298                 ""
299             )
300             log.debug(result.toString())
301             if (result.status !in 200..299) {
302                 throw BlueprintProcessorException(result.body)
303             }
304         } catch (e: Exception) {
305             log.error("Caught exception trying to get k8s rb instance")
306             throw BlueprintProcessorException("${e.message}")
307         }
308     }
309
310     fun rollbackConfigurationValues(instanceId: String): K8sConfigValueResponse? {
311         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
312         try {
313             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
314                 POST.name,
315                 "/rollback",
316                 ""
317             )
318             log.debug(result.toString())
319             return if (result.status in 200..299) {
320                 val parsedObject: K8sConfigValueResponse? = JacksonUtils.readValue(
321                     result.body, K8sConfigValueResponse::class.java
322                 )
323                 parsedObject
324             } else
325                 throw BlueprintProcessorException(result.body)
326         } catch (e: Exception) {
327             log.error("Caught exception trying to get k8s rb instance")
328             throw BlueprintProcessorException("${e.message}")
329         }
330     }
331
332     fun createConfigurationValues(instanceId: String): K8sConfigValueResponse? {
333         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
334         try {
335             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
336                 POST.name,
337                 "/tagit",
338                 ""
339             )
340             log.debug(result.toString())
341             return if (result.status in 200..299) {
342                 val parsedObject: K8sConfigValueResponse? = JacksonUtils.readValue(
343                     result.body, K8sConfigValueResponse::class.java
344                 )
345                 parsedObject
346             } else
347                 throw BlueprintProcessorException(result.body)
348         } catch (e: Exception) {
349             log.error("Caught exception trying to get k8s rb instance")
350             throw BlueprintProcessorException("${e.message}")
351         }
352     }
353
354     fun deleteInstanceHealthCheck(instanceId: String, healthCheckId: String) {
355         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
356         try {
357             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
358                 DELETE.name,
359                 "/healthcheck/$healthCheckId",
360                 ""
361             )
362             log.debug(result.toString())
363             if (result.status !in 200..299)
364                 throw BlueprintProcessorException(result.body)
365         } catch (e: Exception) {
366             log.error("Caught exception trying to get k8s rb instance")
367             throw BlueprintProcessorException("${e.message}")
368         }
369     }
370 }