Search in sources :

Example 1 with UnsupportedAnswer

use of com.cloud.agent.api.UnsupportedAnswer in project cloudstack by apache.

the class AnswerTest method testCreateUnsupportedCommandAnswer.

@Test
public void testCreateUnsupportedCommandAnswer() {
    UnsupportedAnswer usa = Answer.createUnsupportedCommandAnswer(acc);
    boolean b = usa.executeInSequence();
    assertFalse(b);
    b = usa.getResult();
    assertFalse(b);
    String d = usa.getDetails();
    assertTrue(d.contains("Unsupported command issued: " + acc.toString() + ".  Are you sure you got the right type of server?"));
    usa = Answer.createUnsupportedVersionAnswer(acc);
    b = usa.executeInSequence();
    assertFalse(b);
    b = usa.getResult();
    assertFalse(b);
    d = usa.getDetails();
    assertTrue(d.equals("Unsuppored Version."));
}
Also used : UnsupportedAnswer(com.cloud.agent.api.UnsupportedAnswer) Test(org.junit.Test)

Example 2 with UnsupportedAnswer

use of com.cloud.agent.api.UnsupportedAnswer in project cloudstack by apache.

the class HypervDirectConnectResource method postHttpRequest.

public static String postHttpRequest(final String jsonCmd, final URI agentUri) {
    // Using Apache's HttpClient for HTTP POST
    // Java-only approach discussed at on StackOverflow concludes with
    // comment to use Apache HttpClient
    // http://stackoverflow.com/a/2793153/939250, but final comment is to
    // use Apache.
    String logMessage = StringEscapeUtils.unescapeJava(jsonCmd);
    logMessage = cleanPassword(logMessage);
    s_logger.debug("POST request to " + agentUri.toString() + " with contents " + logMessage);
    // Create request
    HttpClient httpClient = null;
    final TrustStrategy easyStrategy = new TrustStrategy() {

        @Override
        public boolean isTrusted(final X509Certificate[] chain, final String authType) throws CertificateException {
            return true;
        }
    };
    try {
        final SSLSocketFactory sf = new SSLSocketFactory(easyStrategy, new AllowAllHostnameVerifier());
        final SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("https", DEFAULT_AGENT_PORT, sf));
        final ClientConnectionManager ccm = new BasicClientConnectionManager(registry);
        httpClient = new DefaultHttpClient(ccm);
    } catch (final KeyManagementException e) {
        s_logger.error("failed to initialize http client " + e.getMessage());
    } catch (final UnrecoverableKeyException e) {
        s_logger.error("failed to initialize http client " + e.getMessage());
    } catch (final NoSuchAlgorithmException e) {
        s_logger.error("failed to initialize http client " + e.getMessage());
    } catch (final KeyStoreException e) {
        s_logger.error("failed to initialize http client " + e.getMessage());
    }
    String result = null;
    // TODO: are there timeout settings and worker thread settings to tweak?
    try {
        final HttpPost request = new HttpPost(agentUri);
        // JSON encode command
        // Assumes command sits comfortably in a string, i.e. not used for
        // large data transfers
        final StringEntity cmdJson = new StringEntity(jsonCmd);
        request.addHeader("content-type", "application/json");
        request.setEntity(cmdJson);
        s_logger.debug("Sending cmd to " + agentUri.toString() + " cmd data:" + logMessage);
        final HttpResponse response = httpClient.execute(request);
        // Unsupported commands will not route.
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND) {
            final String errMsg = "Failed to send : HTTP error code : " + response.getStatusLine().getStatusCode();
            s_logger.error(errMsg);
            final String unsupportMsg = "Unsupported command " + agentUri.getPath() + ".  Are you sure you got the right type of" + " server?";
            final Answer ans = new UnsupportedAnswer(null, unsupportMsg);
            s_logger.error(ans);
            result = s_gson.toJson(new Answer[] { ans });
        } else if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
            final String errMsg = "Failed send to " + agentUri.toString() + " : HTTP error code : " + response.getStatusLine().getStatusCode();
            s_logger.error(errMsg);
            return null;
        } else {
            result = EntityUtils.toString(response.getEntity());
            final String logResult = cleanPassword(StringEscapeUtils.unescapeJava(result));
            s_logger.debug("POST response is " + logResult);
        }
    } catch (final ClientProtocolException protocolEx) {
        // Problem with HTTP message exchange
        s_logger.error(protocolEx);
    } catch (final IOException connEx) {
        // Problem with underlying communications
        s_logger.error(connEx);
    } finally {
        httpClient.getConnectionManager().shutdown();
    }
    return result;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) TrustStrategy(org.apache.http.conn.ssl.TrustStrategy) Scheme(org.apache.http.conn.scheme.Scheme) AllowAllHostnameVerifier(org.apache.http.conn.ssl.AllowAllHostnameVerifier) HttpResponse(org.apache.http.HttpResponse) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) IOException(java.io.IOException) BasicClientConnectionManager(org.apache.http.impl.conn.BasicClientConnectionManager) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) KeyManagementException(java.security.KeyManagementException) ClientProtocolException(org.apache.http.client.ClientProtocolException) StringEntity(org.apache.http.entity.StringEntity) UnsupportedAnswer(com.cloud.agent.api.UnsupportedAnswer) CheckSshAnswer(com.cloud.agent.api.check.CheckSshAnswer) GetDomRVersionAnswer(com.cloud.agent.api.GetDomRVersionAnswer) CheckS2SVpnConnectionsAnswer(com.cloud.agent.api.CheckS2SVpnConnectionsAnswer) SetPortForwardingRulesAnswer(com.cloud.agent.api.routing.SetPortForwardingRulesAnswer) SetSourceNatAnswer(com.cloud.agent.api.routing.SetSourceNatAnswer) PlugNicAnswer(com.cloud.agent.api.PlugNicAnswer) GetVmConfigAnswer(com.cloud.agent.api.GetVmConfigAnswer) NetworkUsageAnswer(com.cloud.agent.api.NetworkUsageAnswer) Answer(com.cloud.agent.api.Answer) UnPlugNicAnswer(com.cloud.agent.api.UnPlugNicAnswer) SetStaticNatRulesAnswer(com.cloud.agent.api.routing.SetStaticNatRulesAnswer) IpAssocAnswer(com.cloud.agent.api.routing.IpAssocAnswer) SetFirewallRulesAnswer(com.cloud.agent.api.routing.SetFirewallRulesAnswer) CheckRouterAnswer(com.cloud.agent.api.CheckRouterAnswer) SetStaticRouteAnswer(com.cloud.agent.api.routing.SetStaticRouteAnswer) UnsupportedAnswer(com.cloud.agent.api.UnsupportedAnswer) UnrecoverableKeyException(java.security.UnrecoverableKeyException) HttpClient(org.apache.http.client.HttpClient) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) BasicClientConnectionManager(org.apache.http.impl.conn.BasicClientConnectionManager) SSLSocketFactory(org.apache.http.conn.ssl.SSLSocketFactory)

