def create_oneview_networks_from_neutron(self):
        LOG.info("Synchronizing Neutron networks not in OneView.")
        session = common.get_database_session()
        for network, network_segment in (
                database_manager.list_networks_and_segments_with_physnet(
                    session)):
            net_id = network.get('id')
            neutron_oneview_network = (
                database_manager.get_neutron_oneview_network(
                    session, net_id))
            if neutron_oneview_network:
                oneview_network = self.get_oneview_network(
                    neutron_oneview_network.oneview_network_id
                )
                if not oneview_network:
                    common.remove_inconsistence_from_db(
                        session,
                        neutron_oneview_network.neutron_network_id,
                        neutron_oneview_network.oneview_network_id
                    )
                else:
                    continue

            physical_network = network_segment.get('physical_network')
            network_type = network_segment.get('network_type')
            segmentation_id = network_segment.get('segmentation_id')
            network_dict = common.network_dict_for_network_creation(
                physical_network, network_type, net_id, segmentation_id
            )

            self.neutron_client.network.create(session, network_dict)
 def delete_unmapped_oneview_networks(self):
     LOG.info("Synchronizing outdated networks in OneView.")
     session = common.get_database_session()
     for network in self.oneview_client.ethernet_networks.get_all():
         mmanaged_network = re.search(r'Neutron \[(.*)\]', network.get(
             'name'))
         if mmanaged_network:
             oneview_network_id = common.id_from_uri(network.get('uri'))
             neutron_network_id = mmanaged_network.group(1)
             neutron_network = database_manager.get_neutron_network(
                 session, neutron_network_id
             )
             network_segment = database_manager.get_network_segment(
                 session, neutron_network_id
             )
             if not neutron_network:
                 self.oneview_client.ethernet_networks.delete(
                     oneview_network_id
                 )
                 common.remove_inconsistence_from_db(
                     session, neutron_network_id, oneview_network_id
                 )
             # NOTE(nicodemos) network_segment will always exists?
             # NOTE(mrtenio) network_segments are created by Neutron when
             #  a Network is created. I think we can assume they always
             #  exist
             else:
                 physnet = network_segment.get('physical_network')
                 network_type = network_segment.get('network_type')
                 if not self.neutron_client.network.is_uplinkset_mapping(
                         physnet, network_type):
                     self._delete_connections(neutron_network_id)
                     self.neutron_client.network.delete(
                         session, {'id': neutron_network_id}
                     )
 def synchronize_uplinkset_from_mapped_networks(self):
     LOG.info("Synchronizing OneView uplinksets.")
     session = common.get_database_session()
     for neutron_oneview_network in (
             database_manager.list_neutron_oneview_network(session)):
         oneview_network_id = neutron_oneview_network.oneview_network_id
         neutron_network_id = neutron_oneview_network.neutron_network_id
         network_segment = database_manager.get_network_segment(
             session, neutron_network_id
         )
         if network_segment:
             self.neutron_client.network.update_network_lig(
                 session, oneview_network_id, network_segment.get(
                     'network_type'), network_segment.get(
                         'physical_network'))
    def recreate_connection(self):
        """Recreate connection that were deleted on Oneview.

        Calls method to fix critical connections in the Server Profile that
        will be used.
        """
        LOG.info("Synchronizing connections in OneView Server Profiles.")
        session = common.get_database_session()

        for port, port_binding in (
                database_manager.get_port_with_binding_profile(session)):
            port_dict = common.port_dict_for_port_creation(
                port.get('network_id'),
                port_binding.get('vnic_type'),
                port.get('mac_address'),
                jsonutils.loads(port_binding.get('profile'))
            )
            local_link_info = common.local_link_information_from_port(
                port_dict)
            server_hardware_id = (
                common.server_hardware_from_local_link_information_list(
                    self.oneview_client, local_link_info))
            server_profile = (
                self.neutron_client.port.server_profile_from_server_hardware(
                    server_hardware_id
                )
            )
            neutron_oneview_network = (
                database_manager.list_neutron_oneview_network(
                    session, neutron_network_id=port.get('network_id')))
            connection_updated = False
            if neutron_oneview_network:
                oneview_uri = "/rest/ethernet-networks/" + (
                    neutron_oneview_network[0].oneview_network_id
                )
                self._fix_connections_with_removed_networks(
                    server_profile
                )
                for c in server_profile.get('connections'):
                    if c.get('mac') == port.get('mac_address'):
                        connection_updated = True
                        if c.get('networkUri') != oneview_uri:
                            self._update_connection(
                                oneview_uri, server_profile, c)
            if not connection_updated:
                self.neutron_client.port.create(session, port_dict)
    def _delete_connections(self, neutron_network_id):
        session = common.get_database_session()
        for port, port_binding in (
                database_manager.get_port_with_binding_profile_by_net(
                    session, neutron_network_id)):
            port_dict = common.port_dict_for_port_creation(
                port.get('network_id'), port_binding.get('vnic_type'),
                port.get('mac_address'),
                jsonutils.loads(port_binding.get('profile'))
            )
            local_link_info = common.local_link_information_from_port(
                port_dict)
            server_hardware = (
                common.server_hardware_from_local_link_information_list(
                    self.oneview_client, local_link_info))

            server_profile = (
                self.neutron_client.port.server_profile_from_server_hardware(
                    server_hardware
                )
            )

            self.neutron_client.port.check_server_hardware_availability(
                server_hardware
            )
            previous_power_state = (
                self.neutron_client.port.get_server_hardware_power_state(
                    server_hardware
                )
            )

            self.neutron_client.port.update_server_hardware_power_state(
                server_hardware, "Off")

            for connection in server_profile.get('connections'):
                if connection.get('mac') == port.get('mac_address'):
                    self._remove_connection(
                        server_profile, connection.get('id')
                    )
            self.neutron_client.port.update_server_hardware_power_state(
                server_hardware, previous_power_state
            )