<commit_before>package io.subutai.core.hubmanager.impl.tunnel;


import java.util.Set;

import javax.ws.rs.core.Response;

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

import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.http.HttpStatus;

import io.subutai.common.command.CommandException;
import io.subutai.common.command.CommandResult;
import io.subutai.common.command.RequestBuilder;
import io.subutai.common.peer.ResourceHost;
import io.subutai.core.hubmanager.api.HubPluginException;
import io.subutai.core.hubmanager.api.StateLinkProccessor;
import io.subutai.core.hubmanager.impl.ConfigManager;
import io.subutai.core.peer.api.PeerManager;
import io.subutai.hub.share.dto.TunnelInfoDto;
import io.subutai.hub.share.json.JsonUtil;

//TODO close web clients and responses
public class TunnelProcessor implements StateLinkProccessor
{

    private final Logger LOG = LoggerFactory.getLogger( getClass() );

    private static final String TUNNEL_COMMAND = "subutai tunnel %s:%s %s -g";

    private PeerManager peerManager;

    private ConfigManager configManager;


    public TunnelProcessor( PeerManager peerManager, ConfigManager configManager )
    {
        this.peerManager = peerManager;
        this.configManager = configManager;
    }


    @Override
    public void processStateLinks( final Set<String> stateLinks ) throws HubPluginException
    {
        for ( String stateLink : stateLinks )
        {
            if ( !stateLink.contains( "tunnel" ) )
            {
                return;
            }
            processLink( stateLink );
        }
    }


    private void processLink( String stateLink )
    {
        TunnelInfoDto tunnelInfoDto = getData( stateLink );
        ResourceHost resourceHost = null;
        try
        {
            resourceHost = peerManager.getLocalPeer().getManagementHost();

            CommandResult result = execute( resourceHost,
                    String.format( TUNNEL_COMMAND, tunnelInfoDto.getIp(), tunnelInfoDto.getPortToOpen(),
                            tunnelInfoDto.getTtl() ) );

            if ( result.hasSucceeded() )
            {
                String[] data = result.getStdOut().split( ":" );
                tunnelInfoDto.setOpenedIp( data[0] );
                tunnelInfoDto.setOpenedPort( data[1] );

                updateTunnelStatus( stateLink, tunnelInfoDto );
            }
        }
        catch ( Exception e )
        {
            e.printStackTrace();
        }
    }


    private void updateTunnelStatus( String link, TunnelInfoDto tunnelInfoDto )
    {
        try
        {
            WebClient client = configManager.getTrustedWebClientWithAuth( link, configManager.getHubIp() );
            byte[] cborData = JsonUtil.toCbor( tunnelInfoDto );
            byte[] encryptedData = configManager.getMessenger().produce( cborData );
            Response r = client.put( encryptedData );

            if ( r.getStatus() == HttpStatus.SC_OK )
            {
                String mgs = "Tunnel peer data successfully sent to hub";
                LOG.debug( mgs );
            }
        }
        catch ( Exception e )
        {
            String mgs = "Could not sent tunnel peer data to hub.";
            LOG.error( mgs, e.getMessage() );
        }
    }


    private CommandResult execute( ResourceHost resourceHost, String cmd )
    {
        boolean exec = true;
        int tryCount = 0;
        CommandResult result = null;

        while ( exec )
        {
            tryCount++;
            exec = tryCount > 3 ? false : true;
            try
            {
                result = resourceHost.execute( new RequestBuilder( cmd ) );
                exec = false;
                return result;
            }
            catch ( CommandException e )
            {
                e.printStackTrace();
            }

            try
            {
                Thread.sleep( 1000 );
            }
            catch ( InterruptedException e )
            {
                e.printStackTrace();
            }
        }

        return null;
    }


