Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce the size of device info payload. #1261

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo.Status;
import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceDetailsMgtException;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.device.mgt.core.geo.GeoCluster;
import org.wso2.carbon.device.mgt.core.geo.geoHash.GeoCoordinate;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* This class represents the key operations associated with persisting device related information.
Expand Down Expand Up @@ -410,5 +412,22 @@ List<Device> getDevicesByStatus(PaginationRequest request, int tenantId)
*/
List<GeoCluster> findGeoClusters(String deviceType, GeoCoordinate southWest, GeoCoordinate northEast,
int geohashLength,int tenantId) throws DeviceManagementDAOException;
/**
* This method is used to retrieve the details of latest device info payload.
*
* @param deviceIdentifier the device identofore.
* @param tenantId tenant id.
* @return returns a map of device info objects.
*/
Map<String,String> getLatestDeviceInfoMap(DeviceIdentifier deviceIdentifier, int tenantId) throws DeviceDetailsMgtException;

/**
* This method is used to set the details of latest device info payload.
*
* @param deviceIdentifier the device identofore.
* @param tenantId tenant id.
*/
void setLatestDeviceInfoMap(DeviceIdentifier deviceIdentifier, int tenantId, Map<String,String> latestDeviceInfoMap, boolean isUpdate)
throws DeviceDetailsMgtException;
}

Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,16 @@
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.util.DeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceDetailsMgtException;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.device.mgt.core.geo.GeoCluster;
import org.wso2.carbon.device.mgt.core.geo.geoHash.GeoCoordinate;

import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
Expand All @@ -39,8 +45,8 @@
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public abstract class AbstractDeviceDAOImpl implements DeviceDAO {

Expand Down Expand Up @@ -1122,4 +1128,83 @@ public List<GeoCluster> findGeoClusters(String deviceType, GeoCoordinate southWe
}
return geoClusters;
}

@Override
public Map<String, String> getLatestDeviceInfoMap(DeviceIdentifier deviceIdentifier, int tenantId)
throws DeviceDetailsMgtException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
ByteArrayInputStream bais;
ObjectInputStream ois;
Map<String, String> deviceInfoMap = null;
byte[] deviceInfoPayloadByteArray = null;
try {
Device device = this.getDevice(deviceIdentifier, tenantId);
int deviceId = device.getId();
String sql = "select LATEST_OPERATION_RESPONSE FROM " +
shagihan marked this conversation as resolved.
Show resolved Hide resolved
"DM_DEVICE_INFO_OPERATION_RESPONSE WHERE DEVICE_ID = ?";
shagihan marked this conversation as resolved.
Show resolved Hide resolved
conn = this.getConnection();
stmt = conn.prepareStatement(sql);
stmt.setInt(1, deviceId);
rs = stmt.executeQuery();
if (rs.next()) {
deviceInfoPayloadByteArray = rs.getBytes("LATEST_OPERATION_RESPONSE");
} else {
return null;
}
bais = new ByteArrayInputStream(deviceInfoPayloadByteArray);
ois = new ObjectInputStream(bais);
deviceInfoMap = (Map<String, String>) ois.readObject();
} catch (DeviceManagementDAOException e) {
throw new DeviceDetailsMgtException("Cannot find valid device for device identifier");
} catch (SQLException e) {
throw new DeviceDetailsMgtException("Database connection issue");
shagihan marked this conversation as resolved.
Show resolved Hide resolved
} catch (IOException e) {
e.printStackTrace();
shagihan marked this conversation as resolved.
Show resolved Hide resolved
} catch (ClassNotFoundException e) {
e.printStackTrace();
shagihan marked this conversation as resolved.
Show resolved Hide resolved
}
return deviceInfoMap;
}

