def test_create_and_delete_network(self):
        tenant_id = 'ten-1'
        network_id = 'net1-id'
        segmentation_id = 1001

        network_context = self._get_network_context(tenant_id, network_id,
                                                    segmentation_id)
        self.drv.create_network_precommit(network_context)
        net_provisioned = db_lib.is_network_provisioned(tenant_id, network_id)
        self.assertTrue(net_provisioned, 'The network should be created')

        expected_num_nets = 1
        num_nets_provisioned = db_lib.num_nets_provisioned(tenant_id)
        self.assertEqual(
            expected_num_nets, num_nets_provisioned,
            'There should be %d nets, not %d' %
            (expected_num_nets, num_nets_provisioned))

        # Now test the delete network
        self.drv.delete_network_precommit(network_context)
        net_provisioned = db_lib.is_network_provisioned(tenant_id, network_id)
        self.assertFalse(net_provisioned, 'The network should be created')

        expected_num_nets = 0
        num_nets_provisioned = db_lib.num_nets_provisioned(tenant_id)
        self.assertEqual(
            expected_num_nets, num_nets_provisioned,
            'There should be %d nets, not %d' %
            (expected_num_nets, num_nets_provisioned))
    def test_create_and_delete_network(self):
        tenant_id = 'ten-1'
        network_id = 'net1-id'
        segmentation_id = 1001

        network_context = self._get_network_context(tenant_id,
                                                    network_id,
                                                    segmentation_id)
        self.drv.create_network_precommit(network_context)
        net_provisioned = db_lib.is_network_provisioned(tenant_id, network_id)
        self.assertTrue(net_provisioned, 'The network should be created')

        expected_num_nets = 1
        num_nets_provisioned = db_lib.num_nets_provisioned(tenant_id)
        self.assertEqual(expected_num_nets, num_nets_provisioned,
                         'There should be %d nets, not %d' %
                         (expected_num_nets, num_nets_provisioned))

        # Now test the delete network
        self.drv.delete_network_precommit(network_context)
        net_provisioned = db_lib.is_network_provisioned(tenant_id, network_id)
        self.assertFalse(net_provisioned, 'The network should be created')

        expected_num_nets = 0
        num_nets_provisioned = db_lib.num_nets_provisioned(tenant_id)
        self.assertEqual(expected_num_nets, num_nets_provisioned,
                         'There should be %d nets, not %d' %
                         (expected_num_nets, num_nets_provisioned))
Esempio n. 3
0
    def create_network_postcommit(self, context):
        """Provision the network on the Arista Hardware."""

        network = context.current
        network_id = network['id']
        network_name = network['name']
        tenant_id = network['tenant_id']
        segments = context.network_segments
        vlan_id = segments[0]['segmentation_id']
        shared_net = network['shared']
        with self.eos_sync_lock:
            if db_lib.is_network_provisioned(tenant_id, network_id):
                try:
                    network_dict = {
                        'network_id': network_id,
                        'segmentation_id': vlan_id,
                        'network_name': network_name,
                        'shared': shared_net
                    }
                    self.rpc.create_network(tenant_id, network_dict)
                except arista_exc.AristaRpcError:
                    LOG.info(EOS_UNREACHABLE_MSG)
                    raise ml2_exc.MechanismDriverError()
            else:
                LOG.info(
                    _LI('Network %s is not created as it is not found in '
                        'Arista DB'), network_id)
    def create_network_postcommit(self, context):
        """Provision the network on the Arista Hardware."""

        network = context.current
        network_id = network['id']
        network_name = network['name']
        tenant_id = network['tenant_id'] or INTERNAL_TENANT_ID
        segments = context.network_segments
        vlan_id = segments[0]['segmentation_id']
        shared_net = network['shared']
        with self.eos_sync_lock:
            if db_lib.is_network_provisioned(tenant_id, network_id):
                try:
                    network_dict = {
                        'network_id': network_id,
                        'segmentation_id': vlan_id,
                        'network_name': network_name,
                        'shared': shared_net}
                    self.rpc.create_network(tenant_id, network_dict)
                except arista_exc.AristaRpcError as err:
                    LOG.error(_LE("create_network_postcommit: Did not create "
                                  "network %(name)s. Reason: %(err)s"),
                              {'name': network_name, 'err': err})
            else:
                LOG.info(_LI('Network %s is not created as it is not found in '
                             'Arista DB'), network_id)