    private TunnelInfoDto getData( String link )
    {
        LOG.debug( "Getting tunnel data from Hub: {}", link );

        try
        {
            WebClient client = configManager.getTrustedWebClientWithAuth( link, configManager.getHubIp() );

            Response res = client.get();

            LOG.debug( "Response: HTTP {} - {}", res.getStatus(), res.getStatusInfo().getReasonPhrase() );

            if ( res.getStatus() != HttpStatus.SC_OK )
            {
                LOG.error( "Error to get tunnel  data from Hub: HTTP {} - {}", res.getStatus(),
                        res.getStatusInfo().getReasonPhrase() );
<commit_msg>Fixed tunnel creation<commit_after>package io.subutai.core.hubmanager.impl.tunnel;


import java.util.Set;

import javax.ws.rs.core.Response;

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

import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.http.HttpStatus;

import io.subutai.common.command.CommandException;
import io.subutai.common.command.CommandResult;
import io.subutai.common.command.RequestBuilder;
import io.subutai.common.peer.ResourceHost;
import io.subutai.core.hubmanager.api.HubPluginException;
import io.subutai.core.hubmanager.api.StateLinkProccessor;
import io.subutai.core.hubmanager.impl.ConfigManager;
import io.subutai.core.peer.api.PeerManager;
import io.subutai.hub.share.dto.TunnelInfoDto;
import io.subutai.hub.share.json.JsonUtil;


//TODO close web clients and responses
public class TunnelProcessor implements StateLinkProccessor
{

    private final Logger LOG = LoggerFactory.getLogger( getClass() );

    private static final String TUNNEL_COMMAND = "subutai tunnel %s:%s %s -g";

    private PeerManager peerManager;

    private ConfigManager configManager;


    public TunnelProcessor( PeerManager peerManager, ConfigManager configManager )
    {
        this.peerManager = peerManager;
        this.configManager = configManager;
    }


    @Override
    public void processStateLinks( final Set<String> stateLinks ) throws HubPluginException
    {
        for ( String stateLink : stateLinks )
        {
            if ( !stateLink.contains( "tunnel" ) )
            {
                return;
            }
            processLink( stateLink );
        }
    }


    private void processLink( String stateLink )
    {
        LOG.debug( "processing tunnel statelink" );

        TunnelInfoDto tunnelInfoDto = getData( stateLink );
        ResourceHost resourceHost = null;
        try
        {
            resourceHost = peerManager.getLocalPeer().getManagementHost();

            CommandResult result = execute( resourceHost,
                    String.format( TUNNEL_COMMAND, tunnelInfoDto.getIp(), tunnelInfoDto.getPortToOpen(),
                            tunnelInfoDto.getTtl() ) );


            if ( result.hasSucceeded() )
            {
                String[] data = result.getStdOut().split( ":" );
                tunnelInfoDto.setOpenedIp( data[0] );
                tunnelInfoDto.setOpenedPort( data[1] );

                updateTunnelStatus( stateLink, tunnelInfoDto );
            }
            else
            {
                TunnelInfoDto tunnelInfoDto1 = new TunnelInfoDto();
                tunnelInfoDto1.setTunnelStatus( null );
                updateTunnelStatus( stateLink, tunnelInfoDto );
                LOG.debug( "Something wrong with creating tunnel" );
            }
        }
        catch ( Exception e )
        {
            e.printStackTrace();
        }
    }


    private void updateTunnelStatus( String link, TunnelInfoDto tunnelInfoDto )
    {
        try
        {
            WebClient client = configManager.getTrustedWebClientWithAuth( link, configManager.getHubIp() );
            byte[] cborData = JsonUtil.toCbor( tunnelInfoDto );
            byte[] encryptedData = configManager.getMessenger().produce( cborData );
            Response r = client.put( encryptedData );


            LOG.debug( "Resonce status: " + r.getStatus() );
            if ( r.getStatus() == HttpStatus.SC_OK )
            {
                LOG.debug( "Tunnel peer data successfully sent to hub" );
            }
            else
            {
                LOG.error( "Tunnel peer data was not successfully sent to hub" );
            }
        }
        catch ( Exception e )
        {
            String mgs = "Could not sent tunnel peer data to hub.";
            LOG.error( mgs, e.getMessage() );
        }
    }


    private CommandResult execute( ResourceHost resourceHost, String cmd )
    {
        boolean exec = true;
        int tryCount = 0;
        CommandResult result = null;

        LOG.debug( "Creating tunnel with cmd: " + cmd );
        while ( exec )
        {
            tryCount++;
            exec = tryCount > 3 ? false : true;
            try
            {
                LOG.debug( "Create tunnel try count: " + tryCount );
                result = resourceHost.execute( new RequestBuilder( cmd ) );
                LOG.debug( "Exit code: " + result.getExitCode() );

                if ( result.getExitCode() == 0 )
                {
                    exec = false;
                }

                return result;
            }
            catch ( CommandException e )
            {
                LOG.error( e.getMessage() );
                e.printStackTrace();
            }

            try
            {
                Thread.sleep( 5000 );
            }
            catch ( InterruptedException e )
            {
                e.printStackTrace();
            }
        }

        return null;
    }


    private TunnelInfoDto getData( String link )
    {
        LOG.debug( "Getting tunnel data from Hub: {}", link );

        try
        {
            WebClient client = configManager.getTrustedWebClientWithAuth( link, configManager.getHubIp() );

            Response res = client.get();

            LOG.debug( "Response: HTTP {} - {}", res.getStatus(), res.getStatusInfo().getReasonPhrase() );

            if ( res.getStatus() != HttpStatus.SC_OK )
            {
                LOG.error( "Error to get tunnel  data from Hub: HTTP {} - {}", res.getStatus(),
                        res.getStatusInfo().getReasonPhrase() );

{'fraction_non_alphanumeric': 0.09484015158876688, 'fraction_numerical': 0.0022349625886697116, 'mean_word_length': 1.836824696802646, 'pattern_counts': {'":': 2, '<': 5, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1925476', 'n_tokens_mistral': 2824, 'n_tokens_neox': 2742, 'n_words': 611}