Search in sources :

Example 1 with RegistryDeploymentStorageConflictException

use of org.bf2.srs.fleetmanager.storage.RegistryDeploymentStorageConflictException in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.

the class RegistryDeploymentServiceImpl method init.

@Override
public void init() throws IOException, RegistryDeploymentStorageConflictException, RegistryDeploymentNotFoundException {
    if (deploymentsConfigFile.isEmpty()) {
        return;
    }
    log.info("Loading registry deployments config file from {}", deploymentsConfigFile.get().getAbsolutePath());
    YAMLMapper mapper = new YAMLMapper();
    RegistryDeploymentsConfigList deploymentsConfigList = mapper.readValue(deploymentsConfigFile.get(), RegistryDeploymentsConfigList.class);
    List<RegistryDeploymentCreate> staticDeployments = deploymentsConfigList.getDeployments();
    Set<String> names = new HashSet<>();
    List<String> duplicatedNames = staticDeployments.stream().map(d -> {
        Set<ConstraintViolation<RegistryDeploymentCreate>> errors = validator.validate(d);
        if (!errors.isEmpty()) {
            throw new ConstraintViolationException(errors);
        }
        return d;
    }).filter(d -> !names.add(d.getName())).map(d -> d.getName()).collect(Collectors.toList());
    if (!duplicatedNames.isEmpty()) {
        throw new IllegalArgumentException("Error in static deployments config, duplicated deployments name: " + duplicatedNames.toString());
    }
    Map<String, RegistryDeploymentData> currentDeployments = storage.getAllRegistryDeployments().stream().collect(Collectors.toMap(d -> d.getName(), d -> d));
    for (RegistryDeploymentCreate dep : staticDeployments) {
        RegistryDeploymentData deploymentData = currentDeployments.get(dep.getName());
        if (deploymentData == null) {
            // deployment is new
            deploymentData = convertRegistryDeployment.convert(dep);
        } else {
            if (deploymentData.getRegistryDeploymentUrl().equals(dep.getRegistryDeploymentUrl()) && deploymentData.getTenantManagerUrl().equals(dep.getTenantManagerUrl())) {
                // no changes in the deployment
                continue;
            }
            deploymentData.setRegistryDeploymentUrl(dep.getRegistryDeploymentUrl());
            deploymentData.setTenantManagerUrl(dep.getTenantManagerUrl());
        }
        createOrUpdateRegistryDeployment(deploymentData);
    }
}
Also used : RegistryDeployment(org.bf2.srs.fleetmanager.rest.service.model.RegistryDeployment) RegistryDeploymentNotFoundException(org.bf2.srs.fleetmanager.storage.RegistryDeploymentNotFoundException) RegistryDeploymentData(org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryDeploymentData) KEY_DEPLOYMENT_ID(org.bf2.srs.fleetmanager.common.operation.auditing.AuditingConstants.KEY_DEPLOYMENT_ID) LoggerFactory(org.slf4j.LoggerFactory) RegistryDeploymentStorageConflictException(org.bf2.srs.fleetmanager.storage.RegistryDeploymentStorageConflictException) Audited(org.bf2.srs.fleetmanager.common.operation.auditing.Audited) HashSet(java.util.HashSet) Inject(javax.inject.Inject) Valid(javax.validation.Valid) YAMLMapper(com.fasterxml.jackson.dataformat.yaml.YAMLMapper) RegistryDeploymentCreate(org.bf2.srs.fleetmanager.rest.service.model.RegistryDeploymentCreate) ResourceStorage(org.bf2.srs.fleetmanager.storage.ResourceStorage) Map(java.util.Map) ConstraintViolation(javax.validation.ConstraintViolation) RegistryDeploymentStatusValue(org.bf2.srs.fleetmanager.rest.service.model.RegistryDeploymentStatusValue) ConvertRegistryDeployment(org.bf2.srs.fleetmanager.rest.service.convert.ConvertRegistryDeployment) Logger(org.slf4j.Logger) ForbiddenException(javax.ws.rs.ForbiddenException) Set(java.util.Set) Validator(javax.validation.Validator) IOException(java.io.IOException) RegistryDeploymentsConfigList(org.bf2.srs.fleetmanager.rest.service.model.RegistryDeploymentsConfigList) Collectors(java.util.stream.Collectors) File(java.io.File) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ConstraintViolationException(javax.validation.ConstraintViolationException) Optional(java.util.Optional) ApplicationScoped(javax.enterprise.context.ApplicationScoped) ConfigProperty(org.eclipse.microprofile.config.inject.ConfigProperty) RegistryDeploymentService(org.bf2.srs.fleetmanager.rest.service.RegistryDeploymentService) HashSet(java.util.HashSet) Set(java.util.Set) YAMLMapper(com.fasterxml.jackson.dataformat.yaml.YAMLMapper) RegistryDeploymentData(org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryDeploymentData) RegistryDeploymentsConfigList(org.bf2.srs.fleetmanager.rest.service.model.RegistryDeploymentsConfigList) ConstraintViolationException(javax.validation.ConstraintViolationException) RegistryDeploymentCreate(org.bf2.srs.fleetmanager.rest.service.model.RegistryDeploymentCreate) HashSet(java.util.HashSet)

