<commit_before>package io.subutai.core.registration.impl;


import java.sql.Timestamp;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import javax.annotation.security.RolesAllowed;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.commons.lang3.StringUtils;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;

import io.subutai.common.dao.DaoManager;
import io.subutai.common.host.ContainerHostInfo;
import io.subutai.common.host.ContainerHostState;
import io.subutai.common.host.HostInfo;
import io.subutai.common.host.HostInterfaceModel;
import io.subutai.common.host.ResourceHostInfo;
import io.subutai.common.metric.QuotaAlertValue;
import io.subutai.common.peer.LocalPeer;
import io.subutai.common.security.crypto.pgp.PGPKeyUtil;
import io.subutai.common.security.objects.SecurityKeyType;
import io.subutai.common.settings.Common;
import io.subutai.common.util.ServiceLocator;
import io.subutai.core.hostregistry.api.HostListener;
import io.subutai.core.registration.api.HostRegistrationManager;
import io.subutai.core.registration.api.ResourceHostRegistrationStatus;
import io.subutai.core.registration.api.exception.HostRegistrationException;
import io.subutai.core.registration.api.service.ContainerInfo;
import io.subutai.core.registration.api.service.ContainerToken;
import io.subutai.core.registration.api.service.RequestedHost;
import io.subutai.core.registration.impl.dao.ContainerTokenDataService;
import io.subutai.core.registration.impl.dao.RequestDataService;
import io.subutai.core.registration.impl.entity.ContainerTokenImpl;
import io.subutai.core.registration.impl.entity.RequestedHostImpl;
import io.subutai.core.security.api.SecurityManager;
import io.subutai.core.security.api.crypto.KeyManager;


//TODO add security annotation
public class HostRegistrationManagerImpl implements HostRegistrationManager, HostListener
{
    private static final Logger LOG = LoggerFactory.getLogger( HostRegistrationManagerImpl.class );
    private SecurityManager securityManager;
    protected RequestDataService requestDataService;
    protected ContainerTokenDataService containerTokenDataService;
    private DaoManager daoManager;
    protected ServiceLocator serviceLocator = new ServiceLocator();


    public HostRegistrationManagerImpl( final SecurityManager securityManager, final DaoManager daoManager )
    {
        Preconditions.checkNotNull( securityManager );
        Preconditions.checkNotNull( daoManager );

        this.securityManager = securityManager;
        this.daoManager = daoManager;
    }


    public void init()
    {
        containerTokenDataService = new ContainerTokenDataService( daoManager );
        requestDataService = new RequestDataService( daoManager );
    }


    protected RequestDataService getRequestDataService()
    {
        return requestDataService;
    }


    @Override
    public void changeRhHostname( final String rhId, String hostname ) throws HostRegistrationException
    {
        try
        {
            LocalPeer localPeer = serviceLocator.getService( LocalPeer.class );

            localPeer.setRhHostname( rhId, hostname );
        }
        catch ( Exception e )
        {
            LOG.error( "Error changing RH hostname", e );

            throw new HostRegistrationException( e );
        }
    }


    @Override
    public List<RequestedHost> getRequests()
    {
        List<RequestedHost> requests = Lists.newArrayList();

        requests.addAll( requestDataService.getAll() );

        return requests;
    }


    @Override
    public RequestedHost getRequest( final String requestId )
    {
        return requestDataService.find( requestId );
    }


