Merge branch 'master' into 'master'

Add properties from xml based device types to DEVICE_PROPERTIES table

See merge request entgra/carbon-device-mgt!552
reporting
Charitha Goonetilleke 5 years ago
commit 1a7661c30d

@ -112,12 +112,12 @@ public class DeviceManagementConfigServiceImpl implements DeviceManagementConfig
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
} catch (DeviceNotFoundException e) {
log.error(e.getMessage(), e);
log.warn(e.getMessage());
return Response.status(Response.Status.BAD_REQUEST).entity(
new ErrorResponse.ErrorResponseBuilder().setMessage(e.getMessage()).build()).build();
} catch (AmbiguousConfigurationException e) {
String msg = "Configurations are ambiguous";
log.error(msg, e);
String msg = "Configurations are ambiguous. " + e.getMessage();
log.warn(msg);
return Response.status(Response.Status.BAD_REQUEST).entity(
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
} catch (JsonParseException | JsonMappingException e) {
@ -126,7 +126,7 @@ public class DeviceManagementConfigServiceImpl implements DeviceManagementConfig
return Response.status(Response.Status.BAD_REQUEST).entity(
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
} catch (IOException e) {
String msg = "Error occurred while parsing query param JSON data";
String msg = "Error occurred while parsing query param JSON data.";
log.error(msg.concat(" ").concat(properties), e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();

@ -273,9 +273,10 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
newEnrolmentInfo.setId(enrolmentInfo.getId());
//We are explicitly setting device status only if matching device enrollment is in
// removed status.
if (enrolmentInfo.getStatus() == EnrolmentInfo.Status.REMOVED) {
if (enrolmentInfo.getStatus() == EnrolmentInfo.Status.REMOVED &&
newEnrolmentInfo.getStatus() == null) {
newEnrolmentInfo.setStatus(EnrolmentInfo.Status.ACTIVE);
} else {
} else if (newEnrolmentInfo.getStatus() == null) {
newEnrolmentInfo.setStatus(enrolmentInfo.getStatus());
}
status = true;

@ -646,7 +646,6 @@ public final class DeviceManagerUtil {
apiEndpoint.setEntity(constructApplicationRegistrationPayload());
HttpResponse response = client.execute(apiEndpoint);
if (response != null) {
log.info("Obtained client credentials: " + response.getStatusLine().getStatusCode());
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
StringBuilder result = new StringBuilder();

@ -53,6 +53,7 @@ import org.wso2.carbon.device.mgt.extensions.device.type.template.config.DataSou
import org.wso2.carbon.device.mgt.extensions.device.type.template.config.DeviceDetails;
import org.wso2.carbon.device.mgt.extensions.device.type.template.config.DeviceTypeConfiguration;
import org.wso2.carbon.device.mgt.extensions.device.type.template.config.Feature;
import org.wso2.carbon.device.mgt.extensions.device.type.template.config.Properties;
import org.wso2.carbon.device.mgt.extensions.device.type.template.config.Table;
import org.wso2.carbon.device.mgt.extensions.device.type.template.config.TableConfig;
import org.wso2.carbon.device.mgt.extensions.device.type.template.dao.DeviceDAODefinition;
@ -81,6 +82,7 @@ import java.io.File;
import java.io.StringReader;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
/**
@ -102,6 +104,7 @@ public class DeviceTypeManager implements DeviceManager {
private FeatureManager featureManager;
private boolean propertiesExist;
private DeviceTypePluginDAOManager deviceTypePluginDAOManager;
private DeviceTypePluginDAOManager propertyBasedDeviceTypePluginDAOManager = null;
public DeviceTypeManager(DeviceTypeConfigIdentifier deviceTypeConfigIdentifier,
DeviceTypeConfiguration deviceTypeConfiguration) {
@ -197,6 +200,15 @@ public class DeviceTypeManager implements DeviceManager {
}
}
deviceTypePluginDAOManager = new DeviceTypePluginDAOManager(datasourceName, deviceDAODefinition);
if (deviceDetails.getProperties() == null || deviceDetails.getProperties().getProperty() == null
|| deviceDetails.getProperties().getProperty().size() == 0) {
Properties properties = new Properties();
List<String> propKeys = new ArrayList<>(deviceDAODefinition.getColumnNames());
propKeys.add("token");
properties.addProperties(propKeys);
deviceDetails.setProperties(properties);
}
propertyBasedDeviceTypePluginDAOManager = new DeviceTypePluginDAOManager(deviceType, deviceDetails);
} else {
throw new DeviceTypeDeployerPayloadException("Invalid datasource name.");
}
@ -205,7 +217,7 @@ public class DeviceTypeManager implements DeviceManager {
}
} else {
if (deviceDetails.getProperties() != null && deviceDetails.getProperties().getProperty() != null
&& deviceDetails.getProperties().getProperty().size() > 0 ) {
&& deviceDetails.getProperties().getProperty().size() > 0) {
deviceTypePluginDAOManager = new DeviceTypePluginDAOManager(deviceType, deviceDetails);
propertiesExist = true;
}
@ -323,7 +335,7 @@ public class DeviceTypeManager implements DeviceManager {
new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()));
try {
if (log.isDebugEnabled()) {
log.debug("Enrolling a new Android device : " + device.getDeviceIdentifier());
log.debug("Enrolling a new device : " + device.getDeviceIdentifier());
}
if (isEnrolled) {
this.modifyEnrollment(device);
@ -339,6 +351,23 @@ public class DeviceTypeManager implements DeviceManager {
} finally {
deviceTypePluginDAOManager.getDeviceTypeDAOHandler().closeConnection();
}
if (propertyBasedDeviceTypePluginDAOManager != null && !isEnrolled && status) {
try {
if (log.isDebugEnabled()) {
log.debug("Adding properties for new device : " + device.getDeviceIdentifier());
}
propertyBasedDeviceTypePluginDAOManager.getDeviceTypeDAOHandler().beginTransaction();
status = propertyBasedDeviceTypePluginDAOManager.getDeviceDAO().addDevice(device);
propertyBasedDeviceTypePluginDAOManager.getDeviceTypeDAOHandler().commitTransaction();
} catch (DeviceTypeMgtPluginException e) {
propertyBasedDeviceTypePluginDAOManager.getDeviceTypeDAOHandler().rollbackTransaction();
String msg = "Error while adding properties for " + deviceType + " device : " +
device.getDeviceIdentifier();
throw new DeviceManagementException(msg, e);
} finally {
propertyBasedDeviceTypePluginDAOManager.getDeviceTypeDAOHandler().closeConnection();
}
}
return status;
}
return true;
@ -350,7 +379,7 @@ public class DeviceTypeManager implements DeviceManager {
boolean status;
try {
if (log.isDebugEnabled()) {
log.debug("Modifying the Android device enrollment data");
log.debug("Adding properties for new device : " + device.getDeviceIdentifier());
}
deviceTypePluginDAOManager.getDeviceTypeDAOHandler().beginTransaction();
status = deviceTypePluginDAOManager.getDeviceDAO().updateDevice(device);
@ -363,6 +392,23 @@ public class DeviceTypeManager implements DeviceManager {
} finally {
deviceTypePluginDAOManager.getDeviceTypeDAOHandler().closeConnection();
}
if (propertyBasedDeviceTypePluginDAOManager != null && status) {
try {
if (log.isDebugEnabled()) {
log.debug("Updating properties for new device : " + device.getDeviceIdentifier());
}
propertyBasedDeviceTypePluginDAOManager.getDeviceTypeDAOHandler().beginTransaction();
status = propertyBasedDeviceTypePluginDAOManager.getDeviceDAO().updateDevice(device);
propertyBasedDeviceTypePluginDAOManager.getDeviceTypeDAOHandler().commitTransaction();
} catch (DeviceTypeMgtPluginException e) {
propertyBasedDeviceTypePluginDAOManager.getDeviceTypeDAOHandler().rollbackTransaction();
String msg = "Error while updating properties for " + deviceType + " device : " +
device.getDeviceIdentifier();
throw new DeviceManagementException(msg, e);
} finally {
propertyBasedDeviceTypePluginDAOManager.getDeviceTypeDAOHandler().closeConnection();
}
}
return status;
}
return true;
@ -598,13 +644,42 @@ public class DeviceTypeManager implements DeviceManager {
log.debug("Error occurred while deleting the " + deviceType + " devices: '" +
deviceIdentifierList + "'. Transaction rolled back");
}
String msg= "Error occurred while deleting the " + deviceType + " devices: '" +
String msg = "Error occurred while deleting the " + deviceType + " devices: '" +
deviceIdentifierList;
log.error(msg,e);
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} finally {
deviceTypePluginDAOManager.getDeviceTypeDAOHandler().closeConnection();
}
if (propertyBasedDeviceTypePluginDAOManager != null) {
try {
if (log.isDebugEnabled()) {
log.debug("Deleting the properties of " + deviceType + " devices : " + deviceIdentifierList);
}
propertyBasedDeviceTypePluginDAOManager.getDeviceTypeDAOHandler().beginTransaction();
if (propertyBasedDeviceTypePluginDAOManager.getDeviceDAO().deleteDevices(deviceIdentifierList)) {
propertyBasedDeviceTypePluginDAOManager.getDeviceTypeDAOHandler().commitTransaction();
} else {
propertyBasedDeviceTypePluginDAOManager.getDeviceTypeDAOHandler().rollbackTransaction();
String msg = "Error occurred while deleting the properties of " + deviceType + " devices: '" +
deviceIdentifierList;
log.error(msg);
throw new DeviceManagementException(msg);
}
} catch (DeviceTypeMgtPluginException e) {
propertyBasedDeviceTypePluginDAOManager.getDeviceTypeDAOHandler().rollbackTransaction();
if (log.isDebugEnabled()) {
log.debug("Error occurred while deleting the properties of " + deviceType + " devices: '" +
deviceIdentifierList + "'. Transaction rolled back");
}
String msg = "Error occurred while deleting the properties of " + deviceType + " devices: '" +
deviceIdentifierList;
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} finally {
propertyBasedDeviceTypePluginDAOManager.getDeviceTypeDAOHandler().closeConnection();
}
}
}
}

@ -25,8 +25,8 @@ import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
public class DeviceTypePluginDAOManager {
private PluginDAO deviceTypePluginDAO;
private DeviceTypeDAOHandler deviceTypeDAOHandler;
private final PluginDAO deviceTypePluginDAO;
private final DeviceTypeDAOHandler deviceTypeDAOHandler;
private static final String DEFAULT_DATASOURCE_NAME = "jdbc/DM_DS";
public DeviceTypePluginDAOManager(String datasourceName, DeviceDAODefinition deviceDAODefinition) {

@ -151,11 +151,16 @@ public class PropertyBasedPluginDAOImpl implements PluginDAO {
"UPDATE DM_DEVICE_PROPERTIES SET PROPERTY_VALUE = ? WHERE DEVICE_TYPE_NAME = ? AND "
+ "DEVICE_IDENTIFICATION = ? AND PROPERTY_NAME = ? AND TENANT_ID= ?");
String propValue;
for (Device.Property property : device.getProperties()) {
if (!deviceProps.contains(property.getName())) {
continue;
}
stmt.setString(1, property.getValue());
propValue = property.getValue();
if (propValue != null && propValue.length() > 100) {
propValue = "Value too long";
}
stmt.setString(1, propValue);
stmt.setString(2, deviceType);
stmt.setString(3, device.getDeviceIdentifier());
stmt.setString(4, property.getName());
@ -253,7 +258,11 @@ public class PropertyBasedPluginDAOImpl implements PluginDAO {
private String getPropertyValue(List<Device.Property> properties, String propertyName) {
for (Device.Property property : properties) {
if (property.getName() != null && property.getName().equals(propertyName)) {
return property.getValue();
String value = property.getValue();
if (value != null && value.length() > 100) {
return "Value too long";
}
return value;
}
}
return null;

Loading…
Cancel
Save