def test_get_vswitch_map(self, mock_read):
     self._mock_feed(self.vswitch_resp)
     resp = utils.get_vswitch_map(self.adpt, 'host_uuid')
     self.assertEqual('https://9.1.2.3:12443/rest/api/uom/ManagedSystem/'
                      'c5d782c7-44e4-3086-ad15-b16fb039d63b/VirtualSwitch/'
                      'e1a852cb-2be5-3a51-9147-43761bc3d720',
                      resp[0])
     mock_read.assert_called_once_with('ManagedSystem',
                                       child_type='VirtualSwitch',
                                       root_id='host_uuid')
Example #2
0
 def test_get_vswitch_map(self, mock_read):
     self._mock_feed(self.vswitch_resp)
     resp = utils.get_vswitch_map(self.adpt, 'host_uuid')
     self.assertEqual(
         'https://9.1.2.3:12443/rest/api/uom/ManagedSystem/'
         'c5d782c7-44e4-3086-ad15-b16fb039d63b/VirtualSwitch/'
         'e1a852cb-2be5-3a51-9147-43761bc3d720', resp[0])
     mock_read.assert_called_once_with('ManagedSystem',
                                       child_type='VirtualSwitch',
                                       root_id='host_uuid')
    def test_get_vswitch_map(self, mock_wrap):
        # Create mocks
        mock_adpt = mock.Mock()
        mock_adpt.read = mock.Mock()
        vswitch = mock.Mock()
        vswitch.related_href = 'http://9.1.2.3/test'
        vswitch.switch_id = 0
        mock_wrap.return_value = [vswitch]

        # Run test and verify results
        resp = utils.get_vswitch_map(mock_adpt, 'host_uuid')
        self.assertEqual('http://9.1.2.3/test', resp[0])
        mock_adpt.read.assert_called_once_with('ManagedSystem',
                                               child_type='VirtualSwitch',
                                               root_id='host_uuid')
Example #4
0
    def test_get_vswitch_map(self, mock_get):
        # Create mocks
        mock_get.return_value = [
            mock.Mock(related_href='http://9.1.2.3/test', switch_id=0),
            mock.Mock(related_href='http://9.4.5.6/test', switch_id=5)
        ]

        # Run test and verify results
        self.assertEqual({
            0: 'http://9.1.2.3/test',
            5: 'http://9.4.5.6/test'
        }, utils.get_vswitch_map('adpt', 'host_uuid'))
        mock_get.assert_called_once_with('adpt',
                                         parent_type=pvm_ms.System,
                                         parent_uuid='host_uuid')
    def test_find_nb_for_cna(self):
        self._mock_feed(self.vswitch_resp)

        nb_wraps = pvm_net.NetBridge.wrap(self.net_br_resp)

        mock_client_adpt = mock.MagicMock()
        mock_client_adpt.vswitch_uri = ('https://9.1.2.3:12443/rest/api/uom/'
                                        'ManagedSystem/'
                                        'c5d782c7-44e4-3086-ad15-b16fb039d63b/'
                                        'VirtualSwitch/'
                                        'e1a852cb-2be5-3a51-9147-43761bc3d720')

        vswitch_map = utils.get_vswitch_map(self.adpt, 'host_uuid')

        # Should have a proper URI, so it should match
        resp = utils.find_nb_for_cna(nb_wraps, mock_client_adpt, vswitch_map)
        self.assertIsNotNone(resp)

        # Should not match if we change the vswitch URI
        mock_client_adpt.vswitch_uri = "Fake"
        resp = utils.find_nb_for_cna(nb_wraps, mock_client_adpt, vswitch_map)
        self.assertIsNone(resp)
Example #6
0
    def test_find_nb_for_cna(self):
        self._mock_feed(self.vswitch_resp)

        nb_wraps = pvm_net.NetBridge.wrap(self.net_br_resp)

        mock_client_adpt = mock.MagicMock()
        mock_client_adpt.vswitch_uri = ('https://9.1.2.3:12443/rest/api/uom/'
                                        'ManagedSystem/'
                                        'c5d782c7-44e4-3086-ad15-b16fb039d63b/'
                                        'VirtualSwitch/'
                                        'e1a852cb-2be5-3a51-9147-43761bc3d720')

        vswitch_map = utils.get_vswitch_map(self.adpt, 'host_uuid')

        # Should have a proper URI, so it should match
        resp = utils.find_nb_for_cna(nb_wraps, mock_client_adpt, vswitch_map)
        self.assertIsNotNone(resp)

        # Should not match if we change the vswitch URI
        mock_client_adpt.vswitch_uri = "Fake"
        resp = utils.find_nb_for_cna(nb_wraps, mock_client_adpt, vswitch_map)
        self.assertIsNone(resp)