Esempio n. 5
0
    def create_port_precommit(self, context):
        """Remember the infromation about a VM and its ports

        A VM information, along with the physical host information
        is saved.
        """
        port = context.current
        device_id = port['device_id']
        device_owner = port['device_owner']
        host = context.host

        # device_id and device_owner are set on VM boot
        is_vm_boot = device_id and device_owner
        if host and is_vm_boot:
            port_id = port['id']
            network_id = port['network_id']
            tenant_id = port['tenant_id']
            if not tenant_id:
                tenant_id = context._plugin_context.tenant_id
            with self.eos_sync_lock:
                # If network does not exist under this tenant,
                # it may be a shared network. Get shared network owner Id
                net_provisioned = (
                    db_lib.is_network_provisioned(tenant_id, network_id)
                    or self.ndb.get_shared_network_owner_id(network_id))
                if not net_provisioned:
                    # Ignore this request if network is not provisioned
                    return
                db_lib.remember_tenant(tenant_id)
                db_lib.remember_vm(device_id, host, port_id, network_id,
                                   tenant_id)
    def update_network_postcommit(self, context):
        """At the moment we only support network name change

        If network name is changed, a new network create request is
        sent to the Arista Hardware.
        """
        new_network = context.current
        orig_network = context.original
        if ((new_network['name'] != orig_network['name']) or
           (new_network['shared'] != orig_network['shared'])):
            network_id = new_network['id']
            network_name = new_network['name']
            tenant_id = new_network['tenant_id'] or INTERNAL_TENANT_ID
            vlan_id = new_network['provider:segmentation_id']
            shared_net = new_network['shared']
            with self.eos_sync_lock:
                if db_lib.is_network_provisioned(tenant_id, network_id):
                    try:
                        network_dict = {
                            'network_id': network_id,
                            'segmentation_id': vlan_id,
                            'network_name': network_name,
                            'shared': shared_net}
                        self.rpc.create_network(tenant_id, network_dict)
                    except arista_exc.AristaRpcError as err:
                        LOG.error(_LE('update_network_postcommit: Did not '
                                      'update network %(name)s. '
                                      'Reason: %(err)s'),
                                  {'name': network_name, 'err': err})
                else:
                    LOG.info(_LI('Network %s is not updated as it is not found'
                                 ' in Arista DB'), network_id)
    def create_port_precommit(self, context):
        """Remember the infromation about a VM and its ports

        A VM information, along with the physical host information
        is saved.
        """
        port = context.current
        device_id = port['device_id']
        device_owner = port['device_owner']
        host = context.host

        # device_id and device_owner are set on VM boot
        is_vm_boot = device_id and device_owner
        if host and is_vm_boot:
            port_id = port['id']
            network_id = port['network_id']
            tenant_id = port['tenant_id']
            if not tenant_id:
                tenant_id = context._plugin_context.tenant_id
            with self.eos_sync_lock:
                # If network does not exist under this tenant,
                # it may be a shared network. Get shared network owner Id
                net_provisioned = (
                    db_lib.is_network_provisioned(tenant_id, network_id) or
                    self.ndb.get_shared_network_owner_id(network_id)
                )
                if not net_provisioned:
                    # Ignore this request if network is not provisioned
                    return
                db_lib.remember_tenant(tenant_id)
                db_lib.remember_vm(device_id, host, port_id,
                                   network_id, tenant_id)
    def create_network_postcommit(self, context):
        """Provision the network on the Arista Hardware."""

        network = context.current
        network_id = network['id']
        network_name = network['name']
        tenant_id = network['tenant_id']
        if not tenant_id:
            tenant_id = context._plugin_context.tenant_id
        segments = context.network_segments
        vlan_id = segments[0]['segmentation_id']
        shared_net = network['shared']
        with self.eos_sync_lock:
            if db_lib.is_network_provisioned(tenant_id, network_id):
                try:
                    network_dict = {
                        'network_id': network_id,
                        'segmentation_id': vlan_id,
                        'network_name': network_name,
                        'shared': shared_net}
                    self.rpc.create_network(tenant_id, network_dict)
                except arista_exc.AristaRpcError:
                    LOG.info(EOS_UNREACHABLE_MSG)
                    raise ml2_exc.MechanismDriverError()
            else:
                LOG.info(_LI('Network %s is not created as it is not found in '
                             'Arista DB'), network_id)
    def create_network_postcommit(self, context):
        """Provision the network on the Arista Hardware."""

        network = context.current
        network_id = network['id']
        network_name = network['name']
        tenant_id = network['tenant_id'] or INTERNAL_TENANT_ID
        segments = context.network_segments
        shared_net = network['shared']
        with self.eos_sync_lock:
            if db_lib.is_network_provisioned(tenant_id, network_id):
                try:
                    network_dict = {
                        'network_id': network_id,
                        'segments': segments,
                        'network_name': network_name,
                        'shared': shared_net
                    }
                    self.rpc.create_network(tenant_id, network_dict)
                except arista_exc.AristaRpcError as err:
                    LOG.error(
                        _LE("create_network_postcommit: Did not create "
                            "network %(name)s. Reason: %(err)s"), {
                                'name': network_name,
                                'err': err
                            })
            else:
                LOG.info(
                    _LI('Network %s is not created as it is not found in '
                        'Arista DB'), network_id)
