3faaac00d1acc8198951fa96e5156b95f6458556
[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.databind.JsonNode
23 import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.K8sConnectionPluginConfiguration
24 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService
25 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintProcessorException
26 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
27 import org.slf4j.LoggerFactory
28 import org.springframework.http.HttpMethod.GET
29
30 class K8sPluginInstanceApi(
31     private val k8sConfiguration: K8sConnectionPluginConfiguration
32 ) {
33     private val log = LoggerFactory.getLogger(K8sPluginInstanceApi::class.java)!!
34
35     fun getAllInstances(): List<K8sRbInstance>? {
36         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration)
37         try {
38             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
39                 GET.name,
40                 "",
41                 ""
42             )
43             log.debug(result.toString())
44             return if (result.status in 200..299) {
45                 val parsedObject: List<K8sRbInstance> = JacksonUtils.readValue(result.body)
46                 parsedObject
47             } else if (result.status == 500 && result.body.contains("Did not find any objects with tag"))
48                 null
49             else
50                 throw BlueprintProcessorException(result.body)
51         } catch (e: Exception) {
52             log.error("Caught exception trying to get k8s rb instance")
53             throw BlueprintProcessorException("${e.message}")
54         }
55     }
56
57     fun getInstanceById(instanceId: String): K8sRbInstance? {
58         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
59         try {
60             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
61                 GET.name,
62                 "",
63                 ""
64             )
65             log.debug(result.toString())
66             return if (result.status in 200..299) {
67                 val instance: JsonNode = JacksonUtils.jsonNode(result.body)
68                 val parsedObject: K8sRbInstance? = JacksonUtils.readValue(result.body, K8sRbInstance::class.java)
69                 parsedObject
70             } else if (result.status == 500 && result.body.contains("Error finding master table"))
71                 null
72             else
73                 throw BlueprintProcessorException(result.body)
74         } catch (e: Exception) {
75             log.error("Caught exception trying to get k8s rb instance")
76             throw BlueprintProcessorException("${e.message}")
77         }
78     }
79
80     fun getInstanceByRequestProperties(
81         rbDefinitionName: String,
82         rbDefinitionVersion: String,
83         rbProfileName: String
84     ): K8sRbInstance? {
85         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration)
86         try {
87             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
88                 GET.name,
89                 "",
90                 ""
91             )
92             log.debug(result.toString())
93             return if (result.status in 200..299) {
94                 val parsedObject: List<K8sRbInstance> = JacksonUtils.readValue(result.body)
95                 var instance: K8sRbInstance? = null
96                 parsedObject.forEach {
97                     if (it.request?.rbName == rbDefinitionName && it.request?.rbVersion == rbDefinitionVersion &&
98                         it.request?.profileName == rbProfileName
99                     )
100                         instance = it
101                 }
102                 instance
103             } else if (result.status == 500 && result.body.contains("Did not find any objects with tag"))
104                 null
105             else
106                 throw BlueprintProcessorException(result.body)
107         } catch (e: Exception) {
108             log.error("Caught exception trying to get k8s rb instance")
109             throw BlueprintProcessorException("${e.message}")
110         }
111     }
112
113     fun getInstanceStatus(instanceId: String): K8sRbInstanceStatus? {
114         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
115         try {
116             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
117                 GET.name,
118                 "/status",
119                 ""
120             )
121             log.debug(result.toString())
122             return if (result.status in 200..299) {
123                 val parsedObject: K8sRbInstanceStatus? = JacksonUtils.readValue(
124                     result.body, K8sRbInstanceStatus::class.java
125                 )
126                 parsedObject
127             } else if (result.status == 500 && result.body.contains("Error finding master table"))
128                 null
129             else
130                 throw BlueprintProcessorException(result.body)
131         } catch (e: Exception) {
132             log.error("Caught exception trying to get k8s rb instance")
133             throw BlueprintProcessorException("${e.message}")
134         }
135     }
136 }