Example #7
0
    def heal_and_optimize(self, is_boot):
        """Heals the system's network bridges and optimizes.

        Will query neutron for all the ports in use on this host.  Ensures that
        all of the VLANs needed for those ports are available on the correct
        network bridge.

        Finally, it optimizes the system by removing any VLANs that may no
        longer be required.  The VLANs that are removed must meet the following
        conditions:
         - Are not in use by ANY virtual machines on the system.  OpenStack
           managed or not.
         - Are not part of the primary load group on the Network Bridge.

        :param is_boot: Indicates if this is the first call on boot up of the
                        agent.
        """
        # List all our clients
        client_adpts = utils.list_cnas(self.adapter, self.host_uuid)

        # Get all the devices that Neutron knows for this host.  Note that
        # we pass in all of the macs on the system.  For VMs that neutron does
        # not know about, we get back an empty structure with just the mac.
        client_macs = [utils.norm_mac(x.mac) for x in client_adpts]
        devs = self.get_devices_details_list(client_macs)

        # Dictionary of the required VLANs on the Network Bridge
        nb_req_vlans = {}
        nb_wraps = utils.list_bridges(self.adapter, self.host_uuid)
        for nb_wrap in nb_wraps:
            nb_req_vlans[nb_wrap.uuid] = set()

        for dev in devs:
            nb_uuid, req_vlan = self._get_nb_and_vlan(dev, emit_warnings=False)

            # This can happen for ports that are on the host, but not in
            # Neutron.
            if nb_uuid is None or req_vlan is None:
                continue

            # If that list does not contain my VLAN, add it
            nb_req_vlans[nb_uuid].add(req_vlan)

        # Lets ensure that all VLANs for the openstack VMs are on the network
        # bridges.
        for nb_uuid in nb_req_vlans.keys():
            net_br.ensure_vlans_on_nb(self.adapter, self.host_uuid, nb_uuid,
                                      nb_req_vlans[nb_uuid])

        # We should clean up old VLANs as well.  However, we only want to clean
        # up old VLANs that are not in use by ANYTHING in the system.
        #
        # The first step is to identify the VLANs that are needed.  That can
        # be done by extending our nb_req_vlans map.
        #
        # We first extend that map by listing all the VMs on the system
        # (whether managed by OpenStack or not) and then seeing what Network
        # Bridge uses them.
        vswitch_map = utils.get_vswitch_map(self.adapter, self.host_uuid)
        for client_adpt in client_adpts:
            nb = utils.find_nb_for_cna(nb_wraps, client_adpt, vswitch_map)
            # Could occur if a system is internal only.
            if nb is None:
                LOG.debug("Client Adapter with mac %s is internal only.",
                          client_adpt.mac)
                continue

            # Make sure that it is on the nb_req_vlans list, as it is now
            # considered required.
            nb_req_vlans[nb.uuid].add(client_adpt.pvid)

            # Extend for each additional vlans as well
            for addl_vlan in client_adpt.tagged_vlans:
                nb_req_vlans[nb.uuid].add(addl_vlan)

        # We will have a list of CNAs that are not yet created, but are pending
        # provisioning from Nova.  Keep track of those so that we don't tear
        # those off the SEA.
        pending_vlans = self.pvid_updater.pending_vlans

        # The list of required VLANs on each network bridge also includes
        # everything on the primary VEA.
        for nb in nb_wraps:
            prim_ld_grp = nb.load_grps[0]
            vlans = [prim_ld_grp.pvid]
            vlans.extend(prim_ld_grp.tagged_vlans)
            for vlan in vlans:
                nb_req_vlans[nb.uuid].add(vlan)

        # If the configuration is set.
        if ACONF.automated_powervm_vlan_cleanup:
            # Loop through and remove VLANs that are no longer needed.
            for nb in nb_wraps:
                # Join the required vlans on the network bridge (already in
                # use) with the pending VLANs.
                req_vlans = nb_req_vlans[nb.uuid] | pending_vlans

                # Get ALL the VLANs on the bridge
                existing_vlans = set(nb.list_vlans())

                # To determine the ones no longer needed, subtract from all the
                # VLANs the ones that are no longer needed.
                vlans_to_del = existing_vlans - req_vlans
                for vlan_to_del in vlans_to_del:
                    LOG.warn(_LW("Cleaning up VLAN %(vlan)s from the system.  "
                                 "It is no longer in use."),
                             {'vlan': vlan_to_del})
                    net_br.remove_vlan_from_nb(self.adapter, self.host_uuid,
                                               nb.uuid, vlan_to_del)
    def heal_and_optimize(self, is_boot, prov_reqs, lpar_uuids, overall_cnas):
        """Heals the system's network bridges and optimizes.

        Will query neutron for all the ports in use on this host.  Ensures that
        all of the VLANs needed for those ports are available on the correct
        network bridge.

        Finally, it optimizes the system by removing any VLANs that may no
        longer be required.  The VLANs that are removed must meet the following
        conditions:
         - Are not in use by ANY virtual machines on the system.  OpenStack
           managed or not.
         - Are not part of the primary load group on the Network Bridge.

        :param is_boot: Indicates if this is the first call on boot up of the
                        agent.
        :param prov_reqs: A list of ProvisionRequest objects that represent
                          the Neutron ports that should exist on this system.
                          It may include ports that have already been
                          provisioned.  This method should make sure it calls
                          update_device_up/down afterwards.
        :param lpar_uuids: A list of the VM UUIDs for the REST API.
        :param overall_cnas: A list of the systems Client Network Adapters.
        """
        # Dictionary of the required VLANs on the Network Bridge
        nb_req_vlans = {}
        nb_wraps = utils.list_bridges(self.adapter, self.host_uuid)
        for nb_wrap in nb_wraps:
            nb_req_vlans[nb_wrap.uuid] = set()

        # Call down to the provision.  This will call device up on the
        # requests.
        self.provision_devices(prov_reqs)

        # Make sure that the provision requests VLAN is captured in the
        # nb_req_vlans list...so that the VLAN is not accidentally removed.
        for req in prov_reqs:
            nb_uuid, req_vlan = self._get_nb_and_vlan(req, emit_warnings=False)
            nb_req_vlans[nb_uuid].add(req_vlan)

        # We should clean up old VLANs as well.  However, we only want to clean
        # up old VLANs that are not in use by ANYTHING in the system.
        #
        # The first step is to identify the VLANs that are needed.  That can
        # be done by extending our nb_req_vlans map.
        #
        # We first extend that map by listing all the VMs on the system
        # (whether managed by OpenStack or not) and then seeing what Network
        # Bridge uses them.
        vswitch_map = utils.get_vswitch_map(self.adapter, self.host_uuid)
        for client_adpt in overall_cnas:
            nb = utils.find_nb_for_cna(nb_wraps, client_adpt, vswitch_map)
            # Could occur if a system is internal only.
            if nb is None:
                LOG.debug("Client Adapter with mac %s is internal only.",
                          client_adpt.mac)
                continue

            # Make sure that it is on the nb_req_vlans list, as it is now
            # considered required.
            nb_req_vlans[nb.uuid].add(client_adpt.pvid)

            # Extend for each additional vlans as well
            for addl_vlan in client_adpt.tagged_vlans:
                nb_req_vlans[nb.uuid].add(addl_vlan)

        # We will have a list of CNAs that are not yet created, but are pending
        # provisioning from Nova.  Keep track of those so that we don't tear
        # those off the SEA.
        pending_vlans = self.pvid_updater.pending_vlans

        # The list of required VLANs on each network bridge also includes
        # everything on the primary VEA.
        for nb in nb_wraps:
            prim_ld_grp = nb.load_grps[0]
            vlans = [prim_ld_grp.pvid]
            vlans.extend(prim_ld_grp.tagged_vlans)
            for vlan in vlans:
                nb_req_vlans[nb.uuid].add(vlan)

        # If the configuration is set.
        if ACONF.automated_powervm_vlan_cleanup:
            # Loop through and remove VLANs that are no longer needed.
            for nb in nb_wraps:
                # Join the required vlans on the network bridge (already in
                # use) with the pending VLANs.
                req_vlans = nb_req_vlans[nb.uuid] | pending_vlans

                # Get ALL the VLANs on the bridge
                existing_vlans = set(nb.list_vlans())

                # To determine the ones no longer needed, subtract from all the
                # VLANs the ones that are no longer needed.
                vlans_to_del = existing_vlans - req_vlans
                for vlan_to_del in vlans_to_del:
                    LOG.warn(_LW("Cleaning up VLAN %(vlan)s from the system.  "
                                 "It is no longer in use."),
                             {'vlan': vlan_to_del})
                    net_br.remove_vlan_from_nb(self.adapter, self.host_uuid,
                                               nb.uuid, vlan_to_del)