Esempio n. 10
0
    def _delete_port(self, port, host, tenant_id):
        """Deletes the port from EOS.

        param port: Port which is to be deleted
        param host: The host on which the port existed
        param tenant_id: The tenant to which the port belongs to. Some times
                         the tenant id in the port dict is not present (as in
                         the case of HA router).
        """
        device_id = port["device_id"]
        port_id = port["id"]
        network_id = port["network_id"]
        device_owner = port["device_owner"]

        try:
            if not db_lib.is_network_provisioned(tenant_id, network_id):
                # If we do not have network associated with this, ignore it
                return
            hostname = self._host_name(host)
            if device_owner == n_const.DEVICE_OWNER_DHCP:
                self.rpc.unplug_dhcp_port_from_network(device_id, hostname, port_id, network_id, tenant_id)
            else:
                self.rpc.unplug_host_from_network(device_id, hostname, port_id, network_id, tenant_id)
            # if necessary, delete tenant as well.
            self.delete_tenant(tenant_id)
        except arista_exc.AristaRpcError:
            LOG.info(EOS_UNREACHABLE_MSG)
            raise ml2_exc.MechanismDriverError()
Esempio n. 11
0
    def create_network_postcommit(self, context):
        """Provision the network on the Arista Hardware."""

        network = context.current
        network_id = network["id"]
        network_name = network["name"]
        tenant_id = network["tenant_id"]
        if not tenant_id:
            tenant_id = context._plugin_context.tenant_id
        segments = context.network_segments
        vlan_id = segments[0]["segmentation_id"]
        shared_net = network["shared"]
        with self.eos_sync_lock:
            if db_lib.is_network_provisioned(tenant_id, network_id):
                try:
                    network_dict = {
                        "network_id": network_id,
                        "segmentation_id": vlan_id,
                        "network_name": network_name,
                        "shared": shared_net,
                    }
                    self.rpc.create_network(tenant_id, network_dict)
                except arista_exc.AristaRpcError:
                    LOG.info(EOS_UNREACHABLE_MSG)
                    raise ml2_exc.MechanismDriverError()
            else:
                LOG.info(_LI("Network %s is not created as it is not found in " "Arista DB"), network_id)
Esempio n. 12
0
    def create_port_precommit(self, context):
        """Remember the infromation about a VM and its ports

        A VM information, along with the physical host information
        is saved.
        """
        port = context.current
        device_id = port["device_id"]
        device_owner = port["device_owner"]
        host = context.host

        # device_id and device_owner are set on VM boot
        is_vm_boot = device_id and device_owner
        if host and is_vm_boot:
            port_id = port["id"]
            network_id = port["network_id"]
            tenant_id = port["tenant_id"]
            if not tenant_id:
                tenant_id = context._plugin_context.tenant_id
            with self.eos_sync_lock:
                if not db_lib.is_network_provisioned(tenant_id, network_id):
                    # Ignore this request if network is not provisioned
                    return
                db_lib.remember_tenant(tenant_id)
                db_lib.remember_vm(device_id, host, port_id, network_id, tenant_id)