    @RolesAllowed( "Resource-Management|Write" )
    @Override
    public synchronized void queueRequest( final RequestedHost requestedHost ) throws HostRegistrationException
    {
        Preconditions.checkNotNull( requestedHost, "'Invalid registration request" );
        Preconditions
                .checkArgument( PGPKeyUtil.isValidPublicKeyring( requestedHost.getPublicKey() ), "Invalid public key" );

        Preconditions.checkArgument( !StringUtils.isBlank( requestedHost.getId() ), "invalid host id" );

        try
        {
            RequestedHostImpl requestedHostImpl = requestDataService.find( requestedHost.getId() );

            if ( requestedHostImpl != null )
            {
                LOG.info( "Already requested registration" );

                //update hostname

                if ( !Strings.isNullOrEmpty( requestedHost.getHostname() ) && !requestedHostImpl.getHostname()
                                                                                                .equalsIgnoreCase(
                                                                                                        requestedHost
                                                                                                                .getHostname() ) )
                {
                    requestedHostImpl.setHostname( requestedHost.getHostname() );

                    requestDataService.update( requestedHostImpl );
                }
            }
            else
            {
                RequestedHostImpl registrationRequest = new RequestedHostImpl( requestedHost );
<commit_msg>fix duplicate RH record<commit_after>package io.subutai.core.registration.impl;


import java.sql.Timestamp;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import javax.annotation.security.RolesAllowed;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.commons.lang3.StringUtils;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;

import io.subutai.common.dao.DaoManager;
import io.subutai.common.host.ContainerHostInfo;
import io.subutai.common.host.ContainerHostState;
import io.subutai.common.host.HostInfo;
import io.subutai.common.host.HostInterfaceModel;
import io.subutai.common.host.ResourceHostInfo;
import io.subutai.common.metric.QuotaAlertValue;
import io.subutai.common.peer.LocalPeer;
import io.subutai.common.security.crypto.pgp.PGPKeyUtil;
import io.subutai.common.security.objects.SecurityKeyType;
import io.subutai.common.settings.Common;
import io.subutai.common.util.ServiceLocator;
import io.subutai.core.hostregistry.api.HostListener;
import io.subutai.core.registration.api.HostRegistrationManager;
import io.subutai.core.registration.api.ResourceHostRegistrationStatus;
import io.subutai.core.registration.api.exception.HostRegistrationException;
import io.subutai.core.registration.api.service.ContainerInfo;
import io.subutai.core.registration.api.service.ContainerToken;
import io.subutai.core.registration.api.service.RequestedHost;
import io.subutai.core.registration.impl.dao.ContainerTokenDataService;
import io.subutai.core.registration.impl.dao.RequestDataService;
import io.subutai.core.registration.impl.entity.ContainerTokenImpl;
import io.subutai.core.registration.impl.entity.RequestedHostImpl;
import io.subutai.core.security.api.SecurityManager;
import io.subutai.core.security.api.crypto.KeyManager;


//TODO add security annotation
public class HostRegistrationManagerImpl implements HostRegistrationManager, HostListener
{
    private static final Logger LOG = LoggerFactory.getLogger( HostRegistrationManagerImpl.class );
    private SecurityManager securityManager;
    protected RequestDataService requestDataService;
    protected ContainerTokenDataService containerTokenDataService;
    private DaoManager daoManager;
    protected ServiceLocator serviceLocator = new ServiceLocator();


    public HostRegistrationManagerImpl( final SecurityManager securityManager, final DaoManager daoManager )
    {
        Preconditions.checkNotNull( securityManager );
        Preconditions.checkNotNull( daoManager );

        this.securityManager = securityManager;
        this.daoManager = daoManager;
    }


    public void init()
    {
        containerTokenDataService = new ContainerTokenDataService( daoManager );
        requestDataService = new RequestDataService( daoManager );
    }


    protected RequestDataService getRequestDataService()
    {
        return requestDataService;
    }


    @Override
    public void changeRhHostname( final String rhId, String hostname ) throws HostRegistrationException
    {
        try
        {
            LocalPeer localPeer = serviceLocator.getService( LocalPeer.class );

            localPeer.setRhHostname( rhId, hostname );
        }
        catch ( Exception e )
        {
            LOG.error( "Error changing RH hostname", e );

            throw new HostRegistrationException( e );
        }
    }


    @Override
    public List<RequestedHost> getRequests()
    {
        List<RequestedHost> requests = Lists.newArrayList();

        requests.addAll( requestDataService.getAll() );

        return requests;
    }


    @Override
    public RequestedHost getRequest( final String requestId )
    {
        return requestDataService.find( requestId );
    }


    @RolesAllowed( "Resource-Management|Write" )
    @Override
    public synchronized void queueRequest( final RequestedHost requestedHost ) throws HostRegistrationException
    {
        Preconditions.checkNotNull( requestedHost, "'Invalid registration request" );
        Preconditions
                .checkArgument( PGPKeyUtil.isValidPublicKeyring( requestedHost.getPublicKey() ), "Invalid public key" );

        Preconditions.checkArgument( !StringUtils.isBlank( requestedHost.getId() ), "Invalid host id" );

        try
        {
            RequestedHostImpl requestedHostImpl = requestDataService.find( requestedHost.getId() );

            if ( requestedHostImpl != null )
            {
                LOG.info( "Already requested registration" );

                //update hostname

                if ( !Strings.isNullOrEmpty( requestedHost.getHostname() ) && !requestedHostImpl.getHostname()
                                                                                                .equalsIgnoreCase(
                                                                                                        requestedHost
                                                                                                                .getHostname() ) )
                {
                    requestedHostImpl.setHostname( requestedHost.getHostname() );

                    requestDataService.update( requestedHostImpl );
                }
            }
            else
            {
                RequestedHostImpl registrationRequest = new RequestedHostImpl( requestedHost );

{'fraction_non_alphanumeric': 0.07912332838038633, 'fraction_numerical': 0.0005572065378900445, 'mean_word_length': 2.736641221374046, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6006193', 'n_tokens_mistral': 2773, 'n_tokens_neox': 2691, 'n_words': 511}