@Override public DeviceManagementOperation update(DeviceManagementOperation entity) throws KapuaException { // // Argument Validation ArgumentValidator.notNull(entity, "deviceManagementOperation"); ArgumentValidator.notNull(entity.getScopeId(), "deviceManagementOperation.scopeId"); ArgumentValidator.notNull(entity.getId(), "deviceManagementOperation.id"); ArgumentValidator.notNull(entity.getStartedOn(), "deviceManagementOperation.startedOn"); ArgumentValidator.notNull(entity.getDeviceId(), "deviceManagementOperation.deviceId"); ArgumentValidator.notNull(entity.getOperationId(), "deviceManagementOperation.operationId"); ArgumentValidator.notNull(entity.getStatus(), "deviceManagementOperation.status"); ArgumentValidator.notNull(entity.getAppId(), "deviceManagementOperation.appId"); ArgumentValidator.notNull(entity.getAction(), "deviceManagementOperation.action"); // // Check access AUTHORIZATION_SERVICE.checkPermission(PERMISSION_FACTORY.newPermission(DeviceManagementRegistryDomains.DEVICE_MANAGEMENT_REGISTRY_DOMAIN, Actions.write, null)); // // Check device existence if (KapuaSecurityUtils.doPrivileged(() -> DEVICE_REGISTRY_SERVICE.find(entity.getScopeId(), entity.getDeviceId()) == null)) { throw new KapuaEntityNotFoundException(Device.TYPE, entity.getDeviceId()); } // // Check existence if (find(entity.getScopeId(), entity.getId()) == null) { throw new KapuaEntityNotFoundException(DeviceManagementOperation.TYPE, entity.getId()); } // // Do update return entityManagerSession.onTransactedResult(em -> DeviceManagementOperationDAO.update(em, entity)); }
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()); } }
default void processCompletedNotification(KapuaId scopeId, KapuaId operationId, Date updateOn, String resource) throws KapuaException { DeviceManagementOperation deviceManagementOperation = getDeviceManagementOperation(scopeId, operationId); // // UGLY 'DEPLOY-V2'-related part // boolean isLastNotification = true; for (DeviceManagementOperationProperty ip : deviceManagementOperation.getInputProperties()) { if (ip.getName().equals("kapua.package.download.install")) { if (resource.equals("download")) { isLastNotification = !Boolean.parseBoolean(ip.getPropertyValue()); } break; } } if (isLastNotification) { closeDeviceManagementOperation(scopeId, operationId, updateOn, OperationStatus.COMPLETED); } else { addManagementNotification(scopeId, operationId, updateOn, OperationStatus.COMPLETED, resource, 100); } }
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)); }
default void addManagementNotification(KapuaId scopeId, KapuaId operationId, Date updateOn, OperationStatus operationStatus, String resource, Integer progress) throws KapuaException { DeviceManagementOperation deviceManagementOperation = getDeviceManagementOperation(scopeId, operationId); ManagementOperationNotificationCreator managementOperationNotificationCreator = MANAGEMENT_OPERATION_NOTIFICATION_FACTORY.newCreator(scopeId); managementOperationNotificationCreator.setOperationId(deviceManagementOperation.getId()); managementOperationNotificationCreator.setSentOn(updateOn); managementOperationNotificationCreator.setStatus(operationStatus); managementOperationNotificationCreator.setResource(resource); managementOperationNotificationCreator.setProgress(progress); MANAGEMENT_OPERATION_NOTIFICATION_REGISTRY_SERVICE.create(managementOperationNotificationCreator); }
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(); }