Esempio n. 13
0
    def _delete_port(self, port, host, tenant_id):
        """Deletes the port from EOS.

        param port: Port which is to be deleted
        param host: The host on which the port existed
        param tenant_id: The tenant to which the port belongs to. Some times
                         the tenant id in the port dict is not present (as in
                         the case of HA router).
        """
        device_id = port['device_id']
        port_id = port['id']
        network_id = port['network_id']
        device_owner = port['device_owner']

        try:
            if not db_lib.is_network_provisioned(tenant_id, network_id):
                # If we do not have network associated with this, ignore it
                return
            hostname = self._host_name(host)
            if device_owner == n_const.DEVICE_OWNER_DHCP:
                self.rpc.unplug_dhcp_port_from_network(device_id, hostname,
                                                       port_id, network_id,
                                                       tenant_id)
            else:
                self.rpc.unplug_host_from_network(device_id, hostname, port_id,
                                                  network_id, tenant_id)
            # if necessary, delete tenant as well.
            self.delete_tenant(tenant_id)
        except arista_exc.AristaRpcError:
            LOG.info(EOS_UNREACHABLE_MSG)
            raise ml2_exc.MechanismDriverError()
Esempio n. 14
0
    def update_network_postcommit(self, context):
        """At the moment we only support network name change

        If network name is changed, a new network create request is
        sent to the Arista Hardware.
        """
        new_network = context.current
        orig_network = context.original
        if ((new_network['name'] != orig_network['name'])
                or (new_network['shared'] != orig_network['shared'])):
            network_id = new_network['id']
            network_name = new_network['name']
            tenant_id = new_network['tenant_id']
            vlan_id = new_network['provider:segmentation_id']
            shared_net = new_network['shared']
            with self.eos_sync_lock:
                if db_lib.is_network_provisioned(tenant_id, network_id):
                    try:
                        network_dict = {
                            'network_id': network_id,
                            'segmentation_id': vlan_id,
                            'network_name': network_name,
                            'shared': shared_net
                        }
                        self.rpc.create_network(tenant_id, network_dict)
                    except arista_exc.AristaRpcError:
                        LOG.info(EOS_UNREACHABLE_MSG)
                        raise ml2_exc.MechanismDriverError()
                else:
                    LOG.info(
                        _LI('Network %s is not updated as it is not found'
                            ' in Arista DB'), network_id)
    def update_network_postcommit(self, context):
        """At the moment we only support network name change

        If network name is changed, a new network create request is
        sent to the Arista Hardware.
        """
        new_network = context.current
        orig_network = context.original
        if ((new_network['name'] != orig_network['name']) or
           (new_network['shared'] != orig_network['shared'])):
            network_id = new_network['id']
            network_name = new_network['name']
            tenant_id = new_network['tenant_id']
            if not tenant_id:
                tenant_id = context._plugin_context.tenant_id
            vlan_id = new_network['provider:segmentation_id']
            shared_net = new_network['shared']
            with self.eos_sync_lock:
                if db_lib.is_network_provisioned(tenant_id, network_id):
                    try:
                        network_dict = {
                            'network_id': network_id,
                            'segmentation_id': vlan_id,
                            'network_name': network_name,
                            'shared': shared_net}
                        self.rpc.create_network(tenant_id, network_dict)
                    except arista_exc.AristaRpcError:
                        LOG.info(EOS_UNREACHABLE_MSG)
                        raise ml2_exc.MechanismDriverError()
                else:
                    LOG.info(_LI('Network %s is not updated as it is not found'
                                 ' in Arista DB'), network_id)
    def test_network_is_removed(self):
        tenant_id = 'test'
        network_id = '123'

        db_lib.remember_network(tenant_id, network_id, '123')
        db_lib.forget_network(tenant_id, network_id)
        net_provisioned = db_lib.is_network_provisioned(tenant_id, network_id)
        self.assertFalse(net_provisioned, 'The network should be deleted')
 def delete_network_precommit(self, context):
     """Delete the network infromation from the DB."""
     network = context.current
     network_id = network['id']
     tenant_id = network['tenant_id']
     with self.eos_sync_lock:
         if db_lib.is_network_provisioned(tenant_id, network_id):
             db_lib.forget_network(tenant_id, network_id)
