Search in sources :

Example 1 with UnsupportedVersionException

use of com.cloud.exception.UnsupportedVersionException in project cloudstack by apache.

the class Request method parse.

/**
     * Factory method for Request and Response. It expects the bytes to be
     * correctly formed so it's possible that it throws underflow exceptions
     * but you shouldn't be concerned about that since that all bytes sent in
     * should already be formatted correctly.
     *
     * @param bytes bytes to be converted.
     * @return Request or Response depending on the data.
     * @throws ClassNotFoundException if the Command or Answer can not be formed.
     * @throws
     */
public static Request parse(final byte[] bytes) throws ClassNotFoundException, UnsupportedVersionException {
    ByteBuffer buff = ByteBuffer.wrap(bytes);
    final byte ver = buff.get();
    final Version version = Version.get(ver);
    if (version.ordinal() != Version.v1.ordinal() && version.ordinal() != Version.v3.ordinal()) {
        throw new UnsupportedVersionException("This version is no longer supported: " + version.toString(), UnsupportedVersionException.IncompatibleVersion);
    }
    buff.get();
    final short flags = buff.getShort();
    final boolean isRequest = (flags & FLAG_REQUEST) > 0;
    final long seq = buff.getLong();
    // The size here is uncompressed size, if the data is compressed.
    final int size = buff.getInt();
    final long mgmtId = buff.getLong();
    final long agentId = buff.getLong();
    long via;
    if (version.ordinal() == Version.v1.ordinal()) {
        via = buff.getLong();
    } else {
        via = agentId;
    }
    if ((flags & FLAG_COMPRESSED) != 0) {
        buff = doDecompress(buff, size);
    }
    byte[] command = null;
    int offset = 0;
    if (buff.hasArray()) {
        command = buff.array();
        offset = buff.arrayOffset() + buff.position();
    } else {
        command = new byte[buff.remaining()];
        buff.get(command);
        offset = 0;
    }
    final String content = new String(command, offset, command.length - offset);
    if (isRequest) {
        return new Request(version, seq, agentId, mgmtId, via, flags, content);
    } else {
        return new Response(Version.get(ver), seq, agentId, mgmtId, via, flags, content);
    }
}
Also used : ByteBuffer(java.nio.ByteBuffer) UnsupportedVersionException(com.cloud.exception.UnsupportedVersionException)

Example 2 with UnsupportedVersionException

use of com.cloud.exception.UnsupportedVersionException in project cloudstack by apache.

the class RequestTest method testSerDeserTO.

public void testSerDeserTO() {
    s_logger.info("Testing serializing and deserializing interface TO works as expected");
    NfsTO nfs = new NfsTO("nfs://192.168.56.10/opt/storage/secondary", DataStoreRole.Image);
    // SecStorageSetupCommand cmd = new SecStorageSetupCommand(nfs, "nfs://192.168.56.10/opt/storage/secondary", null);
    ListTemplateCommand cmd = new ListTemplateCommand(nfs);
    Request sreq = new Request(2, 3, cmd, true);
    sreq.setSequence(892403718);
    byte[] bytes = sreq.getBytes();
    assert Request.getSequence(bytes) == 892403718;
    assert Request.getManagementServerId(bytes) == 3;
    assert Request.getAgentId(bytes) == 2;
    assert Request.getViaAgentId(bytes) == 2;
    Request creq = null;
    try {
        creq = Request.parse(bytes);
    } catch (ClassNotFoundException e) {
        s_logger.error("Unable to parse bytes: ", e);
    } catch (UnsupportedVersionException e) {
        s_logger.error("Unable to parse bytes: ", e);
    }
    assert creq != null : "Couldn't get the request back";
    compareRequest(creq, sreq);
    assertEquals("nfs://192.168.56.10/opt/storage/secondary", ((NfsTO) ((ListTemplateCommand) creq.getCommand()).getDataStore()).getUrl());
}
Also used : ListTemplateCommand(com.cloud.agent.api.storage.ListTemplateCommand) NfsTO(com.cloud.agent.api.to.NfsTO) UnsupportedVersionException(com.cloud.exception.UnsupportedVersionException)

Example 3 with UnsupportedVersionException

use of com.cloud.exception.UnsupportedVersionException in project cloudstack by apache.

the class RequestTest method testSerDeser.

