def _get_cookie(self, mgmt_ip, config):
        """Performs authentication and retries cookie."""

        if mgmt_ip not in self.credentials:
            return None

        security_data = self.credentials[mgmt_ip]
        payload = {
            "aaaUser": {
                "attributes": {
                    "name": security_data[0],
                    "pwd": security_data[1]
                }
            }
        }
        headers = {"Content-type": "application/json", "Accept": "text/plain"}

        url = "http://{0}/api/aaaLogin.json".format(mgmt_ip)

        try:
            response = self.session.request('POST',
                                            url,
                                            data=jsonutils.dumps(payload),
                                            headers=headers,
                                            timeout=self.timeout * 2)
        except Exception as e:
            raise cexc.NexusConnectFailed(nexus_host=mgmt_ip, exc=e)

        self.status = response.status_code
        if response.status_code == requests.codes.OK:
            return response.headers.get('Set-Cookie')
        else:
            e = "REST API connect returned Error code: "
            e += str(self.status)
            raise cexc.NexusConnectFailed(nexus_host=mgmt_ip, exc=e)
    def _get_cookie(self, mgmt_ip, config, refresh=False):
        """Performs authentication and retries cookie."""

        if mgmt_ip not in self.credentials:
            return None

        security_data = self.credentials[mgmt_ip]
        verify = security_data[const.HTTPS_CERT_TUPLE]
        if not verify:
            verify = security_data[const.HTTPS_VERIFY_TUPLE]

        if not refresh and security_data[const.COOKIE_TUPLE]:
            return security_data[const.COOKIE_TUPLE], verify

        payload = {
            "aaaUser": {
                "attributes": {
                    "name": security_data[const.UNAME_TUPLE],
                    "pwd": security_data[const.PW_TUPLE]
                }
            }
        }
        headers = {"Content-type": "application/json", "Accept": "text/plain"}

        url = "{0}://{1}/api/aaaLogin.json".format(DEFAULT_SCHEME, mgmt_ip)

        try:
            response = self.session.request('POST',
                                            url,
                                            data=jsonutils.dumps(payload),
                                            headers=headers,
                                            verify=verify,
                                            timeout=self.timeout * 2)
        except Exception as e:
            raise cexc.NexusConnectFailed(nexus_host=mgmt_ip, exc=e)

        self.status = response.status_code
        if response.status_code == requests.codes.OK:
            cookie = response.headers.get('Set-Cookie')
            security_data = (
                security_data[const.UNAME_TUPLE:const.COOKIE_TUPLE] +
                (cookie, ))
            self.credentials[mgmt_ip] = security_data
            return cookie, verify
        else:
            e = "REST API connect returned Error code: "
            e += str(self.status)
            raise cexc.NexusConnectFailed(nexus_host=mgmt_ip, exc=e)
    def nxos_connect(self, nexus_host):
        """Make SSH connection to the Nexus Switch."""
        if getattr(self.connections.get(nexus_host), 'connected', None):
            return self.connections[nexus_host]

        if not self.ncclient:
            self.ncclient = self._import_ncclient()
        nexus_ssh_port = int(self.nexus_switches[nexus_host, 'ssh_port'])
        nexus_user = self.nexus_switches[nexus_host, const.USERNAME]
        nexus_password = self.nexus_switches[nexus_host, const.PASSWORD]
        hostkey_verify = cfg.CONF.ml2_cisco.host_key_checks
        starttime = time.time()
        try:
            # With new ncclient version, we can pass device_params...
            man = self.ncclient.connect(host=nexus_host,
                                        port=nexus_ssh_port,
                                        username=nexus_user,
                                        password=nexus_password,
                                        hostkey_verify=hostkey_verify,
                                        timeout=30,
                                        device_params={"name": "nexus"})
        except Exception as e:
            # Raise a Neutron exception. Include a description of
            # the original ncclient exception.
            self.capture_and_print_timeshot(starttime,
                                            "connecterr",
                                            switch=nexus_host)
            raise cexc.NexusConnectFailed(nexus_host=nexus_host, exc=e)

        self.capture_and_print_timeshot(starttime,
                                        "connect",
                                        switch=nexus_host)
        self.connections[nexus_host] = man
        return self.connections[nexus_host]
    def nxos_connect(self, nexus_host):
        """Make SSH connection to the Nexus Switch."""
        if getattr(self.connections.get(nexus_host), 'connected', None):
            return self.connections[nexus_host]

        if not self.ncclient:
            self.ncclient = self._import_ncclient()
        nexus_ssh_port = int(self.nexus_switches[nexus_host, 'ssh_port'])
        nexus_user = self.nexus_switches[nexus_host, const.USERNAME]
        nexus_password = self.nexus_switches[nexus_host, const.PASSWORD]
        hostkey_verify = cfg.CONF.ml2_cisco.host_key_checks
        try:
            try:
                # With new ncclient version, we can pass device_params...
                man = self.ncclient.connect(host=nexus_host,
                                            port=nexus_ssh_port,
                                            username=nexus_user,
                                            password=nexus_password,
                                            hostkey_verify=hostkey_verify,
                                            device_params={"name": "nexus"})
            except TypeError:
                # ... but if that causes an error, we appear to have the old
                # ncclient installed, which doesn't understand this parameter.
                man = self.ncclient.connect(host=nexus_host,
                                            port=nexus_ssh_port,
                                            username=nexus_user,
                                            password=nexus_password,
                                            hostkey_verify=hostkey_verify)
        except Exception as e:
            # Raise a Neutron exception. Include a description of
            # the original ncclient exception.
            raise cexc.NexusConnectFailed(nexus_host=nexus_host, exc=e)

        self.connections[nexus_host] = man
        return self.connections[nexus_host]