|
|
|
@ -22,16 +22,21 @@ import org.apache.commons.lang.StringUtils;
|
|
|
|
|
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;
|
|
|
|
@ -40,12 +45,17 @@ 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.report.mgt.Constants;
|
|
|
|
|
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;
|
|
|
|
@ -58,6 +68,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();
|
|
|
|
@ -79,6 +90,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(),
|
|
|
|
@ -117,18 +129,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()};
|
|
|
|
@ -174,14 +174,58 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
|
|
|
|
|
} 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 = HttpReportingUtil.getReportingHost();
|
|
|
|
|
if (!StringUtils.isBlank(reportingHost) && isPublishingEnabledForTenant()) {
|
|
|
|
|
try {
|
|
|
|
|
DeviceDetailsWrapper deviceDetailsWrapper = new DeviceDetailsWrapper();
|
|
|
|
|
deviceDetailsWrapper.setDevice(device);
|
|
|
|
|
deviceDetailsWrapper.setDeviceInfo(deviceInfo);
|
|
|
|
|
deviceDetailsWrapper.setTenantId(DeviceManagerUtil.getTenantId());
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if(log.isTraceEnabled()) {
|
|
|
|
|
log.trace("Event publishing is not enabled for tenant "
|
|
|
|
|
+ DeviceManagerUtil.getTenantId());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
@ -426,6 +470,22 @@ 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate and add a value depending on the device's OS version included in device info
|
|
|
|
|
*
|
|
|
|
|