Search in sources :

Example 6 with SourceDescriptor

use of ddf.catalog.source.SourceDescriptor in project ddf by codice.

the class SourceOperations method getFanoutSourceInfo.

/**
     * Retrieves the {@link SourceDescriptor} info for all {@link FederatedSource}s in the fanout
     * configuration, but the all of the source info, e.g., content types, for all of the available
     * {@link FederatedSource}s is packed into one {@link SourceDescriptor} for the
     * fanout configuration with the fanout's site name in it. This keeps the individual
     * {@link FederatedSource}s' source info hidden from the external client.
     */
private SourceInfoResponse getFanoutSourceInfo(SourceInfoRequest sourceInfoRequest) throws SourceUnavailableException {
    SourceInfoResponse response;
    SourceDescriptorImpl sourceDescriptor;
    try {
        // request
        if (sourceInfoRequest == null) {
            throw new IllegalArgumentException("SourceInfoRequest was null");
        }
        Set<SourceDescriptor> sourceDescriptors = new LinkedHashSet<>();
        Set<String> ids = sourceInfoRequest.getSourceIds();
        // specified
        if (ids != null) {
            Optional<String> notLocal = ids.stream().filter(s -> !s.equals(getId())).findFirst();
            if (notLocal.isPresent()) {
                SourceUnavailableException sourceUnavailableException = new SourceUnavailableException("Unknown source: " + notLocal.get());
                LOGGER.debug("Throwing SourceUnavailableException for unknown source: {}", notLocal.get(), sourceUnavailableException);
                throw sourceUnavailableException;
            }
        }
        // Fanout will only add one source descriptor with all the contents
        Set<Source> availableSources = frameworkProperties.getFederatedSources().values().stream().map(source -> frameworkProperties.getSourcePoller().getCachedSource(source)).filter(source -> source != null && source.isAvailable()).collect(Collectors.toSet());
        Set<ContentType> contentTypes = availableSources.stream().map(source -> frameworkProperties.getSourcePoller().getCachedSource(source)).filter(source -> source.getContentTypes() != null).map(Source::getContentTypes).flatMap(Collection::stream).collect(Collectors.toSet());
        if (catalog != null) {
            Source localSource = frameworkProperties.getSourcePoller().getCachedSource(catalog);
            if (localSource != null && localSource.isAvailable()) {
                availableSources.add(localSource);
                contentTypes.addAll(localSource.getContentTypes());
            }
        }
        // only reveal this sourceDescriptor, not the federated sources
        sourceDescriptor = new SourceDescriptorImpl(this.getId(), contentTypes);
        if (this.getVersion() != null) {
            sourceDescriptor.setVersion(this.getVersion());
        }
        sourceDescriptor.setAvailable(availableSources.size() > 0);
        sourceDescriptors.add(sourceDescriptor);
        response = new SourceInfoResponseImpl(sourceInfoRequest, null, sourceDescriptors);
    } catch (RuntimeException re) {
        throw new SourceUnavailableException("Exception during runtime while performing getSourceInfo", re);
    }
    return response;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) LoggerFactory(org.slf4j.LoggerFactory) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Source(ddf.catalog.source.Source) SourceDescriptor(ddf.catalog.source.SourceDescriptor) StorageProvider(ddf.catalog.content.StorageProvider) SourceInfoResponseImpl(ddf.catalog.operation.impl.SourceInfoResponseImpl) SourceDescriptorComparator(ddf.catalog.util.impl.SourceDescriptorComparator) LinkedHashSet(java.util.LinkedHashSet) DescribableImpl(ddf.catalog.util.impl.DescribableImpl) SourceInfoResponse(ddf.catalog.operation.SourceInfoResponse) ContentType(ddf.catalog.data.ContentType) Logger(org.slf4j.Logger) SourceDescriptorImpl(ddf.catalog.source.impl.SourceDescriptorImpl) Collection(java.util.Collection) FederatedSource(ddf.catalog.source.FederatedSource) Set(java.util.Set) Collectors(java.util.stream.Collectors) FrameworkProperties(ddf.catalog.impl.FrameworkProperties) SourceInfoRequest(ddf.catalog.operation.SourceInfoRequest) List(java.util.List) CatalogProvider(ddf.catalog.source.CatalogProvider) ServiceUnavailableException(org.osgi.service.blueprint.container.ServiceUnavailableException) Optional(java.util.Optional) SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) SourceDescriptor(ddf.catalog.source.SourceDescriptor) ContentType(ddf.catalog.data.ContentType) SourceDescriptorImpl(ddf.catalog.source.impl.SourceDescriptorImpl) Source(ddf.catalog.source.Source) FederatedSource(ddf.catalog.source.FederatedSource) SourceInfoResponseImpl(ddf.catalog.operation.impl.SourceInfoResponseImpl) SourceInfoResponse(ddf.catalog.operation.SourceInfoResponse)