Example 3 with UnsupportedAnswer

use of com.cloud.agent.api.UnsupportedAnswer in project cloudstack by apache.

the class ResourceManagerImpl method getHostStatistics.

@Override
public HostStats getHostStatistics(final long hostId) {
    final Answer answer = _agentMgr.easySend(hostId, new GetHostStatsCommand(_hostDao.findById(hostId).getGuid(), _hostDao.findById(hostId).getName(), hostId));
    if (answer != null && answer instanceof UnsupportedAnswer) {
        return null;
    }
    if (answer == null || !answer.getResult()) {
        final String msg = "Unable to obtain host " + hostId + " statistics. ";
        s_logger.warn(msg);
        return null;
    } else {
        // now construct the result object
        if (answer instanceof GetHostStatsAnswer) {
            return ((GetHostStatsAnswer) answer).getHostStats();
        }
    }
    return null;
}
Also used : UnsupportedAnswer(com.cloud.agent.api.UnsupportedAnswer) GetGPUStatsAnswer(com.cloud.agent.api.GetGPUStatsAnswer) MaintainAnswer(com.cloud.agent.api.MaintainAnswer) GetHostStatsAnswer(com.cloud.agent.api.GetHostStatsAnswer) Answer(com.cloud.agent.api.Answer) UnsupportedAnswer(com.cloud.agent.api.UnsupportedAnswer) GetHostStatsAnswer(com.cloud.agent.api.GetHostStatsAnswer) GetHostStatsCommand(com.cloud.agent.api.GetHostStatsCommand)