@Override
public void setLatestDeviceInfoMap(DeviceIdentifier deviceIdentifier, int tenantId, Map<String, String> latestDeviceInfoMap, boolean isUpdate)
throws DeviceDetailsMgtException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
ByteArrayOutputStream bao = null;
ObjectOutputStream oos = null;
String sql;
try {
Device device = this.getDevice(deviceIdentifier,tenantId);
conn = this.getConnection();
bao = new ByteArrayOutputStream();
oos = new ObjectOutputStream(bao);
oos.writeObject(latestDeviceInfoMap);
if(isUpdate) {
sql = "UPDATE DM_DEVICE_INFO_OPERATION_RESPONSE SET LATEST_OPERATION_RESPONSE = ?, LAST_UPDATED_TIMESTAMP = ?" +
shagihan marked this conversation as resolved.
Show resolved Hide resolved
" WHERE DEVICE_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setBytes(1, bao.toByteArray());
stmt.setTimestamp(2, new Timestamp(new Date().getTime()));
stmt.setInt(3, device.getId());
} else {
sql = "INSERT INTO DM_DEVICE_INFO_OPERATION_RESPONSE " +
"(DEVICE_ID, LATEST_OPERATION_RESPONSE, LAST_UPDATED_TIMESTAMP) VALUES (?, ?, ?)";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, device.getId());
stmt.setBytes(2, bao.toByteArray());
stmt.setTimestamp(3, new Timestamp(new Date().getTime()));
}
stmt.execute();
} catch (SQLException e) {
throw new DeviceDetailsMgtException("Database connection issue");
shagihan marked this conversation as resolved.
Show resolved Hide resolved
} catch (IOException e) {
throw new DeviceDetailsMgtException("Error while serializing the map");
} catch (DeviceManagementDAOException e) {
throw new DeviceDetailsMgtException("Cannot find valid device for device identifier");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
Expand All @@ -55,6 +56,7 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
private static final String DEVICE_INFO_EVENT_STREAM_DEFINITION = "org.wso2.iot.DeviceInfoStream";



public DeviceInformationManagerImpl() {
this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO();
this.deviceDetailsDAO = DeviceManagementDAOFactory.getDeviceDetailsDAO();
Expand All @@ -67,11 +69,12 @@ public void addDeviceInfo(DeviceIdentifier deviceId, DeviceInfo deviceInfo) thro
getDeviceManagementProvider().getDevice(deviceId, false);

DeviceManagementDAOFactory.beginTransaction();
DeviceInfo newDeviceInfo = processDeviceInfo(deviceId, deviceInfo, device);
deviceDAO.updateDevice(device, CarbonContext.getThreadLocalCarbonContext().getTenantId());
deviceDetailsDAO.deleteDeviceInformation(device.getId(), device.getEnrolmentInfo().getId());
deviceDetailsDAO.deleteDeviceProperties(device.getId(), device.getEnrolmentInfo().getId());
deviceDetailsDAO.addDeviceInformation(device.getId(), device.getEnrolmentInfo().getId(), deviceInfo);
deviceDetailsDAO.addDeviceProperties(deviceInfo.getDeviceDetailsMap(), device.getId(),
deviceDetailsDAO.addDeviceInformation(device.getId(), device.getEnrolmentInfo().getId(), newDeviceInfo);
deviceDetailsDAO.addDeviceProperties(newDeviceInfo.getDeviceDetailsMap(), device.getId(),
device.getEnrolmentInfo().getId());
DeviceManagementDAOFactory.commitTransaction();

Expand Down Expand Up @@ -125,6 +128,158 @@ public void addDeviceInfo(DeviceIdentifier deviceId, DeviceInfo deviceInfo) thro
}
}

private DeviceInfo processDeviceInfo(DeviceIdentifier deviceId, DeviceInfo deviceInfo, Device device) throws DeviceDetailsMgtException {

Map<String, String> previousDeviceInfo = null;
previousDeviceInfo = deviceDAO.getLatestDeviceInfoMap(deviceId, CarbonContext.getThreadLocalCarbonContext().getTenantId());
if (previousDeviceInfo == null) {
previousDeviceInfo = new HashMap<String, String>();
previousDeviceInfo.put("NAME", device.getName());
previousDeviceInfo.put("DESCRIPTION", device.getDescription());
previousDeviceInfo.put("DEVICE_IDENTIFICATION", device.getDeviceIdentifier());
previousDeviceInfo.put("DEVICE_ID", String.valueOf(device.getId()));

previousDeviceInfo.put("DEVICE_MODEL", deviceInfo.getDeviceModel());
previousDeviceInfo.put("VENDOR", deviceInfo.getVendor());
previousDeviceInfo.put("OS_VERSION", deviceInfo.getOsVersion());
previousDeviceInfo.put("OS_BUILD_DATE", deviceInfo.getOsBuildDate());
previousDeviceInfo.put("BATTERY_LEVEL", String.valueOf(deviceInfo.getBatteryLevel()));
previousDeviceInfo.put("INTERNAL_TOTAL_MEMORY", String.valueOf(deviceInfo.getInternalTotalMemory()));
previousDeviceInfo.put("INTERNAL_AVAILABLE_MEMORY", String.valueOf(deviceInfo.getInternalAvailableMemory()));
previousDeviceInfo.put("EXTERNAL_TOTAL_MEMORY", String.valueOf(deviceInfo.getExternalTotalMemory()));
previousDeviceInfo.put("EXTERNAL_AVAILABLE_MEMORY", String.valueOf(deviceInfo.getExternalAvailableMemory()));
previousDeviceInfo.put("CONNECTION_TYPE", deviceInfo.getConnectionType());
previousDeviceInfo.put("SSID", deviceInfo.getSsid());
previousDeviceInfo.put("CPU_USAGE", String.valueOf(deviceInfo.getCpuUsage()));
previousDeviceInfo.put("TOTAL_RAM_MEMORY", String.valueOf(deviceInfo.getTotalRAMMemory()));
previousDeviceInfo.put("AVAILABLE_RAM_MEMORY", String.valueOf(deviceInfo.getAvailableRAMMemory()));
previousDeviceInfo.put("PLUGGED_IN", String.valueOf(deviceInfo.isPluggedIn()));
List<String> deviceDetailsMapKeylist = new ArrayList<>();
for (Map.Entry<String, String> entry : deviceInfo.getDeviceDetailsMap().entrySet()) {
previousDeviceInfo.put(entry.getKey(), entry.getValue());
deviceDetailsMapKeylist.add(entry.getKey());
}
previousDeviceInfo.put("DEVICE_DETAILS_KEY", deviceDetailsMapKeylist.toString());
deviceDAO.setLatestDeviceInfoMap(deviceId, CarbonContext.getThreadLocalCarbonContext().getTenantId(),
previousDeviceInfo, false);
return deviceInfo;
} else {
DeviceInfo newDeviceInfo = new DeviceInfo();
if (device.getName() != null && !previousDeviceInfo.get("NAME").equals(device.getName())) {
previousDeviceInfo.put("NAME", device.getName());
}
if (device.getDescription() != null && !previousDeviceInfo.get("DESCRIPTION").equals(device.getDescription())) {
previousDeviceInfo.put("DESCRIPTION", device.getDescription());
}
if (device.getDeviceIdentifier() != null && !previousDeviceInfo.get("DEVICE_IDENTIFICATION").equals(device.getDeviceIdentifier())) {
previousDeviceInfo.put("DEVICE_IDENTIFICATION", device.getDeviceIdentifier());
}
if (String.valueOf(device.getId()) != null && !previousDeviceInfo.get("DEVICE_ID").equals(String.valueOf(device.getId()))) {
previousDeviceInfo.put("DEVICE_ID", String.valueOf(device.getId()));
}
if (deviceInfo.getDeviceModel() != null && !previousDeviceInfo.get("DEVICE_MODEL").equals(deviceInfo.getDeviceModel())) {
previousDeviceInfo.put("DEVICE_MODEL", deviceInfo.getDeviceModel());
}
if (deviceInfo.getVendor() != null && !previousDeviceInfo.get("VENDOR").equals(deviceInfo.getVendor())) {
previousDeviceInfo.put("VENDOR", deviceInfo.getVendor());
}
if (deviceInfo.getOsVersion() != null && !previousDeviceInfo.get("OS_VERSION").equals(deviceInfo.getOsVersion())) {
previousDeviceInfo.put("OS_VERSION", deviceInfo.getOsVersion());
}
if (deviceInfo.getOsBuildDate() != null && !previousDeviceInfo.get("OS_BUILD_DATE").equals(deviceInfo.getOsBuildDate())) {
previousDeviceInfo.put("OS_BUILD_DATE", deviceInfo.getOsBuildDate());
}
if (String.valueOf(deviceInfo.getBatteryLevel()) != null && !previousDeviceInfo.get("BATTERY_LEVEL").equals(String.valueOf(deviceInfo.getBatteryLevel()))) {
previousDeviceInfo.put("BATTERY_LEVEL", String.valueOf(deviceInfo.getBatteryLevel()));
}
if (String.valueOf(deviceInfo.getInternalTotalMemory()) != null && !previousDeviceInfo.get("INTERNAL_TOTAL_MEMORY").equals(String.valueOf(deviceInfo.getInternalTotalMemory()))) {
previousDeviceInfo.put("INTERNAL_TOTAL_MEMORY", String.valueOf(deviceInfo.getInternalTotalMemory()));
}
if (String.valueOf(deviceInfo.getInternalTotalMemory()) != null && !previousDeviceInfo.get("INTERNAL_TOTAL_MEMORY").equals(String.valueOf(deviceInfo.getInternalTotalMemory()))) {
previousDeviceInfo.put("INTERNAL_TOTAL_MEMORY", String.valueOf(deviceInfo.getInternalTotalMemory()));
}
if (String.valueOf(deviceInfo.getInternalAvailableMemory()) != null && !previousDeviceInfo.get("INTERNAL_AVAILABLE_MEMORY").equals(String.valueOf(deviceInfo.getInternalAvailableMemory()))) {
previousDeviceInfo.put("INTERNAL_AVAILABLE_MEMORY", String.valueOf(deviceInfo.getInternalAvailableMemory()));
}
if (String.valueOf(deviceInfo.getExternalTotalMemory()) != null && !previousDeviceInfo.get("EXTERNAL_TOTAL_MEMORY").equals(String.valueOf(deviceInfo.getExternalTotalMemory()))) {
previousDeviceInfo.put("EXTERNAL_TOTAL_MEMORY", String.valueOf(deviceInfo.getExternalTotalMemory()));
}
if (String.valueOf(deviceInfo.getExternalAvailableMemory()) != null && previousDeviceInfo.get("NAME") != null && !previousDeviceInfo.get("EXTERNAL_AVAILABLE_MEMORY").equals(String.valueOf(deviceInfo.getExternalAvailableMemory()))) {
previousDeviceInfo.put("EXTERNAL_AVAILABLE_MEMORY", String.valueOf(deviceInfo.getExternalAvailableMemory()));
}
if (deviceInfo.getConnectionType() != null && !previousDeviceInfo.get("CONNECTION_TYPE").equals(deviceInfo.getConnectionType())) {
previousDeviceInfo.put("CONNECTION_TYPE", deviceInfo.getConnectionType());
}
if (deviceInfo.getSsid() != null && !previousDeviceInfo.get("SSID").equals(deviceInfo.getSsid())) {
previousDeviceInfo.put("SSID", deviceInfo.getSsid());
}
if (String.valueOf(deviceInfo.getCpuUsage()) != null && !previousDeviceInfo.get("CPU_USAGE").equals(String.valueOf(deviceInfo.getCpuUsage()))) {
previousDeviceInfo.put("CPU_USAGE", String.valueOf(deviceInfo.getCpuUsage()));
}
if (String.valueOf(deviceInfo.getTotalRAMMemory()) != null && !previousDeviceInfo.get("TOTAL_RAM_MEMORY").equals(String.valueOf(deviceInfo.getTotalRAMMemory()))) {
previousDeviceInfo.put("TOTAL_RAM_MEMORY", String.valueOf(deviceInfo.getTotalRAMMemory()));
}
if (String.valueOf(deviceInfo.getAvailableRAMMemory()) != null && !previousDeviceInfo.get("AVAILABLE_RAM_MEMORY").equals(String.valueOf(deviceInfo.getAvailableRAMMemory()))) {
previousDeviceInfo.put("AVAILABLE_RAM_MEMORY", String.valueOf(deviceInfo.getAvailableRAMMemory()));
}
if (String.valueOf(deviceInfo.isPluggedIn()) != null && !previousDeviceInfo.get("PLUGGED_IN").equals(String.valueOf(deviceInfo.isPluggedIn()))) {
previousDeviceInfo.put("PLUGGED_IN", String.valueOf(deviceInfo.isPluggedIn()));
}
newDeviceInfo.setDeviceModel(previousDeviceInfo.get("DEVICE_MODEL"));
newDeviceInfo.setVendor(previousDeviceInfo.get("VENDOR"));
newDeviceInfo.setOsVersion(previousDeviceInfo.get("OS_VERSION"));
newDeviceInfo.setOsBuildDate(previousDeviceInfo.get("OS_BUILD_DATE"));
newDeviceInfo.setBatteryLevel(Double.valueOf(previousDeviceInfo.get("BATTERY_LEVEL")));
newDeviceInfo.setInternalTotalMemory(Double.valueOf(previousDeviceInfo.get("INTERNAL_TOTAL_MEMORY")));
newDeviceInfo.setInternalAvailableMemory(Double.valueOf(previousDeviceInfo.get("INTERNAL_AVAILABLE_MEMORY")));
newDeviceInfo.setExternalTotalMemory(Double.valueOf(previousDeviceInfo.get("EXTERNAL_TOTAL_MEMORY")));
newDeviceInfo.setExternalAvailableMemory(Double.valueOf(previousDeviceInfo.get("EXTERNAL_AVAILABLE_MEMORY")));
newDeviceInfo.setConnectionType(previousDeviceInfo.get("CONNECTION_TYPE"));
newDeviceInfo.setSsid(previousDeviceInfo.get("SSID"));
newDeviceInfo.setCpuUsage(Double.valueOf(previousDeviceInfo.get("CPU_USAGE")));
newDeviceInfo.setTotalRAMMemory(Double.valueOf(previousDeviceInfo.get("TOTAL_RAM_MEMORY")));
newDeviceInfo.setAvailableRAMMemory(Double.valueOf(previousDeviceInfo.get("AVAILABLE_RAM_MEMORY")));

shagihan marked this conversation as resolved.
Show resolved Hide resolved
Map<String, String> tempDetailsMap = new HashMap<>();

Map<String, String> agentDetailsMap = deviceInfo.getDeviceDetailsMap();

String tempDetailsMapKeys = previousDeviceInfo.get("DEVICE_DETAILS_KEY");

tempDetailsMapKeys = tempDetailsMapKeys.substring(1, (tempDetailsMapKeys.length() - 1));

List<String> tempDetailsMapKeyList = Arrays.asList(tempDetailsMapKeys.split(","));

List<String> newDetailsMapKeys = new ArrayList<>();

for (String eachKey : tempDetailsMapKeyList) {
eachKey = eachKey.replaceAll(" ", "");
if (agentDetailsMap.get(eachKey) == null) {
tempDetailsMap.put(eachKey, previousDeviceInfo.get(eachKey));
} else if (!agentDetailsMap.get(eachKey).equals(previousDeviceInfo.get(eachKey))) {
tempDetailsMap.put(eachKey, agentDetailsMap.get(eachKey));
previousDeviceInfo.put(eachKey, agentDetailsMap.get(eachKey));
} else {
tempDetailsMap.put(eachKey, agentDetailsMap.get(eachKey));
}
newDetailsMapKeys.add(eachKey);
}
for (String eachKey : agentDetailsMap.keySet()) {
if(!newDetailsMapKeys.contains(eachKey)){
tempDetailsMap.put(eachKey, agentDetailsMap.get(eachKey));
previousDeviceInfo.put(eachKey, agentDetailsMap.get(eachKey));
newDetailsMapKeys.add(eachKey);
}
}
previousDeviceInfo.put("DEVICE_DETAILS_KEY", newDetailsMapKeys.toString());
newDeviceInfo.setDeviceDetailsMap(tempDetailsMap);
deviceDAO.setLatestDeviceInfoMap(deviceId, CarbonContext.getThreadLocalCarbonContext().getTenantId(),
previousDeviceInfo, true);
return newDeviceInfo;
}

}

@Override
public DeviceInfo getDeviceInfo(DeviceIdentifier deviceId) throws DeviceDetailsMgtException {
Device device = getDevice(deviceId);
Expand Down
Loading