From 77ceed53680169755739eeb9865951ae9202f9d4 Mon Sep 17 00:00:00 2001 From: ayyoob Date: Tue, 9 May 2017 00:54:01 +0530 Subject: [PATCH 1/8] renamed device type publisher service to DeviceAnalyticsArtifactUploaderAdminService --- ...> DeviceAnalyticsArtifactUploaderAdminService.java} | 4 ++-- ...viceAnalyticsArtifactUploaderAdminServiceImpl.java} | 10 +++------- .../src/main/webapp/WEB-INF/cxf-servlet.xml | 4 ++-- 3 files changed, 7 insertions(+), 11 deletions(-) rename components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/{DeviceTypePublisherAdminService.java => DeviceAnalyticsArtifactUploaderAdminService.java} (97%) rename components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/{DeviceTypePublisherAdminServiceImpl.java => DeviceAnalyticsArtifactUploaderAdminServiceImpl.java} (98%) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceTypePublisherAdminService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceAnalyticsArtifactUploaderAdminService.java similarity index 97% rename from components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceTypePublisherAdminService.java rename to components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceAnalyticsArtifactUploaderAdminService.java index d3c3ee25c2..6c7f0d5eff 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceTypePublisherAdminService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceAnalyticsArtifactUploaderAdminService.java @@ -38,7 +38,7 @@ import javax.ws.rs.core.Response; title = "", extensions = { @Extension(properties = { - @ExtensionProperty(name = "name", value = "DeviceTypePublisherAdminService"), + @ExtensionProperty(name = "name", value = "DeviceAnalyticsArtifactUploaderAdminService"), @ExtensionProperty(name = "context", value = "/api/device-mgt/v1.0/admin/devicetype"), }) } @@ -62,7 +62,7 @@ import javax.ws.rs.core.Response; } ) -public interface DeviceTypePublisherAdminService { +public interface DeviceAnalyticsArtifactUploaderAdminService { @POST @Path("/deploy/{type}") diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceTypePublisherAdminServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceAnalyticsArtifactUploaderAdminServiceImpl.java similarity index 98% rename from components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceTypePublisherAdminServiceImpl.java rename to components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceAnalyticsArtifactUploaderAdminServiceImpl.java index 9fbd5043e2..9a73a99913 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceTypePublisherAdminServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceAnalyticsArtifactUploaderAdminServiceImpl.java @@ -37,13 +37,10 @@ import org.wso2.carbon.application.mgt.stub.upload.types.carbon.UploadedFileItem import org.wso2.carbon.base.ServerConfiguration; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.core.util.Utils; -import org.wso2.carbon.device.mgt.jaxrs.service.api.admin.DeviceTypePublisherAdminService; +import org.wso2.carbon.device.mgt.jaxrs.service.api.admin.DeviceAnalyticsArtifactUploaderAdminService; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import org.wso2.carbon.identity.jwt.client.extension.JWTClient; import org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientException; -import org.wso2.carbon.registry.core.Registry; -import org.wso2.carbon.registry.core.Resource; -import org.wso2.carbon.registry.core.ResourceImpl; import org.wso2.carbon.registry.core.exceptions.RegistryException; import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.utils.CarbonUtils; @@ -55,7 +52,6 @@ import javax.activation.DataHandler; import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManagerFactory; -import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.PathParam; @@ -73,7 +69,7 @@ import java.util.ArrayList; import java.util.List; @Path("/admin/devicetype") -public class DeviceTypePublisherAdminServiceImpl implements DeviceTypePublisherAdminService { +public class DeviceAnalyticsArtifactUploaderAdminServiceImpl implements DeviceAnalyticsArtifactUploaderAdminService { /** * required soap header for authorization @@ -110,7 +106,7 @@ public class DeviceTypePublisherAdminServiceImpl implements DeviceTypePublisherA private String tenantDomain; - private static final Log log = LogFactory.getLog(DeviceTypePublisherAdminServiceImpl.class); + private static final Log log = LogFactory.getLog(DeviceAnalyticsArtifactUploaderAdminServiceImpl.class); private static final String DEFAULT_RESOURCE_LOCATION = "/resources/devicetypes"; private static final String CAR_FILE_LOCATION = CarbonUtils.getCarbonHome() + File.separator + "repository" + File.separator + "resources" + File.separator + "devicetypes"; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml index fd6c5e0309..e33d1beb26 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml @@ -40,7 +40,7 @@ - + @@ -82,7 +82,7 @@ - + From 3feb6c70dc82b9289dc0581a20141c44631a34ad Mon Sep 17 00:00:00 2001 From: ayyoob Date: Tue, 9 May 2017 00:54:34 +0530 Subject: [PATCH 2/8] created per tenant push notification for each transport type --- .../mqtt/MQTTNotificationStrategy.java | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) 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 8af53af6b2..9a7850e1e5 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 @@ -52,7 +52,7 @@ public class MQTTNotificationStrategy implements NotificationStrategy { OutputEventAdapterConfiguration adapterConfig = new OutputEventAdapterConfiguration(); adapterConfig.setType(MQTTAdapterConstants.MQTT_ADAPTER_TYPE); mqttAdapterName = config.getProperty(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_NAME); - adapterConfig.setName(mqttAdapterName); + adapterConfig.setMessageFormat(MessageType.TEXT); Map configProperties = new HashMap(); @@ -60,16 +60,23 @@ public class MQTTNotificationStrategy implements NotificationStrategy { if (brokerUrl != null && !brokerUrl.isEmpty()) { configProperties.put(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_BROKER_URL, brokerUrl); } - 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)); - configProperties.put(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_CLEAR_SESSION, - config.getProperty(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_CLEAR_SESSION)); - configProperties.put(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_SCOPES, - config.getProperty(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_SCOPES)); - configProperties.put(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_MESSAGE_QOS, - config.getProperty(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_MESSAGE_QOS)); + if (config.getProperties() != null) { + mqttAdapterName = config.getProperty(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_NAME); + 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)); + configProperties.put(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_CLEAR_SESSION, + config.getProperty(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_CLEAR_SESSION)); + configProperties.put(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_SCOPES, + config.getProperty(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_SCOPES)); + configProperties.put(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_MESSAGE_QOS, + config.getProperty(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_MESSAGE_QOS)); + } else { + mqttAdapterName = "mqtt.adapter." + PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain() + .toLowerCase(); + } + adapterConfig.setName(mqttAdapterName); adapterConfig.setStaticProperties(configProperties); try { MQTTDataHolder.getInstance().getOutputEventAdapterService().create(adapterConfig); From b36ff4b874cfc00a4dfffa8f72b20e414a1c2b78 Mon Sep 17 00:00:00 2001 From: ayyoob Date: Tue, 9 May 2017 00:54:49 +0530 Subject: [PATCH 3/8] added a authorisation resource for stat --- ...DeviceAccessAuthorizationAdminService.java | 39 +++++++++++++++++++ ...ceAccessAuthorizationAdminServiceImpl.java | 12 ++++++ 2 files changed, 51 insertions(+) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceAccessAuthorizationAdminService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceAccessAuthorizationAdminService.java index 8a46b9b194..6a4b12d72e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceAccessAuthorizationAdminService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceAccessAuthorizationAdminService.java @@ -115,4 +115,43 @@ public interface DeviceAccessAuthorizationAdminService { response = ErrorResponse.class) }) Response isAuthorized(AuthorizationRequest authorizationRequest); + + @POST + @Path("/stat") + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "POST", + value = "Check for device access authorization for stat\n", + notes = "This is an internal API that can be used to check for authorization.", + response = DeviceAuthorizationResult.class, + tags = "Authorization Administrative Service", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = Constants.SCOPE, value = "perm:authorization:verify") + }) + }) + + @ApiResponses(value = { + @ApiResponse( + code = 200, + message = "OK. \n Authorized device list will be delivered to the requested services", + response = DeviceAuthorizationResult.class), + @ApiResponse( + code = 400, + message = "Bad Request. \n Invalid request or validation error.", + response = ErrorResponse.class), + @ApiResponse( + code = 404, + message = "Not Found. \n The specified resource does not exist."), + @ApiResponse( + code = 415, + message = "Unsupported media type. \n The entity of the request was in a not supported format."), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Server error occurred while checking the authorization" + + " for a specified set of devices.", + response = ErrorResponse.class) + }) + Response isAuthorizedForStat(AuthorizationRequest authorizationRequest); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceAccessAuthorizationAdminServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceAccessAuthorizationAdminServiceImpl.java index fc3fe169ba..57c1ce5f69 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceAccessAuthorizationAdminServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceAccessAuthorizationAdminServiceImpl.java @@ -36,6 +36,8 @@ import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; +import java.util.ArrayList; +import java.util.List; @Path("/admin/authorization") @Produces(MediaType.APPLICATION_JSON) @@ -90,4 +92,14 @@ public class DeviceAccessAuthorizationAdminServiceImpl implements DeviceAccessAu } } + @POST + @Path("/stat") + @Override + public Response isAuthorizedForStat(AuthorizationRequest authorizationRequest) { + List permissions = new ArrayList<>(); + permissions.add("/permission/admin/device-mgt/device/realtime_analytics"); + authorizationRequest.setPermissions(permissions); + return isAuthorizedForStat(authorizationRequest); + } + } From ea48ee425ca87aef853afbcfe3f39929f279f75c Mon Sep 17 00:00:00 2001 From: ayyoob Date: Tue, 9 May 2017 16:09:24 +0530 Subject: [PATCH 4/8] added default configuration for tenant specific push notification provider --- .../template/DeviceTypeManagerService.java | 8 +++++-- .../mqtt/MQTTNotificationStrategy.java | 21 ++++++++++++------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/DeviceTypeManagerService.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/DeviceTypeManagerService.java index e87d0d330c..0ee091ce2a 100644 --- a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/DeviceTypeManagerService.java +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/DeviceTypeManagerService.java @@ -113,8 +113,12 @@ public class DeviceTypeManagerService implements DeviceManagementService { if (pushNotificationProvider != null) { if (pushNotificationProvider.isFileBasedProperties()) { Map staticProps = new HashMap<>(); - for (Property property : pushNotificationProvider.getConfigProperties().getProperty()) { - staticProps.put(property.getName(), property.getValue()); + if (pushNotificationProvider.getConfigProperties() != null && + pushNotificationProvider.getConfigProperties().getProperty() != null && + pushNotificationProvider.getConfigProperties().getProperty().size() > 0) { + for (Property property : pushNotificationProvider.getConfigProperties().getProperty()) { + staticProps.put(property.getName(), property.getValue()); + } } pushNotificationConfig = new PushNotificationConfig(pushNotificationProvider.getType(), pushNotificationProvider.isScheduled(), staticProps); 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 9a7850e1e5..288b902172 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 @@ -51,16 +51,14 @@ public class MQTTNotificationStrategy implements NotificationStrategy { this.config = config; OutputEventAdapterConfiguration adapterConfig = new OutputEventAdapterConfiguration(); adapterConfig.setType(MQTTAdapterConstants.MQTT_ADAPTER_TYPE); - mqttAdapterName = config.getProperty(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_NAME); - adapterConfig.setMessageFormat(MessageType.TEXT); Map configProperties = new HashMap(); - String brokerUrl = config.getProperty(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_BROKER_URL); - if (brokerUrl != null && !brokerUrl.isEmpty()) { - configProperties.put(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_BROKER_URL, brokerUrl); - } - if (config.getProperties() != null) { + if (config.getProperties() != null && config.getProperties().size() > 0) { + String brokerUrl = config.getProperty(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_BROKER_URL); + if (brokerUrl != null && !brokerUrl.isEmpty()) { + configProperties.put(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_BROKER_URL, brokerUrl); + } mqttAdapterName = config.getProperty(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_NAME); configProperties.put(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_USERNAME, config.getProperty(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_USERNAME)); @@ -79,7 +77,14 @@ public class MQTTNotificationStrategy implements NotificationStrategy { adapterConfig.setName(mqttAdapterName); adapterConfig.setStaticProperties(configProperties); try { - MQTTDataHolder.getInstance().getOutputEventAdapterService().create(adapterConfig); + synchronized (MQTTNotificationStrategy.class) { + try { + MQTTDataHolder.getInstance().getOutputEventAdapterService().isPolled(mqttAdapterName); + } catch (OutputEventAdapterException e) { + //event adapter not created + MQTTDataHolder.getInstance().getOutputEventAdapterService().create(adapterConfig); + } + } } catch (OutputEventAdapterException e) { throw new InvalidConfigurationException("Error occurred while initializing MQTT output event adapter", e); } From cb9db4dd1d14b3c44559759e7113283a9b88c0e6 Mon Sep 17 00:00:00 2001 From: ayyoob Date: Tue, 9 May 2017 16:16:14 +0530 Subject: [PATCH 5/8] fixed null pointer in the initial operations --- .../DeviceManagementProviderServiceImpl.java | 35 ++++++++++--------- 1 file changed, 19 insertions(+), 16 deletions(-) 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 6672311c3d..083b795a9d 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 @@ -2114,22 +2114,25 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv deviceIdentifiers.add(deviceIdentifier); if (init != null) { List initialOperations = init.getOperations(); - - for (String str : initialOperations) { - CommandOperation operation = new CommandOperation(); - operation.setEnabled(true); - operation.setType(Operation.Type.COMMAND); - operation.setCode(str); - try { - deviceManagementProviderService. - addOperation(deviceType, - operation, deviceIdentifiers); - } catch (OperationManagementException e) { - throw new DeviceManagementException("Unable to find the device with the id: '" + deviceIdentifier.getId(), - e); - } catch (InvalidDeviceException e) { - throw new DeviceManagementException("Unable to find the device with the id: '" + deviceIdentifier.getId(), - e); + if (initialOperations != null) { + for (String str : initialOperations) { + CommandOperation operation = new CommandOperation(); + operation.setEnabled(true); + operation.setType(Operation.Type.COMMAND); + operation.setCode(str); + try { + deviceManagementProviderService. + addOperation(deviceType, + operation, deviceIdentifiers); + } catch (OperationManagementException e) { + throw new DeviceManagementException( + "Unable to find the device with the id: '" + deviceIdentifier.getId(), + e); + } catch (InvalidDeviceException e) { + throw new DeviceManagementException( + "Unable to find the device with the id: '" + deviceIdentifier.getId(), + e); + } } } } From 035403495c43283c390b15933237262abec16ac2 Mon Sep 17 00:00:00 2001 From: ayyoob Date: Tue, 9 May 2017 16:45:47 +0530 Subject: [PATCH 6/8] made default stat permission as constant --- .../admin/DeviceAccessAuthorizationAdminServiceImpl.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceAccessAuthorizationAdminServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceAccessAuthorizationAdminServiceImpl.java index 57c1ce5f69..035caa3a18 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceAccessAuthorizationAdminServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceAccessAuthorizationAdminServiceImpl.java @@ -45,6 +45,7 @@ import java.util.List; public class DeviceAccessAuthorizationAdminServiceImpl implements DeviceAccessAuthorizationAdminService { private static final Log log = LogFactory.getLog(DeviceAccessAuthorizationAdminServiceImpl.class); + private static final String DEFAULT_STAT_PERMISSION = "/permission/admin/device-mgt/device/realtime_analytics"; @POST @Override @@ -97,9 +98,9 @@ public class DeviceAccessAuthorizationAdminServiceImpl implements DeviceAccessAu @Override public Response isAuthorizedForStat(AuthorizationRequest authorizationRequest) { List permissions = new ArrayList<>(); - permissions.add("/permission/admin/device-mgt/device/realtime_analytics"); + permissions.add(DEFAULT_STAT_PERMISSION); authorizationRequest.setPermissions(permissions); - return isAuthorizedForStat(authorizationRequest); + return isAuthorized(authorizationRequest); } } From 1c3a89f57c381594ccdf554bb888f0db6e1d588b Mon Sep 17 00:00:00 2001 From: ayyoob Date: Wed, 10 May 2017 13:49:20 +0530 Subject: [PATCH 7/8] fixed issues raised through comments - commits --- .../template/DeviceTypeManagerService.java | 13 ++++++++----- .../DeviceManagementProviderServiceImpl.java | 14 +++++--------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/DeviceTypeManagerService.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/DeviceTypeManagerService.java index 0ee091ce2a..6db5d88054 100644 --- a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/DeviceTypeManagerService.java +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/DeviceTypeManagerService.java @@ -32,6 +32,7 @@ import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration import org.wso2.carbon.device.mgt.common.policy.mgt.PolicyMonitoringManager; import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; +import org.wso2.carbon.device.mgt.extensions.device.type.deployer.config.ConfigProperties; import org.wso2.carbon.device.mgt.extensions.device.type.deployer.config.DeviceTypeConfiguration; import org.wso2.carbon.device.mgt.extensions.device.type.deployer.config.Property; import org.wso2.carbon.device.mgt.extensions.device.type.deployer.config.PushNotificationProvider; @@ -113,11 +114,13 @@ public class DeviceTypeManagerService implements DeviceManagementService { if (pushNotificationProvider != null) { if (pushNotificationProvider.isFileBasedProperties()) { Map staticProps = new HashMap<>(); - if (pushNotificationProvider.getConfigProperties() != null && - pushNotificationProvider.getConfigProperties().getProperty() != null && - pushNotificationProvider.getConfigProperties().getProperty().size() > 0) { - for (Property property : pushNotificationProvider.getConfigProperties().getProperty()) { - staticProps.put(property.getName(), property.getValue()); + ConfigProperties configProperties = pushNotificationProvider.getConfigProperties(); + if (configProperties != null) { + List properties = configProperties.getProperty(); + if (properties != null && properties.size() > 0) { + for (Property property : properties) { + staticProps.put(property.getName(), property.getValue()); + } } } pushNotificationConfig = new PushNotificationConfig(pushNotificationProvider.getType(), 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 083b795a9d..13a5547687 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 @@ -2121,17 +2121,13 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv operation.setType(Operation.Type.COMMAND); operation.setCode(str); try { - deviceManagementProviderService. - addOperation(deviceType, - operation, deviceIdentifiers); + deviceManagementProviderService.addOperation(deviceType, operation, deviceIdentifiers); } catch (OperationManagementException e) { - throw new DeviceManagementException( - "Unable to find the device with the id: '" + deviceIdentifier.getId(), - e); + throw new DeviceManagementException("Unable to add the operation for the device with the id: '" + + deviceIdentifier.getId(), e); } catch (InvalidDeviceException e) { - throw new DeviceManagementException( - "Unable to find the device with the id: '" + deviceIdentifier.getId(), - e); + throw new DeviceManagementException("Unable to find the device with the id: '" + + deviceIdentifier.getId(), e); } } } From d2d7be6fd6829afa918404c2bc3a5bd94c3ebd58 Mon Sep 17 00:00:00 2001 From: ayyoob Date: Wed, 10 May 2017 13:50:06 +0530 Subject: [PATCH 8/8] fixed tenant related issue for mqtt push notification and stream artifact publishing --- .../mqtt/MQTTNotificationStrategy.java | 42 ++++++++++++++++--- ...yticsArtifactUploaderAdminServiceImpl.java | 3 ++ 2 files changed, 40 insertions(+), 5 deletions(-) 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 288b902172..121599324a 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 @@ -46,6 +46,8 @@ public class MQTTNotificationStrategy implements NotificationStrategy { private String mqttAdapterName; private static final Log log = LogFactory.getLog(MQTTNotificationStrategy.class); private final PushNotificationConfig config; + private final String providerTenantDomain; + private static final Object lockObj = new Object(); public MQTTNotificationStrategy(PushNotificationConfig config) { this.config = config; @@ -77,7 +79,7 @@ public class MQTTNotificationStrategy implements NotificationStrategy { adapterConfig.setName(mqttAdapterName); adapterConfig.setStaticProperties(configProperties); try { - synchronized (MQTTNotificationStrategy.class) { + synchronized (lockObj) { try { MQTTDataHolder.getInstance().getOutputEventAdapterService().isPolled(mqttAdapterName); } catch (OutputEventAdapterException e) { @@ -88,6 +90,8 @@ public class MQTTNotificationStrategy implements NotificationStrategy { } catch (OutputEventAdapterException e) { throw new InvalidConfigurationException("Error occurred while initializing MQTT output event adapter", e); } + providerTenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain() + .toLowerCase(); } @Override @@ -97,19 +101,47 @@ public class MQTTNotificationStrategy implements NotificationStrategy { @Override public void execute(NotificationContext ctx) throws PushNotificationExecutionFailedException { + String adapterName = mqttAdapterName; + String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(true); + if (!providerTenantDomain.equals(tenantDomain)) { + //this is to handle the device type shared with all tenant mode. + + adapterName = "mqtt.adapter." + PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain() + .toLowerCase(); + try { + MQTTDataHolder.getInstance().getOutputEventAdapterService().isPolled(adapterName); + } catch (OutputEventAdapterException e) { + //event adapter not created + synchronized (lockObj) { + OutputEventAdapterConfiguration adapterConfig = new OutputEventAdapterConfiguration(); + adapterConfig.setType(MQTTAdapterConstants.MQTT_ADAPTER_TYPE); + adapterConfig.setMessageFormat(MessageType.TEXT); + adapterConfig.setName(adapterName); + Map configProperties = new HashMap(); + adapterConfig.setStaticProperties(configProperties); + try { + MQTTDataHolder.getInstance().getOutputEventAdapterService().create(adapterConfig); + } catch (OutputEventAdapterException e1) { + throw new PushNotificationExecutionFailedException + ("Error occurred while initializing MQTT output event adapter for shared tenant: " + + tenantDomain, e); + } + } + } + + } 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, + MQTTDataHolder.getInstance().getOutputEventAdapterService().publish(adapterName, dynamicProperties, operation.getPayLoad()); } else { 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) { @@ -118,7 +150,7 @@ public class MQTTNotificationStrategy implements NotificationStrategy { + deviceType + "/" + deviceId + "/" + profileOperation.getType() .toString().toLowerCase() + "/" + profileOperation.getCode().toLowerCase(); dynamicProperties.put("topic", topic); - MQTTDataHolder.getInstance().getOutputEventAdapterService().publish(mqttAdapterName, dynamicProperties, + MQTTDataHolder.getInstance().getOutputEventAdapterService().publish(adapterName, dynamicProperties, profileOperation.getPayLoad()); } @@ -131,7 +163,7 @@ public class MQTTNotificationStrategy implements NotificationStrategy { if (operation.getPayLoad() == null) { operation.setPayLoad(""); } - MQTTDataHolder.getInstance().getOutputEventAdapterService().publish(mqttAdapterName, dynamicProperties, + MQTTDataHolder.getInstance().getOutputEventAdapterService().publish(adapterName, dynamicProperties, operation.getPayLoad()); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceAnalyticsArtifactUploaderAdminServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceAnalyticsArtifactUploaderAdminServiceImpl.java index 9a73a99913..27e74549b0 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceAnalyticsArtifactUploaderAdminServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceAnalyticsArtifactUploaderAdminServiceImpl.java @@ -174,6 +174,9 @@ public class DeviceAnalyticsArtifactUploaderAdminServiceImpl implements DeviceAn publishDynamicEventReceivers(type, MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, receiverFileList); } } + if (streamFileList != null) { + publishDynamicEventStream(type, tenantDomain, streamFileList); + } if (deployAnalyticsCapp(type, list)){ return Response.status(Response.Status.BAD_REQUEST) .entity("\"Error, Artifact does not exist.\"").build();