Example 7 with SourceDescriptor

use of ddf.catalog.source.SourceDescriptor in project ddf by codice.

the class SourceOperations method getFederatedSourceDescriptors.

/**
     * Creates a {@link Set} of {@link SourceDescriptor} based on the incoming list of
     * {@link Source}.
     *
     * @param sources {@link Collection} of {@link Source} to obtain descriptor information from
     * @return new {@link Set} of {@link SourceDescriptor}
     */
private Set<SourceDescriptor> getFederatedSourceDescriptors(Collection<FederatedSource> sources, boolean addCatalogProviderDescriptor) {
    SourceDescriptorImpl sourceDescriptor;
    Set<SourceDescriptor> sourceDescriptors = new TreeSet<>(new SourceDescriptorComparator());
    if (sources != null) {
        for (Source source : sources) {
            if (source != null) {
                String sourceId = source.getId();
                LOGGER.debug("adding sourceId: {}", sourceId);
                Source cachedSource = null;
                // check the poller for cached information
                if (frameworkProperties.getSourcePoller() != null && frameworkProperties.getSourcePoller().getCachedSource(source) != null) {
                    cachedSource = frameworkProperties.getSourcePoller().getCachedSource(source);
                }
                sourceDescriptor = new SourceDescriptorImpl(sourceId, source.getContentTypes());
                sourceDescriptor.setVersion(source.getVersion());
                sourceDescriptor.setAvailable((cachedSource != null) && cachedSource.isAvailable());
                sourceDescriptors.add(sourceDescriptor);
            }
        }
    }
    if (addCatalogProviderDescriptor) {
        addCatalogSourceDescriptor(sourceDescriptors);
    }
    return sourceDescriptors;
}
Also used : SourceDescriptor(ddf.catalog.source.SourceDescriptor) SourceDescriptorComparator(ddf.catalog.util.impl.SourceDescriptorComparator) SourceDescriptorImpl(ddf.catalog.source.impl.SourceDescriptorImpl) TreeSet(java.util.TreeSet) Source(ddf.catalog.source.Source) FederatedSource(ddf.catalog.source.FederatedSource)

Example 8 with SourceDescriptor

use of ddf.catalog.source.SourceDescriptor in project ddf by codice.

the class SourceConfigurationAdminPlugin method getConfigurationData.

/**
     * Returns a map of configuration data that should be appended to the configurationDataMap
     * parameter. The configurationDataMap that is passed into this function is unmodifiable and is
     * passed in to simply expose what information already exists.
     *
     * @param configurationPid     service.pid for the ConfigurationAdmin configuration
     * @param configurationDataMap map of what properties have already been added to the configuration in question
     * @param bundleContext        used to retrieve list of services
     * @return Map defining additional properties to add to the configuration
     */
