Search in sources :

Example 1 with UnsupportedVersionException

use of com.cloud.legacymodel.exceptions.UnsupportedVersionException in project cosmic by MissionCriticalCloud.

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();
    final 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.legacymodel.exceptions.UnsupportedVersionException)

Example 2 with UnsupportedVersionException

use of com.cloud.legacymodel.exceptions.UnsupportedVersionException in project cosmic by MissionCriticalCloud.

the class RequestTest method testSerDeserTO.

@Test
public void testSerDeserTO() {
    s_logger.info("Testing serializing and deserializing interface TO works as expected");
    final 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);
    final ListTemplateCommand cmd = new ListTemplateCommand(nfs);
    final Request sreq = new Request(2, 3, cmd, true);
    sreq.setSequence(892403718);
    final 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 (final ClassNotFoundException e) {
        s_logger.error("Unable to parse bytes: ", e);
    } catch (final 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.legacymodel.communication.command.ListTemplateCommand) NfsTO(com.cloud.legacymodel.to.NfsTO) UnsupportedVersionException(com.cloud.legacymodel.exceptions.UnsupportedVersionException) Test(org.junit.Test)

Example 3 with UnsupportedVersionException

use of com.cloud.legacymodel.exceptions.UnsupportedVersionException in project cosmic by MissionCriticalCloud.

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 (final 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 (final 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.legacymodel.exceptions.UnsupportedVersionException)

Example 4 with UnsupportedVersionException

use of com.cloud.legacymodel.exceptions.UnsupportedVersionException in project cosmic by MissionCriticalCloud.

the class RequestTest method testSerDeser.

@Test
@Ignore
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");
    final UpdateHostPasswordCommand cmd1 = new UpdateHostPasswordCommand("abc", "def");
    s_logger.info("SecStorageFirewallCfgCommand has a context map that shouldn't show up in debug level");
    final SecStorageFirewallCfgCommand cmd2 = new SecStorageFirewallCfgCommand();
    s_logger.info("GetHostStatsCommand should not show up at all in debug level");
    final GetHostStatsCommand cmd3 = new GetHostStatsCommand("hostguid", "hostname", 101);
    cmd2.addPortConfig("abc", "24", true, "eth0");
    cmd2.addPortConfig("127.0.0.1", "44", false, "eth1");
    final Request sreq = new Request(2, 3, new Command[] { cmd1, cmd2, cmd3 }, true, true);
    sreq.setSequence(892403717);
    final Logger logger = LoggerFactory.getLogger(GsonHelper.class);
    // logger.setLevel(Level.DEBUG);
    String log = sreq.log("Debug", true);
    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);
    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);
    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 (final ClassNotFoundException e) {
        s_logger.error("Unable to parse bytes: ", e);
    } catch (final UnsupportedVersionException e) {
        s_logger.error("Unable to parse bytes: ", e);
    }
    assert creq != null : "Couldn't get the request back";
    compareRequest(creq, sreq);
    final Answer ans = new Answer(cmd1, true, "No Problem");
    final Response cresp = new Response(creq, ans);
    bytes = cresp.getBytes();
    Response sresp = null;
    try {
        sresp = Response.parse(bytes);
    } catch (final ClassNotFoundException e) {
        s_logger.error("Unable to parse bytes: ", e);
    } catch (final UnsupportedVersionException e) {
        s_logger.error("Unable to parse bytes: ", e);
    }
    assert sresp != null : "Couldn't get the response back";
    compareRequest(cresp, sresp);
}
Also used : Answer(com.cloud.legacymodel.communication.answer.Answer) UpdateHostPasswordCommand(com.cloud.legacymodel.communication.command.UpdateHostPasswordCommand) SecStorageFirewallCfgCommand(com.cloud.legacymodel.communication.command.SecStorageFirewallCfgCommand) Logger(org.slf4j.Logger) GetHostStatsCommand(com.cloud.legacymodel.communication.command.GetHostStatsCommand) UnsupportedVersionException(com.cloud.legacymodel.exceptions.UnsupportedVersionException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

UnsupportedVersionException (com.cloud.legacymodel.exceptions.UnsupportedVersionException)4 Test (org.junit.Test)2 Answer (com.cloud.legacymodel.communication.answer.Answer)1 GetHostStatsCommand (com.cloud.legacymodel.communication.command.GetHostStatsCommand)1 ListTemplateCommand (com.cloud.legacymodel.communication.command.ListTemplateCommand)1 SecStorageFirewallCfgCommand (com.cloud.legacymodel.communication.command.SecStorageFirewallCfgCommand)1 UpdateHostPasswordCommand (com.cloud.legacymodel.communication.command.UpdateHostPasswordCommand)1 NfsTO (com.cloud.legacymodel.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 Ignore (org.junit.Ignore)1 Logger (org.slf4j.Logger)1