Skip to content
Permalink
Browse files

Protocol message filters renamed value filters

  • Loading branch information...
richturner committed Jul 2, 2019
1 parent 73e580b commit aa76a292695dd0021779442a63ef972a7cfabb7c
@@ -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;
@@ -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;
@@ -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);
});
@@ -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")
@@ -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) {
@@ -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;
@@ -199,10 +199,10 @@
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(
@@ -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();
}
@@ -305,11 +305,11 @@ void linkProtocolConfiguration(AssetAttribute protocolConfiguration, Consumer<Co

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);
@@ -321,12 +321,12 @@ void linkProtocolConfiguration(AssetAttribute protocolConfiguration, Consumer<Co
}

/**
* 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);
@@ -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


@@ -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;
@@ -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;
@@ -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));
}

@@ -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";

@@ -45,17 +42,17 @@
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;
@@ -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";

@@ -39,12 +36,12 @@
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;
}

@@ -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
@@ -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
@@ -58,14 +50,4 @@
*/
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();
}
}
@@ -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()

0 comments on commit aa76a29

Please sign in to comment.
You can’t perform that action at this time.