All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.gravitee.gateway.standalone.node.GatewayNodeMetadataResolver Maven / Gradle / Ivy

There is a newer version: 4.5.5
Show 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.*;

import io.gravitee.gateway.env.GatewayConfiguration;
import io.gravitee.node.api.NodeMetadataResolver;
import io.gravitee.repository.exceptions.TechnicalException;
import io.gravitee.repository.management.api.EnvironmentRepository;
import io.gravitee.repository.management.api.InstallationRepository;
import io.gravitee.repository.management.api.OrganizationRepository;
import io.gravitee.repository.management.model.Environment;
import io.gravitee.repository.management.model.Installation;
import io.gravitee.repository.management.model.Organization;
import java.util.*;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;

/**
 * @author Jeoffrey HAEYAERT (jeoffrey.haeyaert at graviteesource.com)
 * @author GraviteeSource Team
 */
public class GatewayNodeMetadataResolver implements NodeMetadataResolver {

    private final Logger logger = LoggerFactory.getLogger(GatewayNodeMetadataResolver.class);

    @Lazy
    @Autowired
    private InstallationRepository installationRepository;

    @Lazy
    @Autowired
    private OrganizationRepository organizationRepository;

    @Lazy
    @Autowired
    private EnvironmentRepository environmentRepository;

    @Autowired
    private GatewayConfiguration configuration;

    public Map resolve() {
        final HashMap metadata = new HashMap<>();

        final String installationId = getInstallationId();
        final Set organizationIds = getOrganizationIds();
        final Set environmentIds = getEnvironmentIds(organizationIds);

        configuration.tenant().ifPresent(tenant -> metadata.put("tenant", tenant));

        configuration.shardingTags().ifPresent(shardingTags -> metadata.put("tags", shardingTags));

        configuration.zone().ifPresent(zone -> metadata.put("zone", zone));

        metadata.put(META_INSTALLATION, installationId);
        metadata.put(META_ORGANIZATIONS, organizationIds);
        metadata.put(META_ENVIRONMENTS, environmentIds);

        return metadata;
    }

    private String getInstallationId() {
        try {
            return installationRepository.find().map(Installation::getId).orElse(null);
        } catch (TechnicalException e) {
            logger.warn("Unable to load installation id", e);
        }

        return null;
    }

    private Set getOrganizationIds() {
        try {
            final Optional> optOrganizationsList = configuration.organizations();

            if (optOrganizationsList.isPresent()) {
                Set organizationHrids = new HashSet<>(optOrganizationsList.get());
                final Set organizations = organizationRepository.findByHrids(new HashSet<>(organizationHrids));
                final Set organizationIds = organizations.stream().map(Organization::getId).collect(Collectors.toSet());

                checkOrganizations(organizationHrids, organizations);

                return organizationIds;
            }
        } catch (Exception e) {
            logger.warn("Unable to load organization ids", e);
        }

        return new HashSet<>();
    }

    private Set getEnvironmentIds(Set organizationIds) {
        try {
            final Optional> optEnvironmentsList = configuration.environments();
            Set environmentHrids = new HashSet<>();

            if (optEnvironmentsList.isPresent()) {
                environmentHrids = new HashSet<>(optEnvironmentsList.get());
            }

            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) {
            logger.warn("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);
            logger.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);
            logger.warn("No environment found for hrids {}", hrids);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy