7663699aa39f32854b06d06594d1f4f6f2c57935
[ccsdk/cds.git] /
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 hasConfigurationValues(instanceId: String, configName: String): Boolean {
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 result.status in 200..299
280         } catch (e: Exception) {
281             log.error("Caught exception trying to get k8s rb instance")
282             throw BlueprintProcessorException("${e.message}")
283         }
284     }
285
286     fun rollbackConfigurationValues(instanceId: String): K8sConfigValueResponse? {
287         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
288         try {
289             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
290                 POST.name,
291                 "/rollback",
292                 ""
293             )
294             log.debug(result.toString())
295             return if (result.status in 200..299) {
296                 val parsedObject: K8sConfigValueResponse? = JacksonUtils.readValue(
297                     result.body, K8sConfigValueResponse::class.java
298                 )
299                 parsedObject
300             } else
301                 throw BlueprintProcessorException(result.body)
302         } catch (e: Exception) {
303             log.error("Caught exception trying to get k8s rb instance")
304             throw BlueprintProcessorException("${e.message}")
305         }
306     }
307
308     fun createConfigurationValues(instanceId: String): K8sConfigValueResponse? {
309         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
310         try {
311             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
312                 POST.name,
313                 "/tagit",
314                 ""
315             )
316             log.debug(result.toString())
317             return if (result.status in 200..299) {
318                 val parsedObject: K8sConfigValueResponse? = JacksonUtils.readValue(
319                     result.body, K8sConfigValueResponse::class.java
320                 )
321                 parsedObject
322             } else
323                 throw BlueprintProcessorException(result.body)
324         } catch (e: Exception) {
325             log.error("Caught exception trying to get k8s rb instance")
326             throw BlueprintProcessorException("${e.message}")
327         }
328     }
329
330     fun deleteInstanceHealthCheck(instanceId: String, healthCheckId: String) {
331         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
332         try {
333             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
334                 DELETE.name,
335                 "/healthcheck/$healthCheckId",
336                 ""
337             )
338             log.debug(result.toString())
339             if (result.status !in 200..299)
340                 throw BlueprintProcessorException(result.body)
341         } catch (e: Exception) {
342             log.error("Caught exception trying to get k8s rb instance")
343             throw BlueprintProcessorException("${e.message}")
344         }
345     }
346 }