io.gravitee.gateway.standalone.node.GatewayNodeMetadataResolver Maven / Gradle / Ivy
The newest version!
/**
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.gateway.standalone.node;
import static io.gravitee.node.api.Node.META_ENVIRONMENTS;
import static io.gravitee.node.api.Node.META_ORGANIZATIONS;
import io.gravitee.gateway.env.GatewayConfiguration;
import io.gravitee.node.api.NodeMetadataResolver;
import io.gravitee.repository.management.api.EnvironmentRepository;
import io.gravitee.repository.management.api.OrganizationRepository;
import io.gravitee.repository.management.model.Environment;
import io.gravitee.repository.management.model.Organization;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
/**
* @author Jeoffrey HAEYAERT (jeoffrey.haeyaert at graviteesource.com)
* @author GraviteeSource Team
*/
@Slf4j
public class GatewayNodeMetadataResolver implements NodeMetadataResolver {
@Lazy
@Autowired
private OrganizationRepository organizationRepository;
@Lazy
@Autowired
private EnvironmentRepository environmentRepository;
@Autowired
private GatewayConfiguration configuration;
public Map resolve() {
final HashMap metadata = new HashMap<>();
if (configuration.useLegacyEnvironmentHrids()) {
Set organizationIds = resolveOrganizationsFromHrids();
Set environmentIds = resolveEnvironmentFromHrids(organizationIds);
metadata.put(META_ORGANIZATIONS, organizationIds);
metadata.put(META_ENVIRONMENTS, environmentIds);
} else {
Set environmentIds = resolveEnvironments();
metadata.put(META_ENVIRONMENTS, environmentIds);
}
configuration.tenant().ifPresent(tenant -> metadata.put("tenant", tenant));
configuration.shardingTags().ifPresent(shardingTags -> metadata.put("tags", shardingTags));
configuration.zone().ifPresent(zone -> metadata.put("zone", zone));
return metadata;
}
private Set resolveEnvironments() {
final Optional> optEnvironmentsList = configuration.environments();
return optEnvironmentsList
.stream()
.flatMap(Collection::stream)
.filter(id -> {
if (!validateEnvironmentId(id)) {
log.warn("Environment id [%s] should be an valid UUID ; HRIDs are not supported anymore, ignore it.");
return false;
}
return true;
})
.collect(Collectors.toSet());
}
private static boolean validateEnvironmentId(final String id) {
return Objects.equals(id, "DEFAULT") || validateUUID(id);
}
private static boolean validateUUID(final String id) {
try {
UUID.fromString(id);
return true;
} catch (IllegalArgumentException e) {
return false;
}
}
@Deprecated(forRemoval = true, since = "4.5")
private Set resolveOrganizationsFromHrids() {
try {
final Optional> optOrganizationsList = configuration.organizations();
if (optOrganizationsList.isPresent()) {
List organizationsList = optOrganizationsList.get();
if (!organizationsList.isEmpty()) {
Set organizationHrids = new HashSet<>(organizationsList);
final Set organizations = organizationRepository.findByHrids(organizationHrids);
final Set organizationIds = organizations.stream().map(Organization::getId).collect(Collectors.toSet());
checkOrganizations(organizationHrids, organizations);
return organizationIds;
}
}
} catch (Exception e) {
throw new RuntimeException("Unable to load organization ids", e);
}
return new HashSet<>();
}
@Deprecated(forRemoval = true, since = "4.5")
private Set resolveEnvironmentFromHrids(Set organizationIds) {
try {
final Optional> optEnvironmentsList = configuration.environments();
Set environmentHrids = optEnvironmentsList.map(HashSet::new).orElse(new HashSet<>());
if (!organizationIds.isEmpty() || !environmentHrids.isEmpty()) {
final Set environments = environmentRepository.findByOrganizationsAndHrids(organizationIds, environmentHrids);
final Set environmentIds = new HashSet<>();
for (Environment env : environments) {
organizationIds.add(env.getOrganizationId());
environmentIds.add(env.getId());
}
checkEnvironments(environmentHrids, environments);
return environmentIds;
}
} catch (Exception e) {
throw new RuntimeException("Unable to load environment ids", e);
}
return new HashSet<>();
}
private void checkOrganizations(Set organizationsHrids, Set organizations) {
if (organizationsHrids.size() != organizations.size()) {
final Set hrids = new HashSet<>(organizationsHrids);
final Set returnedHrids = organizations.stream().flatMap(org -> org.getHrids().stream()).collect(Collectors.toSet());
hrids.removeAll(returnedHrids);
log.warn("No organization found for hrids {}", hrids);
}
}
private void checkEnvironments(Set environmentHrids, Set environments) {
final Set returnedHrids = environments
.stream()
.flatMap(env -> env.getHrids().stream())
.filter(environmentHrids::contains)
.collect(Collectors.toSet());
if (environmentHrids.size() != returnedHrids.size()) {
final Set hrids = new HashSet<>(environmentHrids);
hrids.removeAll(returnedHrids);
log.warn("No environment found for hrids {}", hrids);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy