Search in sources :

Example 1 with KuraResponsePayload

use of org.eclipse.kura.message.KuraResponsePayload in project kura by eclipse.

the class CloudDeploymentHandlerV2 method doExecUninstall.

private void doExecUninstall(KuraRequestPayload request, KuraResponsePayload response) {
    final DeploymentPackageUninstallOptions options;
    try {
        options = new DeploymentPackageUninstallOptions(request);
        options.setClientId(this.m_dataTransportService.getClientId());
    } catch (Exception ex) {
        s_logger.error("Malformed uninstall request!");
        response.setResponseCode(KuraResponsePayload.RESPONSE_CODE_ERROR);
        response.setTimestamp(new Date());
        try {
            response.setBody("Malformed uninstall request".getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
        // Ignore
        }
        response.setException(ex);
        return;
    }
    final String packageName = options.getDpName();
    // We only allow one request at a time
    if (!this.m_isInstalling && this.m_pendingUninstPackageName != null) {
        s_logger.info("Another request seems still pending: {}. Checking if stale...", this.m_pendingUninstPackageName);
        response = new KuraResponsePayload(KuraResponsePayload.RESPONSE_CODE_ERROR);
        response.setTimestamp(new Date());
        try {
            response.setBody("Only one request at a time is allowed".getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
        // Ignore
        }
    } else {
        s_logger.info("About to uninstall package {}", packageName);
        try {
            this.m_isInstalling = true;
            this.m_pendingUninstPackageName = packageName;
            s_uninstallImplementation = new UninstallImpl(this, this.m_deploymentAdmin);
            s_logger.info("Uninstalling package...");
            this.installerFuture = executor.submit(new Runnable() {

                @Override
                public void run() {
                    try {
                        s_uninstallImplementation.uninstaller(options, packageName);
                    } catch (Exception e) {
                        try {
                            s_uninstallImplementation.uninstallFailedAsync(options, packageName, e);
                        } catch (KuraException e1) {
                        }
                    } finally {
                        CloudDeploymentHandlerV2.this.m_installOptions = null;
                        CloudDeploymentHandlerV2.this.m_isInstalling = false;
                    }
                }
            });
        } catch (Exception e) {
            s_logger.error("Failed to uninstall package {}: {}", packageName, e);
            response = new KuraResponsePayload(KuraResponsePayload.RESPONSE_CODE_ERROR);
            response.setTimestamp(new Date());
            try {
                response.setBody(e.getMessage().getBytes("UTF-8"));
            } catch (UnsupportedEncodingException uee) {
            // Ignore
            }
        } finally {
            this.m_isInstalling = false;
            this.m_pendingUninstPackageName = null;
        }
    }
}
Also used : UninstallImpl(org.eclipse.kura.core.deployment.uninstall.UninstallImpl) KuraException(org.eclipse.kura.KuraException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) KuraResponsePayload(org.eclipse.kura.message.KuraResponsePayload) DeploymentPackageUninstallOptions(org.eclipse.kura.core.deployment.uninstall.DeploymentPackageUninstallOptions) ComponentException(org.osgi.service.component.ComponentException) KuraException(org.eclipse.kura.KuraException) BundleException(org.osgi.framework.BundleException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Date(java.util.Date)

Example 2 with KuraResponsePayload

use of org.eclipse.kura.message.KuraResponsePayload in project kura by eclipse.

the class CloudDeploymentHandlerTest method testGetPackages.

@TestTarget(targetPlatforms = { TestTarget.PLATFORM_ALL })
@Test
@Ignore
public void testGetPackages() throws Exception {
    assertTrue(s_cloudCallService.isConnected());
    DeploymentPackage dp = s_deploymentAdmin.getDeploymentPackage(LOCAL_DP_NAME);
    if (dp == null) {
        s_logger.warn("Getting dp");
        InputStream is = getTestDpUrl().openStream();
        dp = s_deploymentAdmin.installDeploymentPackage(is);
    }
    StringBuilder sb = new StringBuilder(CloudletTopic.Method.GET.toString()).append("/").append(CloudDeploymentHandlerV2.RESOURCE_PACKAGES);
    KuraResponsePayload resp = s_cloudCallService.call(CloudDeploymentHandlerV2.APP_ID, sb.toString(), null, 5000);
    assertEquals(KuraResponsePayload.RESPONSE_CODE_OK, resp.getResponseCode());
    String s = new String(resp.getBody());
    // XmlDeploymentPackages xmlPackages = XmlUtil.unmarshal(s, XmlDeploymentPackages.class);
    XmlDeploymentPackages xmlPackages = CoreTestXmlUtil.unmarshal(s, XmlDeploymentPackages.class);
    XmlDeploymentPackage[] packages = xmlPackages.getDeploymentPackages();
    XmlDeploymentPackage xmlDp = null;
    if (packages != null) {
        for (int i = 0; i < packages.length; i++) {
            if (packages[i].getName().equals(LOCAL_DP_NAME)) {
                xmlDp = packages[i];
                break;
            }
        }
    }
    assertNotNull(xmlDp);
    assertEquals(LOCAL_DP_VERSION, xmlDp.getVersion());
    XmlBundleInfo[] bundleInfos = xmlDp.getBundleInfos();
    assertEquals(1, bundleInfos.length);
    assertEquals(LOCAL_BUNDLE_NAME, bundleInfos[0].getName());
    assertEquals(LOCAL_BUNDLE_VERSION, bundleInfos[0].getVersion());
}
Also used : InputStream(java.io.InputStream) DeploymentPackage(org.osgi.service.deploymentadmin.DeploymentPackage) XmlDeploymentPackage(org.eclipse.kura.core.deployment.xml.XmlDeploymentPackage) XmlDeploymentPackage(org.eclipse.kura.core.deployment.xml.XmlDeploymentPackage) KuraResponsePayload(org.eclipse.kura.message.KuraResponsePayload) XmlBundleInfo(org.eclipse.kura.core.deployment.xml.XmlBundleInfo) XmlDeploymentPackages(org.eclipse.kura.core.deployment.xml.XmlDeploymentPackages) Ignore(org.junit.Ignore) TestTarget(org.eclipse.kura.test.annotation.TestTarget) Test(org.junit.Test)

Example 3 with KuraResponsePayload

use of org.eclipse.kura.message.KuraResponsePayload in project kura by eclipse.

the class CloudDeploymentHandlerTest method testExecStartStop.

@TestTarget(targetPlatforms = { TestTarget.PLATFORM_ALL })
@Test
public void testExecStartStop() throws Exception {
    assertTrue(s_cloudCallService.isConnected());
    DeploymentPackage dp = s_deploymentAdmin.getDeploymentPackage(LOCAL_DP_NAME);
    if (dp == null) {
        InputStream is = getTestDpUrl().openStream();
        dp = s_deploymentAdmin.installDeploymentPackage(is);
    }
    Bundle bundle = dp.getBundle(LOCAL_BUNDLE_NAME);
    assertNotNull(bundle);
    if (bundle.getState() == Bundle.RESOLVED) {
        bundle.start();
    }
    assertEquals(Bundle.ACTIVE, bundle.getState());
    StringBuilder sb = new StringBuilder(CloudletTopic.Method.EXEC.toString()).append("/").append(CloudDeploymentHandlerV2.RESOURCE_STOP).append("/").append(bundle.getBundleId());
    KuraResponsePayload resp = s_cloudCallService.call(CloudDeploymentHandlerV2.APP_ID, sb.toString(), null, 5000);
    assertEquals(KuraResponsePayload.RESPONSE_CODE_OK, resp.getResponseCode());
    assertEquals(Bundle.RESOLVED, bundle.getState());
    // Start
    sb = new StringBuilder(CloudletTopic.Method.EXEC.toString()).append("/").append(CloudDeploymentHandlerV2.RESOURCE_START).append("/").append(bundle.getBundleId());
    resp = s_cloudCallService.call(CloudDeploymentHandlerV2.APP_ID, sb.toString(), null, 5000);
    assertEquals(KuraResponsePayload.RESPONSE_CODE_OK, resp.getResponseCode());
    assertEquals(Bundle.ACTIVE, bundle.getState());
}
Also used : InputStream(java.io.InputStream) XmlBundle(org.eclipse.kura.core.deployment.xml.XmlBundle) Bundle(org.osgi.framework.Bundle) DeploymentPackage(org.osgi.service.deploymentadmin.DeploymentPackage) XmlDeploymentPackage(org.eclipse.kura.core.deployment.xml.XmlDeploymentPackage) KuraResponsePayload(org.eclipse.kura.message.KuraResponsePayload) TestTarget(org.eclipse.kura.test.annotation.TestTarget) Test(org.junit.Test)

Example 4 with KuraResponsePayload

use of org.eclipse.kura.message.KuraResponsePayload in project kura by eclipse.

the class MessageHandlerCallable method call.

@Override
public Void call() throws Exception {
    s_logger.debug("Control Arrived on topic: {}", this.m_appTopic);
    // Prepare the default response
    KuraRequestPayload reqPayload = KuraRequestPayload.buildFromKuraPayload(this.m_msg);
    KuraResponsePayload respPayload = new KuraResponsePayload(KuraResponsePayload.RESPONSE_CODE_OK);
    try {
        CloudletTopic reqTopic = CloudletTopic.parseAppTopic(this.m_appTopic);
        CloudletTopic.Method method = reqTopic.getMethod();
        switch(method) {
            case GET:
                s_logger.debug("Handling GET request topic: {}", this.m_appTopic);
                this.m_cloudApp.doGet(reqTopic, reqPayload, respPayload);
                break;
            case PUT:
                s_logger.debug("Handling PUT request topic: {}", this.m_appTopic);
                this.m_cloudApp.doPut(reqTopic, reqPayload, respPayload);
                break;
            case POST:
                s_logger.debug("Handling POST request topic: {}", this.m_appTopic);
                this.m_cloudApp.doPost(reqTopic, reqPayload, respPayload);
                break;
            case DEL:
                s_logger.debug("Handling DEL request topic: {}", this.m_appTopic);
                this.m_cloudApp.doDel(reqTopic, reqPayload, respPayload);
                break;
            case EXEC:
                s_logger.debug("Handling EXEC request topic: {}", this.m_appTopic);
                this.m_cloudApp.doExec(reqTopic, reqPayload, respPayload);
                break;
            default:
                s_logger.error("Bad request topic: {}", this.m_appTopic);
                respPayload.setResponseCode(KuraResponsePayload.RESPONSE_CODE_BAD_REQUEST);
                break;
        }
    } catch (IllegalArgumentException e) {
        s_logger.error("Bad request topic: {}", this.m_appTopic);
        respPayload.setResponseCode(KuraResponsePayload.RESPONSE_CODE_BAD_REQUEST);
    } catch (KuraException e) {
        s_logger.error("Error handling request topic: {}\n{}", this.m_appTopic, e);
        respPayload.setResponseCode(KuraResponsePayload.RESPONSE_CODE_ERROR);
        respPayload.setException(e);
    }
    try {
        CloudClient cloudClient = this.m_cloudApp.getCloudApplicationClient();
        respPayload.setTimestamp(new Date());
        StringBuilder sb = new StringBuilder("REPLY").append("/").append(reqPayload.getRequestId());
        String requesterClientId = reqPayload.getRequesterClientId();
        s_logger.debug("Publishing response topic: {}", sb.toString());
        cloudClient.controlPublish(requesterClientId, sb.toString(), respPayload, Cloudlet.DFLT_PUB_QOS, Cloudlet.DFLT_RETAIN, Cloudlet.DFLT_PRIORITY);
    } catch (KuraException e) {
        s_logger.error("Error publishing response for topic: {}\n{}", this.m_appTopic, e);
    }
    return null;
}
Also used : KuraException(org.eclipse.kura.KuraException) KuraResponsePayload(org.eclipse.kura.message.KuraResponsePayload) Date(java.util.Date) KuraRequestPayload(org.eclipse.kura.message.KuraRequestPayload)

Example 5 with KuraResponsePayload

use of org.eclipse.kura.message.KuraResponsePayload in project kura by eclipse.

the class CloudCallServiceImpl method onMessageArrived.

@Override
public void onMessageArrived(String topic, byte[] payload, int qos, boolean retained) {
    s_logger.debug("Message arrived on topic: '{}'", topic);
    if (this.m_respTopic != null) {
        // Filter on application ID and topic
        KuraTopic kuraTopic = new KuraTopic(topic);
        KuraTopic kuraRespTopic = new KuraTopic(this.m_respTopic);
        if (kuraTopic.getApplicationId().equals(kuraRespTopic.getApplicationId()) && kuraTopic.getApplicationTopic().equals(kuraRespTopic.getApplicationTopic())) {
            s_logger.debug("Got response");
            CloudPayloadProtoBufDecoderImpl decoder = new CloudPayloadProtoBufDecoderImpl(payload);
            KuraResponsePayload resp = null;
            try {
                KuraPayload kuraPayload = decoder.buildFromByteArray();
                resp = new KuraResponsePayload(kuraPayload);
            } catch (KuraInvalidMessageException e) {
                s_logger.error("Cannot decode protobuf", e);
            } catch (IOException e) {
                s_logger.error("Cannot decode protobuf", e);
            }
            synchronized (this.m_lock) {
                // Can be null
                this.m_resp = resp;
                this.m_lock.notifyAll();
            }
        }
    }
}
Also used : KuraTopic(org.eclipse.kura.message.KuraTopic) KuraPayload(org.eclipse.kura.message.KuraPayload) CloudPayloadProtoBufDecoderImpl(org.eclipse.kura.core.cloud.CloudPayloadProtoBufDecoderImpl) KuraResponsePayload(org.eclipse.kura.message.KuraResponsePayload) IOException(java.io.IOException) KuraInvalidMessageException(org.eclipse.kura.KuraInvalidMessageException)

Aggregations

KuraResponsePayload (org.eclipse.kura.message.KuraResponsePayload)9 TestTarget (org.eclipse.kura.test.annotation.TestTarget)6 Test (org.junit.Test)6 XmlDeploymentPackage (org.eclipse.kura.core.deployment.xml.XmlDeploymentPackage)4 KuraPayload (org.eclipse.kura.message.KuraPayload)4 DeploymentPackage (org.osgi.service.deploymentadmin.DeploymentPackage)4 InputStream (java.io.InputStream)3 IOException (java.io.IOException)2 Date (java.util.Date)2 KuraException (org.eclipse.kura.KuraException)2 XmlBundle (org.eclipse.kura.core.deployment.xml.XmlBundle)2 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ArrayList (java.util.ArrayList)1 Hashtable (java.util.Hashtable)1 KuraInvalidMessageException (org.eclipse.kura.KuraInvalidMessageException)1 CloudPayloadProtoBufDecoderImpl (org.eclipse.kura.core.cloud.CloudPayloadProtoBufDecoderImpl)1 ComponentConfigurationImpl (org.eclipse.kura.core.configuration.ComponentConfigurationImpl)1 XmlComponentConfigurations (org.eclipse.kura.core.configuration.XmlComponentConfigurations)1