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.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 package org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.instance
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
30 class K8sPluginInstanceApi(
31 private val k8sConfiguration: K8sConnectionPluginConfiguration
33 private val log = LoggerFactory.getLogger(K8sPluginInstanceApi::class.java)!!
35 fun getAllInstances(): List<K8sRbInstance>? {
36 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration)
38 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
43 log.debug(result.toString())
44 return if (result.status in 200..299) {
45 val parsedObject: List<K8sRbInstance> = JacksonUtils.readValue(result.body)
47 } else if (result.status == 500 && result.body.contains("Did not find any objects with tag"))
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}")
57 fun getInstanceById(instanceId: String): K8sRbInstance? {
58 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
60 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
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)
70 } else if (result.status == 500 && result.body.contains("Error finding master table"))
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}")
80 fun getInstanceByRequestProperties(
81 rbDefinitionName: String,
82 rbDefinitionVersion: String,
85 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration)
87 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
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
103 } else if (result.status == 500 && result.body.contains("Did not find any objects with tag"))
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}")
113 fun getInstanceStatus(instanceId: String): K8sRbInstanceStatus? {
114 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
116 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
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
127 } else if (result.status == 500 && result.body.contains("Error finding master table"))
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}")