Example 4 with UnsupportedAnswer

use of com.cloud.agent.api.UnsupportedAnswer in project cloudstack by apache.

the class ResourceManagerImpl method getGPUStatistics.

@Override
public HashMap<String, HashMap<String, VgpuTypesInfo>> getGPUStatistics(final HostVO host) {
    final Answer answer = _agentMgr.easySend(host.getId(), new GetGPUStatsCommand(host.getGuid(), host.getName()));
    if (answer != null && answer instanceof UnsupportedAnswer) {
        return null;
    }
    if (answer == null || !answer.getResult()) {
        final String msg = "Unable to obtain GPU stats for host " + host.getName();
        s_logger.warn(msg);
        return null;
    } else {
        // now construct the result object
        if (answer instanceof GetGPUStatsAnswer) {
            return ((GetGPUStatsAnswer) answer).getGroupDetails();
        }
    }
    return null;
}
Also used : GetGPUStatsCommand(com.cloud.agent.api.GetGPUStatsCommand) UnsupportedAnswer(com.cloud.agent.api.UnsupportedAnswer) GetGPUStatsAnswer(com.cloud.agent.api.GetGPUStatsAnswer) MaintainAnswer(com.cloud.agent.api.MaintainAnswer) GetHostStatsAnswer(com.cloud.agent.api.GetHostStatsAnswer) Answer(com.cloud.agent.api.Answer) UnsupportedAnswer(com.cloud.agent.api.UnsupportedAnswer) GetGPUStatsAnswer(com.cloud.agent.api.GetGPUStatsAnswer)

Aggregations

UnsupportedAnswer (com.cloud.agent.api.UnsupportedAnswer)4 Answer (com.cloud.agent.api.Answer)3 GetGPUStatsAnswer (com.cloud.agent.api.GetGPUStatsAnswer)2 GetHostStatsAnswer (com.cloud.agent.api.GetHostStatsAnswer)2 MaintainAnswer (com.cloud.agent.api.MaintainAnswer)2 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)1 CheckS2SVpnConnectionsAnswer (com.cloud.agent.api.CheckS2SVpnConnectionsAnswer)1 GetDomRVersionAnswer (com.cloud.agent.api.GetDomRVersionAnswer)1 GetGPUStatsCommand (com.cloud.agent.api.GetGPUStatsCommand)1 GetHostStatsCommand (com.cloud.agent.api.GetHostStatsCommand)1 GetVmConfigAnswer (com.cloud.agent.api.GetVmConfigAnswer)1 NetworkUsageAnswer (com.cloud.agent.api.NetworkUsageAnswer)1 PlugNicAnswer (com.cloud.agent.api.PlugNicAnswer)1 UnPlugNicAnswer (com.cloud.agent.api.UnPlugNicAnswer)1 CheckSshAnswer (com.cloud.agent.api.check.CheckSshAnswer)1 IpAssocAnswer (com.cloud.agent.api.routing.IpAssocAnswer)1 SetFirewallRulesAnswer (com.cloud.agent.api.routing.SetFirewallRulesAnswer)1 SetPortForwardingRulesAnswer (com.cloud.agent.api.routing.SetPortForwardingRulesAnswer)1 SetSourceNatAnswer (com.cloud.agent.api.routing.SetSourceNatAnswer)1 SetStaticNatRulesAnswer (com.cloud.agent.api.routing.SetStaticNatRulesAnswer)1