+
+
+class LispGPEKeywords(object):
+ """Implementation of keywords which make it possible to:
+ - enable/disable LISP GPE feature
+ - configure LISP GPE forwarding entries
+ - read operational data for all of the above
+ """
+
+ def __init__(self):
+ """Initializer."""
+ pass
+
+ @staticmethod
+ def _set_lispgpe_properties(node, path, data=None):
+ """Set LISP GPE properties and check the return code.
+
+ :param node: Honeycomb node.
+ :param path: Path which is added to the base path to identify the data.
+ :param data: The new data to be set. If None, the item will be removed.
+ :type node: dict
+ :type path: str
+ :type data: dict
+ :returns: Content of response.
+ :rtype: bytearray
+ :raises HoneycombError: If the status code in response to PUT is not
+ 200 = OK or 201 = ACCEPTED.
+ """
+
+ if data:
+ status_code, resp = HcUtil.\
+ put_honeycomb_data(node, "config_lisp_gpe", data, path,
+ data_representation=DataRepresentation.JSON)
+ else:
+ status_code, resp = HcUtil.\
+ delete_honeycomb_data(node, "config_lisp_gpe", path)
+
+ if status_code not in (HTTPCodes.OK, HTTPCodes.ACCEPTED):
+ raise HoneycombError(
+ "Lisp GPE configuration unsuccessful. "
+ "Status code: {0}.".format(status_code))
+ else:
+ return resp
+
+ @staticmethod
+ def get_lispgpe_operational_data(node):
+ """Retrieve LISP GPE properties from Honeycomb operational data.
+
+ :param node: Honeycomb node.
+ :type node: dict
+ :returns: LISP GPE operational data.
+ :rtype: bytearray
+ :raises HoneycombError: If the status code in response to GET is not
+ 200 = OK.
+ """
+
+ status_code, resp = HcUtil.get_honeycomb_data(node, "oper_lisp_gpe")
+
+ if status_code != HTTPCodes.OK:
+ raise HoneycombError("Could not retrieve Lisp GPE operational data."
+ "Status code: {0}.".format(status_code))
+ else:
+ return resp
+
+ @staticmethod
+ def get_lispgpe_mapping(node, name):
+ """Retrieve LISP GPE operational data and parse for a specific mapping.
+
+ :param node: Honeycomb node.
+ :param name: Name of the mapping to look for.
+ :type node: dict
+ :type name: str
+ :returns: LISP GPE mapping.
+ :rtype: dict
+ :raises HoneycombError: If the mapping is not present in operational
+ data.
+ """
+
+ data = LispGPEKeywords.get_lispgpe_operational_data(node)
+ try:
+ data = data["gpe-state"]["gpe-feature-data"]["gpe-entry-table"] \
+ ["gpe-entry"]
+ except KeyError:
+ raise HoneycombError("No mappings present in operational data.")
+ for item in data:
+ if item["id"] == name:
+ mapping = item
+ break
+ else:
+ raise HoneycombError("Mapping with name {name} not found in "
+ "operational data.".format(name=name))
+
+ return mapping
+
+ @staticmethod
+ def get_lispgpe_config_data(node):
+ """Retrieve LISP GPE properties from Honeycomb config data.
+
+ :param node: Honeycomb node.
+ :type node: dict
+ :returns: LISP GPE config data.
+ :rtype: bytearray
+ :raises HoneycombError: If the status code in response to GET is not
+ 200 = OK.
+ """
+
+ status_code, resp = HcUtil.get_honeycomb_data(node, "config_lisp_gpe")
+
+ if status_code != HTTPCodes.OK:
+ raise HoneycombError("Could not retrieve Lisp GPE config data."
+ "Status code: {0}.".format(status_code))
+ else:
+ return resp
+
+ @staticmethod
+ def set_lispgpe_state(node, state=True):
+ """Enable or disable the LISP GPE feature.
+
+ :param node: Honeycomb node.
+ :param state: Enable or disable LISP.
+ :type node: dict
+ :type state: bool
+ :returns: Content of response.
+ :rtype: bytearray
+ :raises HoneycombError: If the return code is not 200:OK
+ or 404:NOT FOUND.
+ """
+
+ ret_code, data = HcUtil.get_honeycomb_data(node, "config_lisp_gpe")
+ if ret_code == HTTPCodes.OK:
+ data["gpe"]["gpe-feature-data"]["enable"] = bool(state)
+ elif ret_code == HTTPCodes.NOT_FOUND:
+ data = {"gpe": {"gpe-feature-data": {"enable": bool(state)}}}
+ else:
+ raise HoneycombError("Unexpected return code when getting existing"
+ " Lisp GPE configuration.")
+
+ return LispGPEKeywords._set_lispgpe_properties(node, '', data)
+
+ @staticmethod
+ def configure_lispgpe_mapping(node, data=None):
+ """Modify LISP GPE mapping configuration to the data provided.
+
+ :param node: Honeycomb node.
+ :param data: Settings for the LISP GPE mappings.
+ :type node: dict
+ :type data: dict
+ :returns: Content of response.
+ :rtype: bytearray
+ """
+
+ path = "/gpe-feature-data/gpe-entry-table"
+ if data:
+ data = {"gpe-entry-table": {"gpe-entry": data}}
+ return LispGPEKeywords._set_lispgpe_properties(node, path, data)
+ else:
+ return LispGPEKeywords._set_lispgpe_properties(node, path)
+
+ @staticmethod
+ def add_lispgpe_mapping(node, name, data):
+ """Add the specified LISP GPE mapping.
+
+ :param node: Honeycomb node.
+ :param name: Name for the mapping.
+ :param data: Mapping details.
+ :type node: dict
+ :type name: str
+ :type data: dict
+ :returns: Content of response.
+ :rtype: bytearray
+ """
+
+ path = "/gpe-feature-data/gpe-entry-table/gpe-entry/{name}".format(
+ name=name)
+
+ data = {"gpe-entry": data}
+ return LispGPEKeywords._set_lispgpe_properties(node, path, data)
+
+ @staticmethod
+ def delete_lispgpe_mapping(node, mapping):
+ """Delete the specified LISP GPE mapping from configuration.
+
+ :param node: Honeycomb node.
+ :param mapping: Name of the mapping to remove.
+ :type node: dict
+ :type mapping: str
+ :returns: Content of response.
+ :rtype: bytearray
+ """
+
+ path = "/gpe-feature-data/gpe-entry-table/gpe-entry/{0}".format(mapping)
+ return LispGPEKeywords._set_lispgpe_properties(node, path)
+
+ @staticmethod
+ def disable_lispgpe(node):
+ """Remove all LISP GPE settings on the node.
+
+ :param node: Honeycomb node.
+ :type node: dict
+ :returns: Content of response.
+ :rtype: bytearray
+ """
+
+ return LispGPEKeywords._set_lispgpe_properties(node, "")