package io.subutai.core.network.cli;


import java.util.Set;

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

import org.apache.karaf.shell.commands.Command;

import com.google.common.base.Preconditions;

import io.subutai.common.protocol.P2PConnection;
import io.subutai.core.identity.rbac.cli.SubutaiShellCommandSupport;
import io.subutai.core.network.api.NetworkManager;
import io.subutai.core.network.api.NetworkManagerException;


@Command( scope = "net", name = "p2p-list", description = "List P2P connections" )
public class ListP2PCommand extends SubutaiShellCommandSupport
{
    private static final Logger LOG = LoggerFactory.getLogger( ListP2PCommand.class.getName() );

    private final NetworkManager networkManager;


    public ListP2PCommand( final NetworkManager networkManager )
    {
        Preconditions.checkNotNull( networkManager );

        this.networkManager = networkManager;
    }


    @Override
    protected Object doExecute()
    {

        try
        {
            final Set<P2PConnection> connections = networkManager.getP2PConnections();
            System.out.println( String.format( "Found %d P2P connection(s).", connections.size() ) );
            for ( P2PConnection connection : connections )
            {
                System.out.println(
                        String.format( "%s %s %s", connection.getIp(), connection.getHash(), connection.getMac() ) );
            }
        }
        catch ( NetworkManagerException e )
        {
            System.out.println( e.getMessage() );
            LOG.error( "Error in ListP2PCommand", e );
        }

        return null;
    }
}

@@ -113,16 +113,65 @@
 ctory;%0A%0A
+import org.apache.karaf.shell.commands.Argument;%0A
 import o
@@ -255,16 +255,55 @@
 ditions;
+%0Aimport com.google.common.base.Strings;
 %0A%0Aimport
@@ -823,16 +823,160 @@
 nager;%0A%0A
+    @Argument( index = 0, name = %22p2p hash%22, required = false, multiValued = false,%0A            description = %22p2p hash%22 )%0A    String p2pHash;%0A%0A
 %0A    pub
@@ -1216,32 +1216,102 @@
   try%0A        %7B%0A
+            if ( Strings.isNullOrEmpty( p2pHash ) )%0A            %7B%0A    
             fina
@@ -1385,32 +1385,36 @@
 ();%0A            
+    
 System.out.print
@@ -1412,24 +1412,8 @@
 out.
-println( String.
 form
@@ -1443,17 +1443,18 @@
 ction(s)
-.
+%25n
 %22, conne
@@ -1464,28 +1464,28 @@
 ons.size() )
- )
 ;%0A
+  
             
@@ -1484,16 +1484,18 @@
         
+  
 for ( P2
@@ -1537,34 +1537,40 @@
 s )%0A            
-%7B%0A
+    %7B%0A  
                 
@@ -1561,32 +1561,34 @@
                 
+  
 System.out.print
@@ -1586,47 +1586,470 @@
 out.
-println(%0A                        String
+format( %22%25s %25s %25s%25n%22, connection.getIp(), connection.getHash(), connection.getMac() );%0A                %7D%0A            %7D%0A            else%0A            %7B%0A                final P2PConnection connection = networkManager.getP2PConnectionByHash( p2pHash );%0A                if ( connection == null )%0A                %7B%0A                    System.out.println( %22Connection not found%22 );%0A                %7D%0A                else%0A                %7B%0A                    System.out
 .for
@@ -2062,16 +2062,18 @@
 %25s %25s %25s
+%25n
 %22, conne
@@ -2130,19 +2130,35 @@
 tMac() )
- );
+;%0A                %7D
 %0A       

update p2p list command