cql_host_data_source.h 14.1 KB
Newer Older
Andrei Pavel's avatar
Andrei Pavel committed
1
// Copyright (C) 2016-2017 Deutsche Telekom AG.
Andrei Pavel's avatar
Andrei Pavel committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
//
// Author: Andrei Pavel <andrei.pavel@qualitance.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//           http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef CQL_HOST_DATA_SOURCE_H
#define CQL_HOST_DATA_SOURCE_H

#include <dhcpsrv/base_host_data_source.h>
#include <dhcpsrv/cql_connection.h>

#include <string>
Andrei Pavel's avatar
Andrei Pavel committed
24
#include <vector>
Andrei Pavel's avatar
Andrei Pavel committed
25 26 27 28

namespace isc {
namespace dhcp {

Andrei Pavel's avatar
Andrei Pavel committed
29
/// @brief Forward declaration to the implementation of @ref CqlHostDataSource.
Andrei Pavel's avatar
Andrei Pavel committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
class CqlHostDataSourceImpl;

/// @brief Cassandra host data source
///
/// Implements @ref isc::dhcp::BaseHostDataSource interface customized to
/// Cassandra. Use of this backend implies that a Cassandra database is
/// available and that the Kea schema has been created within it.
class CqlHostDataSource : public BaseHostDataSource {
public:
    /// @brief Constructor
    ///
    /// Uses the following keywords in the parameters passed to it to connect
    /// to the database:
    /// - name - Name of the database to which to connect (mandatory)
    /// - host - Host to which to connect (optional, defaults to "localhost")
    /// - user - Username under which to connect (optional)
    /// - password - Password for "user" on the database (optional)
    ///
    /// If the database is successfully opened, the version number in the
    /// schema_version table will be checked against hard-coded value in the
    /// implementation file.
    ///
    /// Finally, all the CQL commands are pre-compiled.
    ///
Andrei Pavel's avatar
Andrei Pavel committed
54
    /// @param parameters a data structure relating keywords and values
Andrei Pavel's avatar
Andrei Pavel committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68
    ///        concerned with the database.
    ///
    /// @throw isc::dhcp::NoDatabaseName Mandatory database name not given
    /// @throw isc::dhcp::DbOpenError Error opening the database
    /// @throw isc::dhcp::DbOperationError An operation on the open database has
    ///        failed.
    explicit CqlHostDataSource(
        const DatabaseConnection::ParameterMap& parameters);

    /// @brief Virtual destructor.
    ///
    /// Releases prepared CQL statements used by the backend.
    virtual ~CqlHostDataSource();

Andrei Pavel's avatar
Andrei Pavel committed
69
    /// @brief Adds a new host to the collection.
Andrei Pavel's avatar
Andrei Pavel committed
70 71 72 73 74 75 76 77 78 79
    ///
    /// The implementations of this method should guard against duplicate
    /// reservations for the same @ref Host, where possible. For example, when
    /// the reservation for the same @ref HWAddr and @ref SubnetID is added
    /// twice, @ref add() should throw a @ref DuplicateEntry exception. Note,
    /// that usually it is impossible to guard against adding duplicated @ref
    /// Host, where one instance is identified by @ref HWAddr, another one by
    /// @ref DUID.
    ///
    /// @param host pointer to the new @ref Host being added.
Andrei Pavel's avatar
Andrei Pavel committed
80
    virtual void add(const HostPtr& host) override;
Andrei Pavel's avatar
Andrei Pavel committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

    /// @brief Retrieves a single @ref Host connected to an IPv4 subnet.
    ///
    /// Implementations of this method should guard against the case when
    /// multiple instances of the @ref Host are present, e.g. when two @ref
    /// Host objects are found, one for the @ref DUID, another one for the @ref
    /// HWAddr. In such case, throw a @ref MultipleRecords exception.
    ///
    /// @param subnet_id subnet identifier to filter by
    /// @param hwaddr hardware address of the client to filter by or NULL if not
    ///     available
    /// @param duid client identifier to filter by or NULL if not available
    ///
    /// @return @ref ConstHostPtr to a @ref Host object using a specified @ref
    ///     HWAddr or @ref DUID
    ///
    /// @throw BadValue if both or neither of subnet_id and duid are specified
    virtual ConstHostPtr get4(const SubnetID& subnet_id,
                              const HWAddrPtr& hwaddr,
Andrei Pavel's avatar
Andrei Pavel committed
100
                              const DuidPtr& duid = DuidPtr()) const override;
Andrei Pavel's avatar
Andrei Pavel committed
101 102 103 104 105

