Skip to content

Commit

Permalink
Added null and zero-sized value support
Browse files Browse the repository at this point in the history
  • Loading branch information
fooker committed Dec 14, 2017
1 parent 0b071d7 commit 3258b29
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 13 deletions.
Expand Up @@ -53,7 +53,7 @@ public static ByteBuffer slice(final ByteBuffer buffer, final int size) {
}

public static UnsignedLong uint(final ByteBuffer buffer, final int octets) {
Preconditions.checkArgument(octets > 0 && octets < 9);
Preconditions.checkArgument(0 <= octets && octets <= 8);

long result = 0;

Expand All @@ -65,7 +65,7 @@ public static UnsignedLong uint(final ByteBuffer buffer, final int octets) {
}

public static Long sint(final ByteBuffer buffer, final int octets) {
Preconditions.checkArgument(octets > 0 && octets < 9);
Preconditions.checkArgument(0 <= octets && octets <= 8);

long result = buffer.get() & 0xFFL;
boolean s = (result & 0x80L) != 0;
Expand Down
Expand Up @@ -49,6 +49,7 @@
import org.opennms.netmgt.telemetry.listeners.flow.ie.values.IPv6AddressValue;
import org.opennms.netmgt.telemetry.listeners.flow.ie.values.ListValue;
import org.opennms.netmgt.telemetry.listeners.flow.ie.values.MacAddressValue;
import org.opennms.netmgt.telemetry.listeners.flow.ie.values.NullValue;
import org.opennms.netmgt.telemetry.listeners.flow.ie.values.OctetArrayValue;
import org.opennms.netmgt.telemetry.listeners.flow.ie.values.SignedValue;
import org.opennms.netmgt.telemetry.listeners.flow.ie.values.StringValue;
Expand Down Expand Up @@ -143,6 +144,11 @@ private Flows.Entry.Builder buildEntry(final String name, final Optional<Semanti
return builder;
}

@Override
public void accept(final NullValue value) {
// Ignored
}

@Override
public void accept(final BooleanValue value) {
this.buildEntry(value.getName(), value.getSemantics())
Expand Down
Expand Up @@ -33,6 +33,7 @@
import java.util.Optional;

import org.opennms.netmgt.telemetry.listeners.flow.Protocol;
import org.opennms.netmgt.telemetry.listeners.flow.ie.values.NullValue;

import com.google.common.collect.ImmutableMap;

Expand Down Expand Up @@ -76,23 +77,23 @@ public interface Adder {

default void add(final Protocol protocol,
final Optional<Long> enterpriseNumber,
final Integer informationElementNumber,
final int informationElementNumber,
final ValueParserFactory parserFactory,
final String name,
final Optional<Semantics> semantics) {
this.add(new InformationElementDatabase.Key(protocol, enterpriseNumber, informationElementNumber), parserFactory.parser(name, semantics));
}

default void add(final Protocol protocol,
final Integer informationElementNumber,
final int informationElementNumber,
final ValueParserFactory parserFactory,
final String name,
final Optional<Semantics> semantics) {
this.add(protocol, Optional.empty(), informationElementNumber, parserFactory, name, semantics);
}

default void add(final Protocol protocol,
final Integer informationElementNumber,
final int informationElementNumber,
final ValueParserFactory parserFactory,
final String name,
final Semantics semantics) {
Expand All @@ -112,6 +113,12 @@ public interface Provider {

private InformationElementDatabase(final Provider... providers) {
final AdderImpl adder = new AdderImpl();

// Add null element - this derives from the standard but is required by some exporters
adder.add(Protocol.NETFLOW9, 0, NullValue::parser, "null", Optional.empty());
adder.add(Protocol.IPFIX, 0, NullValue::parser, "null", Optional.empty());

// Load providers
for (final Provider provider : providers) {
provider.load(adder);
}
Expand Down
Expand Up @@ -38,6 +38,7 @@
import org.opennms.netmgt.telemetry.listeners.flow.ie.values.IPv6AddressValue;
import org.opennms.netmgt.telemetry.listeners.flow.ie.values.ListValue;
import org.opennms.netmgt.telemetry.listeners.flow.ie.values.MacAddressValue;
import org.opennms.netmgt.telemetry.listeners.flow.ie.values.NullValue;
import org.opennms.netmgt.telemetry.listeners.flow.ie.values.OctetArrayValue;
import org.opennms.netmgt.telemetry.listeners.flow.ie.values.SignedValue;
import org.opennms.netmgt.telemetry.listeners.flow.ie.values.StringValue;
Expand All @@ -47,6 +48,8 @@
public abstract class Value<T> {

public interface Visitor {
void accept(final NullValue value);

void accept(final BooleanValue value);

void accept(final DateTimeValue value);
Expand Down
@@ -0,0 +1,85 @@
/*******************************************************************************
* This file is part of OpenNMS(R).
*
* Copyright (C) 2017 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2017 The OpenNMS Group, Inc.
*
* OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
*
* OpenNMS(R) is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* OpenNMS(R) is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with OpenNMS(R). If not, see:
* http://www.gnu.org/licenses/
*
* For more information contact:
* OpenNMS(R) Licensing <license@opennms.org>
* http://www.opennms.org/
* http://www.opennms.com/
*******************************************************************************/

package org.opennms.netmgt.telemetry.listeners.flow.ie.values;

import java.nio.ByteBuffer;
import java.util.Optional;

import org.opennms.netmgt.telemetry.listeners.flow.InvalidPacketException;
import org.opennms.netmgt.telemetry.listeners.flow.ie.InformationElement;
import org.opennms.netmgt.telemetry.listeners.flow.ie.Semantics;
import org.opennms.netmgt.telemetry.listeners.flow.ie.Value;
import org.opennms.netmgt.telemetry.listeners.flow.session.TemplateManager;

import com.google.common.base.MoreObjects;

public class NullValue extends Value<Void> {
public NullValue(final String name,
final Optional<Semantics> semantics) {
super(name, semantics);
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("name", getName())
.add("value", "(null)")
.toString();
}

public static InformationElement parser(final String name, final Optional<Semantics> semantics) {
return new InformationElement() {
@Override
public Value<?> parse(final TemplateManager.TemplateResolver templateResolver,
final ByteBuffer buffer) throws InvalidPacketException {
return new NullValue(name, semantics);
}

@Override
public int getMaximumFieldLength() {
return 0;
}

@Override
public int getMinimumFieldLength() {
return 0;
}
};
}

@Override
public Void getValue() {
return null;
}

@Override
public void visit(final Visitor visitor) {
visitor.accept(this);
}
}
Expand Up @@ -89,10 +89,6 @@ public FieldSpecifier(final ByteBuffer buffer) throws InvalidPacketException {

this.specifier = new EnterpriseField(this.fieldLength, this.informationElementId, enterpriseNumber);
}

if (this.fieldLength < 1) {
throw new InvalidPacketException("Zero-sized field");
}
}

@Override
Expand Down
Expand Up @@ -73,10 +73,6 @@ public FieldSpecifier(final ByteBuffer buffer) throws InvalidPacketException {
}

this.specifier = new StandardField(this.fieldLength, informationElement);

if (this.fieldLength < 1) {
throw new InvalidPacketException("Zero-sized field");
}
}

@Override
Expand Down

0 comments on commit 3258b29

Please sign in to comment.