From e8ffe60e8c0f852b4911ec5029967b87b1f50eb3 Mon Sep 17 00:00:00 2001 From: Jasintha Date: Wed, 18 Jan 2017 14:27:05 +0530 Subject: [PATCH] Fixing firealam policy push notification issue --- .../pom.xml | 1 + .../mqtt/MQTTNotificationStrategy.java | 50 +++++++++++++++---- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/pom.xml b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/pom.xml index 703f3ba3485..c885da42702 100644 --- a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/pom.xml +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/pom.xml @@ -144,6 +144,7 @@ org.wso2.carbon.event.output.adapter.core, org.wso2.carbon.event.output.adapter.core.exception, org.osgi.framework, + org.wso2.carbon.device.mgt.core.operation.mgt, org.wso2.carbon.core diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/mqtt/MQTTNotificationStrategy.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/mqtt/MQTTNotificationStrategy.java index 03f2d8f9d3c..93be8189997 100644 --- a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/mqtt/MQTTNotificationStrategy.java +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/mqtt/MQTTNotificationStrategy.java @@ -22,17 +22,21 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; +import org.wso2.carbon.device.mgt.common.policy.mgt.Profile; import org.wso2.carbon.device.mgt.common.push.notification.NotificationContext; import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy; import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig; import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationExecutionFailedException; +import org.wso2.carbon.device.mgt.core.operation.mgt.ProfileOperation; import org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt.internal.MQTTDataHolder; import org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt.internal.util.MQTTAdapterConstants; import org.wso2.carbon.event.output.adapter.core.MessageType; import org.wso2.carbon.event.output.adapter.core.OutputEventAdapterConfiguration; import org.wso2.carbon.event.output.adapter.core.exception.OutputEventAdapterException; +import org.wso2.carbon.device.mgt.core.operation.mgt.PolicyOperation; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Properties; @@ -57,7 +61,7 @@ public class MQTTNotificationStrategy implements NotificationStrategy { configProperties.put(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_USERNAME, config.getProperty(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_USERNAME)); configProperties.put(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_PASSWORD, - config.getProperty(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_PASSWORD)); + config.getProperty(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_PASSWORD)); configProperties.put(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_CLEAR_SESSION, config.getProperty(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_CLEAR_SESSION)); configProperties.put(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_SCOPES, @@ -79,23 +83,48 @@ public class MQTTNotificationStrategy implements NotificationStrategy { @Override public void execute(NotificationContext ctx) throws PushNotificationExecutionFailedException { - Map dynamicProperties = new HashMap<>(); + Operation operation = ctx.getOperation(); Properties properties = operation.getProperties(); if (properties != null && properties.get(MQTT_ADAPTER_TOPIC) != null) { + Map dynamicProperties = new HashMap<>(); dynamicProperties.put("topic", (String) properties.get(MQTT_ADAPTER_TOPIC)); + MQTTDataHolder.getInstance().getOutputEventAdapterService().publish(mqttAdapterName, dynamicProperties, + operation.getPayLoad()); } else { - String topic = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(true) + "/" - + ctx.getDeviceId().getType() + "/" + ctx.getDeviceId().getId() + "/" + operation.getType() - .toString().toLowerCase() + "/" + operation.getCode(); - dynamicProperties.put("topic", topic); - if (operation.getPayLoad() == null) { - operation.setPayLoad(""); + if (PolicyOperation.POLICY_OPERATION_CODE.equals(operation.getCode())) { + PolicyOperation policyOperation = (PolicyOperation) operation; + List profileOperations = policyOperation.getProfileOperations(); + String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(true); + String deviceType = ctx.getDeviceId().getType(); + String deviceId = ctx.getDeviceId().getId(); + for (ProfileOperation profileOperation : profileOperations) { + Map dynamicProperties = new HashMap<>(); + String topic = tenantDomain + "/" + + deviceType + "/" + deviceId + "/" + profileOperation.getType() + .toString().toLowerCase() + "/" + profileOperation.getCode().toLowerCase(); + dynamicProperties.put("topic", topic); + MQTTDataHolder.getInstance().getOutputEventAdapterService().publish(mqttAdapterName, dynamicProperties, + profileOperation.getPayLoad()); + } + + } else { + Map dynamicProperties = new HashMap<>(); + String topic = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(true) + "/" + + ctx.getDeviceId().getType() + "/" + ctx.getDeviceId().getId() + "/" + operation.getType() + .toString().toLowerCase() + "/" + operation.getCode(); + dynamicProperties.put("topic", topic); + if (operation.getPayLoad() == null) { + operation.setPayLoad(""); + } + MQTTDataHolder.getInstance().getOutputEventAdapterService().publish(mqttAdapterName, dynamicProperties, + operation.getPayLoad()); + } + } - MQTTDataHolder.getInstance().getOutputEventAdapterService().publish(mqttAdapterName, dynamicProperties, - operation.getPayLoad()); + } @Override @@ -109,3 +138,4 @@ public class MQTTNotificationStrategy implements NotificationStrategy { } } +