public void testSerDeser() {
    s_logger.info("Testing serializing and deserializing works as expected");
    s_logger.info("UpdateHostPasswordCommand should have two parameters that doesn't show in logging");
    UpdateHostPasswordCommand cmd1 = new UpdateHostPasswordCommand("abc", "def");
    s_logger.info("SecStorageFirewallCfgCommand has a context map that shouldn't show up in debug level");
    SecStorageFirewallCfgCommand cmd2 = new SecStorageFirewallCfgCommand();
    s_logger.info("GetHostStatsCommand should not show up at all in debug level");
    GetHostStatsCommand cmd3 = new GetHostStatsCommand("hostguid", "hostname", 101);
    cmd2.addPortConfig("abc", "24", true, "eth0");
    cmd2.addPortConfig("127.0.0.1", "44", false, "eth1");
    Request sreq = new Request(2, 3, new Command[] { cmd1, cmd2, cmd3 }, true, true);
    sreq.setSequence(892403717);
    Logger logger = Logger.getLogger(GsonHelper.class);
    Level level = logger.getLevel();
    logger.setLevel(Level.DEBUG);
    String log = sreq.log("Debug", true, Level.DEBUG);
    assert (log.contains(UpdateHostPasswordCommand.class.getSimpleName()));
    assert (log.contains(SecStorageFirewallCfgCommand.class.getSimpleName()));
    assert (!log.contains(GetHostStatsCommand.class.getSimpleName()));
    assert (!log.contains("username"));
    assert (!log.contains("password"));
    logger.setLevel(Level.TRACE);
    log = sreq.log("Trace", true, Level.TRACE);
    assert (log.contains(UpdateHostPasswordCommand.class.getSimpleName()));
    assert (log.contains(SecStorageFirewallCfgCommand.class.getSimpleName()));
    assert (log.contains(GetHostStatsCommand.class.getSimpleName()));
    assert (!log.contains("username"));
    assert (!log.contains("password"));
    logger.setLevel(Level.INFO);
    log = sreq.log("Info", true, Level.INFO);
    assert (log == null);
    logger.setLevel(level);
    byte[] bytes = sreq.getBytes();
    assert Request.getSequence(bytes) == 892403717;
    assert Request.getManagementServerId(bytes) == 3;
    assert Request.getAgentId(bytes) == 2;
    assert Request.getViaAgentId(bytes) == 2;
    Request creq = null;
    try {
        creq = Request.parse(bytes);
    } catch (ClassNotFoundException e) {
        s_logger.error("Unable to parse bytes: ", e);
    } catch (UnsupportedVersionException e) {
        s_logger.error("Unable to parse bytes: ", e);
    }
    assert creq != null : "Couldn't get the request back";
    compareRequest(creq, sreq);
    Answer ans = new Answer(cmd1, true, "No Problem");
    Response cresp = new Response(creq, ans);
    bytes = cresp.getBytes();
    Response sresp = null;
    try {
        sresp = Response.parse(bytes);
    } catch (ClassNotFoundException e) {
        s_logger.error("Unable to parse bytes: ", e);
    } catch (UnsupportedVersionException e) {
        s_logger.error("Unable to parse bytes: ", e);
    }
    assert sresp != null : "Couldn't get the response back";
    compareRequest(cresp, sresp);
}
Also used : DownloadAnswer(com.cloud.agent.api.storage.DownloadAnswer) Answer(com.cloud.agent.api.Answer) UpdateHostPasswordCommand(com.cloud.agent.api.UpdateHostPasswordCommand) SecStorageFirewallCfgCommand(com.cloud.agent.api.SecStorageFirewallCfgCommand) Level(org.apache.log4j.Level) Logger(org.apache.log4j.Logger) GetHostStatsCommand(com.cloud.agent.api.GetHostStatsCommand) UnsupportedVersionException(com.cloud.exception.UnsupportedVersionException)

Example 4 with UnsupportedVersionException

use of com.cloud.exception.UnsupportedVersionException in project cloudstack by apache.

the class ClusteredAgentManagerImpl method routeToPeer.

public boolean routeToPeer(final String peer, final byte[] bytes) {
    int i = 0;
    SocketChannel ch = null;
    SSLEngine sslEngine = null;
    while (i++ < 5) {
        ch = connectToPeer(peer, ch);
        if (ch == null) {
            try {
                logD(bytes, "Unable to route to peer: " + Request.parse(bytes).toString());
            } catch (ClassNotFoundException | UnsupportedVersionException e) {
                // Request.parse thrown exception when we try to log it, log as much as we can
                logD(bytes, "Unable to route to peer, and Request.parse further caught exception" + e.getMessage());
            }
            return false;
        }
        sslEngine = getSSLEngine(peer);
        if (sslEngine == null) {
            logD(bytes, "Unable to get SSLEngine of peer: " + peer);
            return false;
        }
        try {
            if (s_logger.isDebugEnabled()) {
                logD(bytes, "Routing to peer");
            }
            Link.write(ch, new ByteBuffer[] { ByteBuffer.wrap(bytes) }, sslEngine);
            return true;
        } catch (final IOException e) {
            try {
                logI(bytes, "Unable to route to peer: " + Request.parse(bytes).toString() + " due to " + e.getMessage());
            } catch (ClassNotFoundException | UnsupportedVersionException ex) {
                // Request.parse thrown exception when we try to log it, log as much as we can
                logI(bytes, "Unable to route to peer due to" + e.getMessage() + ". Also caught exception when parsing request: " + ex.getMessage());
            }
        }
    }
    return false;
}
Also used : SocketChannel(java.nio.channels.SocketChannel) SSLEngine(javax.net.ssl.SSLEngine) IOException(java.io.IOException) UnsupportedVersionException(com.cloud.exception.UnsupportedVersionException)

Aggregations

UnsupportedVersionException (com.cloud.exception.UnsupportedVersionException)4 Answer (com.cloud.agent.api.Answer)1 GetHostStatsCommand (com.cloud.agent.api.GetHostStatsCommand)1 SecStorageFirewallCfgCommand (com.cloud.agent.api.SecStorageFirewallCfgCommand)1 UpdateHostPasswordCommand (com.cloud.agent.api.UpdateHostPasswordCommand)1 DownloadAnswer (com.cloud.agent.api.storage.DownloadAnswer)1 ListTemplateCommand (com.cloud.agent.api.storage.ListTemplateCommand)1 NfsTO (com.cloud.agent.api.to.NfsTO)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 SocketChannel (java.nio.channels.SocketChannel)1 SSLEngine (javax.net.ssl.SSLEngine)1 Level (org.apache.log4j.Level)1 Logger (org.apache.log4j.Logger)1