Search in sources :

Example 1 with SubscriptionFilterVisitor

use of ddf.catalog.pubsub.internal.SubscriptionFilterVisitor in project ddf by codice.

the class EventProcessorImpl method createSubscription.

@Override
public void createSubscription(Subscription subscription, String subscriptionId) throws InvalidSubscriptionException, SubscriptionExistsException {
    String methodName = "createSubscription";
    LOGGER.debug("ENTERING: {}", methodName);
    LOGGER.debug("Creating Evaluation Criteria... ");
    try {
        for (PreSubscriptionPlugin plugin : preSubscription) {
            LOGGER.debug("Processing subscription with preSubscription plugin");
            subscription = plugin.process(subscription);
        }
        SubscriptionFilterVisitor visitor = new SubscriptionFilterVisitor();
        Predicate finalPredicate = (Predicate) subscription.accept(visitor, null);
        LOGGER.debug("predicate from filter visitor: {}", finalPredicate);
        String[] topics = new String[] { PubSubConstants.PUBLISHED_EVENT_TOPIC_NAME };
        Dictionary<String, String[]> props = new Hashtable<>(1, 1);
        props.put(EventConstants.EVENT_TOPIC, topics);
        ServiceRegistration serviceRegistration = bundleContext.registerService(EventHandler.class.getName(), new PublishedEventHandler(finalPredicate, subscription, preDelivery, catalog, threadPool), props);
        existingSubscriptions.put(subscriptionId, serviceRegistration);
        LOGGER.debug("Subscription {} created.", subscriptionId);
    } catch (Exception e) {
        LOGGER.info("Error while creating subscription predicate: ", e);
        throw new InvalidSubscriptionException(e);
    }
    LOGGER.debug("EXITING: {}", methodName);
}
Also used : InvalidSubscriptionException(ddf.catalog.event.InvalidSubscriptionException) SubscriptionFilterVisitor(ddf.catalog.pubsub.internal.SubscriptionFilterVisitor) Hashtable(java.util.Hashtable) PreSubscriptionPlugin(ddf.catalog.plugin.PreSubscriptionPlugin) EventHandler(org.osgi.service.event.EventHandler) SubscriptionNotFoundException(ddf.catalog.event.SubscriptionNotFoundException) SubscriptionExistsException(ddf.catalog.event.SubscriptionExistsException) PluginExecutionException(ddf.catalog.plugin.PluginExecutionException) InvalidSubscriptionException(ddf.catalog.event.InvalidSubscriptionException) Predicate(ddf.catalog.pubsub.predicate.Predicate) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 2 with SubscriptionFilterVisitor

use of ddf.catalog.pubsub.internal.SubscriptionFilterVisitor in project ddf by codice.

the class PredicateTest method testContextualQuery.

@Test
public void testContextualQuery() throws Exception {
    String methodName = "testContextualQuery";
    LOGGER.debug("***************  START: {}  *****************", methodName);
    String searchPhrase = "serengeti event";
    MockQuery query = new MockQuery();
    query.addContextualFilter(searchPhrase, null);
    SubscriptionFilterVisitor visitor = new SubscriptionFilterVisitor();
    Predicate predicate = (Predicate) query.getFilter().accept(visitor, null);
    MetacardImpl metacard = new MetacardImpl();
    metacard.setId("ABC123");
    metacard.setMetadata(TestDataLibrary.getCatAndDogEntry());
    HashMap<String, Object> properties = new HashMap<>();
    properties.put(PubSubConstants.HEADER_ID_KEY, metacard.getId());
    properties.put(PubSubConstants.HEADER_ENTRY_KEY, metacard);
    properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
    Map<String, Object> contextualMap = constructContextualMap(metacard);
    properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
    // Above Pulled from PubSubProviderImpl
    Event testEvent = new Event("topic", properties);
    assertTrue(predicate.matches(testEvent));
    contextualMap.clear();
    properties.clear();
    metacard.setMetadata(TestDataLibrary.getDogEntry());
    Directory index1 = ContextualEvaluator.buildIndex(metacard.getMetadata());
    contextualMap.put("DEFAULT_INDEX", index1);
    contextualMap.put("METADATA", metacard.getMetadata());
    properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
    properties.put(PubSubConstants.HEADER_ID_KEY, metacard.getId());
    properties.put(PubSubConstants.HEADER_ENTRY_KEY, metacard);
    properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
    testEvent = new Event("topic", properties);
    assertFalse(predicate.matches(testEvent));
    LOGGER.debug("***************  END: {}  *****************", methodName);
}
Also used : SubscriptionFilterVisitor(ddf.catalog.pubsub.internal.SubscriptionFilterVisitor) HashMap(java.util.HashMap) Event(org.osgi.service.event.Event) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) GeospatialPredicate(ddf.catalog.pubsub.predicate.GeospatialPredicate) ContentTypePredicate(ddf.catalog.pubsub.predicate.ContentTypePredicate) Predicate(ddf.catalog.pubsub.predicate.Predicate) Directory(org.apache.lucene.store.Directory) Test(org.junit.Test)

