Search in sources :

Example 26 with Source

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

the class CachingFederationStrategyTest method testFederateQueryCache.

@Test
public void testFederateQueryCache() throws Exception {
    properties.put(QUERY_MODE, CACHE_QUERY_MODE);
    QueryRequest fedQueryRequest = new QueryRequestImpl(mockQuery, properties);
    Source mockSource = mock(Source.class);
    when(mockSource.query(any(QueryRequest.class))).thenReturn(mockResponse);
    QueryResponse federateResponse = strategy.federate(Arrays.asList(mockSource), fedQueryRequest);
    assertThat(requestArgumentCaptor.getValue().getPropertyValue(QUERY_MODE), is(CACHE_QUERY_MODE));
    verify(cache).query(any(QueryRequest.class));
    verify(mockSource, times(0)).query(any(QueryRequest.class));
    verifyCacheNotUpdated();
    assertThat(federateResponse.getRequest().getQuery(), is(requestArgumentCaptor.getValue().getQuery()));
}
Also used : QueryRequest(ddf.catalog.operation.QueryRequest) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryResponse(ddf.catalog.operation.QueryResponse) Source(ddf.catalog.source.Source) Test(org.junit.Test)

Example 27 with Source

use of ddf.catalog.source.Source 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 28 with Source

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

the class SourceOperations method addCatalogSourceDescriptor.

/**
     * Adds the local catalog's {@link SourceDescriptor} to the set of {@link SourceDescriptor}s for
     * this framework.
     *
     * @param descriptors the set of {@link SourceDescriptor}s to add the local catalog's descriptor to
     */
private void addCatalogSourceDescriptor(Set<SourceDescriptor> descriptors) {
    // local site info.
    if (descriptors != null) {
        Source cachedSource = null;
        if (catalog != null) {
            // check the poller for cached information
            if (frameworkProperties.getSourcePoller() != null && frameworkProperties.getSourcePoller().getCachedSource(catalog) != null) {
                cachedSource = frameworkProperties.getSourcePoller().getCachedSource(catalog);
            }
            if (cachedSource != null) {
                SourceDescriptorImpl descriptor = new SourceDescriptorImpl(this.getId(), cachedSource.getContentTypes());
                if (this.getVersion() != null) {
                    descriptor.setVersion(this.getVersion());
                }
                descriptor.setAvailable(cachedSource.isAvailable());
                descriptors.add(descriptor);
            }
        }
    }
}
Also used : SourceDescriptorImpl(ddf.catalog.source.impl.SourceDescriptorImpl) Source(ddf.catalog.source.Source) FederatedSource(ddf.catalog.source.FederatedSource)

Example 29 with Source

use of ddf.catalog.source.Source 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 30 with Source

use of ddf.catalog.source.Source 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)

Aggregations

Source (ddf.catalog.source.Source)46 Test (org.junit.Test)24 QueryRequest (ddf.catalog.operation.QueryRequest)22 ArrayList (java.util.ArrayList)16 FederatedSource (ddf.catalog.source.FederatedSource)15 QueryResponse (ddf.catalog.operation.QueryResponse)14 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)11 Result (ddf.catalog.data.Result)10 CatalogProvider (ddf.catalog.source.CatalogProvider)10 ContentType (ddf.catalog.data.ContentType)9 Query (ddf.catalog.operation.Query)9 SourceResponse (ddf.catalog.operation.SourceResponse)9 CachedSource (ddf.catalog.util.impl.CachedSource)8 SourcePoller (ddf.catalog.util.impl.SourcePoller)8 ByteSource (com.google.common.io.ByteSource)7 QueryResponseImpl (ddf.catalog.operation.impl.QueryResponseImpl)7 PreFederatedQueryPlugin (ddf.catalog.plugin.PreFederatedQueryPlugin)7 SourceDescriptor (ddf.catalog.source.SourceDescriptor)6 SourceUnavailableException (ddf.catalog.source.SourceUnavailableException)6 HashMap (java.util.HashMap)6