private Action getUpdateActionStatus(final Status status, final ActionStatusCreate actionStatus) { if (Status.CANCELED.equals(status)) { return controllerManagement.addCancelActionStatus(actionStatus); } return controllerManagement.addUpdateActionStatus(actionStatus); }
@Override public ResponseEntity<Void> putConfigData(@Valid @RequestBody final DdiConfigData configData, @PathVariable("tenant") final String tenant, @PathVariable("controllerId") final String controllerId) { controllerManagement.updateControllerAttributes(controllerId, configData.getData(), getUpdateMode(configData)); return ResponseEntity.ok().build(); }
private ActionStatus checkAndLogDownload(final HttpServletRequest request, final Target target, final Long module) { final Action action = controllerManagement .getActionForDownloadByTargetAndSoftwareModule(target.getControllerId(), module) .orElseThrow(() -> new SoftwareModuleNotAssignedToTargetException(module, target.getControllerId())); final String range = request.getHeader("Range"); String message; if (range != null) { message = RepositoryConstants.SERVER_MESSAGE_PREFIX + "Target downloads range " + range + " of: " + request.getRequestURI(); } else { message = RepositoryConstants.SERVER_MESSAGE_PREFIX + "Target downloads " + request.getRequestURI(); } return controllerManagement.addInformationalActionStatus( entityFactory.actionStatus().create(action.getId()).status(Status.DOWNLOAD).message(message)); }
@Override public ResponseEntity<DdiControllerBase> getControllerBase(@PathVariable("tenant") final String tenant, @PathVariable("controllerId") final String controllerId) { LOG.debug("getControllerBase({})", controllerId); final Target target = controllerManagement.findOrRegisterTargetIfItDoesNotexist(controllerId, IpUtil .getClientIpFromRequest(requestResponseContextHolder.getHttpServletRequest(), securityProperties)); final Action action = controllerManagement.findOldestActiveActionByTarget(controllerId).orElse(null); checkAndCancelExpiredAction(action); return new ResponseEntity<>( DataConversionHelper.fromTarget(target, action, action == null ? controllerManagement.getPollingTime() : controllerManagement.getPollingTimeForAction(action.getId()), tenantAware), HttpStatus.OK); }
@Override public ResponseEntity<List<DdiArtifact>> getSoftwareModulesArtifacts(@PathVariable("tenant") final String tenant, @PathVariable("controllerId") final String controllerId, @PathVariable("softwareModuleId") final Long softwareModuleId) { LOG.debug("getSoftwareModulesArtifacts({})", controllerId); final Target target = controllerManagement.getByControllerId(controllerId) .orElseThrow(() -> new EntityNotFoundException(Target.class, controllerId)); final SoftwareModule softwareModule = controllerManagement.getSoftwareModule(softwareModuleId) .orElseThrow(() -> new EntityNotFoundException(SoftwareModule.class, softwareModuleId)); return new ResponseEntity<>( DataConversionHelper.createArtifacts(target, softwareModule, artifactUrlHandler, systemManagement, new ServletServerHttpRequest(requestResponseContextHolder.getHttpServletRequest())), HttpStatus.OK); }
private Action sendUpdateActionStatusToTarget(final Status status, final Action updActA, final Collection<String> msgs) { return controllerManagament.addUpdateActionStatus( entityFactory.actionStatus().create(updActA.getId()).status(status).messages(msgs)); }
@Override public ResponseEntity<Void> postBasedeploymentActionFeedback(@Valid @RequestBody final DdiActionFeedback feedback, @PathVariable("tenant") final String tenant, @PathVariable("controllerId") final String controllerId, @PathVariable("actionId") @NotEmpty final Long actionId) { LOG.debug("provideBasedeploymentActionFeedback for target [{},{}]: {}", controllerId, actionId, feedback); final Target target = controllerManagement.getByControllerId(controllerId) .orElseThrow(() -> new EntityNotFoundException(Target.class, controllerId)); if (!actionId.equals(feedback.getId())) { LOG.warn( "provideBasedeploymentActionFeedback: action in payload ({}) was not identical to action in path ({}).", feedback.getId(), actionId); return ResponseEntity.notFound().build(); } final Action action = findActionWithExceptionIfNotFound(actionId); if (!action.getTarget().getId().equals(target.getId())) { LOG.warn(GIVEN_ACTION_IS_NOT_ASSIGNED_TO_GIVEN_TARGET, action.getId(), target.getId()); return ResponseEntity.notFound().build(); } if (!action.isActive()) { LOG.warn("Updating action {} with feedback {} not possible since action not active anymore.", action.getId(), feedback.getId()); return new ResponseEntity<>(HttpStatus.GONE); } controllerManagement.addUpdateActionStatus(generateUpdateStatus(feedback, controllerId, feedback.getId())); return ResponseEntity.ok().build(); }
@Override public ResponseEntity<Void> postCancelActionFeedback(@Valid @RequestBody final DdiActionFeedback feedback, @PathVariable("tenant") final String tenant, @PathVariable("controllerId") @NotEmpty final String controllerId, @PathVariable("actionId") @NotEmpty final Long actionId) { LOG.debug("provideCancelActionFeedback for target [{}]: {}", controllerId, feedback); final Target target = controllerManagement.getByControllerId(controllerId) .orElseThrow(() -> new EntityNotFoundException(Target.class, controllerId)); if (!actionId.equals(feedback.getId())) { LOG.warn( "provideBasedeploymentActionFeedback: action in payload ({}) was not identical to action in path ({}).", feedback.getId(), actionId); return ResponseEntity.notFound().build(); } final Action action = findActionWithExceptionIfNotFound(actionId); if (!action.getTarget().getId().equals(target.getId())) { LOG.warn(GIVEN_ACTION_IS_NOT_ASSIGNED_TO_GIVEN_TARGET, action.getId(), target.getId()); return ResponseEntity.notFound().build(); } controllerManagement .addCancelActionStatus(generateActionCancelStatus(feedback, target, feedback.getId(), entityFactory)); return ResponseEntity.ok().build(); }
LOG.debug("getControllerBasedeploymentAction({},{})", controllerId, resource); final Target target = controllerManagement.getByControllerId(controllerId) .orElseThrow(() -> new EntityNotFoundException(Target.class, controllerId)); controllerManagement); final List<String> actionHistoryMsgs = controllerManagement.getActionHistoryMessages(action.getId(), actionHistoryMessageCount == null ? Integer.parseInt(DdiRestConstants.NO_ACTION_HISTORY) : actionHistoryMessageCount); controllerManagement.registerRetrieved(action.getId(), RepositoryConstants.SERVER_MESSAGE_PREFIX + "Target retrieved update action and should start now the download.");
private void lookIfUpdateAvailable(final Target target) { final Optional<Action> actionOptional = controllerManagement .findOldestActiveActionByTarget(target.getControllerId()); if (!actionOptional.isPresent()) { return; } final Action action = actionOptional.get(); if (action.isCancelingOrCanceled()) { amqpMessageDispatcherService.sendCancelMessageToTarget(target.getTenant(), target.getControllerId(), action.getId(), target.getAddress()); return; } final Map<SoftwareModule, List<SoftwareModuleMetadata>> modules = Maps .newHashMapWithExpectedSize(action.getDistributionSet().getModules().size()); final Map<Long, List<SoftwareModuleMetadata>> metadata = controllerManagement .findTargetVisibleMetaDataBySoftwareModuleId(action.getDistributionSet().getModules().stream() .map(SoftwareModule::getId).collect(Collectors.toList())); action.getDistributionSet().getModules().forEach(module -> modules.put(module, metadata.get(module.getId()))); amqpMessageDispatcherService.sendUpdateMessageToTarget(action.getTenant(), action.getTarget(), action.getId(), modules, action.isMaintenanceWindowAvailable()); }
@Override public ResponseEntity<DdiCancel> getControllerCancelAction(@PathVariable("tenant") final String tenant, @PathVariable("controllerId") @NotEmpty final String controllerId, @PathVariable("actionId") @NotEmpty final Long actionId) { LOG.debug("getControllerCancelAction({})", controllerId); final Target target = controllerManagement.getByControllerId(controllerId) .orElseThrow(() -> new EntityNotFoundException(Target.class, controllerId)); final Action action = findActionWithExceptionIfNotFound(actionId); if (!action.getTarget().getId().equals(target.getId())) { LOG.warn(GIVEN_ACTION_IS_NOT_ASSIGNED_TO_GIVEN_TARGET, action.getId(), target.getId()); return ResponseEntity.notFound().build(); } if (action.isCancelingOrCanceled()) { final DdiCancel cancel = new DdiCancel(String.valueOf(action.getId()), new DdiCancelActionToStop(String.valueOf(action.getId()))); LOG.debug("Found an active CancelAction for target {}. returning cancel: {}", controllerId, cancel); controllerManagement.registerRetrieved(action.getId(), RepositoryConstants.SERVER_MESSAGE_PREFIX + "Target retrieved cancel action and should start now the cancelation."); return new ResponseEntity<>(cancel, HttpStatus.OK); } return ResponseEntity.notFound().build(); }
private Action findActionWithExceptionIfNotFound(final Long actionId) { return controllerManagement.findActionWithDetails(actionId) .orElseThrow(() -> new EntityNotFoundException(Action.class, actionId)); }
@Override public HeaderAuthentication getPreAuthenticatedCredentials(final DmfTenantSecurityToken securityToken) { final Optional<Target> target = systemSecurityContext.runAsSystemAsTenant(() -> { if (securityToken.getTargetId() != null) { return controllerManagement.get(securityToken.getTargetId()); } return controllerManagement.getByControllerId(securityToken.getControllerId()); }, securityToken.getTenant()); return target.map(t -> new HeaderAuthentication(t.getControllerId(), systemSecurityContext.runAsSystemAsTenant(() -> t.getSecurityToken(), securityToken.getTenant()))) .orElse(null); }
static List<DdiChunk> createChunks(final Target target, final Action uAction, final ArtifactUrlHandler artifactUrlHandler, final SystemManagement systemManagement, final HttpRequest request, final ControllerManagement controllerManagement) { final Map<Long, List<SoftwareModuleMetadata>> metadata = controllerManagement .findTargetVisibleMetaDataBySoftwareModuleId(uAction.getDistributionSet().getModules().stream() .map(SoftwareModule::getId).collect(Collectors.toList())); return uAction.getDistributionSet().getModules().stream() .map(module -> new DdiChunk(mapChunkLegacyKeys(module.getType().getKey()), module.getVersion(), module.getName(), createArtifacts(target, module, artifactUrlHandler, systemManagement, request), mapMetadata(metadata.get(module.getId())))) .collect(Collectors.toList()); }
/** * Method to create a new target or to find the target if it already exists. * * @param targetID * the ID of the target/thing * @param ip * the ip of the target/thing */ private void registerTarget(final Message message, final String virtualHost) { final String thingId = getStringHeaderKey(message, MessageHeaderKey.THING_ID, "ThingId is null"); final String replyTo = message.getMessageProperties().getReplyTo(); if (StringUtils.isEmpty(replyTo)) { logAndThrowMessageError(message, "No ReplyTo was set for the createThing message."); } final URI amqpUri = IpUtil.createAmqpUri(virtualHost, replyTo); final Target target = controllerManagement.findOrRegisterTargetIfItDoesNotexist(thingId, amqpUri); LOG.debug("Target {} reported online state.", thingId); lookIfUpdateAvailable(target); }
getTestSchedule(-5), getTestDuration(10), getTestTimeZone()).getActions().get(0); controllerManagement.addInformationalActionStatus( entityFactory.actionStatus().create(actionId).message("Started download").status(Status.DOWNLOAD)); controllerManagement.addInformationalActionStatus(entityFactory.actionStatus().create(actionId) .message("Download failed. ErrorCode #5876745. Retry").status(Status.WARNING)); controllerManagement.addInformationalActionStatus( entityFactory.actionStatus().create(actionId).message("Download done").status(Status.DOWNLOADED)); controllerManagement.addInformationalActionStatus( entityFactory.actionStatus().create(actionId).message("Write firmware").status(Status.RUNNING)); controllerManagement.addInformationalActionStatus( entityFactory.actionStatus().create(actionId).message("Reboot").status(Status.RUNNING));
private Action sendUpdateActionStatusToTarget(final Status status, final Action updActA, final Collection<String> msgs) { return controllerManagament.addUpdateActionStatus( entityFactory.actionStatus().create(updActA.getId()).status(status).messages(msgs)); }
@Override public ResponseEntity<DdiControllerBase> getControllerBase(@PathVariable("tenant") final String tenant, @PathVariable("controllerId") final String controllerId) { LOG.debug("getControllerBase({})", controllerId); final Target target = controllerManagement.findOrRegisterTargetIfItDoesNotexist(controllerId, IpUtil .getClientIpFromRequest(requestResponseContextHolder.getHttpServletRequest(), securityProperties)); final Action action = controllerManagement.findOldestActiveActionByTarget(controllerId).orElse(null); checkAndCancelExpiredAction(action); return new ResponseEntity<>( DataConversionHelper.fromTarget(target, action, action == null ? controllerManagement.getPollingTime() : controllerManagement.getPollingTimeForAction(action.getId()), tenantAware), HttpStatus.OK); }
@Override public ResponseEntity<Void> postBasedeploymentActionFeedback(@Valid @RequestBody final DdiActionFeedback feedback, @PathVariable("tenant") final String tenant, @PathVariable("controllerId") final String controllerId, @PathVariable("actionId") @NotEmpty final Long actionId) { LOG.debug("provideBasedeploymentActionFeedback for target [{},{}]: {}", controllerId, actionId, feedback); final Target target = controllerManagement.getByControllerId(controllerId) .orElseThrow(() -> new EntityNotFoundException(Target.class, controllerId)); if (!actionId.equals(feedback.getId())) { LOG.warn( "provideBasedeploymentActionFeedback: action in payload ({}) was not identical to action in path ({}).", feedback.getId(), actionId); return ResponseEntity.notFound().build(); } final Action action = findActionWithExceptionIfNotFound(actionId); if (!action.getTarget().getId().equals(target.getId())) { LOG.warn(GIVEN_ACTION_IS_NOT_ASSIGNED_TO_GIVEN_TARGET, action.getId(), target.getId()); return ResponseEntity.notFound().build(); } if (!action.isActive()) { LOG.warn("Updating action {} with feedback {} not possible since action not active anymore.", action.getId(), feedback.getId()); return new ResponseEntity<>(HttpStatus.GONE); } controllerManagement.addUpdateActionStatus(generateUpdateStatus(feedback, controllerId, feedback.getId())); return ResponseEntity.ok().build(); }
@Override public ResponseEntity<List<DdiArtifact>> getSoftwareModulesArtifacts(@PathVariable("tenant") final String tenant, @PathVariable("controllerId") final String controllerId, @PathVariable("softwareModuleId") final Long softwareModuleId) { LOG.debug("getSoftwareModulesArtifacts({})", controllerId); final Target target = controllerManagement.getByControllerId(controllerId) .orElseThrow(() -> new EntityNotFoundException(Target.class, controllerId)); final SoftwareModule softwareModule = controllerManagement.getSoftwareModule(softwareModuleId) .orElseThrow(() -> new EntityNotFoundException(SoftwareModule.class, softwareModuleId)); return new ResponseEntity<>( DataConversionHelper.createArtifacts(target, softwareModule, artifactUrlHandler, systemManagement, new ServletServerHttpRequest(requestResponseContextHolder.getHttpServletRequest())), HttpStatus.OK); }