From a1b20f166372022c877491c899de33902778234a Mon Sep 17 00:00:00 2001 From: ayyoob Date: Sat, 3 Jun 2017 13:27:08 +0530 Subject: [PATCH] changed response payload definition for pull notification --- .../PullNotificationSubscriberImpl.java | 10 ++-- .../mgt/jaxrs/beans/EventBeanWrapper.java | 7 +-- .../service/impl/DeviceAgentServiceImpl.java | 15 +++--- .../notification/NotificationContext.java | 49 ------------------- .../notification/NotificationPayload.java | 41 ---------------- .../PullNotificationSubscriber.java | 5 +- .../DeviceManagementProviderService.java | 3 +- .../DeviceManagementProviderServiceImpl.java | 11 ++--- .../template/DeviceTypeManagerService.java | 10 ++-- .../HTTPDeviceTypeManagerService.java | 5 ++ .../config/DeviceTypeConfiguration.java | 8 +-- 11 files changed, 41 insertions(+), 123 deletions(-) delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/pull/notification/NotificationContext.java delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/pull/notification/NotificationPayload.java diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.pull.notification/src/main/java/org/wso2/carbon/device/mgt/extensions/pull/notification/PullNotificationSubscriberImpl.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.pull.notification/src/main/java/org/wso2/carbon/device/mgt/extensions/pull/notification/PullNotificationSubscriberImpl.java index 496a51f556..e757b38fe2 100644 --- a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.pull.notification/src/main/java/org/wso2/carbon/device/mgt/extensions/pull/notification/PullNotificationSubscriberImpl.java +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.pull.notification/src/main/java/org/wso2/carbon/device/mgt/extensions/pull/notification/PullNotificationSubscriberImpl.java @@ -20,9 +20,9 @@ package org.wso2.carbon.device.mgt.extensions.pull.notification; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; -import org.wso2.carbon.device.mgt.common.pull.notification.NotificationContext; import org.wso2.carbon.device.mgt.common.pull.notification.PullNotificationExecutionFailedException; import org.wso2.carbon.device.mgt.common.pull.notification.PullNotificationSubscriber; import org.wso2.carbon.device.mgt.extensions.pull.notification.internal.PullNotificationDataHolder; @@ -37,13 +37,11 @@ public class PullNotificationSubscriberImpl implements PullNotificationSubscribe } - public void execute(NotificationContext ctx) throws PullNotificationExecutionFailedException { - Operation operation = new Operation(); - operation.setId(ctx.getNotificationPayload().getOperationId()); - operation.setPayLoad(ctx.getNotificationPayload().getPayload()); + @Override + public void execute(DeviceIdentifier deviceIdentifier, Operation operation) throws PullNotificationExecutionFailedException { try { PullNotificationDataHolder.getInstance().getDeviceManagementProviderService().updateOperation( - ctx.getDeviceId(), operation); + deviceIdentifier, operation); } catch (OperationManagementException e) { throw new PullNotificationExecutionFailedException(e); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/EventBeanWrapper.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/EventBeanWrapper.java index 7dc667008e..ef90991655 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/EventBeanWrapper.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/EventBeanWrapper.java @@ -22,6 +22,7 @@ import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import javax.validation.constraints.Size; +import java.util.Map; /** * This class is used to wrap the events which receive from the agent application. @@ -31,13 +32,13 @@ import javax.validation.constraints.Size; public class EventBeanWrapper { @ApiModelProperty(name = "payloadData", value = "Event payload payload.", required = true) - Object[] payloadData; + Map payloadData; - public Object[] getPayloadData() { + public Map getPayloadData() { return payloadData; } - public void setPayloadData(Object[] payloadData) { + public void setPayloadData(Map payloadData) { this.payloadData = payloadData; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceAgentServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceAgentServiceImpl.java index 05ff605007..c8c583fd52 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceAgentServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceAgentServiceImpl.java @@ -59,6 +59,7 @@ import javax.ws.rs.core.Response; import java.rmi.RemoteException; import java.util.ArrayList; import java.util.List; +import java.util.Map; @Path("/device/agent") public class DeviceAgentServiceImpl implements DeviceAgentService { @@ -205,7 +206,6 @@ public class DeviceAgentServiceImpl implements DeviceAgentService { public Response publishEvents(@Valid EventBeanWrapper eventBeanWrapper, @PathParam("type") String type , @PathParam("deviceId") String deviceId) { - String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); try { if (eventBeanWrapper == null) { @@ -238,7 +238,7 @@ public class DeviceAgentServiceImpl implements DeviceAgentService { , AttributeType.valueOf(eventStreamAttributeDto.getAttributeType().toUpperCase()))); } - if (eventBeanWrapper.getPayloadData().length != attributes.size()) { + if (eventBeanWrapper.getPayloadData().size() != attributes.size()) { String msg = "payload does not match the stream definition"; return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); } @@ -247,13 +247,16 @@ public class DeviceAgentServiceImpl implements DeviceAgentService { DeviceMgtAPIUtils.getDynamicEventCache().put(type, eventAttributeList); } } - Object[] payload = eventBeanWrapper.getPayloadData(); + Map payload = eventBeanWrapper.getPayloadData(); int i = 0; + Object[] payloadData = new Object[eventAttributeList.getList().size()]; for (Attribute attribute : eventAttributeList.getList()) { if (attribute.getType() == AttributeType.INT) { - payload[i] = ((Double) payload[i]).intValue(); + payloadData[i] = ((Double) payload.get(attribute.getName())).intValue(); } else if (attribute.getType() == AttributeType.LONG) { - payload[i] = ((Double) payload[i]).longValue(); + payloadData[i] = ((Double) payload.get(attribute.getName())).longValue(); + } else { + payloadData[i] = payload.get(attribute.getName()); } i++; } @@ -262,7 +265,7 @@ public class DeviceAgentServiceImpl implements DeviceAgentService { if (DeviceMgtAPIUtils.getEventPublisherService().publishEvent(DeviceMgtAPIUtils.getStreamDefinition(type , PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain()) , Constants.DEFAULT_STREAM_VERSION, metaData - , null, eventBeanWrapper.getPayloadData())) { + , null, payloadData)) { return Response.status(Response.Status.OK).build(); } else { String msg = "Error occurred while publishing the event."; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/pull/notification/NotificationContext.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/pull/notification/NotificationContext.java deleted file mode 100644 index b8bc6f7f04..0000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/pull/notification/NotificationContext.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.wso2.carbon.device.mgt.common.pull.notification; - -import org.wso2.carbon.device.mgt.common.DeviceIdentifier; - -public class NotificationContext { - - private DeviceIdentifier deviceId; - - private NotificationPayload notificationPayload; - - public NotificationContext(DeviceIdentifier deviceId) { - this.deviceId = deviceId; - } - - public NotificationContext(DeviceIdentifier deviceId, NotificationPayload notificationPayload) { - this.deviceId = deviceId; - this.notificationPayload = notificationPayload; - } - - public DeviceIdentifier getDeviceId() { - return deviceId; - } - - public NotificationPayload getNotificationPayload() { - return notificationPayload; - } - - public void setNotificationPayload(NotificationPayload notificationPayload) { - this.notificationPayload = notificationPayload; - } -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/pull/notification/NotificationPayload.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/pull/notification/NotificationPayload.java deleted file mode 100644 index 236011d78a..0000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/pull/notification/NotificationPayload.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.wso2.carbon.device.mgt.common.pull.notification; - -public class NotificationPayload { - - int operationId; - private String payload; - - public String getPayload() { - return payload; - } - - public void setPayload(String payload) { - this.payload = payload; - } - - public int getOperationId() { - return operationId; - } - - public void setOperationId(int operationId) { - this.operationId = operationId; - } -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/pull/notification/PullNotificationSubscriber.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/pull/notification/PullNotificationSubscriber.java index 16134cb8f8..8d59500ee0 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/pull/notification/PullNotificationSubscriber.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/pull/notification/PullNotificationSubscriber.java @@ -19,6 +19,9 @@ package org.wso2.carbon.device.mgt.common.pull.notification; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; + import java.util.Map; /** @@ -28,7 +31,7 @@ public interface PullNotificationSubscriber { void init(Map properties); - void execute(NotificationContext ctx) throws PullNotificationExecutionFailedException; + void execute(DeviceIdentifier deviceIdentifier, Operation operation) throws PullNotificationExecutionFailedException; void clean(); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java index 19e8bcd8d2..0f5500a0e8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java @@ -32,7 +32,6 @@ import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; import org.wso2.carbon.device.mgt.common.policy.mgt.PolicyMonitoringManager; -import org.wso2.carbon.device.mgt.common.pull.notification.NotificationContext; import org.wso2.carbon.device.mgt.common.pull.notification.PullNotificationExecutionFailedException; import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; @@ -356,6 +355,6 @@ public interface DeviceManagementProviderService { * This retrieves the device pull notification payload and passes to device type executor. * @throws PullNotificationExecutionFailedException */ - void executePullNotification(String deviceType, NotificationContext notificationContext) + void updatePullNotificationOperation(DeviceIdentifier deviceIdentifier, Operation operation) throws PullNotificationExecutionFailedException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 84152f7185..cb3d1e24ad 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -28,7 +28,6 @@ import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.DeviceManager; import org.wso2.carbon.device.mgt.common.DeviceNotFoundException; -import org.wso2.carbon.device.mgt.common.pull.notification.NotificationContext; import org.wso2.carbon.device.mgt.common.pull.notification.PullNotificationExecutionFailedException; import org.wso2.carbon.device.mgt.common.pull.notification.PullNotificationSubscriber; import org.wso2.carbon.device.mgt.core.dto.DeviceTypeServiceIdentifier; @@ -2207,12 +2206,12 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } @Override - public void executePullNotification(String deviceType, NotificationContext notificationContext) + public void updatePullNotificationOperation(DeviceIdentifier deviceIdentifier, Operation operation) throws PullNotificationExecutionFailedException { DeviceManagementService dms = - pluginRepository.getDeviceManagementService(deviceType, this.getTenantId()); + pluginRepository.getDeviceManagementService(deviceIdentifier.getType(), this.getTenantId()); if (dms == null) { - String message = "Device type '" + deviceType + "' does not have an associated device management " + + String message = "Device type '" + deviceIdentifier.getType() + "' does not have an associated device management " + "plugin registered within the framework"; if (log.isDebugEnabled()) { log.debug(message); @@ -2222,8 +2221,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv PullNotificationSubscriber pullNotificationSubscriber = dms.getPullNotificationSubscriber(); if (pullNotificationSubscriber == null) { throw new PullNotificationExecutionFailedException("Pull Notification Subscriber is not configured " + - "for device type" + deviceType); + "for device type" + deviceIdentifier.getType()); } - pullNotificationSubscriber.execute(notificationContext); + pullNotificationSubscriber.execute(deviceIdentifier, operation); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerService.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerService.java index 5a3915b925..82ab717e07 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerService.java @@ -80,12 +80,12 @@ public class DeviceTypeManagerService implements DeviceManagementService { this.policyMonitoringManager = new DefaultPolicyMonitoringManager(); } - if (deviceTypeConfiguration.getPullNotificationExecutor() != null) { - String className = deviceTypeConfiguration.getPullNotificationExecutor().getClassName(); + if (deviceTypeConfiguration.getPullNotificationSubscriber() != null) { + String className = deviceTypeConfiguration.getPullNotificationSubscriber().getClassName(); if (className != null && !className.isEmpty()) { - PullNotificationSubscriberLoader pullNotificationExecutorImpl = new PullNotificationSubscriberLoader(className - , deviceTypeConfiguration.getPullNotificationExecutor().getConfigProperties()); - this.pullNotificationSubscriber = pullNotificationExecutorImpl.getPullNotificationSubscriber(); + PullNotificationSubscriberLoader pullNotificationSubscriberLoader = new PullNotificationSubscriberLoader(className + , deviceTypeConfiguration.getPullNotificationSubscriber().getConfigProperties()); + this.pullNotificationSubscriber = pullNotificationSubscriberLoader.getPullNotificationSubscriber(); } } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/HTTPDeviceTypeManagerService.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/HTTPDeviceTypeManagerService.java index e18b8076c4..8663722363 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/HTTPDeviceTypeManagerService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/HTTPDeviceTypeManagerService.java @@ -36,6 +36,7 @@ import org.wso2.carbon.device.mgt.extensions.device.type.template.config.PolicyM import org.wso2.carbon.device.mgt.extensions.device.type.template.config.Properties; import org.wso2.carbon.device.mgt.extensions.device.type.template.config.Property; import org.wso2.carbon.device.mgt.extensions.device.type.template.config.ProvisioningConfig; +import org.wso2.carbon.device.mgt.extensions.device.type.template.config.PullNotificationSubscriber; import org.wso2.carbon.device.mgt.extensions.device.type.template.config.PushNotificationProvider; import org.wso2.carbon.device.mgt.extensions.device.type.template.config.TaskConfiguration; @@ -50,6 +51,7 @@ import java.util.Map; public class HTTPDeviceTypeManagerService extends DeviceTypeManagerService implements DeviceTypeDefinitionProvider { private DeviceTypeMetaDefinition deviceTypeMetaDefinition; + private static final String DEFAULT_PULL_NOTIFICATION_CLASS_NAME = "org.wso2.carbon.device.mgt.extensions.pull.notification"; public HTTPDeviceTypeManagerService(String deviceTypeName, DeviceTypeMetaDefinition deviceTypeMetaDefinition) { super(getDeviceTypeConfigIdentifier(deviceTypeName), getDeviceTypeConfiguration( @@ -163,6 +165,9 @@ public class HTTPDeviceTypeManagerService extends DeviceTypeManagerService imple deviceTypeConfiguration.setOperations(initialOperationConfig.getOperations()); } } + PullNotificationSubscriber pullNotificationSubscriber = new PullNotificationSubscriber(); + pullNotificationSubscriber.setClassName(DEFAULT_PULL_NOTIFICATION_CLASS_NAME); + deviceTypeConfiguration.setPullNotificationSubscriber(pullNotificationSubscriber); return deviceTypeConfiguration; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/config/DeviceTypeConfiguration.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/config/DeviceTypeConfiguration.java index aaecca4726..d1dd854fbb 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/config/DeviceTypeConfiguration.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/config/DeviceTypeConfiguration.java @@ -233,22 +233,22 @@ public class DeviceTypeConfiguration { } /** - * Gets the value of the pullNotificationSubscriber property. + * Gets the value of the PullNotificationSubscriber property. * * @return possible object is * {@link PullNotificationSubscriber } */ - public PullNotificationSubscriber getPullNotificationExecutor() { + public PullNotificationSubscriber getPullNotificationSubscriber() { return pullNotificationSubscriber; } /** - * Sets the value of the pullNotificationSubscriber property. + * Sets the value of the PullNotificationSubscriber property. * * @param value allowed object is * {@link PullNotificationSubscriber } */ - public void setPullNotificationExecutor(PullNotificationSubscriber value) { + public void setPullNotificationSubscriber(PullNotificationSubscriber value) { this.pullNotificationSubscriber = value; }