Esempio n. 18
0
 def delete_network_precommit(self, context):
     """Delete the network infromation from the DB."""
     network = context.current
     network_id = network['id']
     tenant_id = network['tenant_id']
     with self.eos_sync_lock:
         if db_lib.is_network_provisioned(tenant_id, network_id):
             db_lib.forget_network(tenant_id, network_id)
    def test_network_is_remembered(self):
        tenant_id = 'test'
        network_id = '123'
        segmentation_id = 456

        db_lib.remember_network(tenant_id, network_id, segmentation_id)
        net_provisioned = db_lib.is_network_provisioned(tenant_id, network_id)
        self.assertTrue(net_provisioned, 'Network must be provisioned')
    def test_network_is_removed(self):
        tenant_id = 'test'
        network_id = '123'

        db_lib.remember_network(tenant_id, network_id, '123')
        db_lib.forget_network(tenant_id, network_id)
        net_provisioned = db_lib.is_network_provisioned(tenant_id, network_id)
        self.assertFalse(net_provisioned, 'The network should be deleted')
    def update_port_postcommit(self, context):
        """Update the name of a given port in EOS.

        At the moment we only support port name change
        Any other change to port is not supported at this time.
        """
        port = context.current
        orig_port = context.original

        device_id = port['device_id']
        device_owner = port['device_owner']
        host = context.host
        is_vm_boot = device_id and device_owner

        if host and is_vm_boot:
            port_id = port['id']
            port_name = port['name']
            network_id = port['network_id']
            tenant_id = port['tenant_id']
            if not tenant_id:
                tenant_id = context._plugin_context.tenant_id
            with self.eos_sync_lock:
                hostname = self._host_name(host)
                segmentation_id = db_lib.get_segmentation_id(tenant_id,
                                                             network_id)
                vm_provisioned = db_lib.is_vm_provisioned(device_id,
                                                          host,
                                                          port_id,
                                                          network_id,
                                                          tenant_id)
                # If network does not exist under this tenant,
                # it may be a shared network. Get shared network owner Id
                net_provisioned = (
                    db_lib.is_network_provisioned(tenant_id, network_id,
                                                  segmentation_id) or
                    self.ndb.get_shared_network_owner_id(network_id)
                )
                if vm_provisioned and net_provisioned:
                    try:
                        orig_host = orig_port['binding:host_id']
                        if host != orig_host:
                            # The port moved to a different host. So delete the
                            # old port on the old host before creating a new
                            # port on the new host.
                            self._delete_port(port, orig_host, tenant_id)
                        self.rpc.plug_port_into_network(device_id,
                                                        hostname,
                                                        port_id,
                                                        network_id,
                                                        tenant_id,
                                                        port_name,
                                                        device_owner)
                    except arista_exc.AristaRpcError:
                        LOG.info(EOS_UNREACHABLE_MSG)
                        raise ml2_exc.MechanismDriverError()
                else:
                    LOG.info(_LI('VM %s is not updated as it is not found in '
                                 'Arista DB'), device_id)
    def test_network_is_remembered(self):
        tenant_id = 'test'
        network_id = '123'
        segmentation_id = 456

        db_lib.remember_network(tenant_id, network_id, segmentation_id)
        net_provisioned = db_lib.is_network_provisioned(tenant_id,
                                                        network_id)
        self.assertTrue(net_provisioned, 'Network must be provisioned')
