|
|
@ -72,7 +72,28 @@ import java.util.*;
|
|
|
|
public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
static final Log log = LogFactory.getLog(DeviceAgentServiceImpl.class);
|
|
|
|
static final Log log = LogFactory.getLog(DeviceAgentServiceImpl.class);
|
|
|
|
private static final String POLICY_MONITOR = "POLICY_MONITOR";
|
|
|
|
private static final String POLICY_MONITOR = "POLICY_MONITOR";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static List<ComplianceFeature> getComplianceFeatures(Object compliancePayload) throws
|
|
|
|
|
|
|
|
PolicyComplianceException {
|
|
|
|
|
|
|
|
String compliancePayloadString = new Gson().toJson(compliancePayload);
|
|
|
|
|
|
|
|
if (compliancePayload == null) {
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parsing json string to get compliance features.
|
|
|
|
|
|
|
|
JsonElement jsonElement = new JsonParser().parse(compliancePayloadString);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JsonArray jsonArray = jsonElement.getAsJsonArray();
|
|
|
|
|
|
|
|
Gson gson = new Gson();
|
|
|
|
|
|
|
|
ComplianceFeature complianceFeature;
|
|
|
|
|
|
|
|
List<ComplianceFeature> complianceFeatures = new ArrayList<>(jsonArray.size());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (JsonElement element : jsonArray) {
|
|
|
|
|
|
|
|
complianceFeature = gson.fromJson(element, ComplianceFeature.class);
|
|
|
|
|
|
|
|
complianceFeatures.add(complianceFeature);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return complianceFeatures;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@POST
|
|
|
|
@POST
|
|
|
|
@Path("/enroll")
|
|
|
|
@Path("/enroll")
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
@ -102,26 +123,26 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
DeviceIdentifier deviceId = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType());
|
|
|
|
DeviceIdentifier deviceId = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType());
|
|
|
|
DeviceMgtAPIUtils.getPolicyManagementService().getEffectivePolicy(deviceId);
|
|
|
|
DeviceMgtAPIUtils.getPolicyManagementService().getEffectivePolicy(deviceId);
|
|
|
|
pap.publishChanges();
|
|
|
|
pap.publishChanges();
|
|
|
|
|
|
|
|
|
|
|
|
// Save the response time to Google Spreadsheet
|
|
|
|
// Save the response time to Google Spreadsheet
|
|
|
|
long startTime = System.currentTimeMillis(); // Record the start time
|
|
|
|
long startTime = System.currentTimeMillis(); // Record the start time
|
|
|
|
Sheets sheetsService = createSheetsService(); // Create Sheets service using loaded credentials
|
|
|
|
Sheets sheetsService = createSheetsService(); // Create Sheets service using loaded credentials
|
|
|
|
String spreadsheetId = "1OZCS5NRwwSum9ai3ra4lABtU0UGW-9yLYgZk-aQfxpw";
|
|
|
|
String spreadsheetId = "1OZCS5NRwwSum9ai3ra4lABtU0UGW-9yLYgZk-aQfxpw";
|
|
|
|
|
|
|
|
|
|
|
|
// Create the values to be written
|
|
|
|
// Create the values to be written
|
|
|
|
List<List<Object>> values = Collections.singletonList(
|
|
|
|
List<List<Object>> values = Collections.singletonList(
|
|
|
|
Arrays.asList(device.getDeviceIdentifier(), device.getType(), String.valueOf(System.currentTimeMillis() - startTime))
|
|
|
|
Arrays.asList(device.getDeviceIdentifier(), device.getType(), String.valueOf(System.currentTimeMillis() - startTime))
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// Create the value range
|
|
|
|
// Create the value range
|
|
|
|
ValueRange body = new ValueRange().setValues(values);
|
|
|
|
ValueRange body = new ValueRange().setValues(values);
|
|
|
|
|
|
|
|
|
|
|
|
// Write the values to the spreadsheet
|
|
|
|
// Write the values to the spreadsheet
|
|
|
|
sheetsService.spreadsheets().values()
|
|
|
|
sheetsService.spreadsheets().values()
|
|
|
|
.append(spreadsheetId, "Sheet1", body)
|
|
|
|
.append(spreadsheetId, "Sheet1", body)
|
|
|
|
.setValueInputOption("USER_ENTERED")
|
|
|
|
.setValueInputOption("USER_ENTERED")
|
|
|
|
.execute();
|
|
|
|
.execute();
|
|
|
|
|
|
|
|
|
|
|
|
return Response.status(Response.Status.OK).entity(status).build();
|
|
|
|
return Response.status(Response.Status.OK).entity(status).build();
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
String msg = "Error occurred while enrolling the device, which carries the id '" +
|
|
|
|
String msg = "Error occurred while enrolling the device, which carries the id '" +
|
|
|
@ -141,7 +162,7 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@DELETE
|
|
|
|
@DELETE
|
|
|
|
@Path("/enroll/{type}/{id}")
|
|
|
|
@Path("/enroll/{type}/{id}")
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
@ -153,19 +174,19 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
result = DeviceMgtAPIUtils.getDeviceManagementService().disenrollDevice(deviceIdentifier);
|
|
|
|
result = DeviceMgtAPIUtils.getDeviceManagementService().disenrollDevice(deviceIdentifier);
|
|
|
|
long endTime = System.currentTimeMillis(); // Record the end time
|
|
|
|
long endTime = System.currentTimeMillis(); // Record the end time
|
|
|
|
long responseTime = endTime - startTime; // Calculate the response time
|
|
|
|
long responseTime = endTime - startTime; // Calculate the response time
|
|
|
|
|
|
|
|
|
|
|
|
// Save the response time to Google Spreadsheet
|
|
|
|
// Save the response time to Google Spreadsheet
|
|
|
|
Sheets sheetsService = createSheetsService(); // Create Sheets service using loaded credentials
|
|
|
|
Sheets sheetsService = createSheetsService(); // Create Sheets service using loaded credentials
|
|
|
|
String spreadsheetId = "1OZCS5NRwwSum9ai3ra4lABtU0UGW-9yLYgZk-aQfxpw";
|
|
|
|
String spreadsheetId = "1OZCS5NRwwSum9ai3ra4lABtU0UGW-9yLYgZk-aQfxpw";
|
|
|
|
|
|
|
|
|
|
|
|
// Create the values to be written
|
|
|
|
// Create the values to be written
|
|
|
|
List<List<Object>> values = Collections.singletonList(
|
|
|
|
List<List<Object>> values = Collections.singletonList(
|
|
|
|
Arrays.asList(id, type, String.valueOf(responseTime))
|
|
|
|
Arrays.asList(id, type, String.valueOf(responseTime))
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// Create the value range
|
|
|
|
// Create the value range
|
|
|
|
ValueRange body = new ValueRange().setValues(values);
|
|
|
|
ValueRange body = new ValueRange().setValues(values);
|
|
|
|
|
|
|
|
|
|
|
|
// Write the values to the spreadsheet
|
|
|
|
// Write the values to the spreadsheet
|
|
|
|
sheetsService.spreadsheets().values()
|
|
|
|
sheetsService.spreadsheets().values()
|
|
|
|
.append(spreadsheetId, "Sheet1", body)
|
|
|
|
.append(spreadsheetId, "Sheet1", body)
|
|
|
@ -188,7 +209,7 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@PUT
|
|
|
|
@PUT
|
|
|
|
@Path("/enroll/{type}/{id}")
|
|
|
|
@Path("/enroll/{type}/{id}")
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
@ -205,7 +226,7 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
log.error(msg, e);
|
|
|
|
log.error(msg, e);
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (updateDevice == null) {
|
|
|
|
if (updateDevice == null) {
|
|
|
|
String errorMessage = "The payload of the device enrollment is incorrect.";
|
|
|
|
String errorMessage = "The payload of the device enrollment is incorrect.";
|
|
|
|
log.error(errorMessage);
|
|
|
|
log.error(errorMessage);
|
|
|
@ -257,20 +278,20 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
device.setType(type);
|
|
|
|
device.setType(type);
|
|
|
|
result = DeviceMgtAPIUtils.getDeviceManagementService().modifyEnrollment(device);
|
|
|
|
result = DeviceMgtAPIUtils.getDeviceManagementService().modifyEnrollment(device);
|
|
|
|
|
|
|
|
|
|
|
|
// Save the response time to Google Spreadsheet
|
|
|
|
// Save the response time to Google Spreadsheet
|
|
|
|
long startTime = System.currentTimeMillis(); // Record the start time
|
|
|
|
long startTime = System.currentTimeMillis(); // Record the start time
|
|
|
|
Sheets sheetsService = createSheetsService(); // Create Sheets service using loaded credentials
|
|
|
|
Sheets sheetsService = createSheetsService(); // Create Sheets service using loaded credentials
|
|
|
|
String spreadsheetId = "1OZCS5NRwwSum9ai3ra4lABtU0UGW-9yLYgZk-aQfxpw";
|
|
|
|
String spreadsheetId = "1OZCS5NRwwSum9ai3ra4lABtU0UGW-9yLYgZk-aQfxpw";
|
|
|
|
|
|
|
|
|
|
|
|
// Create the values to be written
|
|
|
|
// Create the values to be written
|
|
|
|
List<List<Object>> values = Collections.singletonList(
|
|
|
|
List<List<Object>> values = Collections.singletonList(
|
|
|
|
Arrays.asList(id, type, String.valueOf(System.currentTimeMillis() - startTime))
|
|
|
|
Arrays.asList(id, type, String.valueOf(System.currentTimeMillis() - startTime))
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// Create the value range
|
|
|
|
// Create the value range
|
|
|
|
ValueRange body = new ValueRange().setValues(values);
|
|
|
|
ValueRange body = new ValueRange().setValues(values);
|
|
|
|
|
|
|
|
|
|
|
|
// Write the values to the spreadsheet
|
|
|
|
// Write the values to the spreadsheet
|
|
|
|
sheetsService.spreadsheets().values()
|
|
|
|
sheetsService.spreadsheets().values()
|
|
|
|
.append(spreadsheetId, "Sheet1", body)
|
|
|
|
.append(spreadsheetId, "Sheet1", body)
|
|
|
@ -286,20 +307,20 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
id + "'";
|
|
|
|
id + "'";
|
|
|
|
log.error(msg, e);
|
|
|
|
log.error(msg, e);
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
|
|
|
}catch (IOException e) {
|
|
|
|
} catch (IOException e) {
|
|
|
|
// Handle any errors occurred while writing to the spreadsheet
|
|
|
|
// Handle any errors occurred while writing to the spreadsheet
|
|
|
|
String errorMessage = "Error writing response time to Google Spreadsheet";
|
|
|
|
String errorMessage = "Error writing response time to Google Spreadsheet";
|
|
|
|
log.error(errorMessage, e);
|
|
|
|
log.error(errorMessage, e);
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@POST
|
|
|
|
@POST
|
|
|
|
@Path("/events/publish/{type}/{deviceId}")
|
|
|
|
@Path("/events/publish/{type}/{deviceId}")
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Response publishEvents(@Valid Map<String, Object> payload, @PathParam("type") String type
|
|
|
|
public Response publishEvents(@Valid Map<String, Object> payload, @PathParam("type") String type
|
|
|
|
, @PathParam("deviceId") String deviceId) {
|
|
|
|
, @PathParam("deviceId") String deviceId) {
|
|
|
|
|
|
|
|
|
|
|
|
String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
|
|
|
|
String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
|
|
|
|
EventStreamAdminServiceStub eventStreamAdminServiceStub = null;
|
|
|
|
EventStreamAdminServiceStub eventStreamAdminServiceStub = null;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
@ -330,7 +351,7 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
for (EventStreamAttributeDto eventStreamAttributeDto : eventStreamAttributeDtos) {
|
|
|
|
for (EventStreamAttributeDto eventStreamAttributeDto : eventStreamAttributeDtos) {
|
|
|
|
attributes.add(new Attribute(eventStreamAttributeDto.getAttributeName()
|
|
|
|
attributes.add(new Attribute(eventStreamAttributeDto.getAttributeName()
|
|
|
|
, AttributeType.valueOf(eventStreamAttributeDto.getAttributeType().toUpperCase())));
|
|
|
|
, AttributeType.valueOf(eventStreamAttributeDto.getAttributeType().toUpperCase())));
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (payload.size() != attributes.size()) {
|
|
|
|
if (payload.size() != attributes.size()) {
|
|
|
|
String msg = "Payload does not match with the stream definition";
|
|
|
|
String msg = "Payload does not match with the stream definition";
|
|
|
@ -393,13 +414,13 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@POST
|
|
|
|
@POST
|
|
|
|
@Path("/events/publish/data/{type}/{deviceId}")
|
|
|
|
@Path("/events/publish/data/{type}/{deviceId}")
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Response publishEvents(@Valid List<Object> payload, @PathParam("type") String type
|
|
|
|
public Response publishEvents(@Valid List<Object> payload, @PathParam("type") String type
|
|
|
|
, @PathParam("deviceId") String deviceId) {
|
|
|
|
, @PathParam("deviceId") String deviceId) {
|
|
|
|
|
|
|
|
|
|
|
|
String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
|
|
|
|
String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
|
|
|
|
EventStreamAdminServiceStub eventStreamAdminServiceStub = null;
|
|
|
|
EventStreamAdminServiceStub eventStreamAdminServiceStub = null;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
@ -430,7 +451,7 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
for (EventStreamAttributeDto eventStreamAttributeDto : eventStreamAttributeDtos) {
|
|
|
|
for (EventStreamAttributeDto eventStreamAttributeDto : eventStreamAttributeDtos) {
|
|
|
|
attributes.add(new Attribute(eventStreamAttributeDto.getAttributeName()
|
|
|
|
attributes.add(new Attribute(eventStreamAttributeDto.getAttributeName()
|
|
|
|
, AttributeType.valueOf(eventStreamAttributeDto.getAttributeType().toUpperCase())));
|
|
|
|
, AttributeType.valueOf(eventStreamAttributeDto.getAttributeType().toUpperCase())));
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (payload.size() != attributes.size()) {
|
|
|
|
if (payload.size() != attributes.size()) {
|
|
|
|
String msg = "Payload does not match with the stream definition";
|
|
|
|
String msg = "Payload does not match with the stream definition";
|
|
|
@ -494,7 +515,7 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@GET
|
|
|
|
@GET
|
|
|
|
@Path("/pending/operations/{type}/{id}")
|
|
|
|
@Path("/pending/operations/{type}/{id}")
|
|
|
|
public Response getPendingOperations(@PathParam("type") String type, @PathParam("id") String deviceId) {
|
|
|
|
public Response getPendingOperations(@PathParam("type") String type, @PathParam("id") String deviceId) {
|
|
|
@ -511,28 +532,28 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
return Response.status(Response.Status.NO_CONTENT).entity(msg).build();
|
|
|
|
return Response.status(Response.Status.NO_CONTENT).entity(msg).build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
long startTime = System.currentTimeMillis(); // Record the start time
|
|
|
|
long startTime = System.currentTimeMillis(); // Record the start time
|
|
|
|
|
|
|
|
|
|
|
|
List<? extends Operation> operations = DeviceMgtAPIUtils.getDeviceManagementService().getPendingOperations(
|
|
|
|
List<? extends Operation> operations = DeviceMgtAPIUtils.getDeviceManagementService().getPendingOperations(
|
|
|
|
deviceIdentifier);
|
|
|
|
deviceIdentifier);
|
|
|
|
OperationList operationsList = new OperationList();
|
|
|
|
OperationList operationsList = new OperationList();
|
|
|
|
operationsList.setList(operations);
|
|
|
|
operationsList.setList(operations);
|
|
|
|
operationsList.setCount(operations.size());
|
|
|
|
operationsList.setCount(operations.size());
|
|
|
|
|
|
|
|
|
|
|
|
long endTime = System.currentTimeMillis(); // Record the end time
|
|
|
|
long endTime = System.currentTimeMillis(); // Record the end time
|
|
|
|
long responseTime = endTime - startTime; // Calculate the response time
|
|
|
|
long responseTime = endTime - startTime; // Calculate the response time
|
|
|
|
|
|
|
|
|
|
|
|
// Save the response time to Google Spreadsheet
|
|
|
|
// Save the response time to Google Spreadsheet
|
|
|
|
Sheets sheetsService = createSheetsService(); // Create Sheets service using loaded credentials
|
|
|
|
Sheets sheetsService = createSheetsService(); // Create Sheets service using loaded credentials
|
|
|
|
String spreadsheetId = "1OZCS5NRwwSum9ai3ra4lABtU0UGW-9yLYgZk-aQfxpw"; //Update with Spreadsheet ID
|
|
|
|
String spreadsheetId = "1OZCS5NRwwSum9ai3ra4lABtU0UGW-9yLYgZk-aQfxpw"; //Update with Spreadsheet ID
|
|
|
|
|
|
|
|
|
|
|
|
// Create the values to be written
|
|
|
|
// Create the values to be written
|
|
|
|
List<List<Object>> values = Collections.singletonList(
|
|
|
|
List<List<Object>> values = Collections.singletonList(
|
|
|
|
Arrays.asList(deviceId, String.valueOf(responseTime))
|
|
|
|
Arrays.asList(deviceId, String.valueOf(responseTime))
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// Create the value range
|
|
|
|
// Create the value range
|
|
|
|
ValueRange body = new ValueRange().setValues(values);
|
|
|
|
ValueRange body = new ValueRange().setValues(values);
|
|
|
|
|
|
|
|
|
|
|
|
// Write the values to the spreadsheet
|
|
|
|
// Write the values to the spreadsheet
|
|
|
|
sheetsService.spreadsheets().values()
|
|
|
|
sheetsService.spreadsheets().values()
|
|
|
|
.append(spreadsheetId, "Sheet1", body)
|
|
|
|
.append(spreadsheetId, "Sheet1", body)
|
|
|
@ -554,7 +575,7 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private Sheets createSheetsService() throws IOException {
|
|
|
|
private Sheets createSheetsService() throws IOException {
|
|
|
|
GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream("components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/resources/apicall-382608-48aa6a62800d.json"))
|
|
|
|
GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream("components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/resources/apicall-382608-48aa6a62800d.json"))
|
|
|
|
.createScoped(Collections.singleton(SheetsScopes.SPREADSHEETS));
|
|
|
|
.createScoped(Collections.singleton(SheetsScopes.SPREADSHEETS));
|
|
|
@ -566,7 +587,7 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@GET
|
|
|
|
@GET
|
|
|
|
@Path("/next-pending/operation/{type}/{id}")
|
|
|
|
@Path("/next-pending/operation/{type}/{id}")
|
|
|
|
public Response getNextPendingOperation(@PathParam("type") String type, @PathParam("id") String deviceId) {
|
|
|
|
public Response getNextPendingOperation(@PathParam("type") String type, @PathParam("id") String deviceId) {
|
|
|
@ -586,23 +607,30 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
deviceIdentifier);
|
|
|
|
deviceIdentifier);
|
|
|
|
// Save the response time to Google Spreadsheet
|
|
|
|
// Save the response time to Google Spreadsheet
|
|
|
|
long startTime = System.currentTimeMillis(); // Record the start time
|
|
|
|
long startTime = System.currentTimeMillis(); // Record the start time
|
|
|
|
Sheets sheetsService = createSheetsService(); // Create Sheets service using loaded credentials
|
|
|
|
|
|
|
|
|
|
|
|
// Create Sheets service using loaded credentials
|
|
|
|
|
|
|
|
Sheets sheetsService = createSheetsService();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Spreadsheet ID
|
|
|
|
String spreadsheetId = "1OZCS5NRwwSum9ai3ra4lABtU0UGW-9yLYgZk-aQfxpw";
|
|
|
|
String spreadsheetId = "1OZCS5NRwwSum9ai3ra4lABtU0UGW-9yLYgZk-aQfxpw";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Calculate the response time
|
|
|
|
|
|
|
|
long endTime = System.currentTimeMillis(); // Record the end time
|
|
|
|
|
|
|
|
long responseTime = endTime - startTime; // Calculate the response time
|
|
|
|
|
|
|
|
|
|
|
|
// Create the values to be written
|
|
|
|
// Create the values to be written
|
|
|
|
List<List<Object>> values = Collections.singletonList(
|
|
|
|
List<List<Object>> values = Collections.singletonList(
|
|
|
|
Arrays.asList(deviceId, type, String.valueOf(System.currentTimeMillis() - startTime))
|
|
|
|
Arrays.asList(deviceId, type, String.valueOf(responseTime))
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// Create the value range
|
|
|
|
// Create the value range
|
|
|
|
ValueRange body = new ValueRange().setValues(values);
|
|
|
|
ValueRange body = new ValueRange().setValues(values);
|
|
|
|
|
|
|
|
|
|
|
|
// Write the values to the spreadsheet
|
|
|
|
// Write the values to the spreadsheet
|
|
|
|
sheetsService.spreadsheets().values()
|
|
|
|
sheetsService.spreadsheets().values()
|
|
|
|
.append(spreadsheetId, "Sheet1", body)
|
|
|
|
.append(spreadsheetId, "Sheet1", body)
|
|
|
|
.setValueInputOption("USER_ENTERED")
|
|
|
|
.setValueInputOption("USER_ENTERED")
|
|
|
|
.execute();
|
|
|
|
.execute();
|
|
|
|
|
|
|
|
|
|
|
|
return Response.status(Response.Status.OK).entity(operation).build();
|
|
|
|
return Response.status(Response.Status.OK).entity(operation).build();
|
|
|
|
} catch (OperationManagementException e) {
|
|
|
|
} catch (OperationManagementException e) {
|
|
|
|
String errorMessage = "Issue in retrieving operation management service instance";
|
|
|
|
String errorMessage = "Issue in retrieving operation management service instance";
|
|
|
@ -612,14 +640,14 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
String errorMessage = "Issue in retrieving deivce management service instance";
|
|
|
|
String errorMessage = "Issue in retrieving deivce management service instance";
|
|
|
|
log.error(errorMessage, e);
|
|
|
|
log.error(errorMessage, e);
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
|
|
|
|
}catch (IOException e) {
|
|
|
|
} catch (IOException e) {
|
|
|
|
// Handle any errors occurred while writing to the spreadsheet
|
|
|
|
// Handle any errors occurred while writing to the spreadsheet
|
|
|
|
String errorMessage = "Error writing response time to Google Spreadsheet";
|
|
|
|
String errorMessage = "Error writing response time to Google Spreadsheet";
|
|
|
|
log.error(errorMessage, e);
|
|
|
|
log.error(errorMessage, e);
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@PUT
|
|
|
|
@PUT
|
|
|
|
@Path("/operations/{type}/{id}")
|
|
|
|
@Path("/operations/{type}/{id}")
|
|
|
|
public Response updateOperation(@PathParam("type") String type, @PathParam("id") String deviceId, @Valid Operation operation) {
|
|
|
|
public Response updateOperation(@PathParam("type") String type, @PathParam("id") String deviceId, @Valid Operation operation) {
|
|
|
@ -666,7 +694,7 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
@PUT
|
|
|
|
@PUT
|
|
|
|
@Path("/properties/{type}/{id}")
|
|
|
|
@Path("/properties/{type}/{id}")
|
|
|
@ -690,20 +718,20 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
|
|
|
|
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
long startTime = System.currentTimeMillis(); // Record the start time
|
|
|
|
long startTime = System.currentTimeMillis(); // Record the start time
|
|
|
|
|
|
|
|
|
|
|
|
if (DeviceMgtAPIUtils.getDeviceManagementService().updateProperties(deviceIdentifier, properties)) {
|
|
|
|
if (DeviceMgtAPIUtils.getDeviceManagementService().updateProperties(deviceIdentifier, properties)) {
|
|
|
|
// Save the response time to Google Spreadsheet
|
|
|
|
// Save the response time to Google Spreadsheet
|
|
|
|
Sheets sheetsService = createSheetsService(); // Create Sheets service using loaded credentials
|
|
|
|
Sheets sheetsService = createSheetsService(); // Create Sheets service using loaded credentials
|
|
|
|
String spreadsheetId = "1OZCS5NRwwSum9ai3ra4lABtU0UGW-9yLYgZk-aQfxpw";
|
|
|
|
String spreadsheetId = "1OZCS5NRwwSum9ai3ra4lABtU0UGW-9yLYgZk-aQfxpw";
|
|
|
|
|
|
|
|
|
|
|
|
// Create the values to be written
|
|
|
|
// Create the values to be written
|
|
|
|
List<List<Object>> values = Collections.singletonList(
|
|
|
|
List<List<Object>> values = Collections.singletonList(
|
|
|
|
Arrays.asList(deviceId, type, String.valueOf(System.currentTimeMillis() - startTime))
|
|
|
|
Arrays.asList(deviceId, type, String.valueOf(System.currentTimeMillis() - startTime))
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// Create the value range
|
|
|
|
// Create the value range
|
|
|
|
ValueRange body = new ValueRange().setValues(values);
|
|
|
|
ValueRange body = new ValueRange().setValues(values);
|
|
|
|
|
|
|
|
|
|
|
|
// Write the values to the spreadsheet
|
|
|
|
// Write the values to the spreadsheet
|
|
|
|
sheetsService.spreadsheets().values()
|
|
|
|
sheetsService.spreadsheets().values()
|
|
|
|
.append(spreadsheetId, "Sheet1", body)
|
|
|
|
.append(spreadsheetId, "Sheet1", body)
|
|
|
@ -717,14 +745,14 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
String errorMessage = "Issue in retrieving device management service instance";
|
|
|
|
String errorMessage = "Issue in retrieving device management service instance";
|
|
|
|
log.error(errorMessage, e);
|
|
|
|
log.error(errorMessage, e);
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
|
|
|
|
}catch (IOException e) {
|
|
|
|
} catch (IOException e) {
|
|
|
|
// Handle any errors occurred while writing to the spreadsheet
|
|
|
|
// Handle any errors occurred while writing to the spreadsheet
|
|
|
|
String errorMessage = "Error writing response time to Google Spreadsheet";
|
|
|
|
String errorMessage = "Error writing response time to Google Spreadsheet";
|
|
|
|
log.error(errorMessage, e);
|
|
|
|
log.error(errorMessage, e);
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@GET
|
|
|
|
@GET
|
|
|
|
@Path("/status/operations/{type}/{id}")
|
|
|
|
@Path("/status/operations/{type}/{id}")
|
|
|
|
public Response getOperationsByDeviceAndStatus(@PathParam("type") String type, @PathParam("id") String deviceId,
|
|
|
|
public Response getOperationsByDeviceAndStatus(@PathParam("type") String type, @PathParam("id") String deviceId,
|
|
|
@ -735,8 +763,8 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
return Response.status(Response.Status.BAD_REQUEST).build();
|
|
|
|
return Response.status(Response.Status.BAD_REQUEST).build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
long startTime = System.currentTimeMillis(); // Record the start time
|
|
|
|
long startTime = System.currentTimeMillis(); // Record the start time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
if (!DeviceMgtAPIUtils.getDeviceManagementService().getAvailableDeviceTypes().contains(type)) {
|
|
|
|
if (!DeviceMgtAPIUtils.getDeviceManagementService().getAvailableDeviceTypes().contains(type)) {
|
|
|
|
String errorMessage = "Invalid Device Type";
|
|
|
|
String errorMessage = "Invalid Device Type";
|
|
|
@ -748,22 +776,22 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
OperationList operationsList = new OperationList();
|
|
|
|
OperationList operationsList = new OperationList();
|
|
|
|
operationsList.setList(operations);
|
|
|
|
operationsList.setList(operations);
|
|
|
|
operationsList.setCount(operations.size());
|
|
|
|
operationsList.setCount(operations.size());
|
|
|
|
|
|
|
|
|
|
|
|
long endTime = System.currentTimeMillis(); // Record the end time
|
|
|
|
long endTime = System.currentTimeMillis(); // Record the end time
|
|
|
|
long responseTime = endTime - startTime; // Calculate the response time
|
|
|
|
long responseTime = endTime - startTime; // Calculate the response time
|
|
|
|
|
|
|
|
|
|
|
|
// Save the response time to Google Spreadsheet
|
|
|
|
// Save the response time to Google Spreadsheet
|
|
|
|
Sheets sheetsService = createSheetsService(); // Create Sheets service using loaded credentials
|
|
|
|
Sheets sheetsService = createSheetsService(); // Create Sheets service using loaded credentials
|
|
|
|
String spreadsheetId = "1OZCS5NRwwSum9ai3ra4lABtU0UGW-9yLYgZk-aQfxpw";
|
|
|
|
String spreadsheetId = "1OZCS5NRwwSum9ai3ra4lABtU0UGW-9yLYgZk-aQfxpw";
|
|
|
|
|
|
|
|
|
|
|
|
// Create the values to be written
|
|
|
|
// Create the values to be written
|
|
|
|
List<List<Object>> values = Collections.singletonList(
|
|
|
|
List<List<Object>> values = Collections.singletonList(
|
|
|
|
Arrays.asList(deviceId, type, status.toString(), String.valueOf(responseTime))
|
|
|
|
Arrays.asList(deviceId, type, status.toString(), String.valueOf(responseTime))
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// Create the value range
|
|
|
|
// Create the value range
|
|
|
|
ValueRange body = new ValueRange().setValues(values);
|
|
|
|
ValueRange body = new ValueRange().setValues(values);
|
|
|
|
|
|
|
|
|
|
|
|
// Write the values to the spreadsheet
|
|
|
|
// Write the values to the spreadsheet
|
|
|
|
sheetsService.spreadsheets().values()
|
|
|
|
sheetsService.spreadsheets().values()
|
|
|
|
.append(spreadsheetId, "Sheet1", body)
|
|
|
|
.append(spreadsheetId, "Sheet1", body)
|
|
|
@ -778,32 +806,11 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
String errorMessage = "Issue in retrieving device management service";
|
|
|
|
String errorMessage = "Issue in retrieving device management service";
|
|
|
|
log.error(errorMessage, e);
|
|
|
|
log.error(errorMessage, e);
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
|
|
|
|
}catch (IOException e) {
|
|
|
|
} catch (IOException e) {
|
|
|
|
// Handle any errors occurred while writing to the spreadsheet
|
|
|
|
// Handle any errors occurred while writing to the spreadsheet
|
|
|
|
String errorMessage = "Error writing response time to Google Spreadsheet";
|
|
|
|
String errorMessage = "Error writing response time to Google Spreadsheet";
|
|
|
|
log.error(errorMessage, e);
|
|
|
|
log.error(errorMessage, e);
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static List<ComplianceFeature> getComplianceFeatures(Object compliancePayload) throws
|
|
|
|
|
|
|
|
PolicyComplianceException {
|
|
|
|
|
|
|
|
String compliancePayloadString = new Gson().toJson(compliancePayload);
|
|
|
|
|
|
|
|
if (compliancePayload == null) {
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parsing json string to get compliance features.
|
|
|
|
|
|
|
|
JsonElement jsonElement = new JsonParser().parse(compliancePayloadString);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JsonArray jsonArray = jsonElement.getAsJsonArray();
|
|
|
|
|
|
|
|
Gson gson = new Gson();
|
|
|
|
|
|
|
|
ComplianceFeature complianceFeature;
|
|
|
|
|
|
|
|
List<ComplianceFeature> complianceFeatures = new ArrayList<>(jsonArray.size());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (JsonElement element : jsonArray) {
|
|
|
|
|
|
|
|
complianceFeature = gson.fromJson(element, ComplianceFeature.class);
|
|
|
|
|
|
|
|
complianceFeatures.add(complianceFeature);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return complianceFeatures;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|