@Override
public Map<String, Object> getConfigurationData(String configurationPid, Map<String, Object> configurationDataMap, BundleContext bundleContext) {
    LOGGER.debug("Obtaining configuration data for the following configuration PID: {}", configurationPid);
    Map<String, Object> statusMap = new HashMap<String, Object>();
    try {
        List<ServiceReference<? extends Source>> refs = new ArrayList<ServiceReference<? extends Source>>();
        refs.addAll(bundleContext.getServiceReferences(FederatedSource.class, null));
        refs.addAll(bundleContext.getServiceReferences(CatalogProvider.class, null));
        Set<SourceDescriptor> sources = null;
        if (catalogFramework != null) {
            sources = catalogFramework.getSourceInfo(new SourceInfoRequestEnterprise(true)).getSourceInfo();
        }
        boolean foundSources = CollectionUtils.isNotEmpty(sources);
        for (ServiceReference<? extends Source> ref : refs) {
            Source superService = bundleContext.getService(ref);
            if (superService instanceof ConfiguredService) {
                ConfiguredService cs = (ConfiguredService) superService;
                LOGGER.debug("ConfiguredService configuration PID: {}", cs.getConfigurationPid());
                boolean csConfigPidMatchesTargetPid = false;
                if (StringUtils.isNotEmpty(cs.getConfigurationPid()) && cs.getConfigurationPid().equals(configurationPid)) {
                    csConfigPidMatchesTargetPid = true;
                }
                if (foundSources) {
                    // class name, then we can match them up this way.
                    if (csConfigPidMatchesTargetPid || cs.getClass().getCanonicalName().equals(configurationPid)) {
                        for (SourceDescriptor descriptor : sources) {
                            if (descriptor.getSourceId().equals(superService.getId())) {
                                statusMap.put("available", descriptor.isAvailable());
                                statusMap.put("sourceId", descriptor.getSourceId());
                                return statusMap;
                            }
                        }
                    }
                } else if (csConfigPidMatchesTargetPid) {
                    // we don't want to call isAvailable because that can 
                    // potentially block execution but if for some reason we
                    // have no catalog framework, just hit the source 
                    // directly
                    statusMap.put("available", superService.isAvailable());
                    return statusMap;
                }
            }
        }
    } catch (org.osgi.framework.InvalidSyntaxException ise) {
        // this should never happen because the filter is always null
        LOGGER.debug("Error reading LDAP service filter", ise);
    } catch (SourceUnavailableException sue) {
        LOGGER.info("Unable to retrieve sources from Catalog Framework", sue);
    }
    return statusMap;
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) SourceDescriptor(ddf.catalog.source.SourceDescriptor) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ConfiguredService(ddf.catalog.service.ConfiguredService) SourceInfoRequestEnterprise(ddf.catalog.operation.impl.SourceInfoRequestEnterprise) FederatedSource(ddf.catalog.source.FederatedSource) Source(ddf.catalog.source.Source) ServiceReference(org.osgi.framework.ServiceReference) FederatedSource(ddf.catalog.source.FederatedSource) CatalogProvider(ddf.catalog.source.CatalogProvider)

Example 9 with SourceDescriptor

use of ddf.catalog.source.SourceDescriptor in project ddf by codice.

the class CatalogFrameworkImplTest method testGetFederatedSourcesDuplicates.

@Test
public void testGetFederatedSourcesDuplicates() {
    List<FederatedSource> federatedSources = createDefaultFederatedSourceList(true);
    // Duplicate Site
    FederatedSource siteC2 = new MockSource("C", "Site C2", "v1.0", "DDF", null, true, new Date());
    federatedSources.add(siteC2);
    // Expected Sites
    List<FederatedSource> expectedSources = createDefaultFederatedSourceList(true);
    // Mock register the federated sources in the container
    // Mock the source poller
    SourcePoller mockPoller = mock(SourcePoller.class);
    when(mockPoller.getCachedSource(isA(Source.class))).thenReturn(null);
    FrameworkProperties frameworkProperties = new FrameworkProperties();
    frameworkProperties.setSourcePoller(mockPoller);
    Map<String, FederatedSource> sources = new HashMap<>();
    for (FederatedSource federatedSource : expectedSources) {
        sources.put(federatedSource.getId(), federatedSource);
    }
    frameworkProperties.setFederatedSources(sources);
    CatalogFrameworkImpl framework = createFramework(frameworkProperties);
    // Returned Sites
    SourceInfoRequest request = new SourceInfoRequestEnterprise(true);
    SourceInfoResponse response = null;
    try {
        response = framework.getSourceInfo(request);
    } catch (SourceUnavailableException e) {
        LOGGER.debug("SourceUnavilable", e);
        fail();
    }
    Set<SourceDescriptor> sourceDescriptors = response.getSourceInfo();
    // should contain ONLY the original federated sites and the catalog framework's
    // site info (even though it has no local catalog provider configured) - hence,
    // the "+1"
    assertEquals(expectedSources.size(), sourceDescriptors.size());
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) SourceDescriptor(ddf.catalog.source.SourceDescriptor) HashMap(java.util.HashMap) Matchers.anyString(org.mockito.Matchers.anyString) SourceInfoRequestEnterprise(ddf.catalog.operation.impl.SourceInfoRequestEnterprise) Date(java.util.Date) Source(ddf.catalog.source.Source) ByteSource(com.google.common.io.ByteSource) CachedSource(ddf.catalog.util.impl.CachedSource) FederatedSource(ddf.catalog.source.FederatedSource) SourcePoller(ddf.catalog.util.impl.SourcePoller) FederatedSource(ddf.catalog.source.FederatedSource) SourceInfoRequest(ddf.catalog.operation.SourceInfoRequest) SourceInfoResponse(ddf.catalog.operation.SourceInfoResponse) Test(org.junit.Test)

