Refactor for release; still need integration tests
diff --git a/nb-configuration.xml b/nb-configuration.xml
new file mode 100644
index 0000000..ae9d239
--- /dev/null
+++ b/nb-configuration.xml
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project-shared-configuration>
+ <!--
+This file contains additional configuration written by modules in the NetBeans IDE.
+The configuration is intended to be shared among all the users of project and
+therefore it is assumed to be part of version control checkout.
+Without this configuration present, some functionality in the IDE may be limited or fail altogether.
+-->
+ <properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
+ <!--
+Properties that influence various parts of the IDE, especially code formatting and the like.
+You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
+That way multiple projects can share the same settings (useful for formatting rules for example).
+Any value defined here will override the pom.xml file value but is only applicable to the current project.
+-->
+ <netbeans.hint.license>intel-license</netbeans.hint.license>
+ </properties>
+</project-shared-configuration>
diff --git a/pom.xml b/pom.xml
index 8a65f92..0845fd5 100644
--- a/pom.xml
+++ b/pom.xml
@@ -22,16 +22,6 @@
<artifactId>jndn-utils</artifactId>
<version>RELEASE</version>
</dependency>
- <dependency>
- <groupId>org.apache.logging.log4j</groupId>
- <artifactId>log4j-api</artifactId>
- <version>2.1</version>
- </dependency>
- <dependency>
- <groupId>org.apache.logging.log4j</groupId>
- <artifactId>log4j-core</artifactId>
- <version>2.1</version>
- </dependency>
<!-- Test dependencies -->
<dependency>
<groupId>junit</groupId>
diff --git a/src/main/java/com/intel/jndn/management/ControlResponse.java b/src/main/java/com/intel/jndn/management/ControlResponse.java
deleted file mode 100644
index c221d7c..0000000
--- a/src/main/java/com/intel/jndn/management/ControlResponse.java
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * File name: ControlResponse.java
- *
- * Purpose: Represent a ControlResponse, a Data packet sent in response to a
- * ControlCommand to the NFD, see http://redmine.named-data.net/projects/nfd/wiki/ControlCommand
- *
- * © Copyright Intel Corporation. All rights reserved.
- * Intel Corporation, 2200 Mission College Boulevard,
- * Santa Clara, CA 95052-8119, USA
- */
-package com.intel.jndn.management;
-
-import java.util.List;
-import net.named_data.jndn.ControlParameters;
-import net.named_data.jndn.Data;
-import net.named_data.jndn.encoding.EncodingException;
-
-/**
- *
- * @author Andrew Brown <andrew.brown@intel.com>
- */
-public class ControlResponse {
- public int StatusCode;
- public String StatusText;
- public List<ControlParameters> Body;
-
- /**
- * Decode input as a ControlResponse in NDN-TLV and set the fields of the
- * new object
- *
- * @param data
- * @return
- */
- public static ControlResponse decode(Data data) throws EncodingException{
- ControlResponseDecoder decoder = new ControlResponseDecoder();
- ControlResponse response = new ControlResponse();
- decoder.decodeControlResponse(response, data.getContent().buf());
- return response;
- }
-}
diff --git a/src/main/java/com/intel/jndn/management/ControlResponseDecoder.java b/src/main/java/com/intel/jndn/management/ControlResponseDecoder.java
deleted file mode 100644
index f52c35a..0000000
--- a/src/main/java/com/intel/jndn/management/ControlResponseDecoder.java
+++ /dev/null
@@ -1,68 +0,0 @@
-/*
- * File name: ControlParametersDecoder.java
- *
- * Purpose: See http://redmine.named-data.net/projects/nfd/wiki/ControlCommand
- *
- * © Copyright Intel Corporation. All rights reserved.
- * Intel Corporation, 2200 Mission College Boulevard,
- * Santa Clara, CA 95052-8119, USA
- */
-package com.intel.jndn.management;
-
-import java.nio.ByteBuffer;
-import java.util.ArrayList;
-import net.named_data.jndn.ControlParameters;
-import net.named_data.jndn.encoding.EncodingException;
-import net.named_data.jndn.encoding.TlvWireFormat;
-import net.named_data.jndn.encoding.tlv.Tlv;
-import net.named_data.jndn.encoding.tlv.TlvDecoder;
-import net.named_data.jndn.util.Blob;
-
-/**
- *
- * @author Andrew Brown <andrew.brown@intel.com>
- */
-public class ControlResponseDecoder extends TlvWireFormat {
-
- /**
- * Use TLV codes from jndn.encoding.tlv.Tlv.java See
- * http://redmine.named-data.net/projects/nfd/wiki/ControlCommand
- */
- public final static int ControlResponse = 101;
- public final static int ControlResponse_StatusCode = 102;
- public final static int ControlResponse_StatusText = 103;
-
- /**
- * Decode a ControlResponse TLV object; see
- * http://redmine.named-data.net/projects/nfd/wiki/ControlCommand
- *
- * @param controlResponse
- * @param input
- * @throws EncodingException
- */
- public void decodeControlResponse(ControlResponse controlResponse, ByteBuffer input) throws EncodingException {
- TlvDecoder decoder = new TlvDecoder(input);
- int endOffset = decoder.readNestedTlvsStart(ControlResponse);
-
- // parse
- controlResponse.StatusCode = (int) decoder.readNonNegativeIntegerTlv(ControlResponse_StatusCode);
- Blob statusText = new Blob(decoder.readBlobTlv(ControlResponse_StatusText), true); // copy because buffer is immutable
- controlResponse.StatusText = statusText.toString();
- controlResponse.Body = new ArrayList<>();
- while (decoder.peekType(Tlv.ControlParameters_ControlParameters, endOffset)) {
- ByteBuffer copyInput = input.duplicate();
- copyInput.position(decoder.getOffset());
- int internalEndOffset = decoder.readNestedTlvsStart(Tlv.ControlParameters_ControlParameters);
- // decode
- ControlParameters copyParameters = new ControlParameters();
- copyParameters.wireDecode(copyInput);
- controlResponse.Body.add(copyParameters);
- decoder.seek(internalEndOffset);
- //
- decoder.finishNestedTlvs(internalEndOffset);
- }
-
- // etc...
- decoder.finishNestedTlvs(endOffset);
- }
-}
diff --git a/src/main/java/com/intel/jndn/management/EncodingHelper.java b/src/main/java/com/intel/jndn/management/EncodingHelper.java
new file mode 100644
index 0000000..66e3b9d
--- /dev/null
+++ b/src/main/java/com/intel/jndn/management/EncodingHelper.java
@@ -0,0 +1,142 @@
+/*
+ * File name: EncodingHelper.java
+ *
+ * Purpose: Provide helper methods to cover areas too protected in Tlv0_1_1WireFormat;
+ * this class can be deprecated if WireFormats allow passing in an existing
+ * TlvEncoder/TlvDecoder (currently these methods are protected).
+ *
+ * © Copyright Intel Corporation. All rights reserved.
+ * Intel Corporation, 2200 Mission College Boulevard,
+ * Santa Clara, CA 95052-8119, USA
+ */
+package com.intel.jndn.management;
+
+import java.nio.ByteBuffer;
+import net.named_data.jndn.ControlParameters;
+import net.named_data.jndn.ForwardingFlags;
+import net.named_data.jndn.Name;
+import net.named_data.jndn.encoding.EncodingException;
+import net.named_data.jndn.encoding.tlv.Tlv;
+import net.named_data.jndn.encoding.tlv.TlvDecoder;
+import net.named_data.jndn.encoding.tlv.TlvEncoder;
+import net.named_data.jndn.util.Blob;
+
+/**
+ * Provide helper methods to cover areas too protected in Tlv0_1_1WireFormat;
+ * this class can be deprecated if WireFormats allow passing in an existing
+ * TlvEncoder/TlvDecoder (currently these methods are protected).
+ *
+ * @author Andrew Brown <andrew.brown@intel.com>
+ */
+public class EncodingHelper {
+
+ /**
+ * Helper to decode names since Tlv0_1_1WireFormat.java uses its own
+ * internal, protected implementation.
+ *
+ * @param input
+ * @return
+ * @throws EncodingException
+ */
+ public static Name decodeName(ByteBuffer input) throws EncodingException {
+ TlvDecoder decoder = new TlvDecoder(input);
+ return decodeName(decoder);
+ }
+
+ /**
+ * Helper to decode names using an existing decoding context; could be
+ * merged to Tlv0_1_1WireFormat.java.
+ *
+ * @param decoder
+ * @return
+ * @throws EncodingException
+ */
+ public static Name decodeName(TlvDecoder decoder) throws EncodingException {
+ Name name = new Name();
+ int endOffset = decoder.readNestedTlvsStart(Tlv.Name);
+ while (decoder.getOffset() < endOffset) {
+ name.append(new Blob(decoder.readBlobTlv(Tlv.NameComponent), true));
+ }
+
+ decoder.finishNestedTlvs(endOffset);
+ return name;
+ }
+
+ /**
+ * Helper to encode names since Tlv0_1_1WireFormat.java uses its own
+ * internal, protected implementation.
+ *
+ * @param name
+ * @return
+ */
+ public static Blob encodeName(Name name) {
+ TlvEncoder encoder = new TlvEncoder();
+ encodeName(name, encoder);
+ return new Blob(encoder.getOutput(), false);
+ }
+
+ /**
+ * Helper to encode names using an existing encoding context; could be
+ * merged to Tlv0_1_1WireFormat.java.
+ *
+ * @param name
+ * @param encoder
+ */
+ public static final void encodeName(Name name, TlvEncoder encoder) {
+ int saveLength = encoder.getLength();
+ for (int i = name.size() - 1; i >= 0; --i) {
+ encoder.writeBlobTlv(Tlv.NameComponent, name.get(i).getValue().buf());
+ }
+ encoder.writeTypeAndLength(Tlv.Name, encoder.getLength() - saveLength);
+ }
+
+ /**
+ * Helper to encode control parameters using an existing encoding context;
+ * could be merged to Tlv0_1_1WireFormat.java.
+ *
+ * @param controlParameters
+ * @param encoder
+ */
+ public static final void encodeControlParameters(ControlParameters controlParameters, TlvEncoder encoder) {
+ int saveLength = encoder.getLength();
+
+ // Encode backwards.
+ encoder.writeOptionalNonNegativeIntegerTlvFromDouble(Tlv.ControlParameters_ExpirationPeriod,
+ controlParameters.getExpirationPeriod());
+
+ // Encode strategy
+ if (controlParameters.getStrategy().size() != 0) {
+ int strategySaveLength = encoder.getLength();
+ encodeName(controlParameters.getStrategy(), encoder);
+ encoder.writeTypeAndLength(Tlv.ControlParameters_Strategy,
+ encoder.getLength() - strategySaveLength);
+ }
+
+ // Encode ForwardingFlags
+ int flags = controlParameters.getForwardingFlags().getNfdForwardingFlags();
+ if (flags != new ForwardingFlags().getNfdForwardingFlags()) // The flags are not the default value.
+ {
+ encoder.writeNonNegativeIntegerTlv(Tlv.ControlParameters_Flags, flags);
+ }
+
+ encoder.writeOptionalNonNegativeIntegerTlv(Tlv.ControlParameters_Cost, controlParameters.getCost());
+ encoder.writeOptionalNonNegativeIntegerTlv(Tlv.ControlParameters_Origin, controlParameters.getOrigin());
+ encoder.writeOptionalNonNegativeIntegerTlv(Tlv.ControlParameters_LocalControlFeature,
+ controlParameters.getLocalControlFeature());
+
+ // Encode URI
+ if (!controlParameters.getUri().isEmpty()) {
+ encoder.writeBlobTlv(Tlv.ControlParameters_Uri,
+ new Blob(controlParameters.getUri()).buf());
+ }
+
+ encoder.writeOptionalNonNegativeIntegerTlv(Tlv.ControlParameters_FaceId, controlParameters.getFaceId());
+
+ // Encode name
+ if (controlParameters.getName().size() != 0) {
+ encodeName(controlParameters.getName(), encoder);
+ }
+
+ encoder.writeTypeAndLength(Tlv.ControlParameters_ControlParameters, encoder.getLength() - saveLength);
+ }
+}
diff --git a/src/main/java/com/intel/jndn/management/FaceStatus.java b/src/main/java/com/intel/jndn/management/FaceStatus.java
deleted file mode 100644
index f0a992b..0000000
--- a/src/main/java/com/intel/jndn/management/FaceStatus.java
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * File name: FaceStatus.java
- *
- * Purpose: Represent a FaceStatus object from /localhost/nfd/faces/list;
- * see http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt for details
- *
- * © Copyright Intel Corporation. All rights reserved.
- * Intel Corporation, 2200 Mission College Boulevard,
- * Santa Clara, CA 95052-8119, USA
- */
-package com.intel.jndn.management;
-
-import java.util.List;
-import net.named_data.jndn.Data;
-import net.named_data.jndn.encoding.EncodingException;
-
-/**
- * Represent a FaceStatus object from /localhost/nfd/faces/list;
- * see http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt for details
- * @author Andrew Brown <andrew.brown@intel.com>
- */
-public class FaceStatus {
-
- public int faceId;
- public String uri; // can't use URI because some are invalid syntax
- public String localUri; // can't use URI because some are invalid syntax
- public int expirationPeriod;
- public FaceScope faceScope;
- public FacePersistency facePersistency;
- public LinkType linkType;
- public int inInterests;
- public int outInterests;
- public int inDatas;
- public int outDatas;
- public int inBytes;
- public int outBytes;
-
- /**
- * Helper method for decoding a list of face statuses from
- * /localhost/nfd/faces/list
- * @param data
- * @return
- * @throws EncodingException
- */
- public static List<FaceStatus> decode(Data data) throws EncodingException{
- FaceStatusDecoder decoder = new FaceStatusDecoder();
- return decoder.decodeFaces(data.getContent().buf());
- }
-}
diff --git a/src/main/java/com/intel/jndn/management/FaceStatusDecoder.java b/src/main/java/com/intel/jndn/management/FaceStatusDecoder.java
deleted file mode 100644
index 346f2cb..0000000
--- a/src/main/java/com/intel/jndn/management/FaceStatusDecoder.java
+++ /dev/null
@@ -1,97 +0,0 @@
-/*
- * File name: FaceStatusDecoder.java
- *
- * Purpose: Decode lists of FaceStatus objects from /localhost/nfd/faces/list
- *
- * © Copyright Intel Corporation. All rights reserved.
- * Intel Corporation, 2200 Mission College Boulevard,
- * Santa Clara, CA 95052-8119, USA
- */
-package com.intel.jndn.management;
-
-import java.nio.ByteBuffer;
-import java.util.ArrayList;
-import java.util.List;
-import net.named_data.jndn.encoding.EncodingException;
-import net.named_data.jndn.encoding.tlv.TlvDecoder;
-import net.named_data.jndn.util.Blob;
-
-/**
- * Decode lists of FaceStatus objects from /localhost/nfd/faces/list
- * @author Andrew Brown <andrew.brown@intel.com>
- */
-public class FaceStatusDecoder {
-
- /**
- * Spec from http://redmine.named-data.net/projects/nfd/wiki/ControlCommand
- */
- public static final int FACE_ID = 105;
- public static final int URI = 114;
- public static final int EXPIRATION_PERIOD = 109;
-
- /**
- * Spec from http://redmine.named-data.net/projects/nfd/widi/FaceMgmt
- */
- public static final int FACE_STATUS = 128;
- public static final int LOCAL_URI = 129;
- public static final int CHANNEL_STATUS = 130;
- public static final int FACE_SCOPE = 132;
- public static final int FACE_PERSISTENCY = 133;
- public static final int LINK_TYPE = 134;
- public static final int N_IN_INTERESTS = 144;
- public static final int N_IN_DATAS = 145;
- public static final int N_OUT_INTERESTS = 146;
- public static final int N_OUT_DATAS = 147;
- public static final int N_IN_BYTES = 148;
- public static final int N_OUT_BYTES = 149;
-
- /**
- * Decode a list of faces according to
- * http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt
- * @param buffer
- * @return
- * @throws EncodingException
- */
- public List<FaceStatus> decodeFaces(ByteBuffer buffer) throws EncodingException{
- ArrayList<FaceStatus> faces = new ArrayList<>();
- TlvDecoder decoder = new TlvDecoder(buffer);
- int parentEndOffset;
- do{
- parentEndOffset = decoder.readNestedTlvsStart(FACE_STATUS);
- faces.add(decodeFaceStatus(decoder, parentEndOffset));
- decoder.finishNestedTlvs(parentEndOffset);
- }
- while(parentEndOffset < buffer.limit());
- return faces;
- }
-
- /**
- * Decode one face status using the current decoder
- * @param buffer
- * @return
- * @throws EncodingException
- */
- private static FaceStatus decodeFaceStatus(TlvDecoder decoder, int parentEndOffset) throws EncodingException{
- FaceStatus status = new FaceStatus();
-
- // parse
- status.faceId = (int) decoder.readNonNegativeIntegerTlv(FACE_ID);
- Blob uri = new Blob(decoder.readBlobTlv(URI), true); // copy because buffer is immutable
- status.uri = uri.toString();
- Blob localUri = new Blob(decoder.readBlobTlv(LOCAL_URI), true); // copy because buffer is immutable
- status.localUri = localUri.toString();
- status.expirationPeriod = (int) decoder.readOptionalNonNegativeIntegerTlv(EXPIRATION_PERIOD, parentEndOffset);
- status.faceScope = FaceScope.values()[(int) decoder.readNonNegativeIntegerTlv(FACE_SCOPE)];
- status.facePersistency = FacePersistency.values()[(int) decoder.readNonNegativeIntegerTlv(FACE_PERSISTENCY)];
- status.linkType = LinkType.values()[(int) decoder.readNonNegativeIntegerTlv(LINK_TYPE)];
- status.inInterests = (int) decoder.readNonNegativeIntegerTlv(N_IN_INTERESTS);
- status.inDatas = (int) decoder.readNonNegativeIntegerTlv(N_IN_DATAS);
- status.outInterests = (int) decoder.readNonNegativeIntegerTlv(N_OUT_INTERESTS);
- status.outDatas = (int) decoder.readNonNegativeIntegerTlv(N_OUT_DATAS);
- status.inBytes = (int) decoder.readNonNegativeIntegerTlv(N_IN_BYTES);
- status.outBytes = (int) decoder.readNonNegativeIntegerTlv(N_OUT_BYTES);
-
- return status;
- }
-
-}
diff --git a/src/main/java/com/intel/jndn/management/NFD.java b/src/main/java/com/intel/jndn/management/NFD.java
index 07f241f..b5f531f 100644
--- a/src/main/java/com/intel/jndn/management/NFD.java
+++ b/src/main/java/com/intel/jndn/management/NFD.java
@@ -11,6 +11,10 @@
*/
package com.intel.jndn.management;
+import com.intel.jndn.management.types.StatusDataset;
+import com.intel.jndn.management.types.ControlResponse;
+import com.intel.jndn.management.types.FaceStatus;
+import com.intel.jndn.management.types.RibEntry;
import com.intel.jndn.utils.Client;
import java.io.IOException;
import java.util.List;
@@ -22,8 +26,7 @@
import net.named_data.jndn.Name;
import net.named_data.jndn.encoding.EncodingException;
import net.named_data.jndn.security.SecurityException;
-import org.apache.logging.log4j.LogManager;
-import org.apache.logging.log4j.Logger;
+import java.util.logging.Logger;
/**
* Helper class for interacting with an NDN forwarder daemon; see
@@ -35,7 +38,7 @@
public class NFD {
public final static long DEFAULT_TIMEOUT = 2000;
- private static final Logger logger = LogManager.getLogger();
+ static private final Logger logger = Logger.getLogger(NFD.class.getName());
/**
* Ping a forwarder on an existing face to verify that the forwarder is
@@ -43,7 +46,7 @@
* to /localhost/nfd which should always respond if the requestor is on the
* same machine as the NDN forwarding daemon.
*
- * @param face
+ * @param face
* @return true if successful, false otherwise
*/
public static boolean pingLocal(Face face) {
@@ -97,7 +100,33 @@
}
// parse packet
- return FaceStatus.decode(data);
+ return StatusDataset.wireDecode(data.getContent(), FaceStatus.class);
+ }
+
+ /**
+ * Retrieve a list of routing entries from the RIB; calls
+ * /localhost/nfd/rib/list which requires a local Face (all non-local
+ * packets are dropped)
+ *
+ * @param forwarder Only a localhost Face
+ * @return
+ * @throws Exception
+ */
+ public static List<RibEntry> getRouteList(Face forwarder) throws Exception {
+ // build management Interest packet; see http://redmine.named-data.net/projects/nfd/wiki/StatusDataset
+ Interest interest = new Interest(new Name("/localhost/nfd/rib/list"));
+ interest.setMustBeFresh(true);
+ interest.setChildSelector(Interest.CHILD_SELECTOR_RIGHT);
+ interest.setInterestLifetimeMilliseconds(DEFAULT_TIMEOUT);
+
+ // send packet
+ Data data = Client.getDefault().getSync(forwarder, interest);
+ if (data == null) {
+ throw new Exception("Failed to retrieve list of faces from the forwarder.");
+ }
+
+ // parse packet
+ return StatusDataset.wireDecode(data.getContent(), RibEntry.class);
}
/**
@@ -143,12 +172,12 @@
ControlResponse response = sendCommand(forwarder, new Interest(command));
// check for body and that status code is OK (TODO: 200 should be replaced with a CONSTANT like ControlResponse.STATUS_OK)
- if (response.Body.isEmpty() || response.StatusCode != 200) {
- throw new Exception("Failed to create face: " + uri + " " + response.StatusText != null ? response.StatusText : "");
+ if (response.getBody().isEmpty() || response.getStatusCode() != 200) {
+ throw new Exception("Failed to create face: " + uri + " " + response.getStatusText());
}
// return
- return response.Body.get(0).getFaceId();
+ return response.getBody().get(0).getFaceId();
}
/**
@@ -273,7 +302,9 @@
}
// return response
- return ControlResponse.decode(data);
+ ControlResponse response = new ControlResponse();
+ response.wireDecode(data.getContent().buf());
+ return response;
}
/**
@@ -293,10 +324,10 @@
*/
public static boolean sendCommandAndErrorCheck(Face forwarder, Interest interest) throws SecurityException, IOException, EncodingException {
ControlResponse response = sendCommand(forwarder, interest);
- if (response.StatusCode < 400) {
+ if (response.getStatusCode() < 400) {
return true;
} else {
- logger.warn("Command sent but failed: " + response.StatusCode + " " + response.StatusText);
+ logger.warning("Command sent but failed: " + response.getStatusCode() + " " + response.getStatusText());
return false;
}
}
diff --git a/src/main/java/com/intel/jndn/management/types/ControlResponse.java b/src/main/java/com/intel/jndn/management/types/ControlResponse.java
new file mode 100644
index 0000000..425f2d1
--- /dev/null
+++ b/src/main/java/com/intel/jndn/management/types/ControlResponse.java
@@ -0,0 +1,167 @@
+/*
+ * File name: ControlResponse.java
+ *
+ * Purpose: Represent a ControlResponse, a Data packet sent in response to a
+ * ControlCommand to the NFD, see http://redmine.named-data.net/projects/nfd/wiki/ControlCommand
+ *
+ * © Copyright Intel Corporation. All rights reserved.
+ * Intel Corporation, 2200 Mission College Boulevard,
+ * Santa Clara, CA 95052-8119, USA
+ */
+package com.intel.jndn.management.types;
+
+import com.intel.jndn.management.EncodingHelper;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.List;
+import net.named_data.jndn.ControlParameters;
+import net.named_data.jndn.encoding.EncodingException;
+import net.named_data.jndn.encoding.tlv.Tlv;
+import net.named_data.jndn.encoding.tlv.TlvDecoder;
+import net.named_data.jndn.encoding.tlv.TlvEncoder;
+import net.named_data.jndn.util.Blob;
+
+/**
+ * Represent a ControlResponse, a Data packet sent in response to a
+ * ControlCommand to the NFD, see
+ * http://redmine.named-data.net/projects/nfd/wiki/ControlCommand
+ *
+ * @author Andrew Brown <andrew.brown@intel.com>
+ */
+public class ControlResponse {
+
+ /**
+ * Use TLV codes from jndn.encoding.tlv.Tlv.java See
+ * http://redmine.named-data.net/projects/nfd/wiki/ControlCommand
+ */
+ public final static int TLV_CONTROL_RESPONSE = 101;
+ public final static int TLV_CONTROL_RESPONSE_STATUS_CODE = 102;
+ public final static int TLV_CONTROL_RESPONSE_STATUS_TEXT = 103;
+
+ /**
+ * Encode using a new TLV encoder.
+ *
+ * @return The encoded buffer.
+ */
+ public final Blob wireEncode() {
+ TlvEncoder encoder = new TlvEncoder();
+ wireEncode(encoder);
+ return new Blob(encoder.getOutput(), false);
+ }
+
+ /**
+ * Encode as part of an existing encode context.
+ *
+ * @param encoder
+ */
+ public final void wireEncode(TlvEncoder encoder) {
+ int saveLength = encoder.getLength();
+ for (ControlParameters parameters : body) {
+ EncodingHelper.encodeControlParameters(parameters, encoder);
+ }
+ encoder.writeBlobTlv(TLV_CONTROL_RESPONSE_STATUS_TEXT, new Blob(statusText).buf());
+ encoder.writeNonNegativeIntegerTlv(TLV_CONTROL_RESPONSE_STATUS_CODE, statusCode);
+ encoder.writeTypeAndLength(TLV_CONTROL_RESPONSE, encoder.getLength() - saveLength);
+ }
+
+ /**
+ * Decode the input from its TLV format.
+ *
+ * @param input The input buffer to decode. This reads from position() to
+ * limit(), but does not change the position.
+ * @throws net.named_data.jndn.encoding.EncodingException
+ */
+ public final void wireDecode(ByteBuffer input) throws EncodingException {
+ TlvDecoder decoder = new TlvDecoder(input);
+ wireDecode(decoder, input);
+ }
+
+ /**
+ * Decode as part of an existing decode context.
+ *
+ * @param decoder
+ * @param input the WireFormat version that decodes ControlParameters does
+ * not allow passing a TlvDecoder, so we must pass the buffer itself
+ * @throws EncodingException
+ */
+ public void wireDecode(TlvDecoder decoder, ByteBuffer input) throws EncodingException {
+ int endOffset = decoder.readNestedTlvsStart(TLV_CONTROL_RESPONSE);
+
+ // parse known TLVs
+ this.statusCode = (int) decoder.readNonNegativeIntegerTlv(TLV_CONTROL_RESPONSE_STATUS_CODE);
+ Blob statusText_ = new Blob(decoder.readBlobTlv(TLV_CONTROL_RESPONSE_STATUS_TEXT), true); // copy because buffer is immutable
+ this.statusText = statusText_.toString();
+
+ // use the already-written decoder for ControlParameters (but we have to copy the buffer)
+ while (decoder.peekType(Tlv.ControlParameters_ControlParameters, endOffset)) {
+ ByteBuffer copyInput = input.duplicate();
+ copyInput.position(decoder.getOffset());
+ int internalEndOffset = decoder.readNestedTlvsStart(Tlv.ControlParameters_ControlParameters);
+ ControlParameters copyParameters = new ControlParameters();
+ copyParameters.wireDecode(copyInput);
+ this.body.add(copyParameters);
+ decoder.seek(internalEndOffset);
+ decoder.finishNestedTlvs(internalEndOffset);
+ }
+
+ decoder.finishNestedTlvs(endOffset);
+ }
+
+ /**
+ * Get status code
+ *
+ * @return
+ */
+ public int getStatusCode() {
+ return statusCode;
+ }
+
+ /**
+ * Set status code
+ *
+ * @param statusCode
+ */
+ public void setStatusCode(int statusCode) {
+ this.statusCode = statusCode;
+ }
+
+ /**
+ * Get status text
+ *
+ * @return
+ */
+ public String getStatusText() {
+ return statusText;
+ }
+
+ /**
+ * Set status text
+ *
+ * @param statusText
+ */
+ public void setStatusText(String statusText) {
+ this.statusText = statusText;
+ }
+
+ /**
+ * Get body
+ *
+ * @return
+ */
+ public List<ControlParameters> getBody() {
+ return body;
+ }
+
+ /**
+ * Set body
+ *
+ * @param body
+ */
+ public void setBody(List<ControlParameters> body) {
+ this.body = body;
+ }
+
+ private int statusCode = -1;
+ private String statusText = "";
+ private List<ControlParameters> body = new ArrayList<>();
+}
diff --git a/src/main/java/com/intel/jndn/management/types/Decodable.java b/src/main/java/com/intel/jndn/management/types/Decodable.java
new file mode 100644
index 0000000..17bb0fa
--- /dev/null
+++ b/src/main/java/com/intel/jndn/management/types/Decodable.java
@@ -0,0 +1,25 @@
+/*
+ * File name: Decodable.java
+ *
+ * Purpose: Interface used by StatusDataset to decode generic message types; if
+ * they are Decodable, then StatusDataset will instantiate and decode them.
+ *
+ * Purpose:
+ * © Copyright Intel Corporation. All rights reserved.
+ * Intel Corporation, 2200 Mission College Boulevard,
+ * Santa Clara, CA 95052-8119, USA
+ */
+package com.intel.jndn.management.types;
+
+import net.named_data.jndn.encoding.EncodingException;
+import net.named_data.jndn.encoding.tlv.TlvDecoder;
+
+/**
+ * Interface used by StatusDataset to decode generic message types; if they are
+ * Decodable, then StatusDataset will instantiate and decode them.
+ *
+ * @author Andrew Brown <andrew.brown@intel.com>
+ */
+public interface Decodable {
+ public void wireDecode(TlvDecoder decoder) throws EncodingException;
+}
diff --git a/src/main/java/com/intel/jndn/management/FacePersistency.java b/src/main/java/com/intel/jndn/management/types/FacePersistency.java
similarity index 94%
rename from src/main/java/com/intel/jndn/management/FacePersistency.java
rename to src/main/java/com/intel/jndn/management/types/FacePersistency.java
index 2f2b5e6..d663acc 100644
--- a/src/main/java/com/intel/jndn/management/FacePersistency.java
+++ b/src/main/java/com/intel/jndn/management/types/FacePersistency.java
@@ -8,7 +8,7 @@
* Intel Corporation, 2200 Mission College Boulevard,
* Santa Clara, CA 95052-8119, USA
*/
-package com.intel.jndn.management;
+package com.intel.jndn.management.types;
/**
* Indicate whether the face is persistent; used by FaceStatus
diff --git a/src/main/java/com/intel/jndn/management/FaceScope.java b/src/main/java/com/intel/jndn/management/types/FaceScope.java
similarity index 94%
rename from src/main/java/com/intel/jndn/management/FaceScope.java
rename to src/main/java/com/intel/jndn/management/types/FaceScope.java
index 17028c5..100befc 100644
--- a/src/main/java/com/intel/jndn/management/FaceScope.java
+++ b/src/main/java/com/intel/jndn/management/types/FaceScope.java
@@ -8,7 +8,7 @@
* Intel Corporation, 2200 Mission College Boulevard,
* Santa Clara, CA 95052-8119, USA
*/
-package com.intel.jndn.management;
+package com.intel.jndn.management.types;
/**
* Indicate whether the face is local for scope control purposes; used by FaceStatus
diff --git a/src/main/java/com/intel/jndn/management/types/FaceStatus.java b/src/main/java/com/intel/jndn/management/types/FaceStatus.java
new file mode 100644
index 0000000..6883d54
--- /dev/null
+++ b/src/main/java/com/intel/jndn/management/types/FaceStatus.java
@@ -0,0 +1,370 @@
+/*
+ * File name: FaceStatus.java
+ *
+ * Purpose: Represent a FaceStatus object from /localhost/nfd/faces/list;
+ * see http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt for details
+ *
+ * © Copyright Intel Corporation. All rights reserved.
+ * Intel Corporation, 2200 Mission College Boulevard,
+ * Santa Clara, CA 95052-8119, USA
+ */
+package com.intel.jndn.management.types;
+
+import java.nio.ByteBuffer;
+import net.named_data.jndn.encoding.EncodingException;
+import net.named_data.jndn.encoding.tlv.TlvDecoder;
+import net.named_data.jndn.encoding.tlv.TlvEncoder;
+import net.named_data.jndn.util.Blob;
+
+/**
+ * Represent a FaceStatus object from /localhost/nfd/faces/list; see
+ * http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt for details
+ *
+ * @author Andrew Brown <andrew.brown@intel.com>
+ */
+public class FaceStatus implements Decodable{
+
+ /**
+ * Spec from http://redmine.named-data.net/projects/nfd/wiki/ControlCommand
+ */
+ public static final int TLV_FACE_ID = 105;
+ public static final int TLV_URI = 114;
+ public static final int TLV_EXPIRATION_PERIOD = 109;
+
+ /**
+ * Spec from http://redmine.named-data.net/projects/nfd/widi/FaceMgmt
+ */
+ public static final int TLV_FACE_STATUS = 128;
+ public static final int TLV_LOCAL_URI = 129;
+ public static final int TLV_CHANNEL_STATUS = 130;
+ public static final int TLV_FACE_SCOPE = 132;
+ public static final int TLV_FACE_PERSISTENCY = 133;
+ public static final int TLV_LINK_TYPE = 134;
+ public static final int TLV_N_IN_INTERESTS = 144;
+ public static final int TLV_N_IN_DATAS = 145;
+ public static final int TLV_N_OUT_INTERESTS = 146;
+ public static final int TLV_N_OUT_DATAS = 147;
+ public static final int TLV_N_IN_BYTES = 148;
+ public static final int TLV_N_OUT_BYTES = 149;
+
+ /**
+ * Encode using a new TLV encoder.
+ *
+ * @return The encoded buffer.
+ */
+ public final Blob wireEncode() {
+ TlvEncoder encoder = new TlvEncoder();
+ wireEncode(encoder);
+ return new Blob(encoder.getOutput(), false);
+ }
+
+ /**
+ * Encode as part of an existing encode context.
+ *
+ * @param encoder
+ */
+ public final void wireEncode(TlvEncoder encoder) {
+ int saveLength = encoder.getLength();
+ encoder.writeNonNegativeIntegerTlv(TLV_N_OUT_BYTES, outBytes);
+ encoder.writeNonNegativeIntegerTlv(TLV_N_IN_BYTES, inBytes);
+ encoder.writeNonNegativeIntegerTlv(TLV_N_OUT_DATAS, outDatas);
+ encoder.writeNonNegativeIntegerTlv(TLV_N_OUT_INTERESTS, outInterests);
+ encoder.writeNonNegativeIntegerTlv(TLV_N_IN_DATAS, inDatas);
+ encoder.writeNonNegativeIntegerTlv(TLV_N_IN_INTERESTS, inInterests);
+ encoder.writeNonNegativeIntegerTlv(TLV_LINK_TYPE, linkType.getNumericValue());
+ encoder.writeNonNegativeIntegerTlv(TLV_FACE_PERSISTENCY, facePersistency.getNumericValue());
+ encoder.writeNonNegativeIntegerTlv(TLV_FACE_SCOPE, faceScope.getNumericValue());
+ encoder.writeOptionalNonNegativeIntegerTlv(TLV_EXPIRATION_PERIOD, expirationPeriod);
+ encoder.writeBlobTlv(TLV_LOCAL_URI, new Blob(localUri).buf());
+ encoder.writeBlobTlv(TLV_URI, new Blob(uri).buf());
+ encoder.writeNonNegativeIntegerTlv(TLV_FACE_ID, faceId);
+ encoder.writeTypeAndLength(TLV_FACE_STATUS, encoder.getLength() - saveLength);
+ }
+
+ /**
+ * Decode the input from its TLV format.
+ *
+ * @param input The input buffer to decode. This reads from position() to
+ * limit(), but does not change the position.
+ * @throws net.named_data.jndn.encoding.EncodingException
+ */
+ public final void wireDecode(ByteBuffer input) throws EncodingException {
+ TlvDecoder decoder = new TlvDecoder(input);
+ wireDecode(decoder);
+ }
+
+ /**
+ * Decode as part of an existing decode context.
+ *
+ * @param decoder
+ * @throws EncodingException
+ */
+ @Override
+ public void wireDecode(TlvDecoder decoder) throws EncodingException {
+ int endOffset = decoder.readNestedTlvsStart(TLV_FACE_STATUS);
+ // parse
+ this.faceId = (int) decoder.readNonNegativeIntegerTlv(TLV_FACE_ID);
+ Blob uri_ = new Blob(decoder.readBlobTlv(TLV_URI), true); // copy because buffer is immutable
+ this.uri = uri_.toString();
+ Blob localUri_ = new Blob(decoder.readBlobTlv(TLV_LOCAL_URI), true); // copy because buffer is immutable
+ this.localUri = localUri_.toString();
+ this.expirationPeriod = (int) decoder.readOptionalNonNegativeIntegerTlv(TLV_EXPIRATION_PERIOD, endOffset);
+ this.faceScope = FaceScope.values()[(int) decoder.readNonNegativeIntegerTlv(TLV_FACE_SCOPE)];
+ this.facePersistency = FacePersistency.values()[(int) decoder.readNonNegativeIntegerTlv(TLV_FACE_PERSISTENCY)];
+ this.linkType = LinkType.values()[(int) decoder.readNonNegativeIntegerTlv(TLV_LINK_TYPE)];
+ this.inInterests = (int) decoder.readNonNegativeIntegerTlv(TLV_N_IN_INTERESTS);
+ this.inDatas = (int) decoder.readNonNegativeIntegerTlv(TLV_N_IN_DATAS);
+ this.outInterests = (int) decoder.readNonNegativeIntegerTlv(TLV_N_OUT_INTERESTS);
+ this.outDatas = (int) decoder.readNonNegativeIntegerTlv(TLV_N_OUT_DATAS);
+ this.inBytes = (int) decoder.readNonNegativeIntegerTlv(TLV_N_IN_BYTES);
+ this.outBytes = (int) decoder.readNonNegativeIntegerTlv(TLV_N_OUT_BYTES);
+ decoder.finishNestedTlvs(endOffset);
+ }
+
+ /**
+ * Get face ID
+ *
+ * @return
+ */
+ public int getFaceId() {
+ return faceId;
+ }
+
+ /**
+ * Set face ID
+ *
+ * @param faceId
+ */
+ public void setFaceId(int faceId) {
+ this.faceId = faceId;
+ }
+
+ /**
+ * Get face ID
+ *
+ * @return
+ */
+ public String getUri() {
+ return uri;
+ }
+
+ /**
+ * Set URI
+ *
+ * @param uri
+ */
+ public void setUri(String uri) {
+ this.uri = uri;
+ }
+
+ /**
+ * Get face ID
+ *
+ * @return
+ */
+ public String getLocalUri() {
+ return localUri;
+ }
+
+ /**
+ * Set local URI
+ * @param localUri
+ */
+ public void setLocalUri(String localUri) {
+ this.localUri = localUri;
+ }
+
+ /**
+ * Get expiration period
+ *
+ * @return
+ */
+ public int getExpirationPeriod() {
+ return expirationPeriod;
+ }
+
+ /**
+ * Set expiration period
+ *
+ * @param expirationPeriod
+ */
+ public void setExpirationPeriod(int expirationPeriod) {
+ this.expirationPeriod = expirationPeriod;
+ }
+
+ /**
+ * Get face scope value
+ *
+ * @return
+ */
+ public FaceScope getFaceScope() {
+ return faceScope;
+ }
+
+ /**
+ * Set face scope value
+ *
+ * @param faceScope
+ */
+ public void setFaceScope(FaceScope faceScope) {
+ this.faceScope = faceScope;
+ }
+
+ /**
+ * Get face persistency value
+ *
+ * @return
+ */
+ public FacePersistency getFacePersistency() {
+ return facePersistency;
+ }
+
+ /**
+ * Set face persistency value
+ *
+ * @param facePersistency
+ */
+ public void setFacePersistency(FacePersistency facePersistency) {
+ this.facePersistency = facePersistency;
+ }
+
+ /**
+ * Get link type
+ *
+ * @return
+ */
+ public LinkType getLinkType() {
+ return linkType;
+ }
+
+ /**
+ * Set link type
+ *
+ * @param linkType
+ */
+ public void setLinkType(LinkType linkType) {
+ this.linkType = linkType;
+ }
+
+ /**
+ * Get number of received Interest packets
+ *
+ * @return
+ */
+ public int getInInterests() {
+ return inInterests;
+ }
+
+ /**
+ * Set number of received Interest packets
+ *
+ * @param inInterests
+ */
+ public void setInInterests(int inInterests) {
+ this.inInterests = inInterests;
+ }
+
+ /**
+ * Get number of sent Interest packets
+ *
+ * @return
+ */
+ public int getOutInterests() {
+ return outInterests;
+ }
+
+ /**
+ * Set number of sent Interest packets
+ *
+ * @param outInterests
+ */
+ public void setOutInterests(int outInterests) {
+ this.outInterests = outInterests;
+ }
+
+ /**
+ * Get number of received Data packets
+ *
+ * @return
+ */
+ public int getInDatas() {
+ return inDatas;
+ }
+
+ /**
+ * Set number of received Data packets
+ *
+ * @param inDatas
+ */
+ public void setInDatas(int inDatas) {
+ this.inDatas = inDatas;
+ }
+
+ /**
+ * Get number of sent Data packets
+ *
+ * @return
+ */
+ public int getOutDatas() {
+ return outDatas;
+ }
+
+ /**
+ * Set number of sent Data packets
+ *
+ * @param outDatas
+ */
+ public void setOutDatas(int outDatas) {
+ this.outDatas = outDatas;
+ }
+
+ /**
+ * Get number of input bytes
+ *
+ * @return
+ */
+ public int getInBytes() {
+ return inBytes;
+ }
+
+ /**
+ * Set number of input bytes
+ *
+ * @param inBytes
+ */
+ public void setInBytes(int inBytes) {
+ this.inBytes = inBytes;
+ }
+
+ /**
+ * Get number of output bytes
+ *
+ * @return
+ */
+ public int getOutBytes() {
+ return outBytes;
+ }
+
+ /**
+ * Set number of output bytes
+ *
+ * @param outBytes
+ */
+ public void setOutBytes(int outBytes) {
+ this.outBytes = outBytes;
+ }
+
+ private int faceId = -1;
+ private String uri = ""; // can't use URI because some are invalid syntax
+ private String localUri = ""; // can't use URI because some are invalid syntax
+ private int expirationPeriod = 0;
+ private FaceScope faceScope = FaceScope.LOCAL;
+ private FacePersistency facePersistency = FacePersistency.ON_DEMAND;
+ private LinkType linkType = LinkType.POINT_TO_POINT;
+ private int inInterests = 0;
+ private int outInterests = 0;
+ private int inDatas = 0;
+ private int outDatas = 0;
+ private int inBytes = 0;
+ private int outBytes = 0;
+}
diff --git a/src/main/java/com/intel/jndn/management/types/FibEntry.java b/src/main/java/com/intel/jndn/management/types/FibEntry.java
new file mode 100644
index 0000000..31cadbd
--- /dev/null
+++ b/src/main/java/com/intel/jndn/management/types/FibEntry.java
@@ -0,0 +1,126 @@
+/*
+ * File name: FibEntry.java
+ *
+ * Purpose: Represent a FibEntry returned from /localhost/nfd/fib/list; see
+ * http://redmine.named-data.net/projects/nfd/wiki/FibMgmt#FIB-Dataset
+ *
+ * © Copyright Intel Corporation. All rights reserved.
+ * Intel Corporation, 2200 Mission College Boulevard,
+ * Santa Clara, CA 95052-8119, USA
+ */
+package com.intel.jndn.management.types;
+
+import com.intel.jndn.management.EncodingHelper;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.List;
+import net.named_data.jndn.Name;
+import net.named_data.jndn.encoding.EncodingException;
+import net.named_data.jndn.encoding.tlv.TlvDecoder;
+import net.named_data.jndn.encoding.tlv.TlvEncoder;
+import net.named_data.jndn.util.Blob;
+
+/**
+ * Represent a FibEntry returned from /localhost/nfd/fib/list; see
+ * http://redmine.named-data.net/projects/nfd/wiki/FibMgmt#FIB-Dataset
+ *
+ * @author Andrew Brown <andrew.brown@intel.com>
+ */
+public class FibEntry implements Decodable{
+
+ public final static int TLV_FIB_ENTRY = 128;
+
+ /**
+ * Encode using a new TLV encoder.
+ *
+ * @return The encoded buffer.
+ */
+ public final Blob wireEncode() {
+ TlvEncoder encoder = new TlvEncoder();
+ wireEncode(encoder);
+ return new Blob(encoder.getOutput(), false);
+ }
+
+ /**
+ * Encode as part of an existing encode context.
+ *
+ * @param encoder
+ */
+ public final void wireEncode(TlvEncoder encoder) {
+ int saveLength = encoder.getLength();
+ for (NextHopRecord record : records) {
+ record.wireEncode(encoder);
+ }
+ EncodingHelper.encodeName(name, encoder);
+ encoder.writeTypeAndLength(TLV_FIB_ENTRY, encoder.getLength() - saveLength);
+ }
+
+ /**
+ * Decode the input from its TLV format.
+ *
+ * @param input The input buffer to decode. This reads from position() to
+ * limit(), but does not change the position.
+ * @throws EncodingException For invalid encoding.
+ */
+ public final void wireDecode(ByteBuffer input) throws EncodingException {
+ TlvDecoder decoder = new TlvDecoder(input);
+ wireDecode(decoder);
+ }
+
+ /**
+ * Decode as part of an existing decode context.
+ *
+ * @param decoder
+ * @throws EncodingException
+ */
+ @Override
+ public final void wireDecode(TlvDecoder decoder) throws EncodingException {
+ int endOffset = decoder.readNestedTlvsStart(TLV_FIB_ENTRY);
+ name = EncodingHelper.decodeName(decoder);
+ while (decoder.getOffset() < endOffset) {
+ NextHopRecord record = new NextHopRecord();
+ record.wireDecode(decoder);
+ records.add(record);
+ }
+ decoder.finishNestedTlvs(endOffset);
+ }
+
+ /**
+ * Get name
+ *
+ * @return
+ */
+ public Name getName() {
+ return name;
+ }
+
+ /**
+ * Set name
+ *
+ * @param name
+ */
+ public void setName(Name name) {
+ this.name = name;
+ }
+
+ /**
+ * Get records
+ *
+ * @return
+ */
+ public List<NextHopRecord> getRecords() {
+ return records;
+ }
+
+ /**
+ * Set records
+ *
+ * @param records
+ */
+ public void setRecords(List<NextHopRecord> records) {
+ this.records = records;
+ }
+
+ private Name name = new Name();
+ private List<NextHopRecord> records = new ArrayList<>();
+}
diff --git a/src/main/java/com/intel/jndn/management/LinkType.java b/src/main/java/com/intel/jndn/management/types/LinkType.java
similarity index 94%
rename from src/main/java/com/intel/jndn/management/LinkType.java
rename to src/main/java/com/intel/jndn/management/types/LinkType.java
index e69513e..ea09f47 100644
--- a/src/main/java/com/intel/jndn/management/LinkType.java
+++ b/src/main/java/com/intel/jndn/management/types/LinkType.java
@@ -8,7 +8,7 @@
* Intel Corporation, 2200 Mission College Boulevard,
* Santa Clara, CA 95052-8119, USA
*/
-package com.intel.jndn.management;
+package com.intel.jndn.management.types;
/**
* Indicate the type of communication link; used by FaceStatus
diff --git a/src/main/java/com/intel/jndn/management/types/NextHopRecord.java b/src/main/java/com/intel/jndn/management/types/NextHopRecord.java
new file mode 100644
index 0000000..b9001ad
--- /dev/null
+++ b/src/main/java/com/intel/jndn/management/types/NextHopRecord.java
@@ -0,0 +1,117 @@
+/*
+ * File name: NextHopRecord.java
+ *
+ * Purpose: Represent a NextHopRecord in a FibEntry; see
+ * http://redmine.named-data.net/projects/nfd/wiki/FibMgmt#FIB-Dataset
+
+ *
+ * © Copyright Intel Corporation. All rights reserved.
+ * Intel Corporation, 2200 Mission College Boulevard,
+ * Santa Clara, CA 95052-8119, USA
+ */
+package com.intel.jndn.management.types;
+
+import java.nio.ByteBuffer;
+import net.named_data.jndn.encoding.EncodingException;
+import net.named_data.jndn.encoding.tlv.Tlv;
+import net.named_data.jndn.encoding.tlv.TlvDecoder;
+import net.named_data.jndn.encoding.tlv.TlvEncoder;
+import net.named_data.jndn.util.Blob;
+
+/**
+ * Represent a NextHopRecord in a FibEntry; see
+ * http://redmine.named-data.net/projects/nfd/wiki/FibMgmt#FIB-Dataset
+ *
+ * @author Andrew Brown <andrew.brown@intel.com>
+ */
+public class NextHopRecord {
+
+ public final static int TLV_NEXT_HOP_RECORD = 129;
+
+ /**
+ * Encode using a new TLV encoder.
+ *
+ * @return The encoded buffer.
+ */
+ public final Blob wireEncode() {
+ TlvEncoder encoder = new TlvEncoder();
+ wireEncode(encoder);
+ return new Blob(encoder.getOutput(), false);
+ }
+
+ /**
+ * Encode as part of an existing encode context.
+ *
+ * @param encoder
+ */
+ public final void wireEncode(TlvEncoder encoder) {
+ int saveLength = encoder.getLength();
+ encoder.writeNonNegativeIntegerTlv(Tlv.ControlParameters_Cost, cost);
+ encoder.writeNonNegativeIntegerTlv(Tlv.ControlParameters_FaceId, faceId);
+ encoder.writeTypeAndLength(TLV_NEXT_HOP_RECORD, encoder.getLength() - saveLength);
+ }
+
+ /**
+ * Decode the input from its TLV format.
+ *
+ * @param input The input buffer to decode. This reads from position() to
+ * limit(), but does not change the position.
+ * @throws EncodingException For invalid encoding.
+ */
+ public final void wireDecode(ByteBuffer input) throws EncodingException {
+ TlvDecoder decoder = new TlvDecoder(input);
+ wireDecode(decoder);
+ }
+
+ /**
+ * Decode as part of an existing decode context.
+ *
+ * @param decoder
+ * @throws EncodingException
+ */
+ public final void wireDecode(TlvDecoder decoder) throws EncodingException {
+ int endOffset = decoder.readNestedTlvsStart(TLV_NEXT_HOP_RECORD);
+ this.faceId = (int) decoder.readNonNegativeIntegerTlv(Tlv.ControlParameters_FaceId);
+ this.cost = (int) decoder.readNonNegativeIntegerTlv(Tlv.ControlParameters_Cost);
+ decoder.finishNestedTlvs(endOffset);
+ }
+
+ /**
+ * Get face ID
+ *
+ * @return
+ */
+ public int getFaceId() {
+ return faceId;
+ }
+
+ /**
+ * Set face ID
+ *
+ * @param faceId
+ */
+ public void setFaceId(int faceId) {
+ this.faceId = faceId;
+ }
+
+ /**
+ * Get cost
+ *
+ * @return
+ */
+ public int getCost() {
+ return cost;
+ }
+
+ /**
+ * Set cost
+ *
+ * @param cost
+ */
+ public void setCost(int cost) {
+ this.cost = cost;
+ }
+
+ private int faceId;
+ private int cost;
+}
diff --git a/src/main/java/com/intel/jndn/management/types/RibEntry.java b/src/main/java/com/intel/jndn/management/types/RibEntry.java
new file mode 100644
index 0000000..ac53810
--- /dev/null
+++ b/src/main/java/com/intel/jndn/management/types/RibEntry.java
@@ -0,0 +1,131 @@
+/*
+ * File name: RibEntry.java
+ *
+ * Purpose: Represent a FaceStatus object from /localhost/nfd/faces/list;
+ * see http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt for details
+ *
+ * © Copyright Intel Corporation. All rights reserved.
+ * Intel Corporation, 2200 Mission College Boulevard,
+ * Santa Clara, CA 95052-8119, USA
+ */
+package com.intel.jndn.management.types;
+
+import com.intel.jndn.management.EncodingHelper;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.List;
+import net.named_data.jndn.Name;
+import net.named_data.jndn.encoding.EncodingException;
+import net.named_data.jndn.encoding.tlv.TlvDecoder;
+import net.named_data.jndn.encoding.tlv.TlvEncoder;
+import net.named_data.jndn.util.Blob;
+
+/**
+ * Represent a entry in the RIB; see
+ * http://redmine.named-data.net/projects/nfd/wiki/RibMgmt#RIB-Dataset for
+ * details
+ *
+ * @author Andrew Brown <andrew.brown@intel.com>
+ */
+public class RibEntry implements Decodable{
+
+ /**
+ * TLV type, see
+ * http://redmine.named-data.net/projects/nfd/wiki/RibMgmt#TLV-TYPE-assignments
+ */
+ public final static int TLV_RIB_ENTRY = 128;
+
+ /**
+ * Encode using a new TLV encoder.
+ *
+ * @return The encoded buffer.
+ */
+ public final Blob wireEncode() {
+ TlvEncoder encoder = new TlvEncoder();
+ wireEncode(encoder);
+ return new Blob(encoder.getOutput(), false);
+ }
+
+ /**
+ * Encode as part of an existing encode context.
+ *
+ * @param encoder
+ */
+ public final void wireEncode(TlvEncoder encoder) {
+ int saveLength = encoder.getLength();
+ for (Route route : routes) {
+ route.wireEncode(encoder);
+ }
+ EncodingHelper.encodeName(name, encoder);
+ encoder.writeTypeAndLength(TLV_RIB_ENTRY, encoder.getLength() - saveLength);
+ }
+
+ /**
+ * Decode the input from its TLV format.
+ *
+ * @param input The input buffer to decode. This reads from position() to
+ * limit(), but does not change the position.
+ * @throws EncodingException For invalid encoding.
+ */
+ public final void wireDecode(ByteBuffer input) throws EncodingException {
+ TlvDecoder decoder = new TlvDecoder(input);
+ wireDecode(decoder);
+ }
+
+ /**
+ * Decode as part of an existing decode context.
+ *
+ * @param decoder
+ * @throws EncodingException
+ */
+ @Override
+ public final void wireDecode(TlvDecoder decoder) throws EncodingException {
+ int endOffset = decoder.readNestedTlvsStart(TLV_RIB_ENTRY);
+ name = EncodingHelper.decodeName(decoder);
+ while (decoder.getOffset() < endOffset) {
+ Route route = new Route();
+ route.wireDecode(decoder);
+ routes.add(route);
+ }
+ decoder.finishNestedTlvs(endOffset);
+ }
+
+ /**
+ * Get name
+ *
+ * @return
+ */
+ public Name getName() {
+ return name;
+ }
+
+ /**
+ * Set name
+ *
+ * @param name
+ */
+ public void setName(Name name) {
+ this.name = name;
+ }
+
+ /**
+ * Get routes
+ *
+ * @return
+ */
+ public List<Route> getRoutes() {
+ return routes;
+ }
+
+ /**
+ * Set routes
+ *
+ * @param routes
+ */
+ public void setRoutes(List<Route> routes) {
+ this.routes = routes;
+ }
+
+ private Name name = new Name();
+ private List<Route> routes = new ArrayList<>();
+}
diff --git a/src/main/java/com/intel/jndn/management/types/Route.java b/src/main/java/com/intel/jndn/management/types/Route.java
new file mode 100644
index 0000000..2cbb247
--- /dev/null
+++ b/src/main/java/com/intel/jndn/management/types/Route.java
@@ -0,0 +1,186 @@
+/*
+ * File name: Route.java
+ *
+ * Purpose: Represent a Route object from /localhost/nfd/rib/list; see
+ * http://redmine.named-data.net/projects/nfd/wiki/RibMgmt#RIB-Dataset for
+ * details.
+ *
+ * © Copyright Intel Corporation. All rights reserved.
+ * Intel Corporation, 2200 Mission College Boulevard,
+ * Santa Clara, CA 95052-8119, USA
+ */
+package com.intel.jndn.management.types;
+
+import net.named_data.jndn.encoding.tlv.Tlv;
+import java.nio.ByteBuffer;
+import net.named_data.jndn.ForwardingFlags;
+import net.named_data.jndn.encoding.EncodingException;
+import net.named_data.jndn.encoding.tlv.TlvDecoder;
+import net.named_data.jndn.encoding.tlv.TlvEncoder;
+import net.named_data.jndn.util.Blob;
+
+/**
+ * Represent a Route object from /localhost/nfd/rib/list; see
+ * http://redmine.named-data.net/projects/nfd/wiki/RibMgmt#RIB-Dataset for
+ * details.
+ *
+ * @author Andrew Brown <andrew.brown@intel.com>
+ */
+public class Route {
+
+ /**
+ * TLV type, see
+ * http://redmine.named-data.net/projects/nfd/wiki/RibMgmt#TLV-TYPE-assignments
+ */
+ public final static int TLV_ROUTE = 129;
+
+ /**
+ * Encode using a new TLV encoder.
+ *
+ * @return The encoded buffer.
+ */
+ public final Blob wireEncode() {
+ TlvEncoder encoder = new TlvEncoder();
+ wireEncode(encoder);
+ return new Blob(encoder.getOutput(), false);
+ }
+
+ /**
+ * Encode as part of an existing encode context.
+ *
+ * @param encoder
+ */
+ public final void wireEncode(TlvEncoder encoder) {
+ int saveLength = encoder.getLength();
+ encoder.writeOptionalNonNegativeIntegerTlv(Tlv.ControlParameters_ExpirationPeriod, faceId);
+ encoder.writeNonNegativeIntegerTlv(Tlv.ControlParameters_Flags, flags.getForwardingEntryFlags());
+ encoder.writeNonNegativeIntegerTlv(Tlv.ControlParameters_Cost, cost);
+ encoder.writeNonNegativeIntegerTlv(Tlv.ControlParameters_Origin, origin);
+ encoder.writeNonNegativeIntegerTlv(Tlv.ControlParameters_FaceId, faceId);
+ encoder.writeTypeAndLength(TLV_ROUTE, encoder.getLength() - saveLength);
+ }
+
+ /**
+ * Decode the input from its TLV format.
+ *
+ * @param input The input buffer to decode. This reads from position() to
+ * limit(), but does not change the position.
+ * @throws net.named_data.jndn.encoding.EncodingException
+ */
+ public final void wireDecode(ByteBuffer input) throws EncodingException {
+ TlvDecoder decoder = new TlvDecoder(input);
+ wireDecode(decoder);
+ }
+
+ /**
+ * Decode as part of an existing decode context.
+ *
+ * @param decoder
+ * @throws EncodingException
+ */
+ public final void wireDecode(TlvDecoder decoder) throws EncodingException {
+ int endOffset = decoder.readNestedTlvsStart(TLV_ROUTE);
+ this.faceId = (int) decoder.readNonNegativeIntegerTlv(Tlv.ControlParameters_FaceId);
+ this.origin = (int) decoder.readNonNegativeIntegerTlv(Tlv.ControlParameters_Origin);
+ this.cost = (int) decoder.readNonNegativeIntegerTlv(Tlv.ControlParameters_Cost);
+ this.flags.setForwardingEntryFlags((int) decoder.readNonNegativeIntegerTlv(Tlv.ControlParameters_Flags));
+ this.expirationPeriod = (int) decoder.readOptionalNonNegativeIntegerTlv(Tlv.ControlParameters_ExpirationPeriod, endOffset);
+ decoder.finishNestedTlvs(endOffset);
+ }
+
+ /**
+ * Get Face ID
+ *
+ * @return
+ */
+ public int getFaceId() {
+ return faceId;
+ }
+
+ /**
+ * Set Face ID
+ *
+ * @param faceId
+ */
+ public void setFaceId(int faceId) {
+ this.faceId = faceId;
+ }
+
+ /**
+ * Get origin
+ *
+ * @return
+ */
+ public int getOrigin() {
+ return origin;
+ }
+
+ /**
+ * Set origin
+ *
+ * @param origin
+ */
+ public void setOrigin(int origin) {
+ this.origin = origin;
+ }
+
+ /**
+ * Get cost
+ *
+ * @return
+ */
+ public int getCost() {
+ return cost;
+ }
+
+ /**
+ * Set cost
+ *
+ * @param cost
+ */
+ public void setCost(int cost) {
+ this.cost = cost;
+ }
+
+ /**
+ * Get flags
+ *
+ * @return
+ */
+ public ForwardingFlags getFlags() {
+ return flags;
+ }
+
+ /**
+ * Set flags
+ *
+ * @param flags
+ */
+ public void setFlags(ForwardingFlags flags) {
+ this.flags = flags;
+ }
+
+ /**
+ * Get expiration period
+ *
+ * @return
+ */
+ public double getExpirationPeriod() {
+ return expirationPeriod;
+ }
+
+ /**
+ * Set expiration period
+ *
+ * @param expirationPeriod
+ */
+ public void setExpirationPeriod(double expirationPeriod) {
+ this.expirationPeriod = expirationPeriod;
+ }
+
+ private int faceId = -1;
+ private int origin = -1;
+ private int cost = -1;
+ private ForwardingFlags flags = new ForwardingFlags();
+ private double expirationPeriod = -1.0;
+}
diff --git a/src/main/java/com/intel/jndn/management/types/StatusDataset.java b/src/main/java/com/intel/jndn/management/types/StatusDataset.java
new file mode 100644
index 0000000..0117ee7
--- /dev/null
+++ b/src/main/java/com/intel/jndn/management/types/StatusDataset.java
@@ -0,0 +1,50 @@
+/*
+ * File name: StatusDataset.java
+ *
+ * Purpose: Helper class to handle StatusDatasets, see
+ * http://redmine.named-data.net/projects/nfd/wiki/StatusDataset
+ *
+ * © Copyright Intel Corporation. All rights reserved.
+ * Intel Corporation, 2200 Mission College Boulevard,
+ * Santa Clara, CA 95052-8119, USA
+ */
+package com.intel.jndn.management.types;
+
+import java.util.ArrayList;
+import java.util.List;
+import net.named_data.jndn.encoding.EncodingException;
+import net.named_data.jndn.encoding.tlv.TlvDecoder;
+import net.named_data.jndn.util.Blob;
+
+/**
+ * Helper class to handle StatusDatasets, see
+ * http://redmine.named-data.net/projects/nfd/wiki/StatusDataset
+ *
+ * @author Andrew Brown <andrew.brown@intel.com>
+ */
+public class StatusDataset {
+
+ /**
+ * Decode multiple status entries as part of a StatusDataset, see
+ * http://redmine.named-data.net/projects/nfd/wiki/StatusDataset
+ *
+ * @param <T>
+ * @param statusDataset
+ * @param type
+ * @return
+ * @throws EncodingException
+ * @throws InstantiationException
+ * @throws IllegalAccessException
+ */
+ public static final <T extends Decodable> List<T> wireDecode(Blob statusDataset, Class<T> type) throws EncodingException, InstantiationException, IllegalAccessException {
+ List<T> entries = new ArrayList<>();
+ int endOffset = statusDataset.size();
+ TlvDecoder decoder = new TlvDecoder(statusDataset.buf());
+ while (decoder.getOffset() < endOffset) {
+ T entry = type.newInstance();
+ entry.wireDecode(decoder);
+ entries.add(entry);
+ }
+ return entries;
+ }
+}
diff --git a/src/test/java/com/intel/jndn/management/ControlResponseTest.java b/src/test/java/com/intel/jndn/management/ControlResponseTest.java
new file mode 100644
index 0000000..5d026e0
--- /dev/null
+++ b/src/test/java/com/intel/jndn/management/ControlResponseTest.java
@@ -0,0 +1,52 @@
+/*
+ * File name: ControlResponseTest.java
+ *
+ * Purpose: Test encoding/decoding of ControlResponses.
+ *
+ * © Copyright Intel Corporation. All rights reserved.
+ * Intel Corporation, 2200 Mission College Boulevard,
+ * Santa Clara, CA 95052-8119, USA
+ */
+package com.intel.jndn.management;
+
+import com.intel.jndn.management.types.ControlResponse;
+import junit.framework.Assert;
+import net.named_data.jndn.ControlParameters;
+import net.named_data.jndn.util.Blob;
+import org.junit.Test;
+
+/**
+ * Test encoding/decoding of ControlResponses.
+ *
+ * @author Andrew Brown <andrew.brown@intel.com>
+ */
+public class ControlResponseTest {
+
+ /**
+ * Test encoding/decoding
+ *
+ * @throws java.lang.Exception
+ */
+ @Test
+ public void testEncodeDecode() throws Exception {
+ ControlParameters parameters = new ControlParameters();
+ parameters.setFaceId(3);
+ ControlResponse response = new ControlResponse();
+ response.setStatusCode(404);
+ response.setStatusText("Not Found");
+ response.getBody().add(parameters);
+
+ // encode
+ Blob encoded = response.wireEncode();
+
+ // decode
+ ControlResponse decoded = new ControlResponse();
+ decoded.wireDecode(encoded.buf());
+
+ // test
+ Assert.assertEquals(response.getStatusCode(), decoded.getStatusCode());
+ Assert.assertEquals(response.getStatusText(), decoded.getStatusText());
+ Assert.assertEquals(response.getBody().size(), decoded.getBody().size());
+ Assert.assertEquals(response.getBody().get(0).getFaceId(), decoded.getBody().get(0).getFaceId());
+ }
+}
diff --git a/src/test/java/com/intel/jndn/management/FaceStatusTest.java b/src/test/java/com/intel/jndn/management/FaceStatusTest.java
index c4d5ba0..f0d3eaf 100644
--- a/src/test/java/com/intel/jndn/management/FaceStatusTest.java
+++ b/src/test/java/com/intel/jndn/management/FaceStatusTest.java
@@ -10,8 +10,11 @@
*/
package com.intel.jndn.management;
+import com.intel.jndn.management.types.StatusDataset;
+import com.intel.jndn.management.types.FaceStatus;
import com.intel.jndn.utils.Client;
import java.util.List;
+import junit.framework.Assert;
import net.named_data.jndn.Data;
import net.named_data.jndn.Face;
import net.named_data.jndn.Interest;
@@ -30,24 +33,55 @@
* @author Andrew Brown <andrew.brown@intel.com>
*/
public class FaceStatusTest {
-
+
private static final Logger logger = LogManager.getLogger();
/**
+ * Test encoding/decoding
+ *
+ * @throws java.lang.Exception
+ */
+ @Test
+ public void testEncodeDecode() throws Exception {
+ FaceStatus status = new FaceStatus();
+ status.setFaceId(42);
+ status.setUri("...");
+ status.setLocalUri("...");
+
+ // encode
+ Blob encoded = status.wireEncode();
+
+ // decode
+ FaceStatus decoded = new FaceStatus();
+ decoded.wireDecode(encoded.buf());
+
+ // test
+ Assert.assertEquals(status.getFaceId(), decoded.getFaceId());
+ Assert.assertEquals(status.getUri(), decoded.getUri());
+ Assert.assertEquals(status.getLocalUri(), decoded.getLocalUri());
+ Assert.assertEquals(status.getExpirationPeriod(), decoded.getExpirationPeriod());
+ Assert.assertEquals(status.getFaceScope(), decoded.getFaceScope());
+ Assert.assertEquals(status.getFacePersistency(), decoded.getFacePersistency());
+ Assert.assertEquals(status.getLinkType(), decoded.getLinkType());
+ Assert.assertEquals(status.getInBytes(), decoded.getInBytes());
+ Assert.assertEquals(status.getOutBytes(), decoded.getOutBytes());
+ }
+
+ /**
* Test of decode method, of class FaceStatus.
*
* @throws java.lang.Exception
*/
@Test
- public void testDecode() throws Exception {
+ public void testDecodeFakeData() throws Exception {
Data data = getFaceData(true);
- List<FaceStatus> results = FaceStatus.decode(data);
+ List<FaceStatus> results = StatusDataset.wireDecode(data.getContent(), FaceStatus.class);
assertTrue(results.size() > 4);
for (FaceStatus f : results) {
// the first face (face 1) should always be the internal face
- if (f.faceId == 1) {
- assertEquals("internal://", f.uri);
- assertEquals("internal://", f.localUri);
+ if (f.getFaceId() == 1) {
+ assertEquals("internal://", f.getUri());
+ assertEquals("internal://", f.getLocalUri());
}
}
}
@@ -58,15 +92,15 @@
* @param args
* @throws EncodingException
*/
- public static void main(String[] args) throws EncodingException {
+ public static void main(String[] args) throws Exception {
Data data = getFaceData(false);
- List<FaceStatus> results = FaceStatus.decode(data);
+ List<FaceStatus> results = StatusDataset.wireDecode(data.getContent(), FaceStatus.class);
assertTrue(results.size() > 4);
for (FaceStatus f : results) {
// the first face (face 1) should always be the internal face
- if (f.faceId == 1) {
- assertEquals("internal://", f.uri);
- assertEquals("internal://", f.localUri);
+ if (f.getFaceId() == 1) {
+ assertEquals("internal://", f.getUri());
+ assertEquals("internal://", f.getLocalUri());
}
}
}
diff --git a/src/test/java/com/intel/jndn/management/FibEntryTest.java b/src/test/java/com/intel/jndn/management/FibEntryTest.java
new file mode 100644
index 0000000..85ac273
--- /dev/null
+++ b/src/test/java/com/intel/jndn/management/FibEntryTest.java
@@ -0,0 +1,85 @@
+/*
+ * File name: FibEntryTest.java
+ *
+ * Purpose: Test encode/decode of FibEntry and NextHopRecord
+ *
+ * © Copyright Intel Corporation. All rights reserved.
+ * Intel Corporation, 2200 Mission College Boulevard,
+ * Santa Clara, CA 95052-8119, USA
+ */
+package com.intel.jndn.management;
+
+import com.intel.jndn.management.types.StatusDataset;
+import com.intel.jndn.management.types.NextHopRecord;
+import com.intel.jndn.management.types.FibEntry;
+import com.intel.jndn.utils.Client;
+import java.util.List;
+import junit.framework.Assert;
+import net.named_data.jndn.Data;
+import net.named_data.jndn.Face;
+import net.named_data.jndn.Interest;
+import net.named_data.jndn.Name;
+import net.named_data.jndn.encoding.EncodingException;
+import net.named_data.jndn.util.Blob;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import org.junit.Test;
+
+/**
+ * Test encode/decode of FibEntry and NextHopRecord
+ *
+ * @author Andrew Brown <andrew.brown@intel.com>
+ */
+public class FibEntryTest {
+
+ /**
+ * Test encoding/decoding
+ *
+ * @throws java.lang.Exception
+ */
+ @Test
+ public void testEncodeDecode() throws Exception {
+ NextHopRecord nextHopRecord = new NextHopRecord();
+ nextHopRecord.setFaceId(42);
+ nextHopRecord.setCost(100);
+ FibEntry entry = new FibEntry();
+ entry.setName(new Name("/fib/entry/test"));
+ entry.getRecords().add(nextHopRecord);
+
+ // encode
+ Blob encoded = entry.wireEncode();
+
+ // decode
+ FibEntry decoded = new FibEntry();
+ decoded.wireDecode(encoded.buf());
+
+ // test
+ Assert.assertEquals(entry.getName().toUri(), decoded.getName().toUri());
+ Assert.assertEquals(entry.getRecords().get(0).getFaceId(), decoded.getRecords().get(0).getFaceId());
+ Assert.assertEquals(entry.getRecords().get(0).getCost(), decoded.getRecords().get(0).getCost());
+ }
+
+ /**
+ * Integration test to run on actual system
+ *
+ * @param args
+ * @throws EncodingException
+ */
+ public static void main(String[] args) throws Exception {
+ Face forwarder = new Face("localhost");
+
+ // build management Interest packet; see http://redmine.named-data.net/projects/nfd/wiki/StatusDataset
+ Interest interest = new Interest(new Name("/localhost/nfd/fib/list"));
+ interest.setMustBeFresh(true);
+ interest.setChildSelector(Interest.CHILD_SELECTOR_RIGHT);
+ interest.setInterestLifetimeMilliseconds(2000.0);
+
+ // send packet
+ Data data = Client.getDefault().getSync(forwarder, interest);
+
+ // decode results
+ List<FibEntry> results = StatusDataset.wireDecode(data.getContent(), FibEntry.class);
+ assertTrue(results.size() > 0);
+ assertEquals("/localhost/nfd", results.get(0).getName().toUri());
+ }
+}
diff --git a/src/test/java/com/intel/jndn/management/RibEntryTest.java b/src/test/java/com/intel/jndn/management/RibEntryTest.java
new file mode 100644
index 0000000..96dd70b
--- /dev/null
+++ b/src/test/java/com/intel/jndn/management/RibEntryTest.java
@@ -0,0 +1,87 @@
+/*
+ * File name: RibEntryTest.java
+ *
+ * Purpose: Test encoding/decoding for RibEntry.
+ *
+ * © Copyright Intel Corporation. All rights reserved.
+ * Intel Corporation, 2200 Mission College Boulevard,
+ * Santa Clara, CA 95052-8119, USA
+ */
+package com.intel.jndn.management;
+
+import com.intel.jndn.management.types.StatusDataset;
+import com.intel.jndn.management.types.RibEntry;
+import com.intel.jndn.management.types.Route;
+import com.intel.jndn.utils.Client;
+import java.util.List;
+import junit.framework.Assert;
+import net.named_data.jndn.Data;
+import net.named_data.jndn.Face;
+import net.named_data.jndn.Interest;
+import net.named_data.jndn.Name;
+import net.named_data.jndn.encoding.EncodingException;
+import net.named_data.jndn.util.Blob;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import org.junit.Test;
+
+/**
+ * Test encoding/decoding for RibEntry.
+ *
+ * @author Andrew Brown <andrew.brown@intel.com>
+ */
+public class RibEntryTest {
+
+ /**
+ * Test encoding/decoding
+ *
+ * @throws java.lang.Exception
+ */
+ @Test
+ public void testEncodeDecode() throws Exception {
+ Route route = new Route();
+ route.setFaceId(42);
+ route.setCost(100);
+ route.setOrigin(0);
+ RibEntry entry = new RibEntry();
+ entry.setName(new Name("/rib/entry/test"));
+ entry.getRoutes().add(route);
+
+ // encode
+ Blob encoded = entry.wireEncode();
+
+ // decode
+ RibEntry decoded = new RibEntry();
+ decoded.wireDecode(encoded.buf());
+
+ // test
+ Assert.assertEquals(entry.getName().toUri(), decoded.getName().toUri());
+ Assert.assertEquals(entry.getRoutes().get(0).getFaceId(), decoded.getRoutes().get(0).getFaceId());
+ Assert.assertEquals(entry.getRoutes().get(0).getCost(), decoded.getRoutes().get(0).getCost());
+ Assert.assertEquals(entry.getRoutes().get(0).getOrigin(), decoded.getRoutes().get(0).getOrigin());
+ }
+
+ /**
+ * Integration test to run on actual system
+ *
+ * @param args
+ * @throws EncodingException
+ */
+ public static void main(String[] args) throws Exception {
+ Face forwarder = new Face("localhost");
+
+ // build management Interest packet; see http://redmine.named-data.net/projects/nfd/wiki/StatusDataset
+ Interest interest = new Interest(new Name("/localhost/nfd/rib/list"));
+ interest.setMustBeFresh(true);
+ interest.setChildSelector(Interest.CHILD_SELECTOR_RIGHT);
+ interest.setInterestLifetimeMilliseconds(2000.0);
+
+ // send packet
+ Data data = Client.getDefault().getSync(forwarder, interest);
+
+ // decode results
+ List<RibEntry> results = StatusDataset.wireDecode(data.getContent(), RibEntry.class);
+ assertTrue(results.size() > 0);
+ assertEquals("/localhost/nfd", results.get(0).getName().toUri());
+ }
+}