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.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
37 open class ConfigDeploy : AbstractScriptComponentFunction() {
\r
39 private val log = LoggerFactory.getLogger(ConfigDeploy::class.java)!!
\r
41 override fun getName(): String {
\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
50 val payload = storedContentFromResolvedArtifactNB(resolution_key, "baseconfig")
\r
51 log.info("configuration: $payload")
\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
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
77 val result: BlueprintWebClientService.WebClientResponse<String> = basicAuthRestClientService.exchangeResource(HttpMethod.PUT.name, "", payload)
\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
83 catch (e: Exception) {
\r
84 log.info("Caught exception trying to connect to vLB!!")
\r
85 throw BluePrintProcessorException("${e.message}")
\r
89 override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
\r
90 log.info("Executing Recovery")
\r
91 bluePrintRuntimeService.getBluePrintError().addError("${runtimeException.message}")
\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
104 @get:JsonProperty("connection-time-out")
\r
105 var connectTimeout: Long = 5
\r
107 var source: String? = null
\r
109 var replyTimeout: Int = 5
\r
111 var idleTimeout: Int = 99999
\r
113 override fun toString(): String {
\r
114 return "$ipAddress:$port"
\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
123 override fun hashCode(): Int {
\r
124 return javaClass.hashCode()
\r