Example 10 with SourceDescriptor

use of ddf.catalog.source.SourceDescriptor in project ddf by codice.

the class CatalogFrameworkImplTest method testGetUnavailableFederatedSources.

@Test
public void testGetUnavailableFederatedSources() {
    List<FederatedSource> federatedSources = createDefaultFederatedSourceList(false);
    CatalogProvider catalogProvider = mock(CatalogProvider.class);
    // Mock register the federated sources in the container
    SourcePollerRunner runner = new SourcePollerRunner();
    SourcePoller poller = new SourcePoller(runner);
    for (FederatedSource source : federatedSources) {
        runner.bind(source);
    }
    runner.bind(catalogProvider);
    FrameworkProperties frameworkProperties = new FrameworkProperties();
    frameworkProperties.setSourcePoller(poller);
    Map<String, FederatedSource> sources = new HashMap<>();
    for (FederatedSource federatedSource : federatedSources) {
        sources.put(federatedSource.getId(), federatedSource);
    }
    frameworkProperties.setFederatedSources(sources);
    frameworkProperties.setCatalogProviders(Collections.singletonList(catalogProvider));
    CatalogFrameworkImpl framework = createFramework(frameworkProperties);
    SourceInfoRequest request = new SourceInfoRequestEnterprise(true);
    SourceInfoResponse response = null;
    try {
        response = framework.getSourceInfo(request);
    } catch (SourceUnavailableException e) {
        fail();
    }
    Set<SourceDescriptor> sourceDescriptors = response.getSourceInfo();
    for (SourceDescriptor descriptor : sourceDescriptors) {
        LOGGER.debug("Descriptor id: {}", descriptor.getSourceId());
        if (StringUtils.isNotBlank(descriptor.getId())) {
            assertFalse(descriptor.isAvailable());
            // No contentTypes should be listed if the source is unavailable
            assertTrue(descriptor.getContentTypes().isEmpty());
        }
    }
    // The "+1" is to account for the CatalogFramework source descriptor.
    // Even if no local catalog provider is configured, the catalog
    // framework's
    // site info is included in the SourceDescriptos list.
    assertEquals(federatedSources.size() + 1, sourceDescriptors.size());
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) SourceDescriptor(ddf.catalog.source.SourceDescriptor) HashMap(java.util.HashMap) Matchers.anyString(org.mockito.Matchers.anyString) SourceInfoRequestEnterprise(ddf.catalog.operation.impl.SourceInfoRequestEnterprise) SourcePoller(ddf.catalog.util.impl.SourcePoller) FederatedSource(ddf.catalog.source.FederatedSource) CatalogProvider(ddf.catalog.source.CatalogProvider) SourceInfoRequest(ddf.catalog.operation.SourceInfoRequest) SourcePollerRunner(ddf.catalog.util.impl.SourcePollerRunner) SourceInfoResponse(ddf.catalog.operation.SourceInfoResponse) Test(org.junit.Test)

Aggregations

SourceDescriptor (ddf.catalog.source.SourceDescriptor)18 SourceInfoResponse (ddf.catalog.operation.SourceInfoResponse)13 SourceUnavailableException (ddf.catalog.source.SourceUnavailableException)13 SourceInfoRequestEnterprise (ddf.catalog.operation.impl.SourceInfoRequestEnterprise)10 FederatedSource (ddf.catalog.source.FederatedSource)8 Test (org.junit.Test)8 SourceInfoRequest (ddf.catalog.operation.SourceInfoRequest)6 CatalogProvider (ddf.catalog.source.CatalogProvider)6 Source (ddf.catalog.source.Source)6 HashSet (java.util.HashSet)6 SourceInfoResponseImpl (ddf.catalog.operation.impl.SourceInfoResponseImpl)5 SourceDescriptorImpl (ddf.catalog.source.impl.SourceDescriptorImpl)5 Matchers.anyString (org.mockito.Matchers.anyString)5 ContentType (ddf.catalog.data.ContentType)4 SourceInfoRequestLocal (ddf.catalog.operation.impl.SourceInfoRequestLocal)4 HashMap (java.util.HashMap)4 CatalogFramework (ddf.catalog.CatalogFramework)3 SourceDescriptorComparator (ddf.catalog.util.impl.SourceDescriptorComparator)3 SourcePoller (ddf.catalog.util.impl.SourcePoller)3 Set (java.util.Set)3