Revert "Renaming Files having BluePrint to have Blueprint"
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / restful-executor / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / restful / executor / RestfulCMComponentFunction.kt
1 /*
2  *  Copyright © 2020 Huawei.
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 package org.onap.ccsdk.cds.blueprintsprocessor.functions.restful.executor
17
18 import com.fasterxml.jackson.databind.JsonNode
19 import com.fasterxml.jackson.databind.ObjectMapper
20 import com.fasterxml.jackson.databind.node.ArrayNode
21 import com.fasterxml.jackson.databind.node.ObjectNode
22 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
23 import org.onap.ccsdk.cds.blueprintsprocessor.functions.restful.executor.nrmfunction.RestfulNRMServiceClient
24 import org.onap.ccsdk.cds.blueprintsprocessor.rest.RestLibConstants
25 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BluePrintRestLibPropertyService
26 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService
27 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractScriptComponentFunction
28 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
29 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
30 import org.slf4j.LoggerFactory
31
32 abstract class RestfulCMComponentFunction : AbstractScriptComponentFunction() {
33
34     private val log = LoggerFactory.getLogger(RestfulCMComponentFunction::class.java)
35     override suspend fun processNB(executionRequest: ExecutionServiceInput) {
36         throw BluePrintException("Not Implemented required")
37     }
38
39     override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
40         throw BluePrintException("Not Implemented required")
41     }
42
43     open fun bluePrintRestLibPropertyService(): BluePrintRestLibPropertyService =
44         functionDependencyInstanceAsType(RestLibConstants.SERVICE_BLUEPRINT_REST_LIB_PROPERTY)
45
46     fun restClientService(clientInfo: JsonNode): BlueprintWebClientService {
47         return bluePrintRestLibPropertyService().blueprintWebClientService(clientInfo)
48     }
49
50     fun processNRM(executionRequest: ExecutionServiceInput, input_params: ArrayNode): String {
51         // process the managed object instances
52         log.info("Processing NRM Object")
53         operationInputs = executionServiceInput.stepData!!.properties
54         val dynamic_properties = operationInputs.get("dynamic-properties")
55         // instantiate one restClientService instance
56         val hostname = dynamic_properties?.get("hostname").toString().replace("\"", "")
57         val port = dynamic_properties?.get("port").toString().replace("\"", "")
58         val username = dynamic_properties?.get("username").toString().replace("\"", "")
59         val password = dynamic_properties?.get("password").toString().replace("\"", "")
60         val url = "http://" + hostname + ":" + port
61         val RestInfo: String = "{\n" +
62             " \"type\" : \"basic-auth\",\n" +
63             " \"url\" : \"" + url + "\",\n" +
64             " \"username\" : \"" + username + "\",\n" +
65             " \"password\" : \"" + password + "\"\n" +
66             "}"
67         val mapper = ObjectMapper()
68         val jsonRestInfo: JsonNode = mapper.readTree(RestInfo)
69         val web_client_service = restClientService(jsonRestInfo)
70         val managed_object_instances = input_params
71         var response = JacksonUtils.jsonNode("{}") as ObjectNode
72         // Invoke the corresponding function according to the workflowname
73         when (this.workflowName) {
74             "config-deploy" -> {
75                 for (managed_object_instance in managed_object_instances) {
76                     // invoke createMOI for each managed-object-instance
77                     log.info("invoke createMOI for each managed-object-instance")
78                     var NRM_Restful_client = RestfulNRMServiceClient()
79                     val MOI_id = NRM_Restful_client.generateMOIid()
80                     var httpresponse = NRM_Restful_client.createMOI(web_client_service, MOI_id, managed_object_instance)
81                     var MOIname = managed_object_instance.get("className").toString().replace("\"", "")
82                     response.put("/$MOIname/$MOI_id", httpresponse)
83                 }
84             }
85             "config-get" -> {
86                 for (managed_object_instance in managed_object_instances) {
87                     // invoke getMOIAttributes for each managed-object-instance
88                     log.info("invoke getMOIAttributes for each managed-object-instance")
89                     var NRM_Restful_client = RestfulNRMServiceClient()
90                     val MOI_id = managed_object_instance.get("id").toString().replace("\"", "")
91                     var httpresponse = NRM_Restful_client.getMOIAttributes(web_client_service, MOI_id, managed_object_instance)
92                     var MOIname = managed_object_instance.get("className").toString().replace("\"", "")
93                     response.put("/$MOIname/$MOI_id", httpresponse)
94                 }
95             }
96             "config-modify" -> {
97                 for (managed_object_instance in managed_object_instances) {
98                     // invoke modifyMOIAttributes for each managed-object-instance
99                     log.info("invoke modifyMOIAttributes for each managed-object-instance")
100                     var NRM_Restful_client = RestfulNRMServiceClient()
101                     val MOI_id = managed_object_instance.get("id").toString().replace("\"", "")
102                     var httpresponse = NRM_Restful_client.modifyMOIAttributes(web_client_service, MOI_id, managed_object_instance)
103                     var MOIname = managed_object_instance.get("className").toString().replace("\"", "")
104                     response.put("/$MOIname/$MOI_id", httpresponse)
105                 }
106             }
107             "config-delete" -> {
108                 for (managed_object_instance in managed_object_instances) {
109                     // invoke deleteMOI for each managed-object-instance
110                     log.info("invoke deleteMOI for each managed-object-instance")
111                     var NRM_Restful_client = RestfulNRMServiceClient()
112                     val MOI_id = managed_object_instance.get("id").toString().replace("\"", "")
113                     var httpresponse = NRM_Restful_client.deleteMOI(web_client_service, MOI_id, managed_object_instance)
114                     var MOIname = managed_object_instance.get("className").toString().replace("\"", "")
115                     response.put("/$MOIname/$MOI_id", httpresponse)
116                 }
117             }
118             else -> {
119                 print("not Implemented")
120                 response.put(this.workflowName, "Not Implemented")
121             }
122         }
123         val strresponse = "$response"
124         return strresponse
125     }
126 }