fe660f590f3c721a18c0df196e54b5f13944e19b
[ccsdk/cds.git] / components / model-catalog / blueprint-model / service-blueprint / vLB / Scripts / kotlin / ConfigDeploy.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.RestClientProperties\r
24 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BasicAuthRestClientService\r
25 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService\r
26 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractScriptComponentFunction\r
27 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils\r
28 import org.slf4j.LoggerFactory\r
29 import org.springframework.http.HttpMethod\r
30 import org.springframework.web.client.RestTemplate\r
31 import com.fasterxml.jackson.annotation.JsonIgnore\r
32 import com.fasterxml.jackson.annotation.JsonProperty\r
33 import org.apache.http.client.ClientProtocolException\r
34 import java.io.IOException\r
35 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException\r
36 \r
37 open class ConfigDeploy : AbstractScriptComponentFunction() {\r
38 \r
39     private val log = LoggerFactory.getLogger(ConfigDeploy::class.java)!!\r
40 \r
41     override fun getName(): String {\r
42         return "Check"\r
43     }\r
44 \r
45     override suspend fun processNB(executionRequest: ExecutionServiceInput) {\r
46         log.info("executing script")\r
47         val resolution_key = getDynamicProperties("resolution-key").asText()\r
48         log.info("resolution_key: $resolution_key")\r
49 \r
50         val payload = storedContentFromResolvedArtifactNB(resolution_key, "baseconfig")\r
51         log.info("configuration: $payload")\r
52 \r
53         val payloadObject = JacksonUtils.jsonNode(payload) as ObjectNode\r
54         val vdns_ip: String = payloadObject.get("vdns-instance")[0].get("ip-addr").asText()\r
55 \r
56 \r
57         val blueprintContext = bluePrintRuntimeService.bluePrintContext()\r
58         val requirement = blueprintContext.nodeTemplateRequirement(nodeTemplateName, "restconf-connection")\r
59         val capabilityProperties = bluePrintRuntimeService.resolveNodeTemplateCapabilityProperties(requirement.node!!, requirement.capability!!)\r
60         val netconfDeviceInfo = JacksonUtils.getInstanceFromMap(capabilityProperties, NetconfDeviceInfo::class.java)\r
61         log.info("Waiting for 2 minutes until vLB intializes ...")\r
62         //Thread.sleep(120000)\r
63         val uri = "http://${netconfDeviceInfo.ipAddress}:8183/restconf/config/vlb-business-vnf-onap-plugin:vlb-business-vnf-onap-plugin/vdns-instances/vdns-instance/$vdns_ip"\r
64         val restTemplate = RestTemplate()\r
65         val mapOfHeaders = hashMapOf<String, String>()\r
66         mapOfHeaders.put("Accept", "application/json")\r
67         mapOfHeaders.put("Content-Type", "application/json")\r
68         mapOfHeaders.put("cache-control", " no-cache")\r
69         mapOfHeaders.put("Accept", "application/json")\r
70         val basicAuthRestClientProperties: BasicAuthRestClientProperties = BasicAuthRestClientProperties()\r
71         basicAuthRestClientProperties.username = "admin"\r
72         basicAuthRestClientProperties.password = "admin"\r
73         basicAuthRestClientProperties.url = uri\r
74         basicAuthRestClientProperties.additionalHeaders =mapOfHeaders\r
75         val basicAuthRestClientService: BasicAuthRestClientService= BasicAuthRestClientService(basicAuthRestClientProperties)\r
76         try {\r
77             val result: BlueprintWebClientService.WebClientResponse<String> = basicAuthRestClientService.exchangeResource(HttpMethod.PUT.name, "", payload)\r
78             print(result)\r
79             basicAuthRestClientProperties.url = "http://${netconfDeviceInfo.ipAddress}:8183/restconf/config/vlb-business-vnf-onap-plugin:vlb-business-vnf-onap-plugin/vdns-instances"\r
80             val resultOfGet: BlueprintWebClientService.WebClientResponse<String> = basicAuthRestClientService.exchangeResource(HttpMethod.GET.name, "", "")\r
81             print(resultOfGet)\r
82         }\r
83         catch (e: Exception) {\r
84             log.info("Caught exception trying to connect to vLB!!")\r
85             throw BluePrintProcessorException("${e.message}")\r
86         }\r
87     }\r
88 \r
89     override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {\r
90         log.info("Executing Recovery")\r
91         bluePrintRuntimeService.getBluePrintError().addError("${runtimeException.message}")\r
92     }\r
93 }\r
94 \r
95 class NetconfDeviceInfo {\r
96     @get:JsonProperty("login-account")\r
97     var username: String? = null\r
98     @get:JsonProperty("login-key")\r
99     var password: String? = null\r
100     @get:JsonProperty("target-ip-address")\r
101     var ipAddress: String? = null\r
102     @get:JsonProperty("port-number")\r
103     var port: Int = 0\r
104     @get:JsonProperty("connection-time-out")\r
105     var connectTimeout: Long = 5\r
106     @get:JsonIgnore\r
107     var source: String? = null\r
108     @get:JsonIgnore\r
109     var replyTimeout: Int = 5\r
110     @get:JsonIgnore\r
111     var idleTimeout: Int = 99999\r
112 \r
113     override fun toString(): String {\r
114         return "$ipAddress:$port"\r
115     }\r
116     //TODO: should this be a data class instead? Is anything using the JSON serdes?\r
117     override fun equals(other: Any?): Boolean {\r
118         if (this === other) return true\r
119         if (javaClass != other?.javaClass) return false\r
120         return true\r
121     }\r
122 \r
123     override fun hashCode(): Int {\r
124         return javaClass.hashCode()\r
125     }\r
126 }