Esempio n. 23
0
    def update_port_postcommit(self, context):
        """Update the name of a given port in EOS.

        At the moment we only support port name change
        Any other change to port is not supported at this time.
        """
        port = context.current
        orig_port = context.original

        device_id = port['device_id']
        device_owner = port['device_owner']
        host = context.host
        is_vm_boot = device_id and device_owner

        if host and is_vm_boot:
            port_id = port['id']
            port_name = port['name']
            network_id = port['network_id']
            tenant_id = port['tenant_id'] or INTERNAL_TENANT_ID
            with self.eos_sync_lock:
                hostname = self._host_name(host)
                segmentation_id = db_lib.get_segmentation_id(tenant_id,
                                                             network_id)
                vm_provisioned = db_lib.is_vm_provisioned(device_id,
                                                          host,
                                                          port_id,
                                                          network_id,
                                                          tenant_id)
                # If network does not exist under this tenant,
                # it may be a shared network. Get shared network owner Id
                net_provisioned = (
                    db_lib.is_network_provisioned(tenant_id, network_id,
                                                  segmentation_id) or
                    self.ndb.get_shared_network_owner_id(network_id)
                )
                if vm_provisioned and net_provisioned:
                    try:
                        orig_host = orig_port['binding:host_id']
                        if host != orig_host:
                            # The port moved to a different host. So delete the
                            # old port on the old host before creating a new
                            # port on the new host.
                            self._delete_port(port, orig_host, tenant_id)
                        self.rpc.plug_port_into_network(device_id,
                                                        hostname,
                                                        port_id,
                                                        network_id,
                                                        tenant_id,
                                                        port_name,
                                                        device_owner)
                    except arista_exc.AristaRpcError:
                        LOG.info(EOS_UNREACHABLE_MSG)
                        raise ml2_exc.MechanismDriverError()
                else:
                    LOG.info(_LI('VM %s is not updated as it is not found in '
                                 'Arista DB'), device_id)
    def update_port_postcommit(self, context):
        """Update the name of a given port in EOS.

        At the moment we only support port name change
        Any other change to port is not supported at this time.
        """
        port = context.current
        orig_port = context.original

        device_id = port['device_id']
        device_owner = port['device_owner']
        host = context.host
        is_vm_boot = device_id and device_owner

        port_id = port['id']
        port_name = port['name']
        network_id = port['network_id']
        tenant_id = port['tenant_id'] or INTERNAL_TENANT_ID
        with self.eos_sync_lock:
            hostname = self._host_name(host)
            segmentation_id = db_lib.get_segmentation_id(tenant_id,
                                                         network_id)
            port_provisioned = db_lib.is_port_provisioned(port_id)
            # If network does not exist under this tenant,
            # it may be a shared network. Get shared network owner Id
            net_provisioned = (
                db_lib.is_network_provisioned(tenant_id, network_id,
                                              segmentation_id) or
                self.ndb.get_shared_network_owner_id(network_id)
            )
            try:
                orig_host = orig_port['binding:host_id']
                if host != orig_host:
                    try:
                        # The port moved to a different host or the VM
                        # connected to the port was deleted. So delete the
                        # old port on the old host.
                        self._delete_port(orig_port, orig_host, tenant_id)
                    except ml2_exc.MechanismDriverError:
                        # If deleting a port fails, then not much can be done
                        # about it. Log a warning and move on.
                        LOG.warn(UNABLE_TO_DELETE_PORT_MSG)
                if(port_provisioned and net_provisioned and hostname and
                   is_vm_boot):
                    # Plug port into the network only if it exists on a host
                    # and if it has an owner
                    self.rpc.plug_port_into_network(device_id,
                                                    hostname,
                                                    port_id,
                                                    network_id,
                                                    tenant_id,
                                                    port_name,
                                                    device_owner)
            except arista_exc.AristaRpcError:
                LOG.info(EOS_UNREACHABLE_MSG)
                raise ml2_exc.MechanismDriverError()
    def _network_provisioned(self, tenant_id, network_id,
                             segmentation_id=None):
        # If network does not exist under this tenant,
        # it may be a shared network.

        return (
            db_lib.is_network_provisioned(tenant_id, network_id,
                                          segmentation_id) or
            self.ndb.get_shared_network_owner_id(network_id)
        )
    def _network_provisioned(self,
                             tenant_id,
                             network_id,
                             segmentation_id=None,
                             segment_id=None):
        # If network does not exist under this tenant,
        # it may be a shared network.

        return (db_lib.is_network_provisioned(tenant_id, network_id,
                                              segmentation_id, segment_id)
                or self.ndb.get_shared_network_owner_id(network_id))
 def delete_network_precommit(self, context):
     """Delete the network information from the DB."""
     network = context.current
     network_id = network['id']
     tenant_id = network['tenant_id'] or INTERNAL_TENANT_ID
     with self.eos_sync_lock:
         if db_lib.is_network_provisioned(tenant_id, network_id):
             if db_lib.are_ports_attached_to_network(network_id):
                 LOG.info(_LI('Network %s can not be deleted as it '
                              'has ports attached to it'), network_id)
                 raise ml2_exc.MechanismDriverError(
                     method='delete_network_precommit')
             else:
                 db_lib.forget_network(tenant_id, network_id)
 def delete_network_precommit(self, context):
     """Delete the network information from the DB."""
     network = context.current
     network_id = network['id']
     tenant_id = network['tenant_id'] or INTERNAL_TENANT_ID
     with self.eos_sync_lock:
         if db_lib.is_network_provisioned(tenant_id, network_id):
             if db_lib.are_ports_attached_to_network(network_id):
                 LOG.info(
                     _LI('Network %s can not be deleted as it '
                         'has ports attached to it'), network_id)
                 raise ml2_exc.MechanismDriverError(
                     method='delete_network_precommit')
             else:
                 db_lib.forget_network_segment(tenant_id, network_id)