Example 3 with SubscriptionFilterVisitor

use of ddf.catalog.pubsub.internal.SubscriptionFilterVisitor in project ddf by codice.

the class PredicateTest method testMultipleContentTypes.

@Test
public void testMultipleContentTypes() throws IOException {
    String methodName = "testMultipleContentTypes";
    LOGGER.debug("***************  START: {}  *****************", methodName);
    String type1 = "type1";
    String version1 = "version1";
    String type2 = "type2";
    String version2 = "version2";
    String type3 = "type3";
    String version4 = "version4";
    MetacardImpl metacard = new MetacardImpl();
    metacard.setMetadata(TestDataLibrary.getCatAndDogEntry());
    List<MockTypeVersionsExtension> extensions = createContentTypeVersionList("type1,version1|type2,version2|type3,|,version4");
    MockQuery query = new MockQuery();
    query.addTypeFilter(extensions);
    SubscriptionFilterVisitor visitor = new SubscriptionFilterVisitor();
    Predicate pred = (Predicate) query.getFilter().accept(visitor, null);
    LOGGER.debug("Resulting Predicate: {}", pred);
    HashMap<String, Object> properties = new HashMap<>();
    properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
    Map<String, Object> contextualMap = constructContextualMap(metacard);
    properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
    // Above Pulled from PubSubProviderImpl
    properties.put(PubSubConstants.HEADER_CONTENT_TYPE_KEY, type1 + "," + version1);
    Event testEvent = new Event("topic", properties);
    assertTrue(pred.matches(testEvent));
    properties.clear();
    properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
    properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
    properties.put(PubSubConstants.HEADER_CONTENT_TYPE_KEY, type2 + "," + version2);
    testEvent = new Event("topic", properties);
    assertTrue(pred.matches(testEvent));
    properties.clear();
    properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
    properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
    properties.put(PubSubConstants.HEADER_CONTENT_TYPE_KEY, type2 + "," + version1);
    testEvent = new Event("topic", properties);
    assertFalse(pred.matches(testEvent));
    properties.clear();
    properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
    properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
    properties.put(PubSubConstants.HEADER_CONTENT_TYPE_KEY, type1 + "," + version2);
    testEvent = new Event("topic", properties);
    assertFalse(pred.matches(testEvent));
    properties.clear();
    properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
    properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
    properties.put(PubSubConstants.HEADER_CONTENT_TYPE_KEY, type3 + "," + "random_version");
    testEvent = new Event("topic", properties);
    assertTrue(pred.matches(testEvent));
    properties.clear();
    properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
    properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
    properties.put(PubSubConstants.HEADER_CONTENT_TYPE_KEY, "random_type" + "," + "random_version");
    testEvent = new Event("topic", properties);
    assertFalse(pred.matches(testEvent));
    properties.clear();
    properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
    properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
    properties.put(PubSubConstants.HEADER_CONTENT_TYPE_KEY, "random_type" + "," + version4);
    testEvent = new Event("topic", properties);
    assertFalse(pred.matches(testEvent));
    LOGGER.debug("***************  END: {}  *****************", methodName);
}
Also used : SubscriptionFilterVisitor(ddf.catalog.pubsub.internal.SubscriptionFilterVisitor) HashMap(java.util.HashMap) Event(org.osgi.service.event.Event) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) GeospatialPredicate(ddf.catalog.pubsub.predicate.GeospatialPredicate) ContentTypePredicate(ddf.catalog.pubsub.predicate.ContentTypePredicate) Predicate(ddf.catalog.pubsub.predicate.Predicate) Test(org.junit.Test)

Example 4 with SubscriptionFilterVisitor

use of ddf.catalog.pubsub.internal.SubscriptionFilterVisitor in project ddf by codice.

the class PredicateTest method testMultipleCriteria.