    /// @brief Retrieves a @ref Host connected to an IPv4 subnet.
    ///
    /// @param subnet_id subnet identifier to filter by
    /// @param identifier_type identifier type to filter by
Andrei Pavel's avatar
Andrei Pavel committed
106 107
    /// @param identifier_begin pointer to the beginning of a buffer containing
    ///     a host identifier to filter by
Andrei Pavel's avatar
Andrei Pavel committed
108 109 110 111 112 113 114
    /// @param identifier_len length of the host identifier buffer
    ///
    /// @return @ref Host object for which a reservation has been made using the
    ///     specified identifier
    virtual ConstHostPtr get4(const SubnetID& subnet_id,
                              const Host::IdentifierType& identifier_type,
                              const uint8_t* identifier_begin,
Andrei Pavel's avatar
Andrei Pavel committed
115
                              const size_t identifier_len) const override;
Andrei Pavel's avatar
Andrei Pavel committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129

    /// @brief Retrieves a @ref Host connected to an IPv4 subnet.
    ///
    /// Note that dynamically allocated addresses and reserved addresses can
    /// come into conflict. When the new address is assigned to a client, the
    /// allocation mechanism should check if this address is not reserved for
    /// some other @ref Host and not allocate it if a reservation is present.
    ///
    /// @param subnet_id Subnet identifier.
    /// @param address reserved IPv4 address.
    ///
    /// @return Const @ref Host object
    ///
    /// @throw BadValue if address in not a valid IPv4address
Andrei Pavel's avatar
Andrei Pavel committed
130 131 132
    virtual ConstHostPtr
    get4(const SubnetID& subnet_id,
         const asiolink::IOAddress& address) const override;
Andrei Pavel's avatar
Andrei Pavel committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148

    /// @brief Retrieves a @ref Host connected to an IPv6 subnet.
    ///
    /// Implementations of this method should guard against the case when
    /// multiple instances of the @ref Host are present, e.g. when two
    /// @ref Host objects are found, one for the @ref DUID, another one for the
    /// @ref HWAddr. In such case, throw a @ref MultipleRecords exception.
    ///
    /// @param subnet_id subnet identifier to filter by
    /// @param hwaddr hardware address of the client to filter by or NULL if not
    ///     available
    /// @param duid client identifier to filter by or NULL if not available
    ///
    /// @return @ref Host object using a specified @ref HWAddr or @ref DUID
    ///
    /// @throw BadValue if both or neither of subnet_id and duid are specified
Andrei Pavel's avatar
Andrei Pavel committed
149 150 151 152
    virtual ConstHostPtr
    get6(const SubnetID& subnet_id,
         const DuidPtr& duid,
         const HWAddrPtr& hwaddr = HWAddrPtr()) const override;
Andrei Pavel's avatar
Andrei Pavel committed
153 154 155 156 157

    /// @brief Returns a @ref Host connected to an IPv6 subnet.
    ///
    /// @param subnet_id subnet identifier to filter by
    /// @param identifier_type identifier type to filter by
Andrei Pavel's avatar
Andrei Pavel committed
158 159
    /// @param identifier_begin pointer to the beginning of a buffer containing
    ///     a host identifier to filter by
Andrei Pavel's avatar
Andrei Pavel committed
160 161 162 163 164 165 166
    /// @param identifier_len length of the host identifier buffer
    ///
    /// @return Const @ref Host object for which reservation has been made using
    /// the specified identifier.
    virtual ConstHostPtr get6(const SubnetID& subnet_id,
                              const Host::IdentifierType& identifier_type,
                              const uint8_t* identifier_begin,
Andrei Pavel's avatar
Andrei Pavel committed
167
                              const size_t identifier_len) const override;
Andrei Pavel's avatar
Andrei Pavel committed
168 169 170 171 172 173 174 175 176 177 178