Example #9
0
    def heal_and_optimize(self):
        """Heals the system's network bridges and optimizes.

        Will query neutron for all the ports in use on this host.  Ensures that
        all of the VLANs needed for those ports are available on the correct
        network bridge.

        Finally, it optimizes the system by removing any VLANs that may no
        longer be required.  The VLANs that are removed must meet the following
        conditions:
         - Are not in use by ANY virtual machines on the system.  OpenStack
           managed or not.
         - Are not part of the primary load group on the Network Bridge.
        """
        LOG.info("Running the heal and optimize flow.")

        # Get a map of all the partitions and their CNAs.
        all_lpar_cnas = utils.list_vifs(self.adapter,
                                        self.vif_wrapper_class,
                                        include_vios_and_mgmt=True)
        # There can be CNAs (non-trunk) on VIOSes as well.  They should be
        # taken into account for the overall vifs.
        overall_vifs = [vif for vifs in all_lpar_cnas.values() for vif in vifs]
        # The lpar_cna_map includes only the client VMs.  Don't include mgmt.
        lpar_cna_map = {
            par_w: vifs
            for par_w, vifs in all_lpar_cnas.items()
            if isinstance(par_w, pvm_lpar.LPAR) and not par_w.is_mgmt_partition
        }

        # Build out all of the devices that we have available to us.  Some
        # may be on the system but not part of OpenStack.  Those get ignored.
        prov_reqs = preq.ProvisionRequest.for_wrappers(self, lpar_cna_map,
                                                       preq.PLUG)
        # Dictionary of the required VLANs on the Network Bridge
        nb_req_vlans = {}
        nb_wraps = utils.list_bridges(self.adapter, self.host_uuid)
        for nb_wrap in nb_wraps:
            nb_req_vlans[nb_wrap.uuid] = set()

        # Call down to the provision.  This will call device up on the
        # requests.
        self.provision_devices(prov_reqs)

        # Make sure that the provision requests VLAN is captured in the
        # nb_req_vlans list...so that the VLAN is not accidentally removed.
        for req in prov_reqs:
            nb_uuid, req_vlan = self._get_nb_and_vlan(req.rpc_device,
                                                      emit_warnings=False)
            nb_req_vlans[nb_uuid].add(req_vlan)

        # We should clean up old VLANs as well.  However, we only want to clean
        # up old VLANs that are not in use by ANYTHING in the system.
        #
        # The first step is to identify the VLANs that are needed.  That can
        # be done by extending our nb_req_vlans map.
        #
        # We first extend that map by listing all the VMs on the system
        # (whether managed by OpenStack or not) and then seeing what Network
        # Bridge uses them.
        vswitch_map = utils.get_vswitch_map(self.adapter, self.host_uuid)
        for client_adpt in overall_vifs:
            nb = utils.find_nb_for_cna(nb_wraps, client_adpt, vswitch_map)
            # Could occur if a system is internal only.
            if nb is None:
                LOG.debug("Client Adapter with mac %s is internal only.",
                          client_adpt.mac)
                continue

            # Make sure that it is on the nb_req_vlans list, as it is now
            # considered required.
            nb_req_vlans[nb.uuid].add(client_adpt.pvid)

            # Extend for each additional vlans as well
            for addl_vlan in client_adpt.tagged_vlans:
                nb_req_vlans[nb.uuid].add(addl_vlan)

        # The list of required VLANs on each network bridge also includes
        # everything on the primary VEA.
        for nb in nb_wraps:
            prim_ld_grp = nb.load_grps[0]
            vlans = [prim_ld_grp.pvid]
            vlans.extend(prim_ld_grp.tagged_vlans)
            for vlan in vlans:
                nb_req_vlans[nb.uuid].add(vlan)

        # If the configuration is set.
        if ACONF.automated_powervm_vlan_cleanup:
            self._cleanup_unused_vlans(nb_wraps, nb_req_vlans)