Esempio n. 29
0
    def update_port_postcommit(self, context):
        """Update the name of a given port in EOS.

        At the moment we only support port name change
        Any other change to port is not supported at this time.
        """
        port = context.current
        orig_port = context.original
        if port['name'] == orig_port['name']:
            # nothing to do
            return

        device_id = port['device_id']
        device_owner = port['device_owner']
        host = context.host
        is_vm_boot = device_id and device_owner

        if host and is_vm_boot:
            port_id = port['id']
            port_name = port['name']
            network_id = port['network_id']
            tenant_id = port['tenant_id']
            if not tenant_id:
                tenant_id = context._plugin_context.tenant_id
            with self.eos_sync_lock:
                hostname = self._host_name(host)
                segmentation_id = db_lib.get_segmentation_id(
                    tenant_id, network_id)
                vm_provisioned = db_lib.is_vm_provisioned(
                    device_id, host, port_id, network_id, tenant_id)
                # If network does not exist under this tenant,
                # it may be a shared network. Get shared network owner Id
                net_provisioned = (
                    db_lib.is_network_provisioned(tenant_id, network_id,
                                                  segmentation_id)
                    or self.ndb.get_shared_network_owner_id(network_id))
                if vm_provisioned and net_provisioned:
                    try:
                        self.rpc.plug_port_into_network(
                            device_id, hostname, port_id, network_id,
                            tenant_id, port_name, device_owner)
                    except arista_exc.AristaRpcError:
                        LOG.info(EOS_UNREACHABLE_MSG)
                        raise ml2_exc.MechanismDriverError()
                else:
                    LOG.info(
                        _LI('VM %s is not updated as it is not found in '
                            'Arista DB'), device_id)
    def create_port_postcommit(self, context):
        """Plug a physical host into a network.

        Send provisioning request to Arista Hardware to plug a host
        into appropriate network.
        """
        port = context.current
        device_id = port['device_id']
        device_owner = port['device_owner']
        host = context.host

        # device_id and device_owner are set on VM boot
        is_vm_boot = device_id and device_owner
        if host and is_vm_boot:
            port_id = port['id']
            port_name = port['name']
            network_id = port['network_id']
            tenant_id = port['tenant_id']
            if not tenant_id:
                tenant_id = context._plugin_context.tenant_id
            with self.eos_sync_lock:
                hostname = self._host_name(host)
                vm_provisioned = db_lib.is_vm_provisioned(device_id,
                                                          host,
                                                          port_id,
                                                          network_id,
                                                          tenant_id)
                # If network does not exist under this tenant,
                # it may be a shared network. Get shared network owner Id
                net_provisioned = (
                    db_lib.is_network_provisioned(tenant_id, network_id) or
                    self.ndb.get_shared_network_owner_id(network_id)
                )
                if vm_provisioned and net_provisioned:
                    try:
                        self.rpc.plug_port_into_network(device_id,
                                                        hostname,
                                                        port_id,
                                                        network_id,
                                                        tenant_id,
                                                        port_name,
                                                        device_owner)
                    except arista_exc.AristaRpcError:
                        LOG.info(EOS_UNREACHABLE_MSG)
                        raise ml2_exc.MechanismDriverError()
                else:
                    LOG.info(_LI('VM %s is not created as it is not found in '
                                 'Arista DB'), device_id)
