Simplify PNF-DEMO CBA
[ccsdk/cds.git] / components / model-catalog / blueprint-model / service-blueprint / PNF_DEMO / Scripts / kotlin / ConfigDeploy.kt
1 /*
2  * Copyright © 2019 IBM, Bell Canada, AT&T, Orange
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.ccsdk.cds.blueprintsprocessor.services.execution.scripts
18
19 import com.fasterxml.jackson.databind.node.ObjectNode
20 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
21 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.storedContentFromResolvedArtifactNB
22 import org.onap.ccsdk.cds.blueprintsprocessor.rest.BasicAuthRestClientProperties
23 import org.onap.ccsdk.cds.blueprintsprocessor.rest.RestClientProperties
24 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BasicAuthRestClientService
25 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService
26 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractScriptComponentFunction
27 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
28 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType
29 import org.slf4j.LoggerFactory
30 import org.springframework.http.HttpMethod
31 import org.springframework.web.client.RestTemplate
32 import org.onap.ccsdk.cds.blueprintsprocessor.functions.netconf.executor.netconfClientService
33 import org.onap.ccsdk.cds.blueprintsprocessor.functions.netconf.executor.netconfDevice
34 import org.onap.ccsdk.cds.blueprintsprocessor.functions.netconf.executor.netconfDeviceInfo
35
36 open class ConfigDeploy : AbstractScriptComponentFunction() {
37
38     private val log = LoggerFactory.getLogger(ConfigDeploy::class.java)!!
39
40     override suspend fun processNB(executionRequest: ExecutionServiceInput) {
41
42         /*
43          * Here resolution-key could be day-1, day-2 etc.. 
44          * This will come as part of config-deploy request payload.
45          */
46
47         val resolution_key = getDynamicProperties("resolution-key").asText()
48         log.info("Got the resolution_key: $resolution_key from config-deploy going to retrive the data from DB")
49
50         // Read the config-assing data using the resolution key + Prefix name for the template
51         // We can select the given configuration using the resolution_key=day-1 or day-2
52         // With the template prefix name for example "netconfrpc" to load the
53         // Store configuration from the CDS DB
54         val payload = storedContentFromResolvedArtifactNB(resolution_key, "netconfrpc")
55         log.info("PNF configuration data from DB : \n$payload\n")
56
57         // "netconf-connection" is the tosca Node reference to "execute"
58         // workflow.
59         val netconf_device = netconfDevice("netconf-connection")
60         val netconf_rpc_client = netconf_device.netconfRpcService
61         val netconf_session = netconf_device.netconfSession
62         netconf_session.connect()
63
64         /**
65          * Invoke the NETCONF RPC, we already have the teamplate loaded
66          * using "resolution-key" & template name prefix.
67          */
68         val dev_response = netconf_rpc_client.invokeRpc(payload)
69         log.info("NETCONF device response message : dev_response\n")
70         setAttribute("response-data", dev_response.asJsonType())
71
72         /**
73         netconf_rpc_client.lock("candidate")
74         netconf_rpc_client.discardConfig()
75         netconf_rpc_client.editConfig(payload, "candidate", "merge")
76         netconf_rpc_client.commit()
77         netconf_rpc_client.unLock("candidate")
78         netconf_rpc_client.getConfig("", "running")
79         */
80
81         log.info("Closing NETCONF device sessing with the device\n")
82         netconf_session.disconnect()
83
84     }
85
86     override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
87         log.info("Executing Recovery")
88     }
89 }