|
|
|
@ -21,16 +21,21 @@ package org.wso2.carbon.device.mgt.core.device.details.mgt.impl;
|
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
|
import org.wso2.carbon.context.CarbonContext;
|
|
|
|
|
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
|
|
|
|
import org.wso2.carbon.device.mgt.analytics.data.publisher.exception.DataPublisherConfigurationException;
|
|
|
|
|
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.DeviceDetailsWrapper;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.exceptions.EventPublishingException;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.exceptions.TransactionManagementException;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException;
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.DeviceManagementConstants;
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.config.tenant.PlatformConfigurationManagementServiceImpl;
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.dao.DeviceDAO;
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
|
|
|
|
@ -39,11 +44,15 @@ import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceInformationManag
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsDAO;
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsMgtDAOException;
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService;
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil;
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.util.HttpReportingUtil;
|
|
|
|
|
import org.wso2.carbon.user.api.UserRealm;
|
|
|
|
|
import org.wso2.carbon.user.api.UserStoreException;
|
|
|
|
|
|
|
|
|
|
import java.sql.SQLException;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.Calendar;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
@ -56,6 +65,7 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
|
|
|
|
|
private static final Log log = LogFactory.getLog(DeviceInformationManagerImpl.class);
|
|
|
|
|
private static final String LOCATION_EVENT_STREAM_DEFINITION = "org.wso2.iot.LocationStream";
|
|
|
|
|
private static final String DEVICE_INFO_EVENT_STREAM_DEFINITION = "org.wso2.iot.DeviceInfoStream";
|
|
|
|
|
private static final String IS_EVENT_PUBLISHING_ENABED = "isEventPublishingEnabled";
|
|
|
|
|
|
|
|
|
|
public DeviceInformationManagerImpl() {
|
|
|
|
|
this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO();
|
|
|
|
@ -77,6 +87,7 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
|
|
|
|
|
@Override
|
|
|
|
|
public void addDeviceInfo(Device device, DeviceInfo deviceInfo) throws DeviceDetailsMgtException {
|
|
|
|
|
try {
|
|
|
|
|
publishEvents(device, deviceInfo);
|
|
|
|
|
DeviceManagementDAOFactory.beginTransaction();
|
|
|
|
|
DeviceInfo newDeviceInfo;
|
|
|
|
|
DeviceInfo previousDeviceInfo = deviceDetailsDAO.getDeviceInformation(device.getId(),
|
|
|
|
@ -113,18 +124,6 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
|
|
|
|
|
deviceDAO.updateDevice(device, CarbonContext.getThreadLocalCarbonContext().getTenantId());
|
|
|
|
|
DeviceManagementDAOFactory.commitTransaction();
|
|
|
|
|
|
|
|
|
|
String reportingHost = System.getProperty(DeviceManagementConstants.Report
|
|
|
|
|
.REPORTING_EVENT_HOST);
|
|
|
|
|
if (reportingHost != null && !reportingHost.isEmpty()) {
|
|
|
|
|
DeviceDetailsWrapper deviceDetailsWrapper = new DeviceDetailsWrapper();
|
|
|
|
|
deviceDetailsWrapper.setDevice(device);
|
|
|
|
|
deviceDetailsWrapper.setDeviceInfo(deviceInfo);
|
|
|
|
|
deviceDetailsWrapper.getJSONString();
|
|
|
|
|
|
|
|
|
|
HttpReportingUtil.invokeApi(deviceDetailsWrapper.getJSONString(),
|
|
|
|
|
reportingHost + DeviceManagementConstants.Report.DEVICE_INFO_ENDPOINT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//TODO :: This has to be fixed by adding the enrollment ID.
|
|
|
|
|
if (DeviceManagerUtil.isPublishDeviceInfoResponseEnabled()) {
|
|
|
|
|
Object[] metaData = {device.getDeviceIdentifier(), device.getType()};
|
|
|
|
@ -166,18 +165,58 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
|
|
|
|
|
} catch (DeviceManagementDAOException e) {
|
|
|
|
|
DeviceManagementDAOFactory.rollbackTransaction();
|
|
|
|
|
throw new DeviceDetailsMgtException("Error occurred while updating the last update timestamp of the " +
|
|
|
|
|
"device", e);
|
|
|
|
|
"device", e);
|
|
|
|
|
} catch (DataPublisherConfigurationException e) {
|
|
|
|
|
DeviceManagementDAOFactory.rollbackTransaction();
|
|
|
|
|
throw new DeviceDetailsMgtException("Error occurred while publishing the device location information.", e);
|
|
|
|
|
} catch (EventPublishingException e) {
|
|
|
|
|
DeviceManagementDAOFactory.rollbackTransaction();
|
|
|
|
|
throw new DeviceDetailsMgtException("Error occurred while sending events", e);
|
|
|
|
|
} finally {
|
|
|
|
|
DeviceManagementDAOFactory.closeConnection();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void publishEvents(Device device, DeviceInfo deviceInfo) {
|
|
|
|
|
String reportingHost = System.getProperty(DeviceManagementConstants.Report
|
|
|
|
|
.REPORTING_EVENT_HOST);
|
|
|
|
|
if (reportingHost != null && !reportingHost.isEmpty() && isPublishingEnabledForTenant()) {
|
|
|
|
|
try {
|
|
|
|
|
DeviceDetailsWrapper deviceDetailsWrapper = new DeviceDetailsWrapper();
|
|
|
|
|
deviceDetailsWrapper.setDevice(device);
|
|
|
|
|
deviceDetailsWrapper.setDeviceInfo(deviceInfo);
|
|
|
|
|
deviceDetailsWrapper.getJSONString();
|
|
|
|
|
GroupManagementProviderService groupManagementService = DeviceManagementDataHolder
|
|
|
|
|
.getInstance().getGroupManagementProviderService();
|
|
|
|
|
|
|
|
|
|
List<DeviceGroup> groups = groupManagementService.getGroups(device, false);
|
|
|
|
|
if (groups != null && groups.size() > 0) {
|
|
|
|
|
deviceDetailsWrapper.setGroups(groups);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String[] rolesOfUser = getRolesOfUser(CarbonContext.getThreadLocalCarbonContext()
|
|
|
|
|
.getUsername());
|
|
|
|
|
if (rolesOfUser != null && rolesOfUser.length > 0) {
|
|
|
|
|
deviceDetailsWrapper.setRole(rolesOfUser);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HttpReportingUtil.invokeApi(deviceDetailsWrapper.getJSONString(),
|
|
|
|
|
reportingHost + DeviceManagementConstants.Report.DEVICE_INFO_ENDPOINT);
|
|
|
|
|
} catch (EventPublishingException e) {
|
|
|
|
|
log.error("Error occurred while sending events", e);
|
|
|
|
|
} catch (GroupManagementException e) {
|
|
|
|
|
log.error("Error occurred while getting group list", e);
|
|
|
|
|
} catch (UserStoreException e) {
|
|
|
|
|
log.error("Error occurred while getting role list", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean isPublishingEnabledForTenant() {
|
|
|
|
|
Object configuration = DeviceManagerUtil.getConfiguration(IS_EVENT_PUBLISHING_ENABED);
|
|
|
|
|
if (configuration != null) {
|
|
|
|
|
return Boolean.valueOf(configuration.toString());
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public DeviceInfo getDeviceInfo(DeviceIdentifier deviceId) throws DeviceDetailsMgtException {
|
|
|
|
|
Device device = getDevice(deviceId);
|
|
|
|
@ -420,5 +459,19 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
|
|
|
|
|
return newDeviceInfo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|