diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/reporting/Stream.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/reporting/Stream.java new file mode 100644 index 00000000000..8fcc98c9cb8 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/reporting/Stream.java @@ -0,0 +1,41 @@ +/* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. 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.jaxrs.beans.analytics.reporting; + +import java.util.Map; + +public class Stream { + + String deviceIdentifier; + Map events; + + public String getDeviceIdentifier() { + return deviceIdentifier; + } + + public void setDeviceIdentifier(String deviceIdentifier) { + this.deviceIdentifier = deviceIdentifier; + } + + public Map getEvents() { + return events; + } + + public void setEvents(Map events) { + this.events = events; + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/RequestValidationUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/RequestValidationUtil.java index e024b951ff9..98871ee2b53 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/RequestValidationUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/RequestValidationUtil.java @@ -361,35 +361,30 @@ public class RequestValidationUtil { new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatus.SC_INTERNAL_SERVER_ERROR) .setMessage(msg).build()); } catch (InstantiationException e) { - String msg = "Error when creating an instance of validator related to deviceType " + deviceType; - log.error(msg, e); - throw new InputValidationException( - new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatus.SC_INTERNAL_SERVER_ERROR) - .setMessage(msg).build()); + if (log.isDebugEnabled()) { + String msg = "Error when creating an instance of validator related to deviceType " + deviceType; + log.debug(msg, e); + } } catch (IllegalAccessException e) { - String msg = "Error when accessing an instance of validator related to deviceType " + deviceType; - log.error(msg, e); - throw new InputValidationException( - new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatus.SC_INTERNAL_SERVER_ERROR) - .setMessage(msg).build()); + if (log.isDebugEnabled()) { + String msg = "Error when accessing an instance of validator related to deviceType " + deviceType; + log.debug(msg, e); + } } catch (ClassNotFoundException e) { - String msg = "Error when loading an instance of validator related to deviceType " + deviceType; - log.error(msg, e); - throw new InputValidationException( - new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatus.SC_INTERNAL_SERVER_ERROR) - .setMessage(msg).build()); + if (log.isDebugEnabled()) { + String msg = "Error when loading an instance of validator related to deviceType " + deviceType; + log.debug(msg, e); + } } catch (NoSuchMethodException e) { - String msg = "Error occurred while constructing validator related to deviceType " + deviceType; - log.error(msg, e); - throw new InputValidationException( - new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatus.SC_INTERNAL_SERVER_ERROR) - .setMessage(msg).build()); + if (log.isDebugEnabled()) { + String msg = "Error occurred while constructing validator related to deviceType " + deviceType; + log.debug(msg, e); + } } catch (InvocationTargetException e) { - String msg = "Error occurred while instantiating validator related to deviceType " + deviceType; - log.error(msg, e); - throw new InputValidationException( - new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatus.SC_INTERNAL_SERVER_ERROR) - .setMessage(msg).build()); + if (log.isDebugEnabled()) { + String msg = "Error occurred while instantiating validator related to deviceType " + deviceType; + log.debug(msg, e); + } } return features; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/DeviceDetailsWrapper.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/DeviceDetailsWrapper.java index 975d27b50fa..85622511f06 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/DeviceDetailsWrapper.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/DeviceDetailsWrapper.java @@ -23,6 +23,7 @@ import org.wso2.carbon.device.mgt.common.app.mgt.Application; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import java.util.List; +import java.util.Map; public class DeviceDetailsWrapper { @@ -30,6 +31,7 @@ public class DeviceDetailsWrapper { Device device; List applications; DeviceLocation location; + String events; int tenantId; List groups; @@ -91,6 +93,14 @@ public class DeviceDetailsWrapper { this.location = location; } + public String getEvents() { + return events; + } + + public void setEvents(String events) { + this.events = events; + } + public String getJSONString() { Gson gson = new Gson(); return gson.toJson(this).toString(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java index f6eb5182fe6..7f7610126b4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java @@ -43,6 +43,7 @@ public final class DeviceManagementConstants { public static final String DM_CACHE_MANAGER = "DM_CACHE_MANAGER"; public static final String DEVICE_CACHE = "DEVICE_CACHE"; public static final String ENROLLMENT_NOTIFICATION_API_ENDPOINT = "/api/device-mgt/enrollment-notification"; + public static final String URL_SEPERATOR = "/"; public static final class ConfigurationManagement { private ConfigurationManagement(){ @@ -164,8 +165,8 @@ public final class DeviceManagementConstants { throw new AssertionError(); } public static final String REPORTING_EVENT_HOST = "iot.reporting.event.host"; - public static final String REPORTING_CONTEXT = "/event"; - public static final String DEVICE_INFO_ENDPOINT = REPORTING_CONTEXT + "/device-info"; + public static final String REPORTING_CONTEXT = "/reporting/api/analyticsadmin/v1.0/event"; + public static final String DEVICE_INFO_PARAM = "device-info"; public static final String APP_USAGE_ENDPOINT = REPORTING_CONTEXT + "/app-usage"; } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/DeviceInformationManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/DeviceInformationManager.java index 7e1253ed21f..0e401955eac 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/DeviceInformationManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/DeviceInformationManager.java @@ -96,6 +96,18 @@ public interface DeviceInformationManager { */ List getDeviceLocations(List deviceIdentifiers) throws DeviceDetailsMgtException; + /** + * Send events to reporting backend + * @param deviceId device identifier of the reporting device + * @param deviceType device type of an device + * @param payload payload of the event + * @param eventType Event type being sent + * @return Http status code if a call is made and if failed to make a call 0 + * @throws DeviceDetailsMgtException + */ + int publishEvents(String deviceId, String deviceType, String payload, String eventType) + throws DeviceDetailsMgtException; + // /** // * This method will manage the storing of device application list. // * @param deviceApplication - Device application list. diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java index 5f8132df346..873441968e1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java @@ -27,6 +27,7 @@ import org.wso2.carbon.device.mgt.analytics.data.publisher.exception.DataPublish import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfigurationManagementService; +import org.wso2.carbon.device.mgt.common.device.details.DeviceData; import org.wso2.carbon.device.mgt.common.device.details.DeviceDetailsWrapper; import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.common.exceptions.EventPublishingException; @@ -91,7 +92,10 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { public void addDeviceInfo(Device device, DeviceInfo deviceInfo) throws DeviceDetailsMgtException { try { - publishEvents(device, deviceInfo); + DeviceDetailsWrapper deviceDetailsWrapper = new DeviceDetailsWrapper(); + deviceDetailsWrapper.setDeviceInfo(deviceInfo); + publishEvents(device, deviceDetailsWrapper, DeviceManagementConstants.Report.DEVICE_INFO_PARAM); + DeviceManagementDAOFactory.beginTransaction(); DeviceInfo newDeviceInfo; DeviceInfo previousDeviceInfo = deviceDetailsDAO.getDeviceInformation(device.getId(), @@ -180,14 +184,37 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { } } - private void publishEvents(Device device, DeviceInfo deviceInfo) { + public int publishEvents(String deviceId, String deviceType, String payload, String eventType) + throws DeviceDetailsMgtException { + + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(deviceId, deviceType); + + try { + Device device = DeviceManagementDataHolder.getInstance(). + getDeviceManagementProvider().getDevice(deviceIdentifier, false); + DeviceDetailsWrapper deviceDetailsWrapper = new DeviceDetailsWrapper(); + deviceDetailsWrapper.setEvents(payload); + return publishEvents(device, deviceDetailsWrapper, eventType); + } catch (DeviceManagementException e) { + DeviceManagementDAOFactory.rollbackTransaction(); + String msg = "Event publishing error. Could not get device " + deviceId; + log.error(msg, e); + throw new DeviceDetailsMgtException(msg, e); + } + } + + /** + * Send device details from core to reporting backend + * @param device Device that is sending event + * @param deviceDetailsWrapper Payload to send(example, deviceinfo, applist, raw events) + */ + private int publishEvents(Device device, DeviceDetailsWrapper deviceDetailsWrapper, String + eventType) { String reportingHost = HttpReportingUtil.getReportingHost(); if (!StringUtils.isBlank(reportingHost) && HttpReportingUtil.isPublishingEnabledForTenant()) { try { - DeviceDetailsWrapper deviceDetailsWrapper = new DeviceDetailsWrapper(); deviceDetailsWrapper.setDevice(device); - deviceDetailsWrapper.setDeviceInfo(deviceInfo); deviceDetailsWrapper.setTenantId(DeviceManagerUtil.getTenantId()); GroupManagementProviderService groupManagementService = DeviceManagementDataHolder .getInstance().getGroupManagementProviderService(); @@ -197,14 +224,16 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { deviceDetailsWrapper.setGroups(groups); } - String[] rolesOfUser = getRolesOfUser(CarbonContext.getThreadLocalCarbonContext() + String[] rolesOfUser = DeviceManagerUtil.getRolesOfUser(CarbonContext + .getThreadLocalCarbonContext() .getUsername()); if (rolesOfUser != null && rolesOfUser.length > 0) { deviceDetailsWrapper.setRole(rolesOfUser); } - HttpReportingUtil.invokeApi(deviceDetailsWrapper.getJSONString(), - reportingHost + DeviceManagementConstants.Report.DEVICE_INFO_ENDPOINT); + String eventUrl = reportingHost + DeviceManagementConstants.Report + .REPORTING_CONTEXT + DeviceManagementConstants.URL_SEPERATOR + eventType; + return HttpReportingUtil.invokeApi(deviceDetailsWrapper.getJSONString(), eventUrl); } catch (EventPublishingException e) { log.error("Error occurred while sending events", e); } catch (GroupManagementException e) { @@ -218,6 +247,7 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { + DeviceManagerUtil.getTenantId()); } } + return 0; } @Override @@ -524,19 +554,5 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { } } - private String[] getRolesOfUser(String userName) throws UserStoreException { - UserRealm userRealm = CarbonContext.getThreadLocalCarbonContext().getUserRealm(); - String[] roleList; - if (userRealm != null) { - userRealm.getUserStoreManager().getRoleNames(); - roleList = userRealm.getUserStoreManager().getRoleListOfUser(userName); - } else { - String msg = "User realm is not initiated. Logged in user: " + userName; - log.error(msg); - throw new UserStoreException(msg); - } - return roleList; - } - } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java index 049188bdd47..1f0f16d9fc0 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java @@ -32,6 +32,7 @@ import org.apache.http.protocol.HTTP; import org.w3c.dom.Document; import org.wso2.carbon.base.MultitenantConstants; import org.wso2.carbon.caching.impl.CacheImpl; +import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.analytics.data.publisher.service.EventsPublisherService; import org.wso2.carbon.device.mgt.common.AppRegistrationCredentials; @@ -76,6 +77,7 @@ import org.wso2.carbon.identity.jwt.client.extension.dto.AccessTokenInfo; import org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientException; import org.wso2.carbon.identity.jwt.client.extension.service.JWTClientManagerService; import org.wso2.carbon.user.api.TenantManager; +import org.wso2.carbon.user.api.UserRealm; import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.utils.CarbonUtils; import org.wso2.carbon.utils.ConfigurationContextService; @@ -1005,4 +1007,18 @@ public final class DeviceManagerUtil { } } + + public static String[] getRolesOfUser(String userName) throws UserStoreException { + UserRealm userRealm = CarbonContext.getThreadLocalCarbonContext().getUserRealm(); + String[] roleList; + if (userRealm != null) { + userRealm.getUserStoreManager().getRoleNames(); + roleList = userRealm.getUserStoreManager().getRoleListOfUser(userName); + } else { + String msg = "User realm is not initiated. Logged in user: " + userName; + log.error(msg); + throw new UserStoreException(msg); + } + return roleList; + } }