Parcourir la source

[2490] Added option factory function that takes vector of strings.

Marcin Siodelski il y a 12 ans
Parent
commit
b24fd3ded0

+ 8 - 2
src/lib/dhcp/option_definition.cc

@@ -99,7 +99,7 @@ OptionDefinition::addRecordField(const DataType data_type) {
 OptionPtr
 OptionDefinition::optionFactory(Option::Universe u, uint16_t type,
                                 OptionBufferConstIter begin,
-                                OptionBufferConstIter end) {
+                                OptionBufferConstIter end) const {
     validate();
 
     if (type_ == BINARY_TYPE) {
@@ -138,10 +138,16 @@ OptionDefinition::optionFactory(Option::Universe u, uint16_t type,
 
 OptionPtr
 OptionDefinition::optionFactory(Option::Universe u, uint16_t type,
-                                const OptionBuffer& buf) {
+                                const OptionBuffer& buf) const {
     return (optionFactory(u, type, buf.begin(), buf.end()));
 }
 
+OptionPtr
+OptionDefinition::optionFactory(Option::Universe, uint16_t,
+                                const std::vector<std::string>&) const {
+    return (OptionPtr());
+}
+
 void
 OptionDefinition::sanityCheckUniverse(const Option::Universe expected_universe,
                                       const Option::Universe actual_universe) {

+ 27 - 2
src/lib/dhcp/option_definition.h

@@ -275,9 +275,10 @@ public:
     /// @param end end of the option buffer.
     ///
     /// @return instance of the DHCP option.
+    /// @todo list thrown exceptions.
     OptionPtr optionFactory(Option::Universe u, uint16_t type,
                             OptionBufferConstIter begin,
-                            OptionBufferConstIter end);
+                            OptionBufferConstIter end) const;
 
     /// @brief Option factory.
     ///
@@ -290,8 +291,32 @@ public:
     /// @param buf option buffer.
     ///
     /// @return instance of the DHCP option.
+    /// @todo list thrown exceptions.
     OptionPtr optionFactory(Option::Universe u, uint16_t type,
-                            const OptionBuffer& buf);
+                            const OptionBuffer& buf) const;
+
+    /// @brief Option factory.
+    ///
+    /// This function creates an instance of DHCP option using the vector
+    /// of strings which carry data values for option data fields.
+    /// The order of values in the vector corresponds to the order of data
+    /// fields in the option. The supplied string values are cast to
+    /// their actual data types which are determined based on the
+    /// option definition. If cast fails due to type mismatch, an exception
+    /// is thrown. This factory function can be used to create option
+    /// instance when user specified option value in the <b>comma separated
+    /// values</b> format in the configuration database. Provided string
+    /// must be tokenized into the vector of string values and this vector
+    /// can be supplied to this function.
+    ///
+    /// @param universe option universe (V4 or V6).
+    /// @param type option type.
+    /// @param values a vector of values to be used to set data for an option.
+    ///
+    /// @return instance of the DHCP option.
+    /// @todo list thrown exceptions.
+    OptionPtr optionFactory(Option::Universe u, uint16_t type,
+                            const std::vector<std::string>& values) const;
 
     /// @brief Factory to create option with address list.
     ///

+ 49 - 1
src/lib/dhcp/tests/option_definition_unittest.cc

@@ -135,7 +135,7 @@ TEST_F(OptionDefinitionTest, validate) {
                               static_cast<OptionDefinition::DataType>(OptionDefinition::UNKNOWN_TYPE
                                                                       + 2));
     EXPECT_THROW(opt_def3.validate(), isc::OutOfRange);
-    
+
     // Empty option name is not allowed.
     OptionDefinition opt_def4("", D6O_CLIENTID, "string");
     EXPECT_THROW(opt_def4.validate(), isc::BadValue);
@@ -197,6 +197,54 @@ TEST_F(OptionDefinitionTest, factoryAddrList6) {
     );
 }
 
+// This test checks that a vector of strings, holding IPv6 addresses,
+// can be used to create option instance with the optionFactory function.
+TEST_F(OptionDefinitionTest, factoryTokenizedAddrList6) {
+    OptionDefinition opt_def("OPTION_NIS_SERVERS", D6O_NIS_SERVERS,
+                             "ipv6_address", true);
+
+    // Create a vector of some V6 addresses.
+    std::vector<asiolink::IOAddress> addrs;
+    addrs.push_back(asiolink::IOAddress("2001:0db8::ff00:0042:8329"));
+    addrs.push_back(asiolink::IOAddress("2001:0db8::ff00:0042:2319"));
+    addrs.push_back(asiolink::IOAddress("::1"));
+    addrs.push_back(asiolink::IOAddress("::2"));
+
+    // Create a vector of strings representing addresses given above.
+    std::vector<std::string> addrs_str;
+    for (std::vector<asiolink::IOAddress>::const_iterator it = addrs.begin();
+         it != addrs.end(); ++it) {
+        addrs_str.push_back(it->toText());
+    }
+
+    // Create DHCPv6 option using the list of IPv6 addresses given in the
+    // string form.
+    OptionPtr option_v6;
+    ASSERT_NO_THROW(
+        option_v6 = opt_def.optionFactory(Option::V6, D6O_NIS_SERVERS,
+                                          addrs_str);
+    );
+    // This is temporary check to make this test pass until factory function is
+    // implemented and returns the pointer rather than NULL.
+    ASSERT_FALSE(option_v6);
+
+    /*    // Non-null pointer option is supposed to be returned and it
+    // should have Option6AddrLst type.
+    ASSERT_TRUE(option_v6);
+    ASSERT_TRUE(typeid(*option_v6) == typeid(Option6AddrLst));
+    // Cast to the actual option type to get IPv6 addresses from it.
+    boost::shared_ptr<Option6AddrLst> option_cast_v6 =
+        boost::static_pointer_cast<Option6AddrLst>(option_v6);
+    // Check that cast was successful.
+    ASSERT_TRUE(option_cast_v6);
+    // Get the list of parsed addresses from the option object.
+    std::vector<asiolink::IOAddress> addrs_returned =
+        option_cast_v6->getAddresses();
+    // Returned addresses must match the addresses that have been used to create
+    // the option instance.
+    EXPECT_TRUE(std::equal(addrs.begin(), addrs.end(), addrs_returned.begin())); */
+}
+
 TEST_F(OptionDefinitionTest, factoryAddrList4) {
     OptionDefinition opt_def("OPTION_NAME_SERVERS", D6O_NIS_SERVERS,
                              "ipv4-address", true);