// *********************** DCfgContextBase *************************
-DCfgContextBase::DCfgContextBase():
- boolean_values_(new BooleanStorage()),
- uint32_values_(new Uint32Storage()),
- string_values_(new StringStorage()) {
+DCfgContextBase::DCfgContextBase() {
}
-DCfgContextBase::DCfgContextBase(const DCfgContextBase& rhs):
- boolean_values_(new BooleanStorage(*(rhs.boolean_values_))),
- uint32_values_(new Uint32Storage(*(rhs.uint32_values_))),
- string_values_(new StringStorage(*(rhs.string_values_))) {
-}
-
-const data::Element::Position&
-DCfgContextBase::getParam(const std::string& name, bool& value, bool optional) {
- try {
- value = boolean_values_->getParam(name);
- return (boolean_values_->getPosition(name));
- } catch (DhcpConfigError& ex) {
- // If the parameter is not optional, re-throw the exception.
- if (!optional) {
- throw;
- }
- }
-
- return (data::Element::ZERO_POSITION());
-}
-
-const data::Element::Position&
-DCfgContextBase::getParam(const std::string& name, uint32_t& value,
- bool optional) {
- try {
- value = uint32_values_->getParam(name);
- return (uint32_values_->getPosition(name));
- } catch (DhcpConfigError& ex) {
- // If the parameter is not optional, re-throw the exception.
- if (!optional) {
- throw;
- }
- }
-
- return (data::Element::ZERO_POSITION());
-}
-
-const data::Element::Position&
-DCfgContextBase::getParam(const std::string& name, std::string& value,
- bool optional) {
- try {
- value = string_values_->getParam(name);
- return (string_values_->getPosition(name));
- } catch (DhcpConfigError& ex) {
- // If the parameter is not optional, re-throw the exception.
- if (!optional) {
- throw;
- }
- }
-
- return (data::Element::ZERO_POSITION());
-}
-
DCfgContextBase::~DCfgContextBase() {
}
///
class DCfgContextBase : public ConfigBase {
public:
- /// @brief Indicator that a configuration parameter is optional.
- static const bool OPTIONAL = true;
- static const bool REQUIRED = false;
/// @brief Constructor
DCfgContextBase();
/// @brief Destructor
virtual ~DCfgContextBase();
- /// @brief Fetches the value for a given boolean configuration parameter
- /// from the context.
- ///
- /// @param name is the name of the parameter to retrieve.
- /// @param value is an output parameter in which to return the retrieved
- /// value.
- /// @param optional if true, the parameter is optional and the method
- /// will not throw if the parameter is not found in the context. The
- /// contents of the output parameter, value, will not be altered.
- /// It defaults to false if not specified.
- ///
- /// @return The parameter's element's position information if found,
- /// otherwise it returns isc::data::Element::ZERO_POSITION().
- ///
- /// @throw throws DhcpConfigError if the context does not contain the
- /// parameter and optional is false.
- const data::Element::Position&
- getParam(const std::string& name, bool& value, bool optional=false);
-
- /// @brief Fetches the value for a given uint32_t configuration parameter
- /// from the context.
- ///
- /// @param name is the name of the parameter to retrieve.
- /// @param value is an output parameter in which to return the retrieved
- /// value.
- /// @param optional if true, the parameter is optional and the method
- /// will not throw if the parameter is not found in the context. The
- /// contents of the output parameter, value, will not be altered.
- ///
- /// @return The parameter's element's position information if found,
- /// otherwise it returns isc::data::Element::ZERO_POSITION().
- ///
- /// @throw throws DhcpConfigError if the context does not contain the
- /// parameter and optional is false.
- const data::Element::Position&
- getParam(const std::string& name, uint32_t& value,
- bool optional=false);
-
- /// @brief Fetches the value for a given string configuration parameter
- /// from the context.
- ///
- /// @param name is the name of the parameter to retrieve.
- /// @param value is an output parameter in which to return the retrieved
- /// value.
- /// @param optional if true, the parameter is optional and the method
- /// will not throw if the parameter is not found in the context. The
- /// contents of the output parameter, value, will not be altered.
- ///
- /// @return The parameter's element's position information if found,
- /// otherwise it returns isc::data::Element::ZERO_POSITION().
- ///
- /// @throw throws DhcpConfigError if the context does not contain the
- /// parameter and optional is false.
- const data::Element::Position&
- getParam(const std::string& name, std::string& value,
- bool optional=false);
-
- /// @brief Fetches the Boolean Storage. Typically used for passing
- /// into parsers.
- ///
- /// @return returns a pointer to the Boolean Storage.
- isc::dhcp::BooleanStoragePtr getBooleanStorage() {
- return (boolean_values_);
- }
-
- /// @brief Fetches the uint32 Storage. Typically used for passing
- /// into parsers.
- ///
- /// @return returns a pointer to the uint32 Storage.
- isc::dhcp::Uint32StoragePtr getUint32Storage() {
- return (uint32_values_);
- }
-
- /// @brief Fetches the string Storage. Typically used for passing
- /// into parsers.
- ///
- /// @return returns a pointer to the string Storage.
- isc::dhcp::StringStoragePtr getStringStorage() {
- return (string_values_);
- }
-
/// @brief Creates a clone of this context object.
///
/// As mentioned in the the class brief, derivation must supply an
/// the initial configuration object
virtual isc::data::ElementPtr toElement() const = 0;
-protected:
- /// @brief Copy constructor for use by derivations in clone().
- DCfgContextBase(const DCfgContextBase& rhs);
-
private:
/// @brief Private assignment operator to avoid potential for slicing.
DCfgContextBase& operator=(const DCfgContextBase& rhs);
-
- /// @brief Storage for boolean parameters.
- isc::dhcp::BooleanStoragePtr boolean_values_;
-
- /// @brief Storage for uint32 parameters.
- isc::dhcp::Uint32StoragePtr uint32_values_;
-
- /// @brief Storage for string parameters.
- isc::dhcp::StringStoragePtr string_values_;
};
/// @brief Defines a sequence of Element IDs used to specify a parsing order.
DStubContextPtr context = getStubContext();
ASSERT_TRUE(context);
- // Verify that the boolean parameter was parsed correctly by retrieving
- // its value from the context.
- bool actual_bool = false;
- EXPECT_NO_THROW(context->getParam("bool_test", actual_bool));
- EXPECT_EQ(true, actual_bool);
-
- // Verify that the uint32 parameter was parsed correctly by retrieving
- // its value from the context.
- uint32_t actual_uint32 = 0;
- EXPECT_NO_THROW(context->getParam("uint32_test", actual_uint32));
- EXPECT_EQ(77, actual_uint32);
-
- // Verify that the string parameter was parsed correctly by retrieving
- // its value from the context.
- std::string actual_string = "";
- EXPECT_NO_THROW(context->getParam("string_test", actual_string));
- EXPECT_EQ("hmmm chewy", actual_string);
-
- isc::data::ConstElementPtr object;
- EXPECT_NO_THROW(context->getObjectParam("map_test", object));
- EXPECT_TRUE(object);
-
- EXPECT_NO_THROW(context->getObjectParam("list_test", object));
- EXPECT_TRUE(object);
-
// Create a configuration which "updates" all of the parameter values.
string config2 = "{ \"bool_test\": false , "
" \"uint32_test\": 88 , "
EXPECT_TRUE(checkAnswer(0));
context = getStubContext();
ASSERT_TRUE(context);
-
- // Verify that the boolean parameter was updated correctly by retrieving
- // its value from the context.
- actual_bool = true;
- EXPECT_NO_THROW(context->getParam("bool_test", actual_bool));
- EXPECT_FALSE(actual_bool);
-
- // Verify that the uint32 parameter was updated correctly by retrieving
- // its value from the context.
- actual_uint32 = 0;
- EXPECT_NO_THROW(context->getParam("uint32_test", actual_uint32));
- EXPECT_EQ(88, actual_uint32);
-
- // Verify that the string parameter was updated correctly by retrieving
- // its value from the context.
- actual_string = "";
- EXPECT_NO_THROW(context->getParam("string_test", actual_string));
- EXPECT_EQ("ewww yuk!", actual_string);
-
- // Verify previous objects are not there.
- EXPECT_THROW(context->getObjectParam("map_test", object),
- isc::dhcp::DhcpConfigError);
- EXPECT_THROW(context->getObjectParam("list_test", object),
- isc::dhcp::DhcpConfigError);
-
- // Verify new map object is there.
- EXPECT_NO_THROW(context->getObjectParam("map_test2", object));
- EXPECT_TRUE(object);
-
- // Verify new list object is there.
- EXPECT_NO_THROW(context->getObjectParam("list_test2", object));
- EXPECT_TRUE(object);
}
/// @brief Tests that the configuration context is preserved after failure
DStubContextPtr context = getStubContext();
ASSERT_TRUE(context);
- // Verify that all of parameters have the expected values.
- bool actual_bool = false;
- EXPECT_NO_THROW(context->getParam("bool_test", actual_bool));
- EXPECT_EQ(true, actual_bool);
-
- uint32_t actual_uint32 = 0;
- EXPECT_NO_THROW(context->getParam("uint32_test", actual_uint32));
- EXPECT_EQ(77, actual_uint32);
-
- std::string actual_string = "";
- EXPECT_NO_THROW(context->getParam("string_test", actual_string));
- EXPECT_EQ("hmmm chewy", actual_string);
-
- isc::data::ConstElementPtr object;
- EXPECT_NO_THROW(context->getObjectParam("map_test", object));
- EXPECT_TRUE(object);
-
- EXPECT_NO_THROW(context->getObjectParam("list_test", object));
- EXPECT_TRUE(object);
-
// Create a configuration which "updates" all of the parameter values
// plus one unknown at the end.
string config2 = "{ \"bool_test\": false , "
EXPECT_TRUE(checkAnswer(1));
context = getStubContext();
ASSERT_TRUE(context);
-
- // Verify that all of parameters have the original values.
- actual_bool = false;
- EXPECT_NO_THROW(context->getParam("bool_test", actual_bool));
- EXPECT_EQ(true, actual_bool);
-
- actual_uint32 = 0;
- EXPECT_NO_THROW(context->getParam("uint32_test", actual_uint32));
- EXPECT_EQ(77, actual_uint32);
-
- actual_string = "";
- EXPECT_NO_THROW(context->getParam("string_test", actual_string));
- EXPECT_EQ("hmmm chewy", actual_string);
-
- EXPECT_NO_THROW(context->getObjectParam("map_test", object));
- EXPECT_TRUE(object);
-
- EXPECT_NO_THROW(context->getObjectParam("list_test", object));
- EXPECT_TRUE(object);
}
/// @brief Tests that the configuration context is preserved during
DStubContextPtr context = getStubContext();
ASSERT_TRUE(context);
- // Verify that all of parameters have the expected values.
- bool actual_bool = false;
- EXPECT_NO_THROW(context->getParam("bool_test", actual_bool));
- EXPECT_EQ(true, actual_bool);
-
- uint32_t actual_uint32 = 0;
- EXPECT_NO_THROW(context->getParam("uint32_test", actual_uint32));
- EXPECT_EQ(77, actual_uint32);
-
- std::string actual_string = "";
- EXPECT_NO_THROW(context->getParam("string_test", actual_string));
- EXPECT_EQ("hmmm chewy", actual_string);
-
- isc::data::ConstElementPtr object;
- EXPECT_NO_THROW(context->getObjectParam("map_test", object));
- EXPECT_TRUE(object);
-
- EXPECT_NO_THROW(context->getObjectParam("list_test", object));
- EXPECT_TRUE(object);
// Create a configuration which "updates" all of the parameter values.
string config2 = "{ \"bool_test\": false , "
context = getStubContext();
ASSERT_TRUE(context);
- // Verify that all of parameters have the original values.
- actual_bool = false;
- EXPECT_NO_THROW(context->getParam("bool_test", actual_bool));
- EXPECT_EQ(true, actual_bool);
-
- actual_uint32 = 0;
- EXPECT_NO_THROW(context->getParam("uint32_test", actual_uint32));
- EXPECT_EQ(77, actual_uint32);
-
- actual_string = "";
- EXPECT_NO_THROW(context->getParam("string_test", actual_string));
- EXPECT_EQ("hmmm chewy", actual_string);
-
- EXPECT_NO_THROW(context->getObjectParam("map_test", object));
- EXPECT_TRUE(object);
-
- EXPECT_NO_THROW(context->getObjectParam("list_test", object));
- EXPECT_TRUE(object);
}
// Tests that configuration element position is returned by getParam variants.
DStubContextPtr context = getStubContext();
ASSERT_TRUE(context);
- // Verify that the boolean parameter was parsed correctly by retrieving
- // its value from the context.
- bool actual_bool = false;
- isc::data::Element::Position pos;
- EXPECT_NO_THROW(pos = context->getParam("bool_test", actual_bool));
- EXPECT_EQ(true, actual_bool);
- EXPECT_EQ(1, pos.line_);
-
- // Verify that the uint32 parameter was parsed correctly by retrieving
- // its value from the context.
- uint32_t actual_uint32 = 0;
- EXPECT_NO_THROW(pos = context->getParam("uint32_test", actual_uint32));
- EXPECT_EQ(77, actual_uint32);
- EXPECT_EQ(2, pos.line_);
-
- // Verify that the string parameter was parsed correctly by retrieving
- // its value from the context.
- std::string actual_string = "";
- EXPECT_NO_THROW(pos = context->getParam("string_test", actual_string));
- EXPECT_EQ("hmmm chewy", actual_string);
- EXPECT_EQ(3, pos.line_);
-
- // Verify that an optional parameter that is not defined, returns the
- // zero position.
- pos = isc::data::Element::ZERO_POSITION();
- EXPECT_NO_THROW(pos = context->getParam("bogus_value",
- actual_string, true));
- EXPECT_EQ(pos.file_, isc::data::Element::ZERO_POSITION().file_);
}
// This tests if some aspects of simpleParseConfig are behaving properly.
time_duration elapsed_time;
runWithConfig("{ \"string_test\": \"first value\" }", 500, elapsed_time);
- // Context is still available post launch. Check to see that our
- // configuration value is still the original value.
- std::string actual_value = "";
- ASSERT_NO_THROW(getContext()->getParam("string_test", actual_value));
- EXPECT_EQ("first value", actual_value);
-
// Verify that we saw the signal.
std::vector<int>& signals = controller_->getProcessedSignals();
ASSERT_EQ(1, signals.size());
time_duration elapsed_time;
runWithConfig("{ \"string_test\": \"first value\" }", 500, elapsed_time);
- // Context is still available post launch. Check to see that our
- // configuration value is still the original value.
- std::string actual_value = "";
- ASSERT_NO_THROW(getContext()->getParam("string_test", actual_value));
- EXPECT_EQ("alt value", actual_value);
-
// Verify that we saw the signal.
std::vector<int>& signals = controller_->getProcessedSignals();
ASSERT_EQ(1, signals.size());
time_duration elapsed_time;
runWithConfig("{ \"string_test\": \"first value\" }", 800, elapsed_time);
- // Context is still available post launch.
- // Check to see that our configuration value is what we expect.
- std::string actual_value = "";
- ASSERT_NO_THROW(getContext()->getParam("string_test", actual_value));
- EXPECT_EQ("second value", actual_value);
-
// Verify that we saw two occurrences of the signal.
std::vector<int>& signals = controller_->getProcessedSignals();
ASSERT_EQ(2, signals.size());
//************************** DStubContext *************************
-DStubContext::DStubContext(): object_values_(new ObjectStorage()) {
+DStubContext::DStubContext() {
}
DStubContext::~DStubContext() {
}
-void
-DStubContext::getObjectParam(const std::string& name,
- isc::data::ConstElementPtr& value) {
- value = object_values_->getParam(name);
-}
-
-ObjectStoragePtr&
-DStubContext::getObjectStorage() {
- return (object_values_);
-}
-
DCfgContextBasePtr
DStubContext::clone() {
return (DCfgContextBasePtr(new DStubContext(*this)));
}
-DStubContext::DStubContext(const DStubContext& rhs): DCfgContextBase(rhs),
- object_values_(new ObjectStorage(*(rhs.object_values_))) {
+DStubContext::DStubContext(const DStubContext& rhs): DCfgContextBase(rhs) {
}
isc::data::ElementPtr
DStubContextPtr context
= boost::dynamic_pointer_cast<DStubContext>(getContext());
- if (element_id == "bool_test") {
- bool value = element->boolValue();
- context->getBooleanStorage()->setParam(element_id, value,
- element->getPosition());
- } else if (element_id == "uint32_test") {
- uint32_t value = element->intValue();
- context->getUint32Storage()->setParam(element_id, value,
- element->getPosition());
-
- } else if (element_id == "string_test") {
- std::string value = element->stringValue();
- context->getStringStorage()->setParam(element_id, value,
- element->getPosition());
- } else {
- // Fail only if SimFailure dictates we should. This makes it easier
- // to test parse ordering, by permitting a wide range of element ids
- // to "succeed" without specifically supporting them.
- if (SimFailure::shouldFailOn(SimFailure::ftElementUnknown)) {
- isc_throw(DCfgMgrBaseError,
- "Configuration parameter not supported: " << element_id
- << element->getPosition());
- }
-
- // Going to assume anything else is an object element.
- context->getObjectStorage()->setParam(element_id, element,
- element->getPosition());
+ // Fail only if SimFailure dictates we should. This makes it easier
+ // to test parse ordering, by permitting a wide range of element ids
+ // to "succeed" without specifically supporting them.
+ if (SimFailure::shouldFailOn(SimFailure::ftElementUnknown)) {
+ isc_throw(DCfgMgrBaseError,
+ "Configuration parameter not supported: " << element_id
+ << element->getPosition());
}
parsed_order_.push_back(element_id);
/// @return returns a pointer to the new clone.
virtual DCfgContextBasePtr clone();
- /// @brief Fetches the value for a given "extra" configuration parameter
- /// from the context.
- ///
- /// @param name is the name of the parameter to retrieve.
- /// @param value is an output parameter in which to return the retrieved
- /// value.
- /// @throw throws DhcpConfigError if the context does not contain the
- /// parameter.
- void getObjectParam(const std::string& name,
- isc::data::ConstElementPtr& value);
-
- ObjectStoragePtr& getObjectStorage();
-
protected:
/// @brief Copy constructor
DStubContext(const DStubContext& rhs);
/// @brief Private assignment operator, not implemented.
DStubContext& operator=(const DStubContext& rhs);
- /// @brief Stores non-scalar configuration elements
- ObjectStoragePtr object_values_;
-
/// @brief Unparse a configuration object
///
/// @return a pointer to a configuration