lp: keep LpPacket parsed at all times

refs #4156

Change-Id: I21701ab0da2abacc8a84d6ee1a72c2b414e6669a
diff --git a/src/lp/packet.cpp b/src/lp/packet.cpp
index 751faa6..5714547 100644
--- a/src/lp/packet.cpp
+++ b/src/lp/packet.cpp
@@ -37,54 +37,17 @@
   wireDecode(wire);
 }
 
-template<encoding::Tag TAG>
-size_t
-Packet::wireEncode(EncodingImpl<TAG>& encoder) const
-{
-  if (m_wire.hasWire()) {
-    return m_wire.size();
-  }
-
-  size_t length = 0;
-
-  for (const Block& element : boost::adaptors::reverse(m_wire.elements())) {
-    length += encoder.prependBlock(element);
-  }
-
-  length += encoder.prependVarNumber(length);
-  length += encoder.prependVarNumber(tlv::LpPacket);
-
-  return length;
-}
-
-template size_t
-Packet::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder) const;
-
-template size_t
-Packet::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const;
-
 Block
 Packet::wireEncode() const
 {
-  if (m_wire.hasWire()) {
-    return m_wire;
-  }
-
   // If no header or trailer, return bare network packet
   Block::element_container elements = m_wire.elements();
   if (elements.size() == 1 && elements.front().type() == FragmentField::TlvType::value) {
     elements.front().parse();
-    elements.front().elements().front().parse();
     return elements.front().elements().front();
   }
 
-  EncodingEstimator estimator;
-  size_t estimatedSize = wireEncode(estimator);
-
-  EncodingBuffer buffer(estimatedSize, 0);
-  wireEncode(buffer);
-
-  m_wire = buffer.block();
+  m_wire.encode();
   return m_wire;
 }
 
diff --git a/src/lp/packet.hpp b/src/lp/packet.hpp
index e43652d..b2e003e 100644
--- a/src/lp/packet.hpp
+++ b/src/lp/packet.hpp
@@ -46,13 +46,6 @@
   Packet(const Block& wire);
 
   /**
-   * \brief append packet to encoder
-   */
-  template<encoding::Tag TAG>
-  size_t
-  wireEncode(EncodingImpl<TAG>& encoder) const;
-
-  /**
    * \brief encode packet into wire format
    */
   Block
@@ -84,8 +77,6 @@
   size_t
   count() const
   {
-    m_wire.parse();
-
     return std::count_if(m_wire.elements_begin(), m_wire.elements_end(),
                          [] (const Block& block) {
                            return block.type() == FIELD::TlvType::value; });
@@ -99,8 +90,6 @@
   typename FIELD::ValueType
   get(size_t index = 0) const
   {
-    m_wire.parse();
-
     size_t count = 0;
     for (const Block& element : m_wire.elements()) {
       if (element.type() != FIELD::TlvType::value) {
@@ -123,8 +112,6 @@
   {
     std::vector<typename FIELD::ValueType> output;
 
-    m_wire.parse();
-
     for (const Block& element : m_wire.elements()) {
       if (element.type() != FIELD::TlvType::value) {
         continue;
@@ -180,11 +167,8 @@
   Packet&
   remove(size_t index = 0)
   {
-    m_wire.parse();
-
     size_t count = 0;
-    for (Block::element_const_iterator it = m_wire.elements_begin(); it != m_wire.elements_end();
-         ++it) {
+    for (auto it = m_wire.elements_begin(); it != m_wire.elements_end(); ++it) {
       if (it->type() == FIELD::TlvType::value) {
         if (count == index) {
           m_wire.erase(it);
@@ -204,7 +188,6 @@
   Packet&
   clear()
   {
-    m_wire.parse();
     m_wire.remove(FIELD::TlvType::value);
     return *this;
   }
diff --git a/tests/unit-tests/lp/packet.t.cpp b/tests/unit-tests/lp/packet.t.cpp
index 7929487..c173ed2 100644
--- a/tests/unit-tests/lp/packet.t.cpp
+++ b/tests/unit-tests/lp/packet.t.cpp
@@ -125,9 +125,13 @@
   BOOST_CHECK_NO_THROW(packet.add<FragmentField>(std::make_pair(frag.begin(), frag.end())));
   BOOST_CHECK_NO_THROW(packet.add<FragIndexField>(0));
   BOOST_CHECK_NO_THROW(packet.add<AckField>(2));
+  BOOST_REQUIRE_NO_THROW(packet.wireEncode());
   BOOST_CHECK_NO_THROW(packet.add<FragCountField>(1));
+  BOOST_REQUIRE_NO_THROW(packet.wireEncode());
   BOOST_CHECK_NO_THROW(packet.add<AckField>(4));
+  BOOST_REQUIRE_NO_THROW(packet.wireEncode());
   BOOST_CHECK_NO_THROW(packet.add<AckField>(3));
+  BOOST_REQUIRE_NO_THROW(packet.wireEncode());
   Block wire;
   BOOST_REQUIRE_NO_THROW(wire = packet.wireEncode());
   BOOST_CHECK_EQUAL_COLLECTIONS(expectedBlock, expectedBlock + sizeof(expectedBlock),