Add strategy dataset retrieval and tests
diff --git a/src/main/java/com/intel/jndn/management/NFD.java b/src/main/java/com/intel/jndn/management/NFD.java
index c5c8f20..d1f0450 100644
--- a/src/main/java/com/intel/jndn/management/NFD.java
+++ b/src/main/java/com/intel/jndn/management/NFD.java
@@ -20,6 +20,7 @@
import com.intel.jndn.management.types.ForwarderStatus;
import com.intel.jndn.management.types.LocalControlHeader;
import com.intel.jndn.management.types.RibEntry;
+import com.intel.jndn.management.types.StrategyChoice;
import com.intel.jndn.utils.SimpleClient;
import com.intel.jndn.utils.SegmentedClient;
import java.io.IOException;
@@ -148,6 +149,21 @@
}
/**
+ * Retrieve the list of strategy choice entries from the NFD; calls
+ * /localhost/nfd/rib/list which requires a local Face (all non-local packets
+ * are dropped).
+ *
+ * @param forwarder only a localhost Face
+ * @return a list of strategy choice entries, i.e. routes, see
+ * http://redmine.named-data.net/projects/nfd/wiki/StrategyChoice.
+ * @throws java.lang.Exception
+ */
+ public static List<StrategyChoice> getStrategyList(Face forwarder) throws Exception {
+ Data data = retrieveDataSet(forwarder, new Name("/localhost/nfd/strategy-choice/list"));
+ return StatusDataset.wireDecode(data.getContent(), StrategyChoice.class);
+ }
+
+ /**
* Retrieve the {@link KeyLocator} for an NFD.
*
* @param forwarder only a localhost {@link Face}
@@ -442,7 +458,8 @@
*
* @param forwarder only a localhost Face
* @param prefix the {@link Name} prefix
- * @param strategy the {@link Name} of the strategy to set, e.g. /localhost/nfd/strategy/broadcast
+ * @param strategy the {@link Name} of the strategy to set, e.g.
+ * /localhost/nfd/strategy/broadcast
* @throws Exception if the command fails
*/
public static void setStrategy(Face forwarder, Name prefix, Name strategy) throws Exception {
diff --git a/src/main/java/com/intel/jndn/management/Strategies.java b/src/main/java/com/intel/jndn/management/Strategies.java
new file mode 100644
index 0000000..f8816be
--- /dev/null
+++ b/src/main/java/com/intel/jndn/management/Strategies.java
@@ -0,0 +1,30 @@
+/*
+ * jndn-management
+ * Copyright (c) 2015, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU Lesser General Public License,
+ * version 3, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
+ * more details.
+ */
+package com.intel.jndn.management;
+
+import net.named_data.jndn.Name;
+
+/**
+ * A reference list of the strategies available in NFD; should match
+ * <a href="http://redmine.named-data.net/projects/nfd/wiki/StrategyChoice#Strategy">
+ * http://redmine.named-data.net/projects/nfd/wiki/StrategyChoice#Strategy</a>
+ *
+ * @author Andrew Brown <andrew.brown@intel.com>
+ */
+public class Strategies {
+ public static final Name BEST_ROUTE = new Name("/localhost/nfd/strategy/best-route");
+ public static final Name BROADCAST = new Name("/localhost/nfd/strategy/broadcast");
+ public static final Name CLIENT_CONTROL = new Name("/localhost/nfd/strategy/client-control");
+ public static final Name NCC = new Name("/localhost/nfd/strategy/ncc");
+}
diff --git a/src/main/java/com/intel/jndn/management/types/StrategyChoice.java b/src/main/java/com/intel/jndn/management/types/StrategyChoice.java
new file mode 100644
index 0000000..962b699
--- /dev/null
+++ b/src/main/java/com/intel/jndn/management/types/StrategyChoice.java
@@ -0,0 +1,118 @@
+/*
+ * jndn-management
+ * Copyright (c) 2015, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU Lesser General Public License,
+ * version 3, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
+ * more details.
+ */
+package com.intel.jndn.management.types;
+
+import com.intel.jndn.management.EncodingHelper;
+import static com.intel.jndn.management.types.RibEntry.TLV_RIB_ENTRY;
+import java.nio.ByteBuffer;
+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 strategy choice entry.
+ *
+ * @see http://redmine.named-data.net/projects/nfd/wiki/StrategyChoice
+ * @author Andrew Brown <andrew.brown@intel.com>
+ */
+public class StrategyChoice implements Decodable {
+
+ /**
+ * TLV type, see
+ * <a href="http://redmine.named-data.net/projects/nfd/wiki/StrategyChoice#TLV-TYPE-assignments">http://redmine.named-data.net/projects/nfd/wiki/StrategyChoice#TLV-TYPE-assignments</a>
+ */
+ public final static int TLV_STRATEGY_CHOICE = 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();
+ EncodingHelper.encodeName(name, encoder);
+ EncodingHelper.encodeName(strategy, encoder);
+ encoder.writeTypeAndLength(TLV_STRATEGY_CHOICE, 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);
+ strategy = EncodingHelper.decodeName(decoder);
+ name = EncodingHelper.decodeName(decoder);
+ decoder.finishNestedTlvs(endOffset);
+ }
+
+ /**
+ * @return the {@link Name} of the prefix the strategy is applied to
+ */
+ public Name getName() {
+ return name;
+ }
+
+ /**
+ * @return the {@link Name} of the strategy
+ */
+ public Name getStrategy() {
+ return strategy;
+ }
+
+ /**
+ * @param name the {@link Name} to set
+ */
+ public void setName(Name name) {
+ this.name = name;
+ }
+
+ /**
+ * @param strategy the {@link Name} to set
+ */
+ public void setStrategy(Name strategy) {
+ this.strategy = strategy;
+ }
+
+ private Name name;
+ private Name strategy;
+}
diff --git a/src/test/java/com/intel/jndn/management/StrategyTestIT.java b/src/test/java/com/intel/jndn/management/StrategyTestIT.java
new file mode 100644
index 0000000..3c63dd7
--- /dev/null
+++ b/src/test/java/com/intel/jndn/management/StrategyTestIT.java
@@ -0,0 +1,56 @@
+/*
+ * jndn-management
+ * Copyright (c) 2015, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU Lesser General Public License,
+ * version 3, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
+ * more details.
+ */
+package com.intel.jndn.management;
+
+import com.intel.jndn.management.types.StrategyChoice;
+import com.intel.jndn.mock.MockKeyChain;
+import com.intel.jndn.utils.SegmentedServer;
+import java.util.List;
+import java.util.logging.Logger;
+import static junit.framework.Assert.assertEquals;
+import net.named_data.jndn.Face;
+import net.named_data.jndn.Name;
+import net.named_data.jndn.security.KeyChain;
+import org.junit.Test;
+
+/**
+ * Test strategy management on a real, local NFD
+ *
+ * @author Andrew Brown <andrew.brown@intel.com>
+ */
+public class StrategyTestIT {
+
+ private static final Logger logger = Logger.getLogger(StrategyTestIT.class.getName());
+ Name prefix;
+ Face face;
+
+ public StrategyTestIT() throws net.named_data.jndn.security.SecurityException {
+ this.prefix = new Name("/test/strategy");
+ this.face = new Face("localhost"); // strategy commands only available on localhost
+ KeyChain mockKeyChain = MockKeyChain.configure(new Name("/test/server"));
+ face.setCommandSigningInfo(mockKeyChain, mockKeyChain.getDefaultCertificateName());
+ }
+
+ @Test
+ public void testStrategySetUnset() throws Exception {
+ List<StrategyChoice> choices = NFD.getStrategyList(face);
+ int oldSize = choices.size();
+
+ NFD.setStrategy(face, prefix, Strategies.CLIENT_CONTROL);
+ choices = NFD.getStrategyList(face);
+ assertEquals(oldSize + 1, choices.size());
+
+ NFD.unsetStrategy(face, prefix);
+ }
+}
diff --git a/src/test/java/com/intel/jndn/management/types/StrategyChoiceTest.java b/src/test/java/com/intel/jndn/management/types/StrategyChoiceTest.java
new file mode 100644
index 0000000..7783bb1
--- /dev/null
+++ b/src/test/java/com/intel/jndn/management/types/StrategyChoiceTest.java
@@ -0,0 +1,56 @@
+/*
+ * jndn-management
+ * Copyright (c) 2015, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU Lesser General Public License,
+ * version 3, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
+ * more details.
+ */
+package com.intel.jndn.management.types;
+
+import com.intel.jndn.management.Strategies;
+import java.nio.ByteBuffer;
+import junit.framework.Assert;
+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;
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+/**
+ *
+ * @author Andrew Brown <andrew.brown@intel.com>
+ */
+public class StrategyChoiceTest {
+
+ public StrategyChoiceTest() {
+ }
+
+ /**
+ * Test of wireEncode method, of class StrategyChoice.
+ */
+ @Test
+ public void testEncodeDecode() throws EncodingException {
+ StrategyChoice choice = new StrategyChoice();
+ choice.setName(new Name("/a/b"));
+ choice.setStrategy(Strategies.NCC);
+
+ // encode
+ Blob encoded = choice.wireEncode();
+
+ // decode
+ StrategyChoice decoded = new StrategyChoice();
+ decoded.wireDecode(encoded.buf());
+
+ // test
+ Assert.assertEquals(choice.getName().toUri(), decoded.getName().toUri());
+ Assert.assertEquals(choice.getStrategy().toUri(), decoded.getStrategy().toUri());
+ }
+}