From 3ac0d20b08dd8c31ff85ad3ca5c53df11b6d96e4 Mon Sep 17 00:00:00 2001 From: ottero Date: Sun, 17 Mar 2019 19:38:32 +0000 Subject: Adding custom headers capability to REST client For YANG PATCH requests to ODL to work, they need to have a Content- type header of application/yang.patch+json and should not have Accept as application/json Current REST client inserts a default header to the requests with this content: Content-Type: application/json Accept: application/json The solution was to add the possibility of sending custom headers alon- gside the other parameters. Change-Id: I2cf0cd2ef7b87f4f5a246d427dffafe266cb33f7 Issue-ID: CCSDK-926 Signed-off-by: ottero --- .../Scripts/python/RestconfAssignConfig.py | 38 ------- .../Scripts/python/RestconfConfigDeploy.py | 112 +++++++++++++++++++++ .../Scripts/python/RestconfConfigure.py | 38 ------- 3 files changed, 112 insertions(+), 76 deletions(-) delete mode 100644 components/model-catalog/blueprint-model/test-blueprint/capability_restconf/Scripts/python/RestconfAssignConfig.py create mode 100644 components/model-catalog/blueprint-model/test-blueprint/capability_restconf/Scripts/python/RestconfConfigDeploy.py delete mode 100644 components/model-catalog/blueprint-model/test-blueprint/capability_restconf/Scripts/python/RestconfConfigure.py (limited to 'components/model-catalog/blueprint-model/test-blueprint/capability_restconf/Scripts') diff --git a/components/model-catalog/blueprint-model/test-blueprint/capability_restconf/Scripts/python/RestconfAssignConfig.py b/components/model-catalog/blueprint-model/test-blueprint/capability_restconf/Scripts/python/RestconfAssignConfig.py deleted file mode 100644 index 36dd32ffc..000000000 --- a/components/model-catalog/blueprint-model/test-blueprint/capability_restconf/Scripts/python/RestconfAssignConfig.py +++ /dev/null @@ -1,38 +0,0 @@ -# ============LICENSE_START======================================================= -# Copyright (C) 2019 Nordix Foundation. -# ================================================================================ -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -# SPDX-License-Identifier: Apache-2.0 -# ============LICENSE_END========================================================= - - -from org.onap.ccsdk.apps.blueprintsprocessor.functions.restconf.executor import \ - RestconfComponentFunction - - -class RestconfAssignConfig(RestconfComponentFunction): - - - def process(self, execution_request): - # create instances of the needed objects - # retrieve any needed information not present on the request, like pnf ip - # create the configlet - # persist the configlet - # end - print("process", execution_request) - - - def recover(self, runtime_exception, execution_request): - print("recover") - return None \ No newline at end of file diff --git a/components/model-catalog/blueprint-model/test-blueprint/capability_restconf/Scripts/python/RestconfConfigDeploy.py b/components/model-catalog/blueprint-model/test-blueprint/capability_restconf/Scripts/python/RestconfConfigDeploy.py new file mode 100644 index 000000000..33f9400eb --- /dev/null +++ b/components/model-catalog/blueprint-model/test-blueprint/capability_restconf/Scripts/python/RestconfConfigDeploy.py @@ -0,0 +1,112 @@ +# ============LICENSE_START======================================================= +# Copyright (C) 2019 Nordix Foundation. +# ================================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# ============LICENSE_END========================================================= +from time import sleep + +from org.onap.ccsdk.apps.blueprintsprocessor.functions.restconf.executor import \ + RestconfComponentFunction +from java.lang import Exception as JavaException + + +class RestconfConfigDeploy(RestconfComponentFunction): + + log = globals()["log"] + seconds_to_sleep = 5 + base_mount_url = "restconf/config/network-topology:network-topology/topology/topology-netconf/node/" + server_identifier = "sdncodl" + configlet_template_name = "config-assign" + + def process(self, execution_request): + + self.log.info("Started execution of process method") + try: + self.log.info("getting resolution-key") + resolution_key = self.getDynamicProperties("resolution-key").asText() + self.log.info("resolution_key: {}", resolution_key) + + self.log.info("getting pnf-id") + pnf_id = execution_request.payload.get("config-deploy-request").get("config-deploy-properties").get("entity").get("pnf-id") + pnf_id = str(pnf_id).strip('\"') + self.log.info("pnf-id: {}", pnf_id) + + self.log.info("mounting device {}", pnf_id) + self.mount(pnf_id) + + self.log.info("sleeping for {} seconds", self.seconds_to_sleep) + sleep(self.seconds_to_sleep) + + try: + self.log.info("configuring device {}", pnf_id) + self.apply_configuration(pnf_id, resolution_key, self.configlet_template_name) + except Exception, err: + self.log.error("an error occurred while configuring device {}", err) + raise err + finally: + self.log.info("unmounting device {}", pnf_id) + self.unmount(pnf_id) + + self.log.info("Ended execution of process method") + + except JavaException, err: + self.log.error("Java Exception in the script", err) + raise err + except Exception, err: + self.log.error("Python Exception in the script", err) + raise err + + def mount(self, pnf_id): + self.log.info("meshing mount payload") + mount_payload = self.resolveAndGenerateMessage("config-deploy-mapping", "config-deploy-template") + self.log.info("mount payload: \n {}", mount_payload) + + # defining custom header + headers = { + "Content-Type": "application/xml" + } + + url = self.base_mount_url + str(pnf_id) + self.log.info("sending mount request, url: {}", url) + web_client_service = self.restClientService(self.server_identifier) + web_client_service.exchangeResource("PUT", url, mount_payload, headers) + + def unmount(self, pnf_id): + url = self.base_mount_url + str(pnf_id) + self.log.info("sending unmount request, url: {}", url) + web_client_service = self.restClientService(self.server_identifier) + web_client_service.exchangeResource("DELETE", url, "") + + def apply_configuration(self, pnf_id, resolution_key, template_name): + self.log.info("Retrieving configlet from database (resolution-key: {}, template_name: {}", + resolution_key, template_name) + configlet = self.resolveFromDatabase(resolution_key, template_name) + self.log.info("Configlet: {}", configlet) + + # defining custom header + headers = { + "Content-Type": "application/yang.patch+json" + } + + url = "restconf/config/network-topology:network-topology/topology/topology-netconf/node/" + pnf_id \ + + "/yang-ext:mount/mynetconf:netconflist" + self.log.info("sending patch request, url: {}", url) + web_client_service = self.restClientService(self.server_identifier) + result = web_client_service.exchangeResource("PATCH", url, configlet, headers) + self.log.info("Configuration application result: {}", result) + + def recover(self, runtime_exception, execution_request): + self.log.info("Recover method, no code to execute") + return None diff --git a/components/model-catalog/blueprint-model/test-blueprint/capability_restconf/Scripts/python/RestconfConfigure.py b/components/model-catalog/blueprint-model/test-blueprint/capability_restconf/Scripts/python/RestconfConfigure.py deleted file mode 100644 index c584baa9b..000000000 --- a/components/model-catalog/blueprint-model/test-blueprint/capability_restconf/Scripts/python/RestconfConfigure.py +++ /dev/null @@ -1,38 +0,0 @@ -# ============LICENSE_START======================================================= -# Copyright (C) 2019 Nordix Foundation. -# ================================================================================ -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -# SPDX-License-Identifier: Apache-2.0 -# ============LICENSE_END========================================================= - - -from org.onap.ccsdk.apps.blueprintsprocessor.functions.restconf.executor import \ - RestconfComponentFunction - - -class RestconfConfigure(RestconfComponentFunction): - - - def process(self, execution_request): - # create instances of the needed objects - # retrieve any needed information not present on the request, like pnf ip - # retrieve the configlet - # send the configlet - # end - print("process", execution_request) - - - def recover(self, runtime_exception, execution_request): - print("recover") - return None -- cgit 1.2.3-korg