Example 2 with RegistryDeploymentStorageConflictException

use of org.bf2.srs.fleetmanager.storage.RegistryDeploymentStorageConflictException in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.

the class PanacheResourceStorage method createOrUpdateRegistryDeployment.

// *** RegistryDeployment
@Override
public boolean createOrUpdateRegistryDeployment(RegistryDeploymentData deployment) throws RegistryDeploymentStorageConflictException, RegistryDeploymentNotFoundException {
    // TODO Is this necessary if using @Valid?
    requireNonNull(deployment);
    Optional<RegistryDeploymentData> existing = empty();
    if (deployment.getId() != null) {
        // TODO investigate using locks, such as optimistic locks
        existing = deploymentRepository.findByIdOptional(deployment.getId());
        if (existing.isEmpty()) {
            throw new RegistryDeploymentNotFoundException(deployment.getId().toString());
        }
    }
    try {
        final Instant now = Instant.now();
        deployment.getStatus().setLastUpdated(now);
        if (existing.isEmpty()) {
            deploymentRepository.persistAndFlush(deployment);
        } else {
            em.merge(deployment);
            em.flush();
        }
    } catch (PersistenceException ex) {
        if (ex.getCause() instanceof ConstraintViolationException) {
            throw new RegistryDeploymentStorageConflictException();
        } else {
            throw ex;
        }
    }
    return existing.isEmpty();
}
Also used : Instant(java.time.Instant) PersistenceException(javax.persistence.PersistenceException) RegistryDeploymentNotFoundException(org.bf2.srs.fleetmanager.storage.RegistryDeploymentNotFoundException) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) RegistryDeploymentData(org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryDeploymentData) RegistryDeploymentStorageConflictException(org.bf2.srs.fleetmanager.storage.RegistryDeploymentStorageConflictException)

Aggregations

RegistryDeploymentNotFoundException (org.bf2.srs.fleetmanager.storage.RegistryDeploymentNotFoundException)2 RegistryDeploymentStorageConflictException (org.bf2.srs.fleetmanager.storage.RegistryDeploymentStorageConflictException)2 RegistryDeploymentData (org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryDeploymentData)2 YAMLMapper (com.fasterxml.jackson.dataformat.yaml.YAMLMapper)1 File (java.io.File)1 IOException (java.io.IOException)1 Instant (java.time.Instant)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 Collectors.toList (java.util.stream.Collectors.toList)1 ApplicationScoped (javax.enterprise.context.ApplicationScoped)1 Inject (javax.inject.Inject)1 PersistenceException (javax.persistence.PersistenceException)1 ConstraintViolation (javax.validation.ConstraintViolation)1 ConstraintViolationException (javax.validation.ConstraintViolationException)1 Valid (javax.validation.Valid)1