Commit 2cf3214c authored by Thomas Markwalder's avatar Thomas Markwalder
Browse files

[3030] Replaced use of boost::posix_time with isc::util::time_utilities.

parent ed2fb666
......@@ -21,8 +21,6 @@
namespace isc {
namespace d2 {
using namespace boost::posix_time;
/********************************* D2Dhcid ************************************/
D2Dhcid::D2Dhcid() {
......@@ -62,11 +60,19 @@ NameChangeRequest::NameChangeRequest(const NameChangeType change_type,
const bool forward_change, const bool reverse_change,
const std::string& fqdn, const std::string& ip_address,
const D2Dhcid& dhcid,
#if 0
const boost::posix_time::ptime& lease_expires_on,
#else
const uint64_t lease_expires_on,
#endif
const uint32_t lease_length)
: change_type_(change_type), forward_change_(forward_change),
reverse_change_(reverse_change), fqdn_(fqdn), ip_address_(ip_address),
#if 0
dhcid_(dhcid), lease_expires_on_(new ptime(lease_expires_on)),
#else
dhcid_(dhcid), lease_expires_on_(lease_expires_on),
#endif
lease_length_(lease_length), status_(ST_NEW) {
// Validate the contents. This will throw a NcrMessageError if anything
......@@ -243,10 +249,12 @@ NameChangeRequest::validateContent() {
isc_throw(NcrMessageError, "DHCID cannot be blank");
}
#if 0
// Validate lease expiration.
if (lease_expires_on_->is_not_a_date_time()) {
isc_throw(NcrMessageError, "Invalid value for lease_expires_on");
}
#endif
// Ensure the request specifies at least one direction to update.
if (!forward_change_ && !reverse_change_) {
......@@ -375,39 +383,19 @@ NameChangeRequest::setDhcid(isc::data::ConstElementPtr element) {
std::string
NameChangeRequest::getLeaseExpiresOnStr() const {
if (!lease_expires_on_) {
// This is a programmatic error, should not happen.
isc_throw(NcrMessageError,
"lease_expires_on_ is null, cannot convert to string");
}
// Return the ISO date-time string for the value of lease_expires_on_.
return (to_iso_string(*lease_expires_on_));
return (isc::util::timeToText64(lease_expires_on_));
}
void
NameChangeRequest::setLeaseExpiresOn(const std::string& value) {
try {
// Create a new ptime instance from the ISO date-time string in value
// add assign it to lease_expires_on_.
ptime* tptr = new ptime(from_iso_string(value));
lease_expires_on_.reset(tptr);
lease_expires_on_ = isc::util::timeFromText64(value);
} catch(...) {
// We were given an invalid string, so throw.
isc_throw(NcrMessageError,
"Invalid ISO date-time string: [" << value << "]");
}
}
void
NameChangeRequest::setLeaseExpiresOn(const boost::posix_time::ptime& value) {
if (lease_expires_on_->is_not_a_date_time()) {
isc_throw(NcrMessageError, "Invalid value for lease_expires_on");
"Invalid date-time string: [" << value << "]");
}
// Go to go, make the assignment.
lease_expires_on_.reset(new ptime(value));
}
void NameChangeRequest::setLeaseExpiresOn(isc::data::ConstElementPtr element) {
......
......@@ -24,8 +24,7 @@
#include <exceptions/exceptions.h>
#include <util/buffer.h>
#include <util/encode/hex.h>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <util/time_utilities.h>
#include <time.h>
#include <string>
......@@ -105,16 +104,6 @@ private:
std::vector<uint8_t> bytes_;
};
/// @brief Defines a pointer to a ptime.
/// NameChangeRequest member(s) that are timestamps are ptime instances.
/// Boost ptime was chosen because it supports converting to and from ISO
/// strings in GMT. The Unix style time.h classes convert to GMT but
/// conversion back assumes local time. This is problematic if the "wire"
/// format is string (i.e. JSON) and the request were to cross time zones.
/// Additionally, time_t values should never be used directly so shipping them
/// as string integers across platforms could theoretically be a problem.
typedef boost::shared_ptr<boost::posix_time::ptime> TimePtr;
class NameChangeRequest;
/// @brief Defines a pointer to a NameChangeRequest.
typedef boost::shared_ptr<NameChangeRequest> NameChangeRequestPtr;
......@@ -146,7 +135,7 @@ public:
/// updated.
/// @param ip_address the ip address leased to the given FQDN.
/// @param dhcid the lease client's unique DHCID.
/// @param lease_expires_on a timestamp containing the date/time the lease
/// @param lease_expires_on a timestamp containing the date/time the lease
/// expires.
/// @param lease_length the amount of time in seconds for which the
/// lease is valid (TTL).
......@@ -154,7 +143,7 @@ public:
const bool forward_change, const bool reverse_change,
const std::string& fqdn, const std::string& ip_address,
const D2Dhcid& dhcid,
const boost::posix_time::ptime& lease_expires_on,
const uint64_t lease_expires_on,
const uint32_t lease_length);
/// @brief Static method for creating a NameChangeRequest from a
......@@ -366,10 +355,11 @@ public:
/// or there is an odd number of digits.
void setDhcid(isc::data::ConstElementPtr element);
/// @brief Fetches the request lease expiration as a timestamp.
/// @brief Fetches the request lease expiration
///
/// @return returns a pointer to the ptime containing the lease expiration
const TimePtr& getLeaseExpiresOn() const {
/// @return returns the lease expiration as the number of seconds since
/// the (00:00:00 January 1, 1970)
uint64_t getLeaseExpiresOn() const {
return (lease_expires_on_);
}
......@@ -377,39 +367,33 @@ public:
///
/// The format of the string returned is:
///
/// YYYYMMDDTHHMMSS where T is the date-time separator
/// YYYYMMDDHHmmSS
///
/// Example: 18:54:54 June 26, 2013 would be: 20130626T185455
/// Example: 18:54:54 June 26, 2013 would be: 20130626185455
/// NOTE This is always UTC time.
///
/// @return returns a ISO date-time string of the lease expiration.
std::string getLeaseExpiresOnStr() const;
/// @brief Sets the lease expiration to given ptime value.
///
/// @param value is the ptime value to assign to the lease expiration.
///
/// @throw throws a NcrMessageError if the value is not a valid
/// timestamp.
void setLeaseExpiresOn(const boost::posix_time::ptime& value);
/// @brief Sets the lease expiration based on the given string.
///
/// @param value is an ISO date-time string from which to set the
/// @param value is an date-time string from which to set the
/// lease expiration. The format of the input is:
///
/// YYYYMMDDTHHMMSS where T is the date-time separator
/// YYYYMMDDHHmmSS
///
/// Example: 18:54:54 June 26, 2013 would be: 20130626T185455
/// Example: 18:54:54 June 26, 2013 would be: 20130626185455
/// NOTE This is always UTC time.
///
/// @throw throws a NcrMessageError if the ISO string is invalid.
void setLeaseExpiresOn(const std::string& value);
/// @brief Sets the lease expiration based on the given Element.
///
/// @param element is string Element containing an ISO date-time string.
/// @param element is string Element containing a date-time string.
///
/// @throw throws a NcrMessageError if the element is not a string
/// Element, or if the element value is an invalid ISO date-time string.
/// Element, or if the element value is an invalid date-time string.
void setLeaseExpiresOn(isc::data::ConstElementPtr element);
/// @brief Fetches the request lease length.
......@@ -487,7 +471,7 @@ private:
D2Dhcid dhcid_;
/// @brief The date-time the lease expires.
TimePtr lease_expires_on_;
uint64_t lease_expires_on_;
/// @brief The amount of time in seconds for which the lease is valid (TTL).
uint32_t lease_length_;
......
......@@ -13,18 +13,15 @@
// PERFORMANCE OF THIS SOFTWARE.
#include <d2/ncr_msg.h>
#include <util/time_utilities.h>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <gtest/gtest.h>
#include <algorithm>
using namespace std;
using namespace isc;
using namespace isc::d2;
using namespace boost::posix_time;
namespace {
/// @brief Defines a list of valid JSON NameChangeRequest renditions.
......@@ -39,7 +36,7 @@ const char *valid_msgs[] =
" \"fqdn\" : \"walah.walah.com\" , "
" \"ip_address\" : \"192.168.2.1\" , "
" \"dhcid\" : \"010203040A7F8E3D\" , "
" \"lease_expires_on\" : \"19620121T132405\" , "
" \"lease_expires_on\" : \"20130121132405\" , "
" \"lease_length\" : 1300 "
"}",
// Valid Remove.
......@@ -50,7 +47,7 @@ const char *valid_msgs[] =
" \"fqdn\" : \"walah.walah.com\" , "
" \"ip_address\" : \"192.168.2.1\" , "
" \"dhcid\" : \"010203040A7F8E3D\" , "
" \"lease_expires_on\" : \"19620121T132405\" , "
" \"lease_expires_on\" : \"20130121132405\" , "
" \"lease_length\" : 1300 "
"}",
// Valid Add with IPv6 address
......@@ -61,7 +58,7 @@ const char *valid_msgs[] =
" \"fqdn\" : \"walah.walah.com\" , "
" \"ip_address\" : \"fe80::2acf:e9ff:fe12:e56f\" , "
" \"dhcid\" : \"010203040A7F8E3D\" , "
" \"lease_expires_on\" : \"19620121T132405\" , "
" \"lease_expires_on\" : \"20130121132405\" , "
" \"lease_length\" : 1300 "
"}"
};
......@@ -78,7 +75,7 @@ const char *invalid_msgs[] =
" \"fqdn\" : \"walah.walah.com\" , "
" \"ip_address\" : \"192.168.2.1\" , "
" \"dhcid\" : \"010203040A7F8E3D\" , "
" \"lease_expires_on\" : \"19620121T132405\" , "
" \"lease_expires_on\" : \"20130121132405\" , "
" \"lease_length\" : 1300 "
"}",
// Invalid forward change.
......@@ -89,7 +86,7 @@ const char *invalid_msgs[] =
" \"fqdn\" : \"walah.walah.com\" , "
" \"ip_address\" : \"192.168.2.1\" , "
" \"dhcid\" : \"010203040A7F8E3D\" , "
" \"lease_expires_on\" : \"19620121T132405\" , "
" \"lease_expires_on\" : \"20130121132405\" , "
" \"lease_length\" : 1300 "
"}",
// Invalid reverse change.
......@@ -100,7 +97,7 @@ const char *invalid_msgs[] =
" \"fqdn\" : \"walah.walah.com\" , "
" \"ip_address\" : \"192.168.2.1\" , "
" \"dhcid\" : \"010203040A7F8E3D\" , "
" \"lease_expires_on\" : \"19620121T132405\" , "
" \"lease_expires_on\" : \"20130121132405\" , "
" \"lease_length\" : 1300 "
"}",
// Forward and reverse change both false.
......@@ -111,7 +108,7 @@ const char *invalid_msgs[] =
" \"fqdn\" : \"walah.walah.com\" , "
" \"ip_address\" : \"192.168.2.1\" , "
" \"dhcid\" : \"010203040A7F8E3D\" , "
" \"lease_expires_on\" : \"19620121T132405\" , "
" \"lease_expires_on\" : \"20130121132405\" , "
" \"lease_length\" : 1300 "
"}",
// Blank FQDN
......@@ -122,7 +119,7 @@ const char *invalid_msgs[] =
" \"fqdn\" : \"\" , "
" \"ip_address\" : \"192.168.2.1\" , "
" \"dhcid\" : \"010203040A7F8E3D\" , "
" \"lease_expires_on\" : \"19620121T132405\" , "
" \"lease_expires_on\" : \"20130121132405\" , "
" \"lease_length\" : 1300 "
"}",
// Bad IP address
......@@ -133,7 +130,7 @@ const char *invalid_msgs[] =
" \"fqdn\" : \"walah.walah.com\" , "
" \"ip_address\" : \"xxxxxx\" , "
" \"dhcid\" : \"010203040A7F8E3D\" , "
" \"lease_expires_on\" : \"19620121T132405\" , "
" \"lease_expires_on\" : \"20130121132405\" , "
" \"lease_length\" : 1300 "
"}",
// Blank DHCID
......@@ -144,7 +141,7 @@ const char *invalid_msgs[] =
" \"fqdn\" : \"walah.walah.com\" , "
" \"ip_address\" : \"192.168.2.1\" , "
" \"dhcid\" : \"\" , "
" \"lease_expires_on\" : \"19620121T132405\" , "
" \"lease_expires_on\" : \"20130121132405\" , "
" \"lease_length\" : 1300 "
"}",
// Odd number of digits in DHCID
......@@ -155,7 +152,7 @@ const char *invalid_msgs[] =
" \"fqdn\" : \"walah.walah.com\" , "
" \"ip_address\" : \"192.168.2.1\" , "
" \"dhcid\" : \"010203040A7F8E3\" , "
" \"lease_expires_on\" : \"19620121T132405\" , "
" \"lease_expires_on\" : \"20130121132405\" , "
" \"lease_length\" : 1300 "
"}",
// Text in DHCID
......@@ -166,7 +163,7 @@ const char *invalid_msgs[] =
" \"fqdn\" : \"walah.walah.com\" , "
" \"ip_address\" : \"192.168.2.1\" , "
" \"dhcid\" : \"THIS IS BOGUS!!!\" , "
" \"lease_expires_on\" : \"19620121T132405\" , "
" \"lease_expires_on\" : \"20130121132405\" , "
" \"lease_length\" : 1300 "
"}",
// Invalid lease expiration string
......@@ -188,7 +185,7 @@ const char *invalid_msgs[] =
" \"fqdn\" : \"walah.walah.com\" , "
" \"ip_address\" : \"192.168.2.1\" , "
" \"dhcid\" : \"010203040A7F8E3D\" , "
" \"lease_expires_on\" : \"19620121T132405\" , "
" \"lease_expires_on\" : \"20130121132405\" , "
" \"lease_length\" : \"BOGUS\" "
"}"
......@@ -201,8 +198,7 @@ const char *invalid_msgs[] =
/// 3. "Full" constructor, given a blank FQDN fails
/// 4. "Full" constructor, given an invalid IP Address FQDN fails
/// 5. "Full" constructor, given a blank DHCID fails
/// 6. "Full" constructor, given an invalid lease expiration fails
/// 7. "Full" constructor, given false for both forward and reverse fails
/// 6. "Full" constructor, given false for both forward and reverse fails
TEST(NameChangeRequestTest, constructionTests) {
// Verify the default constructor works.
NameChangeRequestPtr ncr;
......@@ -210,7 +206,7 @@ TEST(NameChangeRequestTest, constructionTests) {
EXPECT_TRUE(ncr);
// Verify that full constructor works.
ptime expiry(second_clock::universal_time());
uint64_t expiry = isc::util::detail::gettimeWrapper();
D2Dhcid dhcid("010203040A7F8E3D");
EXPECT_NO_THROW(ncr.reset(new NameChangeRequest(
......@@ -232,11 +228,6 @@ TEST(NameChangeRequestTest, constructionTests) {
EXPECT_THROW(NameChangeRequest(CHG_ADD, true, true, "walah.walah.com",
"192.168.1.101", blank_dhcid, expiry, 1300), NcrMessageError);
// Verify that an invalid lease expiration is detected.
ptime blank_expiry;
EXPECT_THROW(NameChangeRequest(CHG_ADD, true, true, "valid.fqdn",
"192.168.1.101", dhcid, blank_expiry, 1300), NcrMessageError);
// Verify that one or both of direction flags must be true.
EXPECT_THROW(NameChangeRequest(CHG_ADD, false, false, "valid.fqdn",
"192.168.1.101", dhcid, expiry, 1300), NcrMessageError);
......@@ -282,24 +273,6 @@ TEST(NameChangeRequestTest, dhcidTest) {
EXPECT_EQ(test_str, next_str);
}
/// @brief Tests that boost::posix_time library functions as expected.
/// This test verifies that converting ptimes to and from ISO strings
/// works properly. This test is perhaps unnecessary but just to avoid any
/// OS specific surprises it is better safe than sorry.
TEST(NameChangeRequestTest, boostTime) {
// Create a ptime with the time now.
ptime pt1(second_clock::universal_time());
// Get the ISO date-time string.
std::string pt1_str = to_iso_string(pt1);
// Use the ISO date-time string to create a new ptime.
ptime pt2 = from_iso_string(pt1_str);
// Verify the two times are equal.
EXPECT_EQ (pt1, pt2);
}
/// @brief Verifies the fundamentals of converting from and to JSON.
/// It verifies that:
/// 1. A NameChangeRequest can be created from a valid JSON string.
......@@ -313,7 +286,7 @@ TEST(NameChangeRequestTest, basicJsonTest) {
"\"fqdn\":\"walah.walah.com\","
"\"ip_address\":\"192.168.2.1\","
"\"dhcid\":\"010203040A7F8E3D\","
"\"lease_expires_on\":\"19620121T132405\","
"\"lease_expires_on\":\"20130121132405\","
"\"lease_length\":1300"
"}";
......@@ -394,7 +367,7 @@ TEST(NameChangeRequestTest, toFromBufferTest) {
"\"fqdn\":\"walah.walah.com\","
"\"ip_address\":\"192.168.2.1\","
"\"dhcid\":\"010203040A7F8E3D\","
"\"lease_expires_on\":\"19620121T132405\","
"\"lease_expires_on\":\"20130121132405\","
"\"lease_length\":1300"
"}";
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment