120c38044c52a6f45a7b50435b223c47188d921f
[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
34 class K8sPluginInstanceApi(
35     private val k8sConfiguration: K8sConnectionPluginConfiguration
36 ) {
37     private val log = LoggerFactory.getLogger(K8sPluginInstanceApi::class.java)!!
38
39     fun getInstanceList(): List<K8sRbInstance>? {
40         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration)
41         try {
42             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
43                 GET.name,
44                 "",
45                 ""
46             )
47             log.debug(result.toString())
48             return if (result.status in 200..299) {
49                 val objectMapper = jacksonObjectMapper()
50                 val parsedObject: ArrayList<K8sRbInstance>? = objectMapper.readValue(result.body)
51                 parsedObject
52             } else if (result.status == 500 && result.body.contains("Did not find any objects with tag"))
53                 null
54             else
55                 throw BlueprintProcessorException(result.body)
56         } catch (e: Exception) {
57             log.error("Caught exception trying to get k8s rb instance")
58             throw BlueprintProcessorException("${e.message}")
59         }
60     }
61
62     fun getInstanceById(instanceId: String): K8sRbInstance? {
63         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
64         try {
65             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
66                 GET.name,
67                 "",
68                 ""
69             )
70             log.debug(result.toString())
71             return if (result.status in 200..299) {
72                 val parsedObject: K8sRbInstance? = JacksonUtils.readValue(result.body, K8sRbInstance::class.java)
73                 parsedObject
74             } else if (result.status == 500 && result.body.contains("Error finding master table"))
75                 null
76             else
77                 throw BlueprintProcessorException(result.body)
78         } catch (e: Exception) {
79             log.error("Caught exception trying to get k8s rb instance")
80             throw BlueprintProcessorException("${e.message}")
81         }
82     }
83
84     fun getInstanceByRequestProperties(
85         rbDefinitionName: String,
86         rbDefinitionVersion: String,
87         rbProfileName: String
88     ): K8sRbInstance? {
89         val instances: List<K8sRbInstance>? = this.getInstanceList()
90         instances?.forEach {
91             if (it.request?.rbName == rbDefinitionName && it.request?.rbVersion == rbDefinitionVersion &&
92                 it.request?.profileName == rbProfileName
93             )
94                 return it
95         }
96         return null
97     }
98
99     fun getInstanceStatus(instanceId: String): K8sRbInstanceStatus? {
100         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
101         try {
102             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
103                 GET.name,
104                 "/status",
105                 ""
106             )
107             log.debug(result.toString())
108             return if (result.status in 200..299) {
109                 val parsedObject: K8sRbInstanceStatus? = JacksonUtils.readValue(
110                     result.body, K8sRbInstanceStatus::class.java
111                 )
112                 parsedObject
113             } else if (result.status == 500 && result.body.contains("Error finding master table"))
114                 null
115             else
116                 throw BlueprintProcessorException(result.body)
117         } catch (e: Exception) {
118             log.error("Caught exception trying to get k8s rb instance")
119             throw BlueprintProcessorException("${e.message}")
120         }
121     }
122
123     fun getInstanceHealthCheckList(instanceId: String): List<K8sRbInstanceHealthCheck>? {
124         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
125         try {
126             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
127                 GET.name,
128                 "/healthcheck",
129                 ""
130             )
131             log.debug(result.toString())
132             return if (result.status in 200..299) {
133                 val objectMapper = jacksonObjectMapper()
134                 val parsedObject: ArrayList<K8sRbInstanceHealthCheck>? = objectMapper.readValue(result.body)
135                 parsedObject
136             } else if (result.status == 500 && result.body.contains("Error finding master table"))
137                 null
138             else
139                 throw BlueprintProcessorException(result.body)
140         } catch (e: Exception) {
141             log.error("Caught exception trying to get k8s rb instance")
142             throw BlueprintProcessorException("${e.message}")
143         }
144     }
145
146     fun getInstanceHealthCheck(instanceId: String, healthCheckId: String): K8sRbInstanceHealthCheck? {
147         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
148         try {
149             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
150                 GET.name,
151                 "/healthcheck/$healthCheckId",
152                 ""
153             )
154             log.debug(result.toString())
155             return if (result.status in 200..299) {
156                 val parsedObject: K8sRbInstanceHealthCheck? = JacksonUtils.readValue(
157                     result.body,
158                     K8sRbInstanceHealthCheck::class.java
159                 )
160                 parsedObject
161             } else if (result.status == 500 && result.body.contains("Error finding master table"))
162                 null
163             else
164                 throw BlueprintProcessorException(result.body)
165         } catch (e: Exception) {
166             log.error("Caught exception trying to get k8s rb instance")
167             throw BlueprintProcessorException("${e.message}")
168         }
169     }
170
171     fun startInstanceHealthCheck(instanceId: String): K8sRbInstanceHealthCheck? {
172         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
173         try {
174             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
175                 POST.name,
176                 "/healthcheck",
177                 ""
178             )
179             log.debug(result.toString())
180             return if (result.status in 200..299) {
181                 val parsedObject: K8sRbInstanceHealthCheck? = JacksonUtils.readValue(
182                     result.body,
183                     K8sRbInstanceHealthCheck::class.java
184                 )
185                 parsedObject
186             } else if (result.status == 500 && result.body.contains("Error finding master table"))
187                 null
188             else
189                 throw BlueprintProcessorException(result.body)
190         } catch (e: Exception) {
191             log.error("Caught exception trying to get k8s rb instance")
192             throw BlueprintProcessorException("${e.message}")
193         }
194     }
195
196     fun deleteInstanceHealthCheck(instanceId: String, healthCheckId: String) {
197         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
198         try {
199             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
200                 DELETE.name,
201                 "/healthcheck/$healthCheckId",
202                 ""
203             )
204             log.debug(result.toString())
205             if (result.status !in 200..299)
206                 throw BlueprintProcessorException(result.body)
207         } catch (e: Exception) {
208             log.error("Caught exception trying to get k8s rb instance")
209             throw BlueprintProcessorException("${e.message}")
210         }
211     }
212 }