    /// @brief Returns a @ref Host with the specified reservation prefix.
    ///
    /// @param prefix IPv6 prefix for which the @ref Host object is searched.
    /// @param prefix_len IPv6 prefix length.
    ///
    /// @return Const @ref Host object using a specified HW address or DUID.
    ///
    /// @throw MultipleRecords if two or more rows are returned from the
    ///     Cassandra database
    virtual ConstHostPtr get6(const asiolink::IOAddress& prefix,
Andrei Pavel's avatar
Andrei Pavel committed
179
                              const uint8_t prefix_len) const override;
Andrei Pavel's avatar
Andrei Pavel committed
180 181 182 183 184 185 186 187

    /// @brief Returns a host connected to the IPv6 subnet and having
    /// a reservation for a specified IPv6 address or prefix.
    ///
    /// @param subnet_id Subnet identifier.
    /// @param address reserved IPv6 address/prefix.
    ///
    /// @return Const @c Host object using a specified IPv6 address/prefix.
Andrei Pavel's avatar
Andrei Pavel committed
188 189 190
    virtual ConstHostPtr
    get6(const SubnetID& subnet_id,
         const asiolink::IOAddress& address) const override;
Andrei Pavel's avatar
Andrei Pavel committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213

    /// @brief Return all @ref Host objects for the specified @ref HWAddr or
    /// @ref DUID.
    ///
    /// Returns all @ref Host objects which represent reservations
    /// for the specified HW address or DUID. Note, that this method may
    /// return multiple reservations because a particular client may have
    /// reservations in multiple subnets and the same client may be identified
    /// by HW address or DUID. The server is unable to verify that the specific
    /// DUID and HW address belong to the same client, until the client sends
    /// a DHCP message.
    ///
    /// Specifying both @ref HWAddr and @ref DUID is allowed for this method
    /// and results in returning all objects that are associated with hardware
    /// address OR duid. For example: if one @ref Host is associated with the
    /// specified @ref HWAddr and another @ref Host is associated with the
    /// specified @ref DUID, two hosts will be returned.
    ///
    /// @param hwaddr HW address of the client or NULL if no HW address
    /// available.
    /// @param duid client id or NULL if not available, e.g. DHCPv4 client case.
    ///
    /// @return collection of const @ref Host objects.
Andrei Pavel's avatar
Andrei Pavel committed
214 215 216
    virtual ConstHostCollection
    getAll(const HWAddrPtr& hwaddr,
           const DuidPtr& duid = DuidPtr()) const override;
Andrei Pavel's avatar
Andrei Pavel committed
217 218 219 220 221 222 223 224 225 226

    /// @brief Return all hosts connected to any subnet for which reservations
    /// have been made using a specified identifier.
    ///
    /// This method returns all @ref Host objects which represent reservations
    /// for a specified identifier. This method may return multiple hosts
    /// because a particular client may have reservations in multiple subnets.
    ///
    /// @param identifier_type Identifier type.
    /// @param identifier_begin Pointer to a beginning of a buffer containing
Andrei Pavel's avatar
Andrei Pavel committed
227
    ///     an identifier.
Andrei Pavel's avatar
Andrei Pavel committed
228 229 230 231 232 233
    /// @param identifier_len Identifier length.
    ///
    /// @return Collection of const @ref Host objects.
    virtual ConstHostCollection
    getAll(const Host::IdentifierType& identifier_type,
           const uint8_t* identifier_begin,
Andrei Pavel's avatar
Andrei Pavel committed
234
           const size_t identifier_len) const override;
Andrei Pavel's avatar
Andrei Pavel committed
235 236 237 238 239 240 241 242 243 244

    /// @brief Returns a collection of hosts using the specified IPv4 address.
    ///
    /// This method may return multiple @ref Host objects if they are connected
    /// to different subnets.
    ///
    /// @param address IPv4 address for which the @ref Host object is searched.
    ///
    /// @return Collection of const @ref Host objects.
    virtual ConstHostCollection
Andrei Pavel's avatar
Andrei Pavel committed
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
    getAll4(const asiolink::IOAddress& address) const override;

