nfd-control: Implementation of FibManagementOptions data structure

Change-Id: Ife5e0d3503be13896e2c9a7d02ac8f1801672603
refs: #1162
diff --git a/src/management/fib-management-options.hpp b/src/management/fib-management-options.hpp
new file mode 100644
index 0000000..db3f765
--- /dev/null
+++ b/src/management/fib-management-options.hpp
@@ -0,0 +1,140 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/**
+ * Copyright (C) 2013 Regents of the University of California.
+ *
+ * @author: Wentao Shang <wentao@cs.ucla.edu>
+ *
+ * See COPYING for copyright and distribution information.
+ */
+
+#ifndef NDN_FIB_MANAGEMENT_HPP
+#define NDN_FIB_MANAGEMENT_HPP
+
+#include "encoding/block.hpp"
+#include "encoding/tlv-nfd-control.hpp"
+#include "name.hpp"
+
+namespace ndn {
+
+class FibManagementOptions {
+public:
+  FibManagementOptions ()
+    : faceId_ (-1)
+    , cost_ (-1)
+  {
+  }
+  
+  const Name& 
+  getName () const { return name_; }
+  
+  void
+  setName (const Name &name) { name_ = name; wire_.reset (); }
+  
+  int 
+  getFaceId () const { return faceId_; }
+
+  void 
+  setFaceId (int faceId) { faceId_ = faceId; wire_.reset (); }
+
+  int 
+  getCost () const { return cost_; }
+
+  void 
+  setCost (int cost) { cost_ = cost; wire_.reset (); }
+
+  inline const Block&
+  wireEncode () const;
+  
+  inline void 
+  wireDecode (const Block &wire);
+  
+private:
+  Name name_;
+  int faceId_;
+  int cost_;
+  //Name strategy_;
+  //TODO: implement strategy after its use is properly defined
+
+  mutable Block wire_;
+};
+
+inline const Block&
+FibManagementOptions::wireEncode () const
+{
+  if (wire_.hasWire ())
+    return wire_;
+  
+  wire_ = Block (tlv::nfd_control::FibManagementOptions);
+
+  // Name
+  wire_.push_back (name_.wireEncode ());
+  
+  // FaceId
+  if (faceId_ != -1)
+    wire_.push_back (nonNegativeIntegerBlock (tlv::nfd_control::FaceId, faceId_));
+
+  // Cost
+  if (cost_ != -1)
+    wire_.push_back (nonNegativeIntegerBlock (tlv::nfd_control::Cost, cost_));
+
+  //TODO: Strategy
+  
+  wire_.encode ();
+  return wire_;
+}
+  
+inline void 
+FibManagementOptions::wireDecode (const Block &wire)
+{
+  name_.clear ();
+  faceId_ = -1;
+  cost_ = -1;
+
+  wire_ = wire;
+  wire_.parse ();
+
+  // Name
+  Block::element_iterator val = wire_.find(Tlv::Name);
+  if (val != wire_.getAll().end())
+    {
+      name_.wireDecode(*val);
+    }
+
+  // FaceID
+  val = wire_.find(tlv::nfd_control::FaceId);
+  if (val != wire_.getAll().end())
+    {
+      faceId_ = readNonNegativeInteger(*val);
+    }
+
+  // Cost
+  val = wire_.find(tlv::nfd_control::Cost);
+  if (val != wire_.getAll().end())
+    {
+      cost_ = readNonNegativeInteger(*val);
+    }
+
+  //TODO: Strategy
+}
+
+inline std::ostream&
+operator << (std::ostream &os, const FibManagementOptions &option)
+{
+  os << "ForwardingEntry(";
+  
+  // Name
+  os << "Prefix: " << option.getName () << ", ";
+
+  // FaceID
+  os << "FaceID: " << option.getFaceId () << ", ";
+
+  // Cost
+  os << "Cost: " << option.getCost ();
+
+  os << ")";
+  return os;
+}
+
+}
+
+#endif
diff --git a/tests/test-nfd-control.cpp b/tests/test-nfd-control.cpp
index 8e282a0..ae898f7 100644
--- a/tests/test-nfd-control.cpp
+++ b/tests/test-nfd-control.cpp
@@ -4,10 +4,10 @@
  * See COPYING for copyright and distribution information.
  */
 
-#include <boost/test/unit_test.hpp>
-
 #include "management/control-response.hpp"
+#include "management/fib-management-options.hpp"
 
+#include <boost/test/unit_test.hpp>
 #include <boost/test/output_test_stream.hpp>
 
 using namespace std;
@@ -18,6 +18,14 @@
 
 const uint8_t TestControlResponse[] = {0x65, 0x17, 0x66, 0x02, 0x01, 0x94, 0x67, 0x11, 0x4e, 0x6f, 0x74, 0x68, 0x69, 0x6e, 0x67, 0x20, 0x6e, 0x6f, 0x74, 0x20, 0x66, 0x6f, 0x75, 0x6e, 0x64};
 
+const uint8_t TestFibManagementOptions[] = {
+  0x68, 0x1e, 0x03, 0x16, 0x04, 0x09, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68,
+  0x6f, 0x73, 0x74, 0x04, 0x03, 0x72, 0x65, 0x67, 0x04, 0x04, 0x74, 0x65,
+  0x73, 0x74, 0x69, 0x01, 0x00, 0x6a, 0x01, 0x00
+};
+
+// ControlResponse
+
 BOOST_AUTO_TEST_CASE (ControlResponseEncode)
 {
   ndn::ControlResponse controlResponse(404, "Nothing not found");
@@ -37,6 +45,38 @@
   BOOST_REQUIRE_EQUAL(controlResponse.getText(), "Nothing not found");
 }
 
+// FibManagementOptions
+
+BOOST_AUTO_TEST_CASE (FibManagementOptionsEncoding)
+{
+  Name n ("/localhost/reg/test");
+  FibManagementOptions opt;
+
+  opt.setName (n);
+  opt.setFaceId (0);
+  opt.setCost (0);
+
+  const ndn::Block& blk = opt.wireEncode ();
+
+  BOOST_REQUIRE_EQUAL_COLLECTIONS (TestFibManagementOptions,
+                                   TestFibManagementOptions + sizeof (TestFibManagementOptions),
+                                   blk.begin (), blk.end ());
+}
+
+BOOST_AUTO_TEST_CASE (FibManagementOptionsDecoding)
+{
+  Block blk (TestFibManagementOptions, sizeof (TestFibManagementOptions));
+  Name n ("/localhost/reg/test");
+  FibManagementOptions opt;
+
+  BOOST_REQUIRE_NO_THROW (opt.wireDecode (blk));
+  
+  BOOST_CHECK_EQUAL (opt.getName (), n);
+  BOOST_CHECK_EQUAL (opt.getFaceId (), 0);
+  BOOST_CHECK_EQUAL (opt.getCost (), 0);
+}
+
+
 BOOST_AUTO_TEST_SUITE_END()
 
 } // namespace ndn