memfile_lease_mgr.h 9.12 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

#ifndef MEMFILE_LEASE_MGR_H
#define MEMFILE_LEASE_MGR_H

18 19
#include <dhcpsrv/lease_mgr.h>

20 21
#include <boost/multi_index/indexed_by.hpp>
#include <boost/multi_index/member.hpp>
22 23
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index_container.hpp>
24

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
namespace isc {
namespace dhcp {

// This is a concrete implementation of a Lease database.
//
// It is for testing purposes only. It is NOT a production code.
//
// It does not do anything useful now, and is used for abstract LeaseMgr
// class testing. It may later evolve into more useful backend if the
// need arises. We can reuse code from memfile benchmark. See code in
// tests/tools/dhcp-ubench/memfile_bench.{cc|h}
class Memfile_LeaseMgr : public LeaseMgr {
public:

    /// @brief The sole lease manager constructor
    ///
    /// dbconfig is a generic way of passing parameters. Parameters
    /// are passed in the "name=value" format, separated by spaces.
    /// Values may be enclosed in double quotes, if needed.
    ///
45 46 47
    /// @param parameters A data structure relating keywords and values
    ///        concerned with the database.
    Memfile_LeaseMgr(const ParameterMap& parameters);
48 49 50 51 52 53

    /// @brief Destructor (closes file)
    virtual ~Memfile_LeaseMgr();

    /// @brief Adds an IPv4 lease.
    ///
Tomek Mrugalski's avatar
Tomek Mrugalski committed
54
    /// @todo Not implemented yet
55
    /// @param lease lease to be added
Tomek Mrugalski's avatar
Tomek Mrugalski committed
56
    virtual bool addLease(const Lease4Ptr& lease);
57 58 59 60

    /// @brief Adds an IPv6 lease.
    ///
    /// @param lease lease to be added
Tomek Mrugalski's avatar
Tomek Mrugalski committed
61
    virtual bool addLease(const Lease6Ptr& lease);
62 63 64

    /// @brief Returns existing IPv4 lease for specified IPv4 address.
    ///
Tomek Mrugalski's avatar
Tomek Mrugalski committed
65
    /// @todo Not implemented yet
66 67 68
    /// @param addr address of the searched lease
    ///
    /// @return a collection of leases
69
    virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress& addr) const;
70 71

    /// @brief Returns existing IPv4 lease for specific address and subnet
Tomek Mrugalski's avatar
Tomek Mrugalski committed
72 73
    ///
    /// @todo Not implemented yet
74 75 76 77
    /// @param addr address of the searched lease
    /// @param subnet_id ID of the subnet the lease must belong to
    ///
    /// @return smart pointer to the lease (or NULL if a lease is not found)
78
    virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress& addr,
79 80 81 82
                                SubnetID subnet_id) const;

    /// @brief Returns existing IPv4 leases for specified hardware address.
    ///
Tomek Mrugalski's avatar
Tomek Mrugalski committed
83 84
    /// @todo Not implemented yet
    ///
85 86 87 88 89 90 91 92 93 94 95 96 97
    /// Although in the usual case there will be only one lease, for mobile
    /// clients or clients with multiple static/fixed/reserved leases there
    /// can be more than one. Thus return type is a container, not a single
    /// pointer.
    ///
    /// @param hwaddr hardware address of the client
    ///
    /// @return lease collection
    virtual Lease4Collection getLease4(const HWAddr& hwaddr) const;

    /// @brief Returns existing IPv4 leases for specified hardware address
    ///        and a subnet
    ///
Tomek Mrugalski's avatar
Tomek Mrugalski committed
98 99
    /// @todo Not implemented yet
    ///
100 101 102 103 104 105 106 107 108 109 110 111
    /// There can be at most one lease for a given HW address in a single
    /// pool, so this method with either return a single lease or NULL.
    ///
    /// @param hwaddr hardware address of the client
    /// @param subnet_id identifier of the subnet that lease must belong to
    ///
    /// @return a pointer to the lease (or NULL if a lease is not found)
    virtual Lease4Ptr getLease4(const HWAddr& hwaddr,
                                SubnetID subnet_id) const;

    /// @brief Returns existing IPv4 lease for specified client-id
    ///
Tomek Mrugalski's avatar
Tomek Mrugalski committed
112 113
    /// @todo Not implemented yet
    ///
114 115 116 117 118 119 120 121
    /// @param clientid client identifier
    virtual Lease4Collection getLease4(const ClientId& clientid) const;

    /// @brief Returns existing IPv4 lease for specified client-id
    ///
    /// There can be at most one lease for a given HW address in a single
    /// pool, so this method with either return a single lease or NULL.
    ///
Tomek Mrugalski's avatar
Tomek Mrugalski committed
122 123
    /// @todo Not implemented yet
    ///
124 125 126 127 128 129 130 131 132 133 134 135
    /// @param clientid client identifier
    /// @param subnet_id identifier of the subnet that lease must belong to
    ///
    /// @return a pointer to the lease (or NULL if a lease is not found)
    virtual Lease4Ptr getLease4(const ClientId& clientid,
                                SubnetID subnet_id) const;