    /// @brief Attempts to delete a host by (subnet-id, address)
    ///
    /// This method supports both v4 and v6.
    ///
    /// @param subnet_id subnet identfier.
    /// @param addr specified address.
    /// @return true if deletion was successful, false if the host was not
    ///     there.
    /// @throw various exceptions in case of errors
    virtual bool del(const SubnetID& subnet_id,
                     const asiolink::IOAddress& addr);

    /// @brief Attempts to delete a host by (subnet-id4, identifier,
    /// identifier-type)
    ///
    /// This method supports both v4 hosts only.
    ///
    /// @param subnet_id IPv4 Subnet identifier.
    /// @param identifier_type Identifier type.
    /// @param identifier_begin Pointer to a beginning of a buffer containing
    ///     an identifier.
    /// @param identifier_len Identifier length.
    /// @return true if deletion was successful, false if the host was not
    ///     there.
    /// @throw various exceptions in case of errors
    virtual bool del4(const SubnetID& subnet_id,
                      const Host::IdentifierType& identifier_type,
                      const uint8_t* identifier_begin,
                      const size_t identifier_len);

    /// @brief Attempts to delete a host by (subnet-id6, identifier,
    /// identifier-type)
    ///
    /// This method supports both v6 hosts only.
    ///
    /// @param subnet_id IPv6 Subnet identifier.
    /// @param identifier_type Identifier type.
    /// @param identifier_begin Pointer to a beginning of a buffer containing
    ///     an identifier.
    /// @param identifier_len Identifier length.
    /// @return true if deletion was successful, false if the host was not
    ///     there.
    /// @throw various exceptions in case of errors
    virtual bool del6(const SubnetID& subnet_id,
                      const Host::IdentifierType& identifier_type,
                      const uint8_t* identifier_begin,
                      const size_t identifier_len);
Andrei Pavel's avatar
Andrei Pavel committed
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309

    /// @brief Returns description of the backend.
    ///
    /// This description may be multiline text that describes the backend.
    ///
    /// @return Description of the backend.
    virtual std::string getDescription() const;

    /// @brief Returns the name of the database.
    ///
    /// @return database name
    virtual std::string getName() const;

    /// @brief Return backend type
    ///
    /// @return backend type "cql"
Andrei Pavel's avatar
Andrei Pavel committed
310
    virtual std::string getType() const override;
Andrei Pavel's avatar
Andrei Pavel committed
311 312 313 314 315 316 317 318 319

    /// @brief Retrieves schema version.
    ///
    /// @return Version number stored in the database, as a pair of unsigned
    ///         integers. "first" is the major version number, "second" is the
    ///         minor version number.
    ///
    /// @throw isc::dhcp::DbOperationError An operation on the open database
    ///        has failed.
Andrei Pavel's avatar
Andrei Pavel committed
320
    virtual VersionPair getVersion() const;
Andrei Pavel's avatar
Andrei Pavel committed
321 322 323 324

    /// @brief Commit Transactions
    ///
    /// Commits all pending database operations.
Andrei Pavel's avatar
Andrei Pavel committed
325
    virtual void commit() override;
Andrei Pavel's avatar
Andrei Pavel committed
326 327 328 329

    /// @brief Rollback Transactions
    ///
    /// Rolls back all pending database operations.
Andrei Pavel's avatar
Andrei Pavel committed
330
    virtual void rollback() override;
Andrei Pavel's avatar
Andrei Pavel committed
331 332 333 334 335 336

private:
    /// @brief Pointer to the implementation of the @ref CqlHostDataSource.
    CqlHostDataSourceImpl* impl_;
};  // class CqlHostDataSource

Andrei Pavel's avatar
Andrei Pavel committed
337 338
}  // namespace dhcp
}  // namespace isc
Andrei Pavel's avatar
Andrei Pavel committed
339 340

#endif  // CQL_HOST_DATA_SOURCE_H