Esempio n. 31
0
    def create_port_postcommit(self, context):
        """Plug a physical host into a network.

        Send provisioning request to Arista Hardware to plug a host
        into appropriate network.
        """
        port = context.current
        device_id = port['device_id']
        device_owner = port['device_owner']
        host = context.host

        # device_id and device_owner are set on VM boot
        is_vm_boot = device_id and device_owner
        if host and is_vm_boot:
            port_id = port['id']
            port_name = port['name']
            network_id = port['network_id']
            tenant_id = port['tenant_id'] or INTERNAL_TENANT_ID
            with self.eos_sync_lock:
                hostname = self._host_name(host)
                vm_provisioned = db_lib.is_vm_provisioned(device_id,
                                                          host,
                                                          port_id,
                                                          network_id,
                                                          tenant_id)
                # If network does not exist under this tenant,
                # it may be a shared network. Get shared network owner Id
                net_provisioned = (
                    db_lib.is_network_provisioned(tenant_id, network_id) or
                    self.ndb.get_shared_network_owner_id(network_id)
                )
                if vm_provisioned and net_provisioned:
                    try:
                        self.rpc.plug_port_into_network(device_id,
                                                        hostname,
                                                        port_id,
                                                        network_id,
                                                        tenant_id,
                                                        port_name,
                                                        device_owner)
                    except arista_exc.AristaRpcError:
                        LOG.info(EOS_UNREACHABLE_MSG)
                        raise ml2_exc.MechanismDriverError()
                else:
                    LOG.info(_LI('VM %s is not created as it is not found in '
                                 'Arista DB'), device_id)
    def _delete_port(self, port, host, tenant_id):
        """Deletes the port from EOS.

        param port: Port which is to be deleted
        param host: The host on which the port existed
        param tenant_id: The tenant to which the port belongs to. Some times
                         the tenant id in the port dict is not present (as in
                         the case of HA router).
        """
        device_id = port['device_id']
        port_id = port['id']
        network_id = port['network_id']
        device_owner = port['device_owner']

        if not device_id or not host:
            LOG.warn(UNABLE_TO_DELETE_DEVICE_MSG)
            return

        try:
            # If network does not exist under this tenant,
            # it may be a shared network. Get shared network owner Id
            net_provisioned = (
                db_lib.is_network_provisioned(tenant_id, network_id) or
                self.ndb.get_shared_network_owner_id(network_id)
            )
            if not net_provisioned:
                # If we do not have network associated with this, ignore it
                return
            hostname = self._host_name(host)
            if device_owner == n_const.DEVICE_OWNER_DHCP:
                self.rpc.unplug_dhcp_port_from_network(device_id,
                                                       hostname,
                                                       port_id,
                                                       network_id,
                                                       tenant_id)
            else:
                self.rpc.unplug_host_from_network(device_id,
                                                  hostname,
                                                  port_id,
                                                  network_id,
                                                  tenant_id)
            # if necessary, delete tenant as well.
            self.delete_tenant(tenant_id)
        except arista_exc.AristaRpcError:
            LOG.info(EOS_UNREACHABLE_MSG)
            raise ml2_exc.MechanismDriverError()
    def update_network_postcommit(self, context):
        """At the moment we only support network name change

        If network name is changed, a new network create request is
        sent to the Arista Hardware.
        """
        new_network = context.current
        orig_network = context.original
        if ((new_network['name'] != orig_network['name'])
                or (new_network['shared'] != orig_network['shared'])):
            network_id = new_network['id']
            network_name = new_network['name']
            tenant_id = new_network['tenant_id'] or INTERNAL_TENANT_ID
            shared_net = new_network['shared']
            with self.eos_sync_lock:
                if db_lib.is_network_provisioned(tenant_id, network_id):
                    try:
                        network_dict = {
                            'network_id': network_id,
                            'segments': context.network_segments,
                            'network_name': network_name,
                            'shared': shared_net
                        }
                        self.rpc.create_network(tenant_id, network_dict)
                    except arista_exc.AristaRpcError as err:
                        LOG.error(
                            _LE('update_network_postcommit: Did not '
                                'update network %(name)s. '
                                'Reason: %(err)s'), {
                                    'name': network_name,
                                    'err': err
                                })
                else:
                    LOG.info(
                        _LI('Network %s is not updated as it is not found'
                            ' in Arista DB'), network_id)