Merge branch 'reporting' into 'reporting'

Add group and roles

See merge request entgra/carbon-device-mgt!453
feature/appm-store/pbac
Inosh Perara 5 years ago
commit ed1fae5c08

@ -20,6 +20,7 @@ package org.wso2.carbon.device.mgt.common.device.details;
import com.google.gson.Gson; import com.google.gson.Gson;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.app.mgt.Application; 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.List;
@ -31,6 +32,25 @@ public class DeviceDetailsWrapper {
DeviceLocation location; DeviceLocation location;
int tenantId; int tenantId;
List<DeviceGroup> groups;
String [] role;
public List<DeviceGroup> getGroups() {
return groups;
}
public void setGroups(List<DeviceGroup> groups) {
this.groups = groups;
}
public String [] getRole() {
return role;
}
public void setRole(String [] role) {
this.role = role;
}
public int getTenantId() { public int getTenantId() {
return tenantId; return tenantId;
} }

@ -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.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.CarbonContext; 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.analytics.data.publisher.exception.DataPublisherConfigurationException;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; 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.device.details.DeviceDetailsWrapper;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; 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.EventPublishingException;
import org.wso2.carbon.device.mgt.common.exceptions.TransactionManagementException; 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.DeviceInfo;
import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation; 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.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.DeviceDAO;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; 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.DeviceDetailsDAO;
import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsMgtDAOException; 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.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.DeviceManagerUtil;
import org.wso2.carbon.device.mgt.core.util.HttpReportingUtil; 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.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar; import java.util.Calendar;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; 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 Log log = LogFactory.getLog(DeviceInformationManagerImpl.class);
private static final String LOCATION_EVENT_STREAM_DEFINITION = "org.wso2.iot.LocationStream"; 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 DEVICE_INFO_EVENT_STREAM_DEFINITION = "org.wso2.iot.DeviceInfoStream";
private static final String IS_EVENT_PUBLISHING_ENABED = "isEventPublishingEnabled";
public DeviceInformationManagerImpl() { public DeviceInformationManagerImpl() {
this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO();
@ -77,6 +87,7 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
@Override @Override
public void addDeviceInfo(Device device, DeviceInfo deviceInfo) throws DeviceDetailsMgtException { public void addDeviceInfo(Device device, DeviceInfo deviceInfo) throws DeviceDetailsMgtException {
try { try {
publishEvents(device, deviceInfo);
DeviceManagementDAOFactory.beginTransaction(); DeviceManagementDAOFactory.beginTransaction();
DeviceInfo newDeviceInfo; DeviceInfo newDeviceInfo;
DeviceInfo previousDeviceInfo = deviceDetailsDAO.getDeviceInformation(device.getId(), DeviceInfo previousDeviceInfo = deviceDetailsDAO.getDeviceInformation(device.getId(),
@ -113,18 +124,6 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
deviceDAO.updateDevice(device, CarbonContext.getThreadLocalCarbonContext().getTenantId()); deviceDAO.updateDevice(device, CarbonContext.getThreadLocalCarbonContext().getTenantId());
DeviceManagementDAOFactory.commitTransaction(); 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. //TODO :: This has to be fixed by adding the enrollment ID.
if (DeviceManagerUtil.isPublishDeviceInfoResponseEnabled()) { if (DeviceManagerUtil.isPublishDeviceInfoResponseEnabled()) {
Object[] metaData = {device.getDeviceIdentifier(), device.getType()}; Object[] metaData = {device.getDeviceIdentifier(), device.getType()};
@ -170,14 +169,54 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
} catch (DataPublisherConfigurationException e) { } catch (DataPublisherConfigurationException e) {
DeviceManagementDAOFactory.rollbackTransaction(); DeviceManagementDAOFactory.rollbackTransaction();
throw new DeviceDetailsMgtException("Error occurred while publishing the device location information.", e); 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 { } finally {
DeviceManagementDAOFactory.closeConnection(); 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 @Override
public DeviceInfo getDeviceInfo(DeviceIdentifier deviceId) throws DeviceDetailsMgtException { public DeviceInfo getDeviceInfo(DeviceIdentifier deviceId) throws DeviceDetailsMgtException {
Device device = getDevice(deviceId); Device device = getDevice(deviceId);
@ -420,5 +459,19 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
return newDeviceInfo; 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;
}
} }

@ -243,6 +243,16 @@ public interface GroupManagementProviderService {
*/ */
List<DeviceGroup> getGroups(DeviceIdentifier deviceIdentifier, boolean requireGroupProps) throws GroupManagementException; List<DeviceGroup> getGroups(DeviceIdentifier deviceIdentifier, boolean requireGroupProps) throws GroupManagementException;
/**
* Get groups which contains particular device.
*
* @param device interested devoce.
* @return groups contain the device.
* @throws GroupManagementException
*/
public List<DeviceGroup> getGroups(Device device, boolean requireGroupProps)
throws GroupManagementException;
/** /**
* Checks for the default group existence and create group based on device ownership. * Checks for the default group existence and create group based on device ownership.
* @param groupName of the group * @param groupName of the group

@ -938,6 +938,42 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
} }
} }
@Override
public List<DeviceGroup> getGroups(Device device, boolean requireGroupProps)
throws GroupManagementException {
if (device.getDeviceIdentifier() == null) {
String msg = "Received empty device identifier for getGroups";
log.error(msg);
throw new GroupManagementException(msg);
}
if (log.isDebugEnabled()) {
log.debug("Get groups of device " + device.getDeviceIdentifier());
}
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
GroupManagementDAOFactory.openConnection();
List<DeviceGroup> deviceGroups = groupDAO.getGroups(device.getId(), tenantId);
if (requireGroupProps) {
if (deviceGroups != null && !deviceGroups.isEmpty()) {
for (DeviceGroup group : deviceGroups) {
populateGroupProperties(group, tenantId);
}
}
}
return deviceGroups;
} catch (GroupManagementDAOException | SQLException e) {
String msg = "Error occurred while retrieving device groups.";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (Exception e) {
String msg = "Error occurred in getGroups";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} finally {
GroupManagementDAOFactory.closeConnection();
}
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */

@ -38,6 +38,10 @@ import org.wso2.carbon.device.mgt.common.ApplicationRegistration;
import org.wso2.carbon.device.mgt.common.ApplicationRegistrationException; import org.wso2.carbon.device.mgt.common.ApplicationRegistrationException;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationEntry;
import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationManagementException;
import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration;
import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfigurationManagementService;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.GroupPaginationRequest; import org.wso2.carbon.device.mgt.common.GroupPaginationRequest;
@ -53,6 +57,8 @@ import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager;
import org.wso2.carbon.device.mgt.core.config.DeviceManagementConfig; import org.wso2.carbon.device.mgt.core.config.DeviceManagementConfig;
import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig; import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig;
import org.wso2.carbon.device.mgt.core.config.datasource.JNDILookupDefinition; import org.wso2.carbon.device.mgt.core.config.datasource.JNDILookupDefinition;
import org.wso2.carbon.device.mgt.core.config.policy.PolicyConfiguration;
import org.wso2.carbon.device.mgt.core.config.tenant.PlatformConfigurationManagementServiceImpl;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO; import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO;
@ -95,6 +101,7 @@ import java.util.concurrent.TimeUnit;
public final class DeviceManagerUtil { public final class DeviceManagerUtil {
private static final Log log = LogFactory.getLog(DeviceManagerUtil.class); private static final Log log = LogFactory.getLog(DeviceManagerUtil.class);
public static final String GENERAL_CONFIG_RESOURCE_PATH = "general";
private static boolean isDeviceCacheInitialized = false; private static boolean isDeviceCacheInitialized = false;
@ -694,4 +701,28 @@ public final class DeviceManagerUtil {
JWTClient jwtClient = jwtClientManagerService.getJWTClient(); JWTClient jwtClient = jwtClientManagerService.getJWTClient();
return jwtClient.getAccessToken(clientId, clientSecret, deviceOwner, scopes); return jwtClient.getAccessToken(clientId, clientSecret, deviceOwner, scopes);
} }
public static Object getConfiguration(String key) {
PlatformConfigurationManagementService configMgtService =
new PlatformConfigurationManagementServiceImpl();
try {
PlatformConfiguration tenantConfiguration = configMgtService.getConfiguration
(GENERAL_CONFIG_RESOURCE_PATH);
List<ConfigurationEntry> configuration = tenantConfiguration.getConfiguration();
if (configuration != null && !configuration.isEmpty()) {
for (ConfigurationEntry cEntry : configuration) {
if (key.equalsIgnoreCase(cEntry.getName())) {
return cEntry.getValue();
}
}
}
} catch (ConfigurationManagementException e) {
log.error("Error while getting the configurations from registry.", e);
return null;
}
return null;
}
} }

@ -109,6 +109,21 @@
class="form-control" class="form-control"
placeholder="[ Required Field ]"> placeholder="[ Required Field ]">
</div> </div>
<div class="wr-input-control">
<label class="wr-input-label" for="publish-for-analytics">
Publish For Analytics
<span class="helper" title="Should publish events">
<span class="wr-help-tip glyphicon glyphicon-question-sign"></span>
</span>
</label>
<select id="publish-for-analytics"
class="form-control">
<option value="false" selected="selected">
false
</option>
<option value="true">true</option>
</select>
</div>
<div class="wr-input-control wr-btn-grp"> <div class="wr-input-control wr-btn-grp">
<button id="save-general-btn" class="wr-btn">Save</button> <button id="save-general-btn" class="wr-btn">Save</button>
</div> </div>

@ -22,7 +22,8 @@ $(document).ready(function () {
var configParams = { var configParams = {
"NOTIFIER_TYPE": "notifierType", "NOTIFIER_TYPE": "notifierType",
"NOTIFIER_FREQUENCY": "notifierFrequency" "NOTIFIER_FREQUENCY": "notifierFrequency",
"IS_EVENT_PUBLISHING_ENABLED": "isEventPublishingEnabled"
}; };
var responseCodes = { var responseCodes = {
@ -51,6 +52,8 @@ $(document).ready(function () {
var config = data.configuration[i]; var config = data.configuration[i];
if (config.name == configParams["NOTIFIER_FREQUENCY"]) { if (config.name == configParams["NOTIFIER_FREQUENCY"]) {
$("input#monitoring-config-frequency").val(config.value / 1000); $("input#monitoring-config-frequency").val(config.value / 1000);
} else if (config.name == configParams["IS_EVENT_PUBLISHING_ENABLED"]) {
$("select#publish-for-analytics").val(config.value);
} }
} }
} }
@ -65,6 +68,7 @@ $(document).ready(function () {
*/ */
$("button#save-general-btn").click(function () { $("button#save-general-btn").click(function () {
var notifierFrequency = $("input#monitoring-config-frequency").val(); var notifierFrequency = $("input#monitoring-config-frequency").val();
var publishEvents = $("select#publish-for-analytics").val();
var errorMsgWrapper = "#email-config-error-msg"; var errorMsgWrapper = "#email-config-error-msg";
var errorMsg = "#email-config-error-msg span"; var errorMsg = "#email-config-error-msg span";
@ -84,6 +88,13 @@ $(document).ready(function () {
"contentType": "text" "contentType": "text"
}; };
var publishEventsDetails = {
"name": configParams["IS_EVENT_PUBLISHING_ENABLED"],
"value": publishEvents,
"contentType": "text"
};
configList.push(publishEventsDetails);
configList.push(monitorFrequency); configList.push(monitorFrequency);
addConfigFormData.configuration = configList; addConfigFormData.configuration = configList;

Loading…
Cancel
Save