+ data = templ.substitute(
+ {'group': group, 'filter_type': filter_type, 'ip': ip, 'entries': entries, 'filter_policy': policy})
+ return data
+
+
+def add_domain_filter_xml(domain, domains, entries, ip, filter_name=None):
+ """Generate xml for Add Domain Filter request
+
+ :param domain: Name of Domain containing filter
+ :type domain: str
+ :param domains: Domains on which filter will be applied
+ :type domains: str
+ :param entries: XML formatted entries that will be added in filter
+ :type entries: str
+ :param ip: Ipv4 address of node
+ :type ip: str
+ :param filter_name: Name of filter
+ :type filter_name: str
+ :returns: String containing xml data for request
+
+ """
+ if filter_name:
+ filter_name = "<filter-name>" + filter_name + "</filter-name>"
+ templ = Template('''<input>
+ <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
+ <domain-name xmlns="urn:opendaylight:sxp:controller">$domain</domain-name>
+ <sxp-domain-filter xmlns="urn:opendaylight:sxp:controller">
+ $filter_name
+ <domains>$domains</domains>
+ $entries
+ </sxp-domain-filter>
+</input>''')
+ data = templ.substitute(
+ {'domain': domain, 'domains': domains, 'ip': ip, 'entries': entries, 'filter_name': filter_name})
+ return data
+
+
+def delete_filter_xml(group, filter_type, ip):
+ """Generate xml for Delete Filter request
+
+ :param group: Name of group containing filter
+ :type group: str
+ :param filter_type: Type of filter
+ :type filter_type: str
+ :param ip: Ipv4 address of node
+ :type ip: str
+ :returns: String containing xml data for request
+
+ """
+ templ = Template('''<input>
+ <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
+ <peer-group-name xmlns="urn:opendaylight:sxp:controller">$group</peer-group-name>
+ <filter-type xmlns="urn:opendaylight:sxp:controller">$filter_type</filter-type>
+</input>''')
+ data = templ.substitute(
+ {'group': group, 'filter_type': filter_type, 'ip': ip})
+ return data
+
+
+def delete_domain_filter_xml(domain, ip, filter_name=None):
+ """Generate xml for Delete Filter request
+
+ :param domain: Name of Domain containing filter
+ :type domain: str
+ :param ip: Ipv4 address of node
+ :type ip: str
+ :param filter_name: Name of filter
+ :type filter_name: str
+ :returns: String containing xml data for request
+
+ """
+ if filter_name:
+ filter_name = '<filter-name xmlns="urn:opendaylight:sxp:controller">' + filter_name + "</filter-name>"
+ templ = Template('''<input>
+ <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
+ <domain-name xmlns="urn:opendaylight:sxp:controller">$domain</domain-name>
+ $filter_name
+</input>''')
+ data = templ.substitute(
+ {'domain': domain, 'ip': ip, 'filter_name': filter_name})
+ return data
+
+
+def get_connections_from_node_xml(ip, domain_name):
+ """Generate xml for Get Connections request
+
+ :param ip: Ipv4 address of node
+ :type ip: str
+ :param domain_name: Name of Domain
+ :type domain_name: str
+ :returns: String containing xml data for request
+
+ """
+ templ = Template('''<input>
+ <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
+ $domain
+</input>''')
+ data = templ.substitute({'ip': ip, 'domain': get_domain_name(domain_name)})
+ return data
+
+
+def get_bindings_from_node_xml(ip, binding_range, domain_name):
+ """Generate xml for Get Bindings request
+
+ :param binding_range: All or only Local bindings
+ :type binding_range: str
+ :param ip: Ipv4 address of node
+ :type ip: str
+ :param domain_name: Name of Domain
+ :type domain_name: str
+ :returns: String containing xml data for request
+
+ """
+ templ = Template('''<input>
+ <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
+ <bindings-range xmlns="urn:opendaylight:sxp:controller">$range</bindings-range>
+ $domain
+</input>''')
+ data = templ.substitute({'ip': ip, 'range': binding_range, 'domain': get_domain_name(domain_name)})
+ return data
+
+
+def add_node_xml(node_id, port, password, version, node_ip=None, expansion=0, bindings_timeout=0, keystores=None,
+ retry_open_timer=1):
+ """Generate xml for Add Node request
+
+ :param node_id: Ipv4 address formatted node id
+ :type node_id: str
+ :param node_ip: Ipv4 address of node
+ :type node_ip: strl
+ :param port: Node port number
+ :type port: int
+ :param password: TCP-MD5 password
+ :type password: str
+ :param version: Sxp device version
+ :type version: str
+ :param expansion: Bindings expansion
+ :type expansion: int
+ :param bindings_timeout: Specifies DHD and Reconciliation timers
+ :type bindings_timeout: int
+ :param keystores: SSL keystore and truststore specification
+ :type keystores: dict
+ :returns: String containing xml data for request
+
+ """
+ tls = ''
+ if keystores:
+ tls = Template('''
+ <tls>
+ <keystore>
+ <location>$keystore</location>
+ <type>JKS</type>
+ <path-type>PATH</path-type>
+ <password>$passwd</password>
+ </keystore>
+ <truststore>
+ <location>$truststore</location>
+ <type>JKS</type>
+ <path-type>PATH</path-type>
+ <password>$passwd</password>
+ </truststore>
+ <certificate-password>$passwd</certificate-password>
+ </tls>
+ ''').substitute(
+ {'keystore': keystores['keystore'], 'truststore': keystores['truststore'], 'passwd': keystores['password']})
+
+ templ = Template('''<input xmlns="urn:opendaylight:sxp:controller">
+ <node-id>$id</node-id>
+ <timers>
+ <retry-open-time>$retry_open_timer</retry-open-time>
+ <hold-time-min-acceptable>120</hold-time-min-acceptable>
+ <delete-hold-down-time>$timeout</delete-hold-down-time>
+ <hold-time-min>90</hold-time-min>
+ <reconciliation-time>$timeout</reconciliation-time>
+ <hold-time>90</hold-time>
+ <hold-time-max>180</hold-time-max>
+ <keep-alive-time>30</keep-alive-time>
+ </timers>
+ <mapping-expanded>$expansion</mapping-expanded>
+ <security>
+ $tls
+ <password>$password</password>
+ </security>
+ <tcp-port>$port</tcp-port>
+ <version>$version</version>
+ <description>ODL SXP Controller</description>
+ <source-ip>$ip</source-ip>
+ <master-database></master-database>
+</input>''')
+ data = templ.substitute(
+ {'ip': node_ip or node_id, 'id': node_id, 'port': port, 'password': password,
+ 'version': version, 'expansion': expansion, 'timeout': bindings_timeout, 'tls': tls,
+ 'retry_open_timer': retry_open_timer})
+ return data
+
+
+def delete_node_xml(node_id):
+ """Generate xml for Delete node request
+
+ :param node_id: Ipv4 address formatted node id
+ :type node_id: str
+ :returns: String containing xml data for request
+
+ """
+ templ = Template('''<input xmlns="urn:opendaylight:sxp:controller">
+ <node-id>$id</node-id>
+</input>''')
+ data = templ.substitute({'id': node_id})
+ return data
+
+
+def add_domain_xml(node_id, name):
+ """Generate xml for Add Domain request
+
+ :param node_id: Id of node
+ :type node_id: str
+ :param name: Name of Domain
+ :type name: str
+ :returns: String containing xml data for request
+
+ """
+ templ = Template('''<input>
+ <node-id xmlns="urn:opendaylight:sxp:controller">$id</node-id>
+ <domain-name xmlns="urn:opendaylight:sxp:controller">$name</domain-name>
+</input>''')
+ data = templ.substitute({'name': name, 'id': node_id})
+ return data
+
+
+def delete_domain_xml(node_id, name):
+ """Generate xml for Remove Domain request
+
+ :param node_id: Id of node
+ :type node_id: str
+ :param name: Name of Domain
+ :type name: str
+ :returns: String containing xml data for request
+
+ """
+ return add_domain_xml(node_id, name)
+
+
+def get_domain_name(domain_name):
+ """Generate xml for Get Bindings request
+
+ :param domain_name: Name of Domain
+ :type domain_name: str
+ :returns: String containing xml data for request
+
+ """
+ if domain_name == 'global':
+ return ''
+ else:
+ return '<domain-name xmlns="urn:opendaylight:sxp:controller">' + domain_name + '</domain-name>'
+
+
+def add_bindings_xml(node_id, domain, sgt, prefixes):
+ """Generate xml for Add Bindings request
+
+ :param node_id: Id of node
+ :type node_id: str
+ :param domain: Name of Domain
+ :type domain: str
+ :param sgt: Security group
+ :type sgt: int
+ :param prefixes: List of ip-prefixes
+ :type prefixes: str
+ :returns: String containing xml data for request
+
+ """
+ bindings = ''
+ for prefix in prefixes.split(','):
+ bindings += '\n' + '<ip-prefix>' + prefix + '</ip-prefix>'
+ templ = Template('''<input>
+ <node-id xmlns="urn:opendaylight:sxp:controller">$id</node-id>
+ <domain-name xmlns="urn:opendaylight:sxp:controller">$name</domain-name>
+ <binding xmlns="urn:opendaylight:sxp:controller">
+ <sgt>$sgt</sgt>
+ $bindings
+ </binding>
+</input>''')
+ data = templ.substitute({'name': domain, 'id': node_id, 'sgt': sgt, 'bindings': bindings})
+ return data
+
+
+def delete_bindings_xml(node_id, domain, sgt, prefixes):
+ """Generate xml for Remove Bindings request
+
+ :param node_id: Id of node
+ :type node_id: str
+ :param domain: Name of Domain
+ :type domain: str
+ :param sgt: Security group
+ :type sgt: int
+ :param prefixes: List of ip-prefixes
+ :type prefixes: str
+ :returns: String containing xml data for request
+
+ """
+ return add_bindings_xml(node_id, domain, sgt, prefixes)
+
+
+def prefix_range(start, end):
+ """Generate and concatenate ip-prefixes
+
+ :param start: Start index
+ :type start: str
+ :param end: End index
+ :type end: str
+ :returns: String containing concatenated ip-prefixes
+
+ """
+ start = int(start)
+ end = int(end)
+ index = 0
+ prefixes = ''
+ while index < end:
+ prefixes += get_ip_from_number(index + start) + '/32'
+ index += 1
+ if index < end:
+ prefixes += ','
+ return prefixes
+
+
+def route_definition_xml(virtual_ip, net_mask, interface):
+ """Generate xml for Add Bindings request
+
+ :param interface: Network interface name
+ :type interface: str
+ :param net_mask: NetMask of virtual ip
+ :type net_mask: str
+ :param virtual_ip: Virtual ip
+ :type virtual_ip: str
+ :returns: String containing xml data for request
+
+ """
+ templ = Template('''
+ <routing-definition>
+ <ip-address>$vip</ip-address>
+ <interface>$interface</interface>
+ <netmask>$mask</netmask>
+ </routing-definition>
+ ''')
+ data = templ.substitute({'mask': net_mask, 'vip': virtual_ip, 'interface': interface})
+ return data
+
+
+def route_definitions_xml(routes, old_routes=None):
+ """Generate xml for Add Bindings request
+
+ :param routes: XML formatted data containing RouteDefinitions
+ :type routes: str
+ :param old_routes: Routes add to request that needs to persist
+ :type old_routes: str
+ :returns: String containing xml data for request
+
+ """
+ if old_routes and "</sxp-cluster-route>" in old_routes:
+ templ = Template(old_routes.replace("</sxp-cluster-route>", "$routes</sxp-cluster-route>"))
+ else:
+ templ = Template('''<sxp-cluster-route xmlns="urn:opendaylight:sxp:cluster:route">
+ $routes
+</sxp-cluster-route>
+ ''')
+ data = templ.substitute({'routes': routes})