/** * Queries the results with the given {@link DeviceManagementOperationQuery} parameter. * * @param scopeId The {@link ScopeId} in which to search results. * @param deviceId The id of the {@link Device} in which to search results * @param query The {@link DeviceManagementOperationQuery} to use to filter results. * @return The {@link DeviceManagementOperationListResult} of all the result matching the given {@link DeviceManagementOperationQuery} parameter. * @throws Exception Whenever something bad happens. See specific {@link KapuaService} exceptions. * @since 1.0.0 */ @ApiOperation(nickname = "deviceManagementOperationQuery", value = "Queries the DeviceManagementOperations", notes = "Queries the DeviceManagementOperations with the given DeviceManagementOperations parameter returning all matching DeviceManagementOperations", response = DeviceManagementOperationListResult.class) @POST @Path("_query") @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public DeviceManagementOperationListResult query( @ApiParam(value = "The ScopeId in which to search results.", required = true, defaultValue = DEFAULT_SCOPE_ID) @PathParam("scopeId") ScopeId scopeId, @ApiParam(value = "The id of the Device in which to search results") @PathParam("deviceId") EntityId deviceId, @ApiParam(value = "The DeviceManagementOperationQuery to use to filter results.", required = true) DeviceManagementOperationQuery query) throws Exception { query.setScopeId(scopeId); AndPredicateImpl andPredicate = new AndPredicateImpl(); andPredicate.and(new AttributePredicateImpl<>(DeviceManagementOperationAttributes.DEVICE_ID, deviceId)); query.setPredicate(andPredicate); return deviceManagementOperationRegistryService.query(query); }
/** * Deletes the DeviceManagementOperation specified by the "deviceManagementOperationId" path parameter. * * @param deviceId The id of the Device in which to delete the ManagementOperation * @param deviceManagementOperationId The id of the DeviceManagementOperation to be deleted. * @return HTTP 200 if operation has completed successfully. * @throws Exception Whenever something bad happens. See specific {@link KapuaService} exceptions. * @since 1.0.0 */ @ApiOperation(nickname = "deviceManagementOperationDelete", value = "Delete a DeviceManagementOperation", notes = "Deletes the DeviceManagementOperation specified by the \"deviceManagementOperationId\" path parameter.") @DELETE @Path("{deviceManagementOperationId}") public Response deleteDeviceManagementOperation(@PathParam("scopeId") ScopeId scopeId, @ApiParam(value = "The id of the Device in which to delete the ManagementOperation.", required = true) @PathParam("deviceId") EntityId deviceId, @ApiParam(value = "The id of the DeviceManagementOperation to be deleted", required = true) @PathParam("deviceManagementOperationId") EntityId deviceManagementOperationId) throws Exception { deviceManagementOperationRegistryService.delete(scopeId, deviceManagementOperationId); return returnOk(); } }
protected void closeManagementOperation(KapuaId scopeId, KapuaId deviceId, KapuaId operationId, KapuaResponseMessage<?, ?> responseMessageMessage) throws KapuaException { DeviceManagementOperationQuery query = DEVICE_MANAGEMENT_OPERATION_FACTORY.newQuery(scopeId); query.setPredicate( new AndPredicateImpl( new AttributePredicateImpl<>(DeviceManagementOperationAttributes.DEVICE_ID, deviceId), new AttributePredicateImpl<>(DeviceManagementOperationAttributes.OPERATION_ID, operationId) ) ); DeviceManagementOperation deviceManagementOperation = DEVICE_MANAGEMENT_OPERATION_REGISTRY_SERVICE.query(query).getFirstItem(); if (deviceManagementOperation == null) { throw new KapuaEntityNotFoundException(DeviceManagementOperation.TYPE, operationId); } deviceManagementOperation.setStatus(responseMessageMessage.getResponseCode().isAccepted() ? OperationStatus.COMPLETED : OperationStatus.FAILED); deviceManagementOperation.setEndedOn(responseMessageMessage.getReceivedOn()); KapuaSecurityUtils.doPrivileged(() -> DEVICE_MANAGEMENT_OPERATION_REGISTRY_SERVICE.update(deviceManagementOperation)); }
protected KapuaId createManagementOperation(KapuaId scopeId, KapuaId deviceId, KapuaId operationId, int totalCheckpoints, KapuaRequestMessage<?, ?> requestMessage) throws KapuaException { DeviceManagementOperationCreator deviceManagementOperationCreator = DEVICE_MANAGEMENT_OPERATION_FACTORY.newCreator(scopeId); deviceManagementOperationCreator.setDeviceId(deviceId); deviceManagementOperationCreator.setOperationId(operationId); deviceManagementOperationCreator.setStartedOn(new Date()); deviceManagementOperationCreator.setAppId(requestMessage.getChannel().getAppName().getValue()); deviceManagementOperationCreator.setAction(requestMessage.getChannel().getMethod()); deviceManagementOperationCreator.setResource(!requestMessage.getChannel().getSemanticParts().isEmpty() ? requestMessage.getChannel().getSemanticParts().get(0) : null); deviceManagementOperationCreator.setStatus(OperationStatus.RUNNING); deviceManagementOperationCreator.setInputProperties(extractInputProperties(requestMessage)); DeviceManagementOperation deviceManagementOperation = KapuaSecurityUtils.doPrivileged(() -> DEVICE_MANAGEMENT_OPERATION_REGISTRY_SERVICE.create(deviceManagementOperationCreator)); return deviceManagementOperation.getId(); }
default void closeDeviceManagementOperation(KapuaId scopeId, KapuaId operationId, Date updateOn, OperationStatus finalStatus) throws KapuaException { DeviceManagementOperation deviceManagementOperation = null; short attempts = 0; short limit = 3; boolean failed = false; do { try { deviceManagementOperation = getDeviceManagementOperation(scopeId, operationId); deviceManagementOperation.setEndedOn(updateOn); deviceManagementOperation.setStatus(finalStatus); DEVICE_MANAGEMENT_OPERATION_REGISTRY_SERVICE.update(deviceManagementOperation); LOG.info("Update DeviceManagementOperation {} with status {}... SUCCEEDED!", operationId, finalStatus); break; } catch (Exception e) { failed = true; attempts++; if (attempts >= limit) { throw e; } else { LOG.warn("Update DeviceManagementOperation {} with status {}... FAILED! Retrying...", operationId, finalStatus); } } } while (failed); ManagementOperationNotificationQuery query = MANAGEMENT_OPERATION_NOTIFICATION_FACTORY.newQuery(scopeId); query.setPredicate(new AttributePredicateImpl<>(ManagementOperationNotificationAttributes.OPERATION_ID, deviceManagementOperation.getId())); ManagementOperationNotificationListResult notifications = MANAGEMENT_OPERATION_NOTIFICATION_REGISTRY_SERVICE.query(query); for (ManagementOperationNotification mon : notifications.getItems()) { MANAGEMENT_OPERATION_NOTIFICATION_REGISTRY_SERVICE.delete(mon.getScopeId(), mon.getId()); } }
@Override public ManagementOperationNotification create(ManagementOperationNotificationCreator creator) throws KapuaException { // // Argument Validation ArgumentValidator.notNull(creator, "managementOperationNotificationCreator"); ArgumentValidator.notNull(creator.getScopeId(), "managementOperationNotificationCreator.scopeId"); ArgumentValidator.notNull(creator.getOperationId(), "managementOperationNotificationCreator.operationId"); ArgumentValidator.notNull(creator.getSentOn(), "managementOperationNotificationCreator.sentOn"); ArgumentValidator.notNull(creator.getStatus(), "managementOperationNotificationCreator.status"); ArgumentValidator.notNull(creator.getProgress(), "managementOperationNotificationCreator.progress"); ArgumentValidator.notNegative(creator.getProgress(), "managementOperationNotificationCreator.progress"); // // Check access authorizationService.checkPermission(permissionFactory.newPermission(DeviceManagementRegistryDomains.DEVICE_MANAGEMENT_REGISTRY_DOMAIN, Actions.write, null)); // // Check operation existence if (KapuaSecurityUtils.doPrivileged(() -> deviceManagementOperationRegistryService.find(creator.getScopeId(), creator.getOperationId()) == null)) { throw new KapuaEntityNotFoundException(DeviceManagementOperation.TYPE, creator.getOperationId()); } // // Do create return entityManagerSession.onTransactedInsert(em -> ManagementOperationNotificationDAO.create(em, creator)); }
query.setLimit(1); DeviceManagementOperationListResult results = deviceManagementOperationRegistryService.query(query);
default DeviceManagementOperation getDeviceManagementOperation(KapuaId scopeId, KapuaId operationId) throws KapuaException { DeviceManagementOperationQuery query = DEVICE_MANAGEMENT_OPERATION_FACTORY.newQuery(scopeId); query.setPredicate(new AttributePredicateImpl<>(DeviceManagementOperationAttributes.OPERATION_ID, operationId)); DeviceManagementOperationListResult operations = DEVICE_MANAGEMENT_OPERATION_REGISTRY_SERVICE.query(query); DeviceManagementOperation deviceManagementOperation = operations.getFirstItem(); if (deviceManagementOperation == null) { throw new KapuaEntityNotFoundException(DeviceManagementOperation.TYPE, operationId); } return deviceManagementOperation; } }