Search in sources :

Example 1 with QuotaPlan

use of org.bf2.srs.fleetmanager.service.model.QuotaPlan in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.

the class FileQuotaPlansService method init.

@Override
public void init() throws IOException {
    log.debug("Using FileQuotaPlansService implementation of QuotaPlansService");
    if (plansConfigFile.isEmpty()) {
        throw new IllegalArgumentException("Error in static quota plans config: Property 'registry.quota.plans.config.file' is required.");
    }
    log.info("Loading registry quota plans config file from {}", plansConfigFile.get().getAbsolutePath());
    YAMLMapper mapper = SerDesObjectMapperProducer.getYAMLMapper();
    QuotaPlansConfigList quotaPlansConfigList = mapper.readValue(plansConfigFile.get(), QuotaPlansConfigList.class);
    List<QuotaPlan> staticQuotaPlans = quotaPlansConfigList.getPlans();
    Set<String> names = new HashSet<>();
    List<String> duplicatedNames = staticQuotaPlans.stream().map(d -> {
        Set<ConstraintViolation<QuotaPlan>> 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 quota plans config, duplicated plan name: " + duplicatedNames.toString());
    }
    if (!names.contains(defaultQuotaPlan)) {
        throw new IllegalArgumentException("Error in static quota plans config, default plan does not exist in plans config, default plan name: " + defaultQuotaPlan);
    }
    for (QuotaPlan p : staticQuotaPlans) {
        tmClient.validateConfig(p.getResources());
        plans.put(p.getName(), p);
    }
    List<OrganizationAssignment> staticOrganizationAssignments = quotaPlansConfigList.getOrganizations();
    if (staticOrganizationAssignments == null)
        staticOrganizationAssignments = Collections.emptyList();
    for (OrganizationAssignment assignment : staticOrganizationAssignments) {
        if (!plans.containsKey(assignment.getPlan())) {
            throw new IllegalStateException("Could not find quota plan named '" + assignment.getPlan() + "' intended for organization ID '" + assignment.getOrgId() + "'");
        }
        organizationAssignments.put(assignment.getOrgId(), assignment);
    }
    if (quotaPlansConfigList.getReconcile() != null && quotaPlansConfigList.getReconcile()) {
        reconcile();
    }
}
Also used : TenantManagerService(org.bf2.srs.fleetmanager.spi.tenants.TenantManagerService) UpdateTenantRequest(org.bf2.srs.fleetmanager.spi.tenants.model.UpdateTenantRequest) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) HashSet(java.util.HashSet) Inject(javax.inject.Inject) TenantLimit(org.bf2.srs.fleetmanager.spi.tenants.model.TenantLimit) YAMLMapper(com.fasterxml.jackson.dataformat.yaml.YAMLMapper) ResourceStorage(org.bf2.srs.fleetmanager.storage.ResourceStorage) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) QuotaPlan(org.bf2.srs.fleetmanager.service.model.QuotaPlan) SerDesObjectMapperProducer(org.bf2.srs.fleetmanager.common.SerDesObjectMapperProducer) NoSuchElementException(java.util.NoSuchElementException) ConstraintViolation(javax.validation.ConstraintViolation) TenantManagerServiceException(org.bf2.srs.fleetmanager.spi.tenants.TenantManagerServiceException) QuotaPlansService(org.bf2.srs.fleetmanager.service.QuotaPlansService) QuotaPlansConfigList(org.bf2.srs.fleetmanager.service.model.QuotaPlansConfigList) Logger(org.slf4j.Logger) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Validator(javax.validation.Validator) IOException(java.io.IOException) RegistryData(org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryData) Collectors(java.util.stream.Collectors) File(java.io.File) OrganizationAssignment(org.bf2.srs.fleetmanager.service.model.OrganizationAssignment) List(java.util.List) ConstraintViolationException(javax.validation.ConstraintViolationException) IfBuildProfile(io.quarkus.arc.profile.IfBuildProfile) Optional(java.util.Optional) Utils(org.bf2.srs.fleetmanager.execution.impl.workers.Utils) TenantNotFoundServiceException(org.bf2.srs.fleetmanager.spi.tenants.TenantNotFoundServiceException) ApplicationScoped(javax.enterprise.context.ApplicationScoped) ConfigProperty(org.eclipse.microprofile.config.inject.ConfigProperty) Collections(java.util.Collections) HashSet(java.util.HashSet) Set(java.util.Set) YAMLMapper(com.fasterxml.jackson.dataformat.yaml.YAMLMapper) QuotaPlan(org.bf2.srs.fleetmanager.service.model.QuotaPlan) OrganizationAssignment(org.bf2.srs.fleetmanager.service.model.OrganizationAssignment) ConstraintViolationException(javax.validation.ConstraintViolationException) QuotaPlansConfigList(org.bf2.srs.fleetmanager.service.model.QuotaPlansConfigList) HashSet(java.util.HashSet)

Aggregations

YAMLMapper (com.fasterxml.jackson.dataformat.yaml.YAMLMapper)1 IfBuildProfile (io.quarkus.arc.profile.IfBuildProfile)1 File (java.io.File)1 IOException (java.io.IOException)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 NoSuchElementException (java.util.NoSuchElementException)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 Optional (java.util.Optional)1 Set (java.util.Set)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Collectors (java.util.stream.Collectors)1 ApplicationScoped (javax.enterprise.context.ApplicationScoped)1 Inject (javax.inject.Inject)1 ConstraintViolation (javax.validation.ConstraintViolation)1 ConstraintViolationException (javax.validation.ConstraintViolationException)1 Validator (javax.validation.Validator)1