Revert "Renaming Files having BluePrint to have Blueprint"
[ccsdk/cds.git] / components / model-catalog / blueprint-model / service-blueprint / vLB_CDS_RESTCONF / Scripts / kotlin / KotlinRestCall.kt
1 /*\r
2  * Copyright © 2019 IBM, Bell Canada, Orange\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 package org.onap.ccsdk.cds.blueprintsprocessor.services.execution.scripts\r
18 \r
19 import com.fasterxml.jackson.databind.node.ObjectNode\r
20 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput\r
21 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.storedContentFromResolvedArtifactNB\r
22 import org.onap.ccsdk.cds.blueprintsprocessor.rest.BasicAuthRestClientProperties\r
23 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BasicAuthRestClientService\r
24 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService\r
25 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractScriptComponentFunction\r
26 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils\r
27 import org.slf4j.LoggerFactory\r
28 import org.springframework.http.HttpMethod\r
29 import org.springframework.web.client.RestTemplate\r
30 import com.fasterxml.jackson.annotation.JsonIgnore\r
31 import com.fasterxml.jackson.annotation.JsonProperty\r
32 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException\r
33 \r
34 open class ConfigDeploy : AbstractScriptComponentFunction() {\r
35 \r
36     private val log = LoggerFactory.getLogger(ConfigDeploy::class.java)!!\r
37 \r
38     override fun getName(): String {\r
39         return "Check"\r
40     }\r
41 \r
42     override suspend fun processNB(executionRequest: ExecutionServiceInput) {\r
43         log.info("executing script")\r
44         val resolution_key = getDynamicProperties("resolution-key").asText()\r
45         log.info("resolution_key: $resolution_key")\r
46 \r
47         val payload = storedContentFromResolvedArtifactNB(resolution_key, "baseconfig")\r
48         log.info("configuration: $payload")\r
49 \r
50         val payloadObject = JacksonUtils.jsonNode(payload) as ObjectNode\r
51         val vdns_ip: String = payloadObject.get("vdns-instance")[0].get("ip-addr").asText()\r
52 \r
53         val blueprintContext = bluePrintRuntimeService.bluePrintContext()\r
54         val requirement = blueprintContext.nodeTemplateRequirement(nodeTemplateName, "netconf-connection")\r
55         val capabilityProperties = bluePrintRuntimeService.resolveNodeTemplateCapabilityProperties(requirement.node!!, requirement.capability!!)\r
56         val netconfDeviceInfo = JacksonUtils.getInstanceFromMap(capabilityProperties, NetconfDeviceInfo::class.java)\r
57         log.info("Waiting for 2 minutes until vLB intializes ...")\r
58         Thread.sleep(120000)\r
59         val uri =\r
60             "http://${netconfDeviceInfo.ipAddress}:8183/restconf/config/vlb-business-vnf-onap-plugin:vlb-business-vnf-onap-plugin/vdns-instances/vdns-instance/$vdns_ip"\r
61         val restTemplate = RestTemplate()\r
62         val mapOfHeaders = hashMapOf<String, String>()\r
63         mapOfHeaders.put("Accept", "application/json")\r
64         mapOfHeaders.put("Content-Type", "application/json")\r
65         mapOfHeaders.put("cache-control", " no-cache")\r
66         mapOfHeaders.put("Accept", "application/json")\r
67         val basicAuthRestClientProperties: BasicAuthRestClientProperties = BasicAuthRestClientProperties()\r
68         basicAuthRestClientProperties.username = "admin"\r
69         basicAuthRestClientProperties.password = "admin"\r
70         basicAuthRestClientProperties.url = uri\r
71         basicAuthRestClientProperties.additionalHeaders = mapOfHeaders\r
72         val basicAuthRestClientService: BasicAuthRestClientService = BasicAuthRestClientService(basicAuthRestClientProperties)\r
73         try {\r
74             val result: BlueprintWebClientService.WebClientResponse<String> =\r
75                 basicAuthRestClientService.exchangeResource(HttpMethod.PUT.name, "", payload)\r
76             print(result)\r
77             basicAuthRestClientProperties.url =\r
78                 "http://${netconfDeviceInfo.ipAddress}:8183/restconf/config/vlb-business-vnf-onap-plugin:vlb-business-vnf-onap-plugin/vdns-instances"\r
79             val resultOfGet: BlueprintWebClientService.WebClientResponse<String> =\r
80                 basicAuthRestClientService.exchangeResource(HttpMethod.GET.name, "", "")\r
81             print(resultOfGet)\r
82         } catch (e: Exception) {\r
83             log.info("Caught exception trying to connect to vLB!!")\r
84             throw BluePrintProcessorException("${e.message}")\r
85         }\r
86     }\r
87 \r
88     override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {\r
89         log.info("Executing Recovery")\r
90         addError("${runtimeException.message}")\r
91     }\r
92 }\r
93 \r
94 class NetconfDeviceInfo {\r
95 \r
96     @get:JsonProperty("login-account")\r
97     var username: String? = null\r
98 \r
99     @get:JsonProperty("login-key")\r
100     var password: String? = null\r
101 \r
102     @get:JsonProperty("target-ip-address")\r
103     var ipAddress: String? = null\r
104 \r
105     @get:JsonProperty("port-number")\r
106     var port: Int = 0\r
107 \r
108     @get:JsonProperty("connection-time-out")\r
109     var connectTimeout: Long = 5\r
110 \r
111     @get:JsonIgnore\r
112     var source: String? = null\r
113 \r
114     @get:JsonIgnore\r
115     var replyTimeout: Int = 5\r
116 \r
117     @get:JsonIgnore\r
118     var idleTimeout: Int = 99999\r
119 \r
120     override fun toString(): String {\r
121         return "$ipAddress:$port"\r
122     }\r
123 \r
124     // TODO: should this be a data class instead? Is anything using the JSON serdes?\r
125     override fun equals(other: Any?): Boolean {\r
126         if (this === other) return true\r
127         if (javaClass != other?.javaClass) return false\r
128         return true\r
129     }\r
130 \r
131     override fun hashCode(): Int {\r
132         return javaClass.hashCode()\r
133     }\r
134 }\r