package org.ovirt.engine.core.vdsbroker.vdsbroker;

import java.util.ArrayList;
import java.util.List;

import org.ovirt.engine.core.common.businessentities.StorageServerConnections;
import org.ovirt.engine.core.common.vdscommands.DiscoverSendTargetsVDSCommandParameters;

public class DiscoverSendTargetsVDSCommand<P extends DiscoverSendTargetsVDSCommandParameters>
        extends VdsBrokerCommand<P> {
    protected IQNListReturnForXmlRpc _result;

    public DiscoverSendTargetsVDSCommand(P parameters) {
        super(parameters);
    }

    @Override
    protected void executeVdsBrokerCommand() {
        _result = getBroker().discoverSendTargets(
                ConnectStorageServerVDSCommand.createStructFromConnection(getParameters().getConnection(), null));
        proceedProxyReturnValue();
        setReturnValue(_result.isFullTargets() ? parseFullTargets(_result.getIqnList())
                : parseTargets(_result.getIqnList()));
    }

    @Override
    protected StatusForXmlRpc getReturnStatus() {
        return _result.mStatus;
    }

    @Override
    protected Object getReturnValueFromBroker() {
        return _result;
    }

    private List<StorageServerConnections> parseFullTargets(List<String> iqnList) {
        ArrayList<StorageServerConnections> connections = new ArrayList<StorageServerConnections>(iqnList.size());
        for (String fullTarget : iqnList) {
            StorageServerConnections con = StorageServerConnections.copyOf(getParameters().getConnection());
            // fullTarget format: <ip>:<port>, <portal> <targetName>
            // e.g 10.35.104.8:3600,1 blue-20G
            String[] tokens = fullTarget.split(",");
            String[] address = tokens[0].split(":");
            String[] literals = tokens[1].split(" ");

            con.setconnection(address[0]);
            con.setport(address[1]);
            con.setportal(literals[0]);
            con.setiqn(literals[1]);
            connections.add(con);
        }

        return connections;
    }

    private List<StorageServerConnections> parseTargets(List<String> iqnList) {
        List<StorageServerConnections> connections = new ArrayList<StorageServerConnections>(iqnList.size());
        for (String iqn : iqnList) {
            StorageServerConnections con = StorageServerConnections.copyOf(getParameters().getConnection());
            con.setiqn(iqn);
            connections.add(con);
        }
        return connections;
    }

}

@@ -264,16 +264,73 @@
 ameters;
+%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0Apublic
@@ -454,16 +454,117 @@
 and%3CP%3E %7B
+%0A%0A    private static final Logger log = LoggerFactory.getLogger(DiscoverSendTargetsVDSCommand.class);
 %0A    pro
@@ -1937,51 +1937,8 @@
 );%0A%0A
-            con.setconnection(address%5B0%5D);%0A
     
@@ -2043,24 +2043,484 @@
 terals%5B1%5D);%0A
+%0A            // address%5B0%5D is the IP address of the target as returned from VDSM.%0A            // Since we want to use the original address from the user's input (which can also be a host name),%0A            // and since VDSM always returns the IP address of the target, we don't want to override it%0A            // by calling con.setconnection(address%5B0%5D).%0A            log.info(%22Connection to %7B%7D requested, VDSM returned %7B%7D.%22, con.getconnection(), address%5B0%5D);%0A%0A
             

Store iSCSI Target Address instead of IP Address in DB