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;
}
}
}
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());
}
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());
}
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;
}
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();
}
}
}
}
Aggregations