2 * Copyright © 2019 IBM, Bell Canada, Orange
\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
8 * http://www.apache.org/licenses/LICENSE-2.0
\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
17 package org.onap.ccsdk.cds.blueprintsprocessor.services.execution.scripts
\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
34 open class ConfigDeploy : AbstractScriptComponentFunction() {
\r
36 private val log = LoggerFactory.getLogger(ConfigDeploy::class.java)!!
\r
38 override fun getName(): String {
\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
47 val payload = storedContentFromResolvedArtifactNB(resolution_key, "baseconfig")
\r
48 log.info("configuration: $payload")
\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
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
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
74 val result: BlueprintWebClientService.WebClientResponse<String> =
\r
75 basicAuthRestClientService.exchangeResource(HttpMethod.PUT.name, "", payload)
\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
82 } catch (e: Exception) {
\r
83 log.info("Caught exception trying to connect to vLB!!")
\r
84 throw BlueprintProcessorException("${e.message}")
\r
88 override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
\r
89 log.info("Executing Recovery")
\r
90 bluePrintRuntimeService.getBlueprintError().addError("${runtimeException.message}")
\r
94 class NetconfDeviceInfo {
\r
96 @get:JsonProperty("login-account")
\r
97 var username: String? = null
\r
99 @get:JsonProperty("login-key")
\r
100 var password: String? = null
\r
102 @get:JsonProperty("target-ip-address")
\r
103 var ipAddress: String? = null
\r
105 @get:JsonProperty("port-number")
\r
108 @get:JsonProperty("connection-time-out")
\r
109 var connectTimeout: Long = 5
\r
112 var source: String? = null
\r
115 var replyTimeout: Int = 5
\r
118 var idleTimeout: Int = 99999
\r
120 override fun toString(): String {
\r
121 return "$ipAddress:$port"
\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
131 override fun hashCode(): Int {
\r
132 return javaClass.hashCode()
\r