@Test
public void testMultipleCriteria() throws Exception {
    String methodName = "testMultipleCriteria";
    LOGGER.debug("***************  START: {}  *****************", methodName);
    // test with temporal, spatial, and entry
    MockQuery query = new MockQuery();
    DatatypeFactory df = DatatypeFactory.newInstance();
    XMLGregorianCalendar start = df.newXMLGregorianCalendarDate(2011, 10, 25, 0);
    XMLGregorianCalendar end = df.newXMLGregorianCalendarDate(2011, 10, 27, 0);
    query.addTemporalFilter(start, end, Metacard.MODIFIED);
    String wkt = "POLYGON((0 10, 0 0, 10 0, 10 10, 0 10))";
    query.addSpatialFilter(wkt, 0.0, "Meter", "CONTAINS");
    // create entry criteria
    String catalogId = "ABC123";
    query.addEntryFilter(catalogId);
    MetacardImpl metacard = new MetacardImpl();
    metacard.setLocation("POINT(5 5)");
    metacard.setId(catalogId);
    metacard.setCreatedDate(new Date());
    metacard.setExpirationDate(new Date());
    metacard.setEffectiveDate(new Date());
    metacard.setMetadata(TestDataLibrary.getCatAndDogEntry());
    XMLGregorianCalendar cal = df.newXMLGregorianCalendarDate(2011, 10, 26, 0);
    Date modifiedDate = cal.toGregorianCalendar().getTime();
    metacard.setModifiedDate(modifiedDate);
    HashMap<String, Object> properties = new HashMap<>();
    properties.put(PubSubConstants.HEADER_ID_KEY, metacard.getId());
    properties.put(PubSubConstants.HEADER_ENTRY_KEY, metacard);
    properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
    Map<String, Object> contextualMap = constructContextualMap(metacard);
    properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
    // Above Pulled from PubSubProviderImpl
    Event testEvent = new Event("topic", properties);
    // input passes temporal, id, and geo
    SubscriptionFilterVisitor visitor = new SubscriptionFilterVisitor();
    Predicate pred = (Predicate) query.getFilter().accept(visitor, null);
    LOGGER.debug("Resulting Predicate: {}", pred);
    Filter filter = query.getFilter();
    FilterTransformer transform = new FilterTransformer();
    transform.setIndentation(2);
    String filterXml = transform.transform(filter);
    LOGGER.debug(filterXml);
    assertTrue(pred.matches(testEvent));
    // input passes temporal, id, but fails geo
    // geo out of range
    metacard.setLocation("POINT(5 50)");
    properties.clear();
    properties.put(PubSubConstants.HEADER_ID_KEY, metacard.getId());
    properties.put(PubSubConstants.HEADER_ENTRY_KEY, metacard);
    properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
    // Below Pulled from PubSubProviderImpl
    contextualMap = constructContextualMap(metacard);
    properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
    // Above Pulled from PubSubProviderImpl
    testEvent = new Event("topic", properties);
    assertFalse(pred.matches(testEvent));
    // input passes geo, and id, but fails temporal
    metacard.setLocation("POINT(5 5)");
    XMLGregorianCalendar cal1 = df.newXMLGregorianCalendarDate(2011, 10, 28, 0);
    Date modifiedDate1 = cal1.toGregorianCalendar().getTime();
    // date out of range
    metacard.setModifiedDate(modifiedDate1);
    properties.clear();
    properties.put(PubSubConstants.HEADER_ID_KEY, metacard.getId());
    properties.put(PubSubConstants.HEADER_ENTRY_KEY, metacard);
    properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
    // Below Pulled from PubSubProviderImpl
    contextualMap = constructContextualMap(metacard);
    properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
    // Above Pulled from PubSubProviderImpl
    testEvent = new Event("topic", properties);
    assertFalse(pred.matches(testEvent));
    // input passes temporal, geo, but fails id
    XMLGregorianCalendar cal2 = df.newXMLGregorianCalendarDate(2011, 10, 26, 0);
    Date modifiedDate2 = cal2.toGregorianCalendar().getTime();
    metacard.setModifiedDate(modifiedDate2);
    // bad id
    metacard.setId("invalid_id");
    properties.clear();
    properties.put(PubSubConstants.HEADER_ID_KEY, metacard.getId());
    properties.put(PubSubConstants.HEADER_ENTRY_KEY, metacard);
    properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
    // Below Pulled from PubSubProviderImpl
    contextualMap = constructContextualMap(metacard);
    properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
    // Above Pulled from PubSubProviderImpl
    testEvent = new Event("topic", properties);
    assertFalse(pred.matches(testEvent));
    LOGGER.debug("***************  END: {}  *****************", methodName);
}
Also used : DatatypeFactory(javax.xml.datatype.DatatypeFactory) HashMap(java.util.HashMap) SubscriptionFilterVisitor(ddf.catalog.pubsub.internal.SubscriptionFilterVisitor) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Date(java.util.Date) GeospatialPredicate(ddf.catalog.pubsub.predicate.GeospatialPredicate) ContentTypePredicate(ddf.catalog.pubsub.predicate.ContentTypePredicate) Predicate(ddf.catalog.pubsub.predicate.Predicate) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) Filter(org.opengis.filter.Filter) Event(org.osgi.service.event.Event) FilterTransformer(org.geotools.filter.FilterTransformer) Test(org.junit.Test)

