Skip to content

Commit

Permalink
Protocol message filters renamed value filters
Browse files Browse the repository at this point in the history
  • Loading branch information
richturner committed Jul 2, 2019
1 parent 73e580b commit aa76a29
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 79 deletions.
Expand Up @@ -21,7 +21,7 @@

import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.openremote.agent.protocol.filter.MessageFilter;
import org.openremote.model.value.ValueFilter;
import org.openremote.container.Container;
import org.openremote.container.ContainerService;
import org.openremote.container.concurrent.GlobalLock;
Expand Down Expand Up @@ -111,7 +111,7 @@ protected void setCurrentConnectionStatus(ConnectionStatus currentConnectionStat
public static final int PRIORITY = ContainerService.DEFAULT_PRIORITY - 100;
protected final Map<AttributeRef, AssetAttribute> linkedAttributes = new HashMap<>();
protected final Map<AttributeRef, LinkedProtocolInfo> linkedProtocolConfigurations = new HashMap<>();
protected final Map<AttributeRef, List<MessageFilter>> linkedAttributeFilters = new HashMap<>();
protected final Map<AttributeRef, List<ValueFilter>> linkedAttributeFilters = new HashMap<>();
protected static final List<MetaItemDescriptor> attributeMetaItemDescriptors;
protected MessageBrokerContext messageBrokerContext;
protected ProducerTemplate producerTemplate;
Expand Down Expand Up @@ -217,7 +217,7 @@ final public void linkAttributes(Collection<AssetAttribute> attributes, AssetAtt
// linking process and without entry in the map any update would be blocked
linkedAttributes.put(attributeRef, attribute);

Optional<List<MessageFilter>> messageFilters = Protocol.getLinkedAttributeMessageFilters(attribute);
Optional<List<ValueFilter>> messageFilters = Protocol.getLinkedAttributeMessageFilters(attribute);
messageFilters.ifPresent(mFilters -> {
linkedAttributeFilters.put(attributeRef, mFilters);
});
Expand Down Expand Up @@ -309,7 +309,7 @@ final protected void sendAttributeEvent(AttributeEvent event) {

/**
* Update the value of a linked attribute. Call this to publish new sensor values. This will apply any
* {@link MessageFilter}s that have been set for the {@link Attribute} against the {@link AttributeState#getValue}
* {@link ValueFilter}s that have been set for the {@link Attribute} against the {@link AttributeState#getValue}
* before sending on the sensor queue.
*/
@SuppressWarnings("unchecked")
Expand All @@ -324,15 +324,15 @@ final protected void updateLinkedAttribute(final AttributeState finalState, long
}

if (state.getValue().isPresent()) {
List<MessageFilter> filters;
List<ValueFilter> filters;
Value value = state.getValue().get();

filters = linkedAttributeFilters.get(state.getAttributeRef());

if (filters != null) {
LOG.fine("Applying message filters to sensor value...");

for (MessageFilter filter : filters) {
for (ValueFilter filter : filters) {
boolean filterOk = filter.getMessageType() == value.getType().getModelType();

if (!filterOk) {
Expand Down
20 changes: 10 additions & 10 deletions agent/src/main/java/org/openremote/agent/protocol/Protocol.java
Expand Up @@ -21,7 +21,7 @@

import org.apache.commons.codec.binary.BinaryCodec;
import org.apache.commons.codec.binary.Hex;
import org.openremote.agent.protocol.filter.MessageFilter;
import org.openremote.model.value.ValueFilter;
import org.openremote.container.Container;
import org.openremote.container.ContainerService;
import org.openremote.model.AbstractValueHolder;
Expand Down Expand Up @@ -199,10 +199,10 @@ public interface Protocol extends ContainerService {
null);

/**
* {@link MetaItem} for defining {@link MessageFilter}s to apply to values before they are sent on the
* {@link MetaItem} for defining {@link ValueFilter}s to apply to values before they are sent on the
* {@link #SENSOR_QUEUE} (i.e. before it is used to update a protocol linked attribute); this is particularly
* useful for generic protocols. The {@link MetaItem} value should be an {@link ArrayValue} of {@link ObjectValue}s
* where each {@link ObjectValue} represents a serialised {@link MessageFilter}. The message should pass through the
* where each {@link ObjectValue} represents a serialised {@link ValueFilter}. The message should pass through the
* filters in array order.
*/
MetaItemDescriptor META_VALUE_FILTERS = metaItemArray(
Expand Down Expand Up @@ -289,9 +289,9 @@ void linkProtocolConfiguration(AssetAttribute protocolConfiguration, Consumer<Co
AttributeValidationResult validateProtocolConfiguration(AssetAttribute protocolConfiguration);

/**
* Extract the {@link MessageFilter}s from the specified {@link Attribute}
* Extract the {@link ValueFilter}s from the specified {@link Attribute}
*/
static Optional<List<MessageFilter>> getLinkedAttributeMessageFilters(Attribute attribute) {
static Optional<List<ValueFilter>> getLinkedAttributeMessageFilters(Attribute attribute) {
if (attribute == null) {
return Optional.empty();
}
Expand All @@ -305,11 +305,11 @@ static Optional<List<MessageFilter>> getLinkedAttributeMessageFilters(Attribute

try {
ArrayValue arrayValue = arrayValueOptional.get();
List<MessageFilter> messageFilters = new ArrayList<>(arrayValue.length());
List<ValueFilter> messageFilters = new ArrayList<>(arrayValue.length());
for (int i = 0; i < arrayValue.length(); i++) {
ObjectValue objValue = arrayValue.getObject(i).orElseThrow(() ->
new IllegalArgumentException("Attribute protocol filters meta item is invalid"));
MessageFilter filter = deserialiseMessageFilter(objValue);
ValueFilter filter = deserialiseMessageFilter(objValue);
messageFilters.add(filter);
}
return messageFilters.isEmpty() ? Optional.empty() : Optional.of(messageFilters);
Expand All @@ -321,12 +321,12 @@ static Optional<List<MessageFilter>> getLinkedAttributeMessageFilters(Attribute
}

/**
* Deserialise a {@link MessageFilter} from an {@link ObjectValue}
* Deserialise a {@link ValueFilter} from an {@link ObjectValue}
*/
static MessageFilter deserialiseMessageFilter(ObjectValue objectValue) throws IllegalArgumentException {
static ValueFilter deserialiseMessageFilter(ObjectValue objectValue) throws IllegalArgumentException {
String json = objectValue.toJson();
try {
return Container.JSON.readValue(json, MessageFilter.class);
return Container.JSON.readValue(json, ValueFilter.class);
} catch (IOException ioException) {
LOG.log(Level.WARNING, "Failed to deserialise message filter", ioException);
throw new IllegalArgumentException(ioException);
Expand Down
Expand Up @@ -407,9 +407,9 @@ metaItemDisplayName[urnopenremoteprotocolcontrollerClientcommandsMap]=Controller
metaItemDisplayName[urnopenremoteprotocolcontrollerClientbaseUri]=Controller base URI
metaItemDisplayName[urnopenremoteprotocoludpClienthost]=UDP server hostname/IP
metaItemDisplayName[urnopenremoteprotocoludpClientport]=UDP server port
metaItemDisplayName[urnopenremoteprotocoludpClientbindPort]=UDP Client bind port
metaItemDisplayName[urnopenremoteprotocoludpClientbindPort]=UDP client bind port
metaItemDisplayName[urnopenremoteprotocoludpClientresponseTimeoutMillis]=Response timeout (ms)
metaItemDisplayName[urnopenremoteprotocoludpClientsendRetries]=No. of Retries
metaItemDisplayName[urnopenremoteprotocoludpClientsendRetries]=Send retries
metaItemDisplayName[urnopenremoteprotocoludpClientserverAlwaysResponds]=UDP server always responds


Expand Down
19 changes: 19 additions & 0 deletions container/src/main/java/org/openremote/container/util/Util.java
@@ -1,9 +1,16 @@
package org.openremote.container.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.openremote.container.Container;
import org.openremote.model.value.ObjectValue;
import org.openremote.model.value.Values;

import javax.validation.constraints.NotNull;
import java.lang.reflect.Array;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
Expand All @@ -13,6 +20,18 @@
* TODO Old functions for old rules, can be deleted?
*/
public class Util {

/**
* Convert a JSON serialisable object to an {@link ObjectValue}
*/
public static Optional<ObjectValue> objectToValue(@NotNull Object obj) {
try {
String json = Container.JSON.writeValueAsString(obj);
return Values.parse(json);
} catch (JsonProcessingException ignore) {}
return Optional.empty();
}

public static <T> T[] reverseArray(T[] array, Class<T> clazz) {
if (array == null) {
return null;
Expand Down
Expand Up @@ -17,36 +17,32 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.openremote.agent.protocol.filter;
package org.openremote.model.value;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import org.openremote.model.util.TextUtil;
import org.openremote.model.value.*;

import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import static org.openremote.agent.protocol.filter.RegexFilter.NAME;
import static org.openremote.model.value.RegexValueFilter.NAME;

@JsonTypeName(NAME)
public class JsonFilter extends MessageFilter<ObjectValue> {
public class JsonValueFilter extends ValueFilter<ObjectValue> {

public static final String NAME = "json";

@JsonProperty
protected List<String> path;

@JsonCreator
public JsonFilter(@JsonProperty("path") List<String> path) {
public JsonValueFilter(@JsonProperty("path") List<String> path) {
this.path = path;
}

public JsonFilter(String... path) {
public JsonValueFilter(String... path) {
this(Arrays.asList(path));
}

Expand Down
Expand Up @@ -17,23 +17,20 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.openremote.agent.protocol.filter;
package org.openremote.model.value;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import org.openremote.model.value.StringValue;
import org.openremote.model.value.Value;
import org.openremote.model.value.Values;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import static org.openremote.agent.protocol.filter.RegexFilter.NAME;
import static org.openremote.model.value.RegexValueFilter.NAME;

@JsonTypeName(NAME)
public class RegexFilter extends MessageFilter<StringValue> {
public class RegexValueFilter extends ValueFilter<StringValue> {

public static final String NAME = "regex";

Expand All @@ -45,17 +42,17 @@ public class RegexFilter extends MessageFilter<StringValue> {
protected int matchIndex;

@JsonCreator
public RegexFilter(@JsonProperty("pattern") String regex,
@JsonProperty("matchGroup") int matchGroup,
@JsonProperty("matchIndex") int matchIndex) {
public RegexValueFilter(@JsonProperty("pattern") String regex,
@JsonProperty("matchGroup") int matchGroup,
@JsonProperty("matchIndex") int matchIndex) {
try {
pattern = Pattern.compile(regex);
this.matchGroup = matchGroup;
this.matchIndex = matchIndex;
} catch (PatternSyntaxException ignore) {}
}

public RegexFilter(Pattern pattern, int matchGroup, int matchIndex) {
public RegexValueFilter(Pattern pattern, int matchGroup, int matchIndex) {
this.pattern = pattern;
this.matchGroup = matchGroup;
this.matchIndex = matchIndex;
Expand Down
Expand Up @@ -17,19 +17,16 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.openremote.agent.protocol.filter;
package org.openremote.model.value;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import org.openremote.model.value.StringValue;
import org.openremote.model.value.Value;
import org.openremote.model.value.Values;

import static org.openremote.agent.protocol.filter.SubStringFilter.NAME;
import static org.openremote.model.value.SubStringValueFilter.NAME;

@JsonTypeName(NAME)
public class SubStringFilter extends MessageFilter<StringValue> {
public class SubStringValueFilter extends ValueFilter<StringValue> {

public static final String NAME = "substring";

Expand All @@ -39,12 +36,12 @@ public class SubStringFilter extends MessageFilter<StringValue> {
protected Integer endIndex;

@JsonCreator
public SubStringFilter(@JsonProperty("beginIndex") int beginIndex, @JsonProperty("endIndex") Integer endIndex) {
public SubStringValueFilter(@JsonProperty("beginIndex") int beginIndex, @JsonProperty("endIndex") Integer endIndex) {
this.beginIndex = beginIndex;
this.endIndex = endIndex;
}

public SubStringFilter(int beginIndex) {
public SubStringValueFilter(int beginIndex) {
this.beginIndex = beginIndex;
}

Expand Down
Expand Up @@ -17,18 +17,10 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.openremote.agent.protocol.filter;
package org.openremote.model.value;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.openremote.container.Container;
import org.openremote.model.value.ObjectValue;
import org.openremote.model.value.Value;
import org.openremote.model.value.ValueType;
import org.openremote.model.value.Values;

import java.util.Optional;

/**
* Interface for a filter that can be applied to messages of type &lt;T&gt; the filter can return a different
Expand All @@ -42,11 +34,11 @@
*/
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(name = RegexFilter.NAME, value = RegexFilter.class),
@JsonSubTypes.Type(name = SubStringFilter.NAME, value = SubStringFilter.class),
@JsonSubTypes.Type(name = JsonFilter.NAME, value = JsonFilter.class)
@JsonSubTypes.Type(name = RegexValueFilter.NAME, value = RegexValueFilter.class),
@JsonSubTypes.Type(name = SubStringValueFilter.NAME, value = SubStringValueFilter.class),
@JsonSubTypes.Type(name = JsonValueFilter.NAME, value = JsonValueFilter.class)
})
public abstract class MessageFilter<T extends Value> {
public abstract class ValueFilter<T extends Value> {

/**
* Get the message type
Expand All @@ -58,14 +50,4 @@ public abstract class MessageFilter<T extends Value> {
*/
public abstract Value process(T message);

/**
* Convert this filter to an {@link ObjectValue}
*/
public Optional<ObjectValue> toValue() {
try {
String json = Container.JSON.writeValueAsString(this);
return Values.parse(json);
} catch (JsonProcessingException ignore) {}
return Optional.empty();
}
}
Expand Up @@ -372,7 +372,7 @@ class ConsoleTest extends Specification implements ManagerContainerTrait {
}
// TODO: Update once console permissions model finalised
// when: "an anonymous user updates the location of a console linked to users"
// anonymousAssetResource.writeAttributeValue(null, testUser3Console1.id, LOCATION.name, new GeoJSONPoint(0d, 0d, 0d).toValue().toJson())
// anonymousAssetResource.writeAttributeValue(null, testUser3Console1.id, LOCATION.name, new GeoJSONPoint(0d, 0d, 0d).objectToValue().toJson())
//
// then: "the result should be forbidden"
// ex = thrown()
Expand Down

0 comments on commit aa76a29

Please sign in to comment.