Skip to content

Commit

Permalink
Added a PACKAGE filter criterion
Browse files Browse the repository at this point in the history
Allows to filter on the model package URI

Signed-off-by: Thomas Calmant <thomas.calmant@gmail.com>
  • Loading branch information
tcalmant committed Feb 16, 2024
1 parent ab9958a commit bddd280
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

public interface ProviderSnapshot extends Snapshot {

String getModelPackageUri();

String getModelName();

<T extends ServiceSnapshot> List<T> getServices();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public class ProviderSnapshotImpl extends AbstractSnapshot implements ProviderSn
*/
private final List<ServiceSnapshotImpl> services = new ArrayList<>();

/**
* Provider model package URI
*/
private final String modelPackageUri;

/**
* Provider model name
*/
Expand All @@ -39,21 +44,31 @@ public class ProviderSnapshotImpl extends AbstractSnapshot implements ProviderSn
private Provider modelProvider;

/**
* @param modelPackageUri Provider model package URI
* @param modelName Provider model name
* @param provider Provider model
* @param snapshotInstant Instant of snapshot
*/
public ProviderSnapshotImpl(final String modelName, final Provider provider, final Instant snapshotInstant) {
public ProviderSnapshotImpl(final String modelPackageUri, final String modelName, final Provider provider,
final Instant snapshotInstant) {
super(provider.getId(), snapshotInstant);
this.modelPackageUri = modelPackageUri;
this.modelName = modelName;
this.modelProvider = provider;
}

@Override
public String toString() {
return String.format("ProviderSnapshot(%s/%s, %s)", modelName, getName(), getSnapshotTime());
return String.format("ProviderSnapshot(%s/%s/%s, %s)", modelPackageUri, modelName, getName(),
getSnapshotTime());
}

@Override
public String getModelPackageUri() {
return modelPackageUri;
}

@Override
public String getModelName() {
return modelName;
}
Expand All @@ -62,6 +77,7 @@ public void add(final ServiceSnapshotImpl svc) {
this.services.add(svc);
}

@Override
public List<ServiceSnapshotImpl> getServices() {
return services;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,8 @@ public List<ProviderSnapshot> filteredSnapshot(Predicate<GeoJsonObject> geoFilte

// Filter providers with their API model
Stream<ProviderSnapshotImpl> providersStream = rawProvidersStream
.map(p -> new ProviderSnapshotImpl(nexusImpl.getProviderModel(p.getId()), p, snapshotTime));
.map(p -> new ProviderSnapshotImpl(nexusImpl.getProviderPackageUri(p.getId()),
nexusImpl.getProviderModel(p.getId()), p, snapshotTime));
if (providerFilter != null) {
providersStream = providersStream.filter(providerFilter);
}
Expand Down Expand Up @@ -398,6 +399,7 @@ public ProviderSnapshot snapshotProvider(String providerName) {
}

final ProviderSnapshotImpl providerSnapshot = new ProviderSnapshotImpl(
nexusImpl.getProviderPackageUri(nexusProvider.getId()),
nexusImpl.getProviderModel(nexusProvider.getId()), nexusProvider, snapshotTime);

// Add all services
Expand Down Expand Up @@ -439,6 +441,7 @@ public ServiceSnapshot snapshotService(String providerName, String serviceName)

// Minimal snapshot of the provider owning the service
final ProviderSnapshotImpl providerSnapshot = new ProviderSnapshotImpl(
nexusImpl.getProviderPackageUri(nexusProvider.getId()),
nexusImpl.getProviderModel(nexusProvider.getId()), nexusProvider, snapshotTime);

// Describe the service
Expand Down Expand Up @@ -485,6 +488,7 @@ public ResourceSnapshot snapshotResource(String providerName, String serviceName

// Minimal description of the provider owning the service
final ProviderSnapshotImpl providerSnapshot = new ProviderSnapshotImpl(
nexusImpl.getProviderPackageUri(nexusProvider.getId()),
nexusImpl.getProviderModel(nexusProvider.getId()), nexusProvider, snapshotTime);

// Minimal description of the service owning the resource
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*********************************************************************
* Copyright (c) 2023 Contributors to the Eclipse Foundation.
* Copyright (c) 2024 Contributors to the Eclipse Foundation.
*
* This program and the accompanying materials are made available under the terms of the Eclipse
* Public License 2.0 which is available at https://www.eclipse.org/legal/epl-2.0/
Expand Down Expand Up @@ -42,6 +42,7 @@ PLUS: '+';
MINUS: '-';
COLUMN: ':';

PACKAGE: 'PACKAGE';
MODEL: 'MODEL';
PROVIDER: 'PROVIDER';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*********************************************************************
* Copyright (c) 2023 Contributors to the Eclipse Foundation.
* Copyright (c) 2024 Contributors to the Eclipse Foundation.
*
* This program and the accompanying materials are made available under the terms of the Eclipse
* Public License 2.0 which is available at https://www.eclipse.org/legal/epl-2.0/
Expand Down Expand Up @@ -42,7 +42,7 @@ andFilter: AND filter+;
orFilter: OR filter+;
notFilter: NOT filter;

attr: MODEL | PROVIDER | (valid_attr (DOT valid_attr)*);
attr: PACKAGE | MODEL | PROVIDER | (valid_attr (DOT valid_attr)*);

value:
anyValue
Expand All @@ -68,6 +68,7 @@ pureString: (
| PLUS
| comparator
| WS
| PACKAGE
| MODEL
| PROVIDER
| COLUMN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.function.Predicate;

import org.eclipse.sensinact.core.snapshot.ProviderSnapshot;
import org.eclipse.sensinact.core.snapshot.ResourceValueFilter;

/**
* Common code for Provider field comparison
Expand Down Expand Up @@ -74,4 +75,9 @@ public Predicate<ProviderSnapshot> getProviderFilter() {
return p -> expectedValue.matches(getProviderFieldValue(p), approxMatch);
}
}

@Override
public ResourceValueFilter getResourceValueFilter() {
return (p, rs) -> !rs.isEmpty() && getProviderFilter().test(p);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*********************************************************************
* Copyright (c) 2024 Contributors to the Eclipse Foundation.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Kentyou - initial implementation
**********************************************************************/
package org.eclipse.sensinact.northbound.filters.ldap.antlr.impl;

import org.eclipse.sensinact.core.snapshot.ProviderSnapshot;

/**
* Package URI criterion
*/
public class CriterionProviderPackageUri extends AbstractProviderCriterion {

public CriterionProviderPackageUri(final IStringValue pkgUri, final LdapComparator comparator) {
super("PACKAGE", pkgUri, comparator);
}

@Override
protected String getProviderFieldValue(final ProviderSnapshot provider) {
return provider.getModelPackageUri();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,25 @@ private ILdapCriterion handleComparisonWithConstant(final TerminalNode node, fin
}

switch (node.getSymbol().getType()) {
case LdapFilterLexer.PACKAGE:
// Target a model package URI
if (value == null) {
throw new IllegalArgumentException("Package URIs can't be null");
} else if (value == Constants.ANY) {
// Ignore test: model package URI are always set
return null;
}

switch (comparator) {
case APPROX:
case EQUAL:
return new CriterionProviderPackageUri(strValue, comparator);

default:
throw new IllegalArgumentException(
"Model package URIs can only be tested with ~= or =, not " + comparator);
}

case LdapFilterLexer.MODEL:
// Target a model
if (value == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ public List<ServiceSnapshot> getServices() {
return List.of(getSvc());
}

@Override
public String getModelPackageUri() {
return "https://eclipse.org/sensinact/test/";
}

@Override
public String getModelName() {
return "model1";
Expand Down Expand Up @@ -388,4 +393,24 @@ void testSyntax() throws Exception {
query = "(filename=C:\\5cMyFile)";
assertQueryTrue(query, makeResource("test", "filename", "C:\\MyFile"));
}

@Test
void testSpecificCriteria() throws Exception {
ResourceSnapshot rc = makeResource("test", "test", 42);
assertQueryTrue("(MODEL=model1)", rc);
assertQueryFalse("(MODEL=model2)", rc);
assertQueryTrue("(MODEL=model*)", rc);

assertQueryTrue("(PACKAGE=https://eclipse.org/sensinact/test/)", rc);
assertQueryFalse("(PACKAGE=https://eclipse.org/sensinact/test)", rc);
assertQueryFalse("(PACKAGE=https://eclipse.org/sensinact/)", rc);
assertQueryFalse("(PACKAGE=https://eclipse.org/sensinact/test2)", rc);
assertQueryTrue("(PACKAGE=https://eclipse.org/sensinact/*)", rc);
assertQueryTrue("(PACKAGE=https://eclipse.org/*)", rc);
assertQueryTrue("(PACKAGE=*test*)", rc);

assertQueryTrue("(PROVIDER=provider1)", rc);
assertQueryFalse("(PROVIDER=provider2)", rc);
assertQueryTrue("(PROVIDER=provider*)", rc);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public Instant getSnapshotTime() {
return Instant.now();
}

@Override
public String getModelPackageUri() {
return "https://eclipse.org/sensinact/test/";
}

@Override
public String getName() {
return providerName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ public Instant getSnapshotTime() {
return timestamp;
}

@Override
public String getModelPackageUri() {
return "https://eclipse.org/sensinact/test/";
}

@Override
public String getModelName() {
return notification.model;
Expand Down

0 comments on commit bddd280

Please sign in to comment.