def _get_netscaler_monitorbinding_from_healthmonitor(
            self, loadBalancerId, node, healthMonitor):

        monitor_name = NitroUtils.get_monitorname_from_loadBalancerId(
            loadBalancerId)
        service_name = NitroUtils.get_servicename_from_nodeid(
            loadBalancerId, node.id)

        monitor_binding = {}
        monitor_binding["monitorname"] = monitor_name
        monitor_binding["servicename"] = service_name

        return monitor_binding
    def _extract_healthmonitor(self, results, loadBalancerId, loadBalancer):

        monitor_name = NitroUtils.get_monitorname_from_loadBalancerId(
            loadBalancerId)

        monitor = NitroTasks.extract_monitor_from_task_list(
            results, monitor_name)

        if monitor:
            healthMonitor = self.monitoradapter.get_netscaler_healthmonitor_from_monitor(
                loadBalancerId, loadBalancer, monitor)

            return healthMonitor
        else:
            return None
    def _get_netscaler_addmonitor_task(self, loadBalancerId, loadBalancer,
                                       healthMonitor):

        monitor_name = NitroUtils.get_monitorname_from_loadBalancerId(
            loadBalancerId)

        state = self._get_netscaler_monitor_from_healthmonitor(
            loadBalancerId, loadBalancer, healthMonitor)

        task = {}
        task["type"] = "lbmonitor"
        task["name"] = monitor_name
        task["operation"] = "ADD"
        task["state"] = state

        return task
    def _get_netscaler_loadBalancer_monitor(self, loadBalancerId,
                                            loadBalancer):

        resource_type = "lbmonitor"
        monitor = None

        monitor_name = NitroUtils.get_monitorname_from_loadBalancerId(
            loadBalancerId)

        try:
            monitor = self.nitrowrapper.get_netscaler_entity(
                resource_type, monitor_name)
        except ItemNotFoundException:
            return None

        return monitor
    def get_netscaler_removemonitorbinding_task(self, loadBalancerId,
                                                loadBalancer, nodeId):

        monitor_name = NitroUtils.get_monitorname_from_loadBalancerId(
            loadBalancerId)
        servicename = NitroUtils.get_servicename_from_nodeid(
            loadBalancerId, nodeId)

        args = "servicename:" + servicename

        task = {}
        task["type"] = "lbmonitor_service_binding"
        task["name"] = monitor_name + "?args=" + args
        task["operation"] = "REMOVE"
        task["state"] = None

        return task
    def get_netscaler_gethealthmonitor_tasks(self, loadBalancerId,
                                             loadBalancer):

        task_list = []

        monitor_name = NitroUtils.get_monitorname_from_loadBalancerId(
            loadBalancerId)

        task = {}
        task["type"] = "lbmonitor"
        task["name"] = monitor_name
        task["operation"] = "GET"
        task["state"] = None

        task_list.append(task)

        return task_list
    def _get_LBHealthMonitorInternal(self, loadBalancerId, loadBalancer):

        task_list = self._get_netscaler_tasks("GET", loadBalancerId,
                                              loadBalancer)

        self.logger.debug("GET nodes tasklist: " + str(task_list))

        results = self.nitrowrapper.process_netscaler_task_list(task_list)

        monitor_name = NitroUtils.get_monitorname_from_loadBalancerId(
            loadBalancerId)

        monitor = NitroTasks.extract_monitor_from_task_list(
            results, monitor_name)

        healthMonitor = self.get_netscaler_healthmonitor_from_monitor(
            loadBalancerId, loadBalancer, monitor)

        return healthMonitor
    def _get_netscaler_updatemonitor_tasks(self, loadBalancerId, loadBalancer,
                                           healthMonitor):

        task_list = []

        monitor_name = NitroUtils.get_monitorname_from_loadBalancerId(
            loadBalancerId)

        if "healthMonitor" in loadBalancer and loadBalancer.healthMonitor:
            existingType = loadBalancer.healthMonitor.type.upper()

            if "type" in healthMonitor:
                newType = healthMonitor.type.upper()
            else:
                healthMonitor.type = loadBalancer.healthMonitor.type
                newType = existingType

            if newType != existingType:
                """ The monitor type is different, we need to remove the existing health monitor 
                    and add a new one """

                tasks = self.get_netscaler_removehealthmonitor_tasks(
                    loadBalancerId, loadBalancer)

                if tasks:
                    task_list.extend(tasks)
                    self._adjust_healthmonitor_state(
                        healthMonitor, loadBalancer.healthMonitor)
                    tasks = self.get_netscaler_addhealthmonitor_tasks(
                        loadBalancerId, loadBalancer, healthMonitor)

                    if tasks:
                        task_list.extend(tasks)
            else:
                self._adjust_healthmonitor_state(healthMonitor,
                                                 loadBalancer.healthMonitor)
                task = self._get_netscaler_updatemonitor_task(
                    loadBalancerId, loadBalancer, healthMonitor)

                if task:
                    task_list.append(task)

        return task_list
    def _get_netscaler_updatemonitor_task(self, loadBalancerId, loadBalancer,
                                          healthMonitor):

        monitor_name = NitroUtils.get_monitorname_from_loadBalancerId(
            loadBalancerId)

        state = self._get_netscaler_monitor_from_healthmonitor(
            loadBalancerId, loadBalancer, healthMonitor)

        if len(state.keys()) < 3:
            return None

        task = {}
        task["type"] = "lbmonitor"
        task["name"] = monitor_name
        task["operation"] = "UPDATE"
        task["state"] = state

        return task
    def _get_netscaler_monitor_from_healthmonitor(self, loadBalancerId,
                                                  loadBalancer, healthMonitor):

        self.logger.debug(
            "obtaining a netscaler monitor from an OpenStack healthmonitor: %s"
            % str(healthMonitor))

        if not "type" in healthMonitor or not healthMonitor.type:
            self.logger.debug("healthMonitor type attribute missing")
            raise BadRequestException("validation fault", "invalid object",
                                      "healthMonitor type attribute missing")

        if not healthMonitor.type.upper() in ["CONNECT", "HTTP", "HTTPS"]:
            self.logger.debug(
                "healthMonitor type attribute has an invalid value")
            raise BadRequestException(
                "validation fault", "invalid object",
                "healthMonitor type attribute has an invalid value")

        monitor = {}

        monitor[
            "monitorname"] = NitroUtils.get_monitorname_from_loadBalancerId(
                loadBalancerId)

        if healthMonitor.type.upper() in ["HTTP", "HTTPS"]:
            monitor["type"] = "USER"
            monitor["scriptname"] = self.usermonitor
        else:
            monitor["type"] = "TCP"

        if "delay" in healthMonitor:
            monitor["interval"] = int(healthMonitor.delay)

        if "timeout" in healthMonitor:
            monitor["resptimeout"] = int(healthMonitor.timeout)

        if "attemptsBeforeDeactivation" in healthMonitor:
            monitor["retries"] = int(healthMonitor.attemptsBeforeDeactivation)

        if monitor["type"] == "TCP":
            return monitor

        if not "path" in healthMonitor or not healthMonitor.path:
            self.logger.debug(
                "healthMonitor of type HTTP or HTTPS has the path attribute missing"
            )
            raise BadRequestException(
                "validation fault", "invalid object",
                "healthMonitor of type HTTP or HTTPS has the path attribute missing"
            )

        type_expr = '='.join(["type", healthMonitor.type])
        scriptargs = type_expr

        path = '\'' + healthMonitor.path + '\''
        path_expr = '='.join(["path", path])
        scriptargs = ';'.join([scriptargs, path_expr])

        if not "statusRegex" in healthMonitor or not healthMonitor.statusRegex:
            healthMonitor.statusRegex = "[1-5]{3}"

        statusRegex = '\'' + healthMonitor.statusRegex + "\'"
        status_expr = '='.join(["statusRegex", statusRegex])
        scriptargs = ';'.join([scriptargs, status_expr])

        if not "bodyRegex" in healthMonitor or not healthMonitor.bodyRegex:
            healthMonitor.bodyRegex = ".*"

        bodyRegex = '\'' + healthMonitor.bodyRegex + "\'"
        body_expr = '='.join(["bodyRegex", bodyRegex])
        scriptargs = ';'.join([scriptargs, body_expr])

        monitor["scriptargs"] = scriptargs

        self.logger.debug("Netscaler monitor built from healthmonitor: %s" %
                          str(monitor))

        return monitor