259670d47ba569f8efb760e0d90de842af05dba4
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / restful-executor / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / restful / executor / nrmfunction / RestfulNRMServiceClient.kt
1 /*
2  *  Copyright © 2019 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
17 package org.onap.ccsdk.cds.blueprintsprocessor.functions.restful.executor.nrmfunction
18
19 import com.fasterxml.jackson.databind.JsonNode
20 import com.fasterxml.jackson.databind.ObjectMapper
21 import com.fasterxml.jackson.databind.node.ObjectNode
22 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService
23 import org.onap.ccsdk.cds.controllerblueprints.core.logger
24 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
25 import org.springframework.http.HttpMethod
26 import org.springframework.web.util.UriComponentsBuilder
27 import java.util.UUID
28
29 class RestfulNRMServiceClient() {
30
31     private val log = logger(RestfulNRMServiceClient::class.java)
32
33     fun createMOI(web_client_service: BlueprintWebClientService, idStr: String, managed_object_instance: JsonNode): ObjectNode {
34         val classNameStr = managed_object_instance.get("className").toString().replace("\"", "")
35         val pathStr = "/ProvisioningMnS/v1500/$classNameStr/$idStr"
36         log.info("MOI Path: " + pathStr)
37         var request_object_value = JacksonUtils.jsonNode("{}") as ObjectNode
38         request_object_value.put("attributes", managed_object_instance.get("data"))
39         request_object_value.put("href", "/" + classNameStr + "/" + idStr)
40         request_object_value.put("class", classNameStr)
41         request_object_value.put("id", idStr)
42         var request_object = JacksonUtils.jsonNode("{}") as ObjectNode
43         request_object.put("data", request_object_value)
44         val requestBodystr = request_object.toString()
45         log.info("MOI request body: " + requestBodystr)
46         val response = web_client_service.exchangeResource(HttpMethod.PUT.name, pathStr, requestBodystr)
47         var response_object = generateResponse(response.status, response.body)
48         log.info("MOI response status: " + response.status)
49         return response_object
50     }
51
52     fun getMOIAttributes(web_client_service: BlueprintWebClientService, idStr: String, managed_object_instance: JsonNode): ObjectNode {
53         val classNameStr = managed_object_instance.get("className").toString().replace("\"", "")
54         var pathStr = "/ProvisioningMnS/v1500/$classNameStr/$idStr"
55         pathStr = addQueryParameters(pathStr, "scope", managed_object_instance.get("scope").toString().replace("\"", ""))
56         pathStr = addQueryParameters(pathStr, "filter", managed_object_instance.get("filter").toString().replace("\"", ""))
57         for (attribute_value in managed_object_instance.get("fields")) {
58             pathStr = addQueryParameters(pathStr, "fields", attribute_value.toString().replace("\"", ""))
59         }
60         log.info("MOI Path: " + pathStr)
61         val response = web_client_service.exchangeResource(HttpMethod.GET.name, pathStr, "")
62         log.info("MOI response status: " + response.status)
63         var response_object = generateResponse(response.status, response.body)
64         return response_object
65     }
66
67     fun modifyMOIAttributes(web_client_service: BlueprintWebClientService, idStr: String, managed_object_instance: JsonNode): ObjectNode {
68         val classNameStr = managed_object_instance.get("className").toString().replace("\"", "")
69         var pathStr = "/ProvisioningMnS/v1500/$classNameStr/$idStr"
70         pathStr = addQueryParameters(pathStr, "scope", managed_object_instance.get("scope").toString().replace("\"", ""))
71         pathStr = addQueryParameters(pathStr, "filter", managed_object_instance.get("filter").toString().replace("\"", ""))
72         log.info("MOI Path: " + pathStr)
73         var request_object = JacksonUtils.jsonNode("{}") as ObjectNode
74         request_object.put("data", managed_object_instance.get("data"))
75         val requestBodystr = request_object.toString()
76         log.info("MOI request body: " + requestBodystr)
77         val response = web_client_service.exchangeResource(HttpMethod.PATCH.name, pathStr, requestBodystr)
78         log.info("MOI response status: " + response.status)
79         var response_object = generateResponse(response.status, response.body)
80         return response_object
81     }
82
83     fun deleteMOI(web_client_service: BlueprintWebClientService, idStr: String, managed_object_instance: JsonNode): ObjectNode {
84         val classNameStr = managed_object_instance.get("className").toString().replace("\"", "")
85         var pathStr = "/ProvisioningMnS/v1500/$classNameStr/$idStr"
86         pathStr = addQueryParameters(pathStr, "scope", managed_object_instance.get("scope").toString().replace("\"", ""))
87         pathStr = addQueryParameters(pathStr, "filter", managed_object_instance.get("filter").toString().replace("\"", ""))
88         log.info("MOI Path: " + pathStr)
89         val response = web_client_service.exchangeResource(HttpMethod.DELETE.name, pathStr, "")
90         log.info("MOI response status: " + response.status)
91         var response_object = generateResponse(response.status, response.body)
92         return response_object
93     }
94
95     fun generateResponse(status: Int, body: String): ObjectNode {
96         var response_object = JacksonUtils.jsonNode("{}") as ObjectNode
97         response_object.put("status", status)
98         val mapper = ObjectMapper()
99         val response_body: JsonNode = mapper.readTree(body)
100         response_object.put("body", response_body)
101         return response_object
102     }
103
104     fun generateMOIid(): String {
105         return UUID.randomUUID().toString()
106     }
107
108     fun addQueryParameters(old_uri: String, key: String, value: String): String {
109         return UriComponentsBuilder.fromUriString(old_uri).queryParam(key, value).build().toString()
110     }
111 }