Example 5 with SubscriptionFilterVisitor

use of ddf.catalog.pubsub.internal.SubscriptionFilterVisitor in project ddf by codice.

the class PredicateTest method testContentTypeFilterTypeOnly.

@Test
public void testContentTypeFilterTypeOnly() throws Exception {
    String methodName = "testContentTypeFilterTypeOnly";
    LOGGER.debug("***************  START: {}  *****************", methodName);
    MetacardImpl metacard = new MetacardImpl();
    metacard.setMetadata(TestDataLibrary.getCatAndDogEntry());
    String type1 = "type_1";
    List<MockTypeVersionsExtension> extensions = new ArrayList<>();
    MockTypeVersionsExtension ext1 = new MockTypeVersionsExtension();
    ext1.setExtensionTypeName(type1);
    extensions.add(ext1);
    MockQuery query = new MockQuery();
    query.addTypeFilter(extensions);
    SubscriptionFilterVisitor visitor = new SubscriptionFilterVisitor();
    ContentTypePredicate pred = (ContentTypePredicate) query.getFilter().accept(visitor, null);
    assertEquals(type1, pred.getType());
    assertNull(pred.getVersion());
    HashMap<String, Object> properties = new HashMap<>();
    properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
    Map<String, Object> contextualMap = constructContextualMap(metacard);
    properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
    // Above Pulled from PubSubProviderImpl
    // handle null case
    properties.put(PubSubConstants.HEADER_CONTENT_TYPE_KEY, null);
    Event testEvent = new Event("topic", properties);
    assertFalse(pred.matches(testEvent));
    // handle content type
    properties.put(PubSubConstants.HEADER_CONTENT_TYPE_KEY, type1 + ",");
    testEvent = new Event("topic", properties);
    assertTrue(pred.matches(testEvent));
    // handle content version that matches content type
    properties.put(PubSubConstants.HEADER_CONTENT_TYPE_KEY, "," + type1);
    testEvent = new Event("topic", properties);
    assertFalse(pred.matches(testEvent));
    properties.clear();
    properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
    properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
    properties.put(PubSubConstants.HEADER_CONTENT_TYPE_KEY, type1 + "," + "random_version");
    testEvent = new Event("topic", properties);
    assertTrue(pred.matches(testEvent));
    properties.clear();
    properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
    properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
    properties.put(PubSubConstants.HEADER_CONTENT_TYPE_KEY, "unmatchingtype" + "," + "random_version");
    testEvent = new Event("topic", properties);
    assertFalse(pred.matches(testEvent));
    properties.clear();
    properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
    properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
    properties.put(PubSubConstants.HEADER_CONTENT_TYPE_KEY, // Invalid
    "," + "unmatchingversion");
    // input
    testEvent = new Event("topic", properties);
    assertFalse(pred.matches(testEvent));
    LOGGER.debug("***************  END: {}  *****************", methodName);
}
Also used : SubscriptionFilterVisitor(ddf.catalog.pubsub.internal.SubscriptionFilterVisitor) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ContentTypePredicate(ddf.catalog.pubsub.predicate.ContentTypePredicate) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Event(org.osgi.service.event.Event) Test(org.junit.Test)

Aggregations

SubscriptionFilterVisitor (ddf.catalog.pubsub.internal.SubscriptionFilterVisitor)14 ContentTypePredicate (ddf.catalog.pubsub.predicate.ContentTypePredicate)13 Predicate (ddf.catalog.pubsub.predicate.Predicate)13 GeospatialPredicate (ddf.catalog.pubsub.predicate.GeospatialPredicate)12 Test (org.junit.Test)12 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)11 HashMap (java.util.HashMap)11 Event (org.osgi.service.event.Event)11 FilterTransformer (org.geotools.filter.FilterTransformer)6 Filter (org.opengis.filter.Filter)6 Date (java.util.Date)5 DatatypeFactory (javax.xml.datatype.DatatypeFactory)5 XMLGregorianCalendar (javax.xml.datatype.XMLGregorianCalendar)5 ArrayList (java.util.ArrayList)4 InvalidSubscriptionException (ddf.catalog.event.InvalidSubscriptionException)1 SubscriptionExistsException (ddf.catalog.event.SubscriptionExistsException)1 SubscriptionNotFoundException (ddf.catalog.event.SubscriptionNotFoundException)1 PluginExecutionException (ddf.catalog.plugin.PluginExecutionException)1 PreSubscriptionPlugin (ddf.catalog.plugin.PreSubscriptionPlugin)1 Hashtable (java.util.Hashtable)1