+
+
+def get_ietf_network_link_request(network: str, link: str, content: str):
+ url = {'rfc8040': '{}/data/ietf-network:networks/network={}/ietf-network-topology:link={}?content={}',
+ 'draft-bierman02': '{}/{}/ietf-network:networks/network/{}/ietf-network-topology:link/{}'}
+ if RESTCONF_VERSION == 'rfc8040':
+ format_args = ('{}', network, link, content)
+ elif content == 'config':
+ format_args = ('{}', content, network, link)
+ else:
+ format_args = ('{}', 'operational', network, link)
+ response = get_request(url[RESTCONF_VERSION].format(*format_args))
+ res = response.json()
+ return_key = {'rfc8040': 'ietf-network-topology:link',
+ 'draft-bierman02': 'ietf-network-topology:link'}
+ link = res[return_key[RESTCONF_VERSION]][0]
+ return {'status_code': response.status_code,
+ 'link': link}
+
+
+def del_ietf_network_link_request(network: str, link: str, content: str):
+ url = {'rfc8040': '{}/data/ietf-network:networks/network={}/ietf-network-topology:link={}?content={}',
+ 'draft-bierman02': '{}/{}/ietf-network:networks/network/{}/ietf-network-topology:link/{}'}
+ if RESTCONF_VERSION == 'rfc8040':
+ format_args = ('{}', network, link, content)
+ elif content == 'config':
+ format_args = ('{}', content, network, link)
+ else:
+ format_args = ('{}', 'operational', network, link)
+ response = delete_request(url[RESTCONF_VERSION].format(*format_args))
+ return response
+
+
+def add_oms_attr_request(link: str, oms_attr: str):
+ url = {'rfc8040': '{}/data/ietf-network:networks/network={}/ietf-network-topology:link={}',
+ 'draft-bierman02': '{}/config/ietf-network:networks/network/{}/ietf-network-topology:link/{}'}
+ url2 = url[RESTCONF_VERSION] + '/org-openroadm-network-topology:OMS-attributes/span'
+ network = 'openroadm-topology'
+ response = put_request(url2.format('{}', network, link), oms_attr)
+ return response
+
+
+def del_oms_attr_request(link: str,):
+ url = {'rfc8040': '{}/data/ietf-network:networks/network={}/ietf-network-topology:link={}',
+ 'draft-bierman02': '{}/config/ietf-network:networks/network/{}/ietf-network-topology:link/{}'}
+ url2 = url[RESTCONF_VERSION] + '/org-openroadm-network-topology:OMS-attributes/span'
+ network = 'openroadm-topology'
+ response = delete_request(url2.format('{}', network, link))
+ return response
+
+
+def del_ietf_network_node_request(network: str, node: str, content: str):
+ url = {'rfc8040': '{}/data/ietf-network:networks/network={}/node={}?content={}',
+ 'draft-bierman02': '{}/{}/ietf-network:networks/network/{}/node/{}'}
+ if RESTCONF_VERSION == 'rfc8040':
+ format_args = ('{}', network, node, content)
+ elif content == 'config':
+ format_args = ('{}', content, network, node)
+ else:
+ format_args = ('{}', 'operational', network, node)
+ response = delete_request(url[RESTCONF_VERSION].format(*format_args))
+ return response
+
+#
+# TransportPCE internal API RPCs
+#
+
+
+def prepend_dict_keys(input_dict: dict, prefix: str):
+ return_dict = {}
+ for key, value in input_dict.items():
+ newkey = prefix + key
+ if isinstance(value, dict):
+ return_dict[newkey] = prepend_dict_keys(value, prefix)
+ # TODO: perhaps some recursion depth limit or another solution has to be considered here
+ # even if recursion depth is given by the input_dict argument
+ # direct (self-)recursive functions may carry unwanted side-effects such as ressource consumptions
+ else:
+ return_dict[newkey] = value
+ return return_dict
+
+
+def connect_xpdr_to_rdm_request(payload: dict):
+ url = "{}/operations/transportpce-networkutils:init-xpdr-rdm-links"
+ if RESTCONF_VERSION == 'draft-bierman02':
+ data = prepend_dict_keys({'input': {'links-input': payload}}, 'networkutils:')
+ else:
+ data = {'input': {'links-input': payload}}
+ return post_request(url, data)
+
+
+def connect_rdm_to_xpdr_request(payload: dict):
+ url = "{}/operations/transportpce-networkutils:init-rdm-xpdr-links"
+ if RESTCONF_VERSION == 'draft-bierman02':
+ data = prepend_dict_keys({'input': {'links-input': payload}}, 'networkutils:')
+ else:
+ data = {'input': {'links-input': payload}}
+ return post_request(url, data)
+
+
+def device_renderer_create_ots_oms_request(nodeid: str, lcp: str):
+ url = "{}/operations/transportpce-device-renderer:create-ots-oms"
+ payload = {
+ 'node-id': nodeid,
+ 'logical-connection-point': lcp}
+ if RESTCONF_VERSION == 'draft-bierman02':
+ data = prepend_dict_keys({'input': payload}, 'transportpce-device-renderer:')
+ else:
+ data = {'input': payload}
+ return post_request(url, data)
+
+
+def transportpce_api_rpc_request(api_module: str, rpc: str, payload: dict):
+ # pylint: disable=consider-using-f-string
+ url = "{}/operations/{}:{}".format('{}', api_module, rpc)
+ if payload is None:
+ data = None
+ elif RESTCONF_VERSION == 'draft-bierman02':
+ data = prepend_dict_keys({'input': payload}, api_module + ':')
+ else:
+ data = {'input': payload}
+ response = post_request(url, data)
+ res = response.json()
+ return_key = {'rfc8040': api_module + ':output',
+ 'draft-bierman02': 'output'}
+ return_output = res[return_key[RESTCONF_VERSION]]
+ return {'status_code': response.status_code,
+ 'output': return_output}