    /// @brief Returns existing IPv6 lease for a given IPv6 address.
    ///
    /// @param addr address of the searched lease
    ///
    /// @return smart pointer to the lease (or NULL if a lease is not found)
136
    virtual Lease6Ptr getLease6(const isc::asiolink::IOAddress& addr) const;
137 138 139

    /// @brief Returns existing IPv6 lease for a given DUID+IA combination
    ///
Tomek Mrugalski's avatar
Tomek Mrugalski committed
140 141
    /// @todo Not implemented yet
    ///
142 143 144 145
    /// @param duid client DUID
    /// @param iaid IA identifier
    ///
    /// @return collection of IPv6 leases
146
    virtual Lease6Collection getLease6(const DUID& duid, uint32_t iaid) const;
147 148 149

    /// @brief Returns existing IPv6 lease for a given DUID+IA combination
    ///
Tomek Mrugalski's avatar
Tomek Mrugalski committed
150 151
    /// @todo Not implemented yet
    ///
152 153 154 155 156
    /// @param duid client DUID
    /// @param iaid IA identifier
    /// @param subnet_id identifier of the subnet the lease must belong to
    ///
    /// @return smart pointer to the lease (or NULL if a lease is not found)
157
    virtual Lease6Ptr getLease6(const DUID& duid, uint32_t iaid, SubnetID subnet_id) const;
158 159 160

    /// @brief Updates IPv4 lease.
    ///
Tomek Mrugalski's avatar
Tomek Mrugalski committed
161 162
    /// @todo Not implemented yet
    ///
163 164 165
    /// @param lease4 The lease to be updated.
    ///
    /// If no such lease is present, an exception will be thrown.
166
    virtual void updateLease4(const Lease4Ptr& lease4);
167 168 169

    /// @brief Updates IPv4 lease.
    ///
Tomek Mrugalski's avatar
Tomek Mrugalski committed
170 171
    /// @todo Not implemented yet
    ///
172
    /// @param lease6 The lease to be updated.
173 174
    ///
    /// If no such lease is present, an exception will be thrown.
175
    virtual void updateLease6(const Lease6Ptr& lease6);
176 177 178 179 180 181

    /// @brief Deletes a lease.
    ///
    /// @param addr IPv4 address of the lease to be deleted.
    ///
    /// @return true if deletion was successful, false if no such lease exists
182
    virtual bool deleteLease4(const isc::asiolink::IOAddress& addr);
183 184 185 186 187 188

    /// @brief Deletes a lease.
    ///
    /// @param addr IPv4 address of the lease to be deleted.
    ///
    /// @return true if deletion was successful, false if no such lease exists
189
    virtual bool deleteLease6(const isc::asiolink::IOAddress& addr);
190

191 192 193 194 195 196 197 198 199
    /// @brief Return backend type
    ///
    /// Returns the type of the backend.
    ///
    /// @return Type of the backend.
    virtual std::string getType() const {
        return (std::string("memfile"));
    }

200 201
    /// @brief Returns backend name.
    ///
202 203 204 205 206 207 208
    /// As there is no variation, in this case we return the type of the
    /// backend.
    ///
    /// @return Name of the backend.
    virtual std::string getName() const {
        return ("memfile");
    }
209 210 211 212

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

    /// @brief Returns backend version.
218 219 220
    ///
    /// @return Version number as a pair of unsigned integers.  "first" is the
    ///         major version number, "second" the minor number.
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
    virtual std::pair<uint32_t, uint32_t> getVersion() const {
        return (std::make_pair(1, 0));
    }

    /// @brief Commit Transactions
    ///
    /// Commits all pending database operations.  On databases that don't
    /// support transactions, this is a no-op.
    virtual void commit();

    /// @brief Rollback Transactions
    ///
    /// Rolls back all pending database operations.  On databases that don't
    /// support transactions, this is a no-op.
    virtual void rollback();
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256

protected:

    typedef boost::multi_index_container< // this is a multi-index container...
    Lease6Ptr, // it will hold shared_ptr to leases6
        boost::multi_index::indexed_by< // and will be sorted by
            // IPv6 address that are unique. That particular key is a member
            // of the Lease6 structure, is of type IOAddress and can be accessed
            // by doing &Lease6::addr_
            boost::multi_index::ordered_unique< 
                boost::multi_index::member<Lease6, isc::asiolink::IOAddress, &Lease6::addr_> 
            >
        >
    > Lease6Storage; // Let the whole contraption be called Lease6Storage.

    Lease6Storage storage6_;
};

}; // end of isc::dhcp namespace
}; // end of isc namespace

257
#endif // MEMFILE_LEASE_MGR
258