memfile_lease_mgr.cc 7.96 KB
Newer Older
Stephen Morris's avatar
Stephen Morris committed
1
// Copyright (C) 2012-2013 Internet Systems Consortium, Inc. ("ISC")
2 3 4 5 6 7 8 9 10 11 12 13 14
//
// 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.

15
#include <dhcpsrv/dhcpsrv_log.h>
16
#include <dhcpsrv/memfile_lease_mgr.h>
17
#include <exceptions/exceptions.h>
18

19
#include <iostream>
20 21 22

using namespace isc::dhcp;

23 24
Memfile_LeaseMgr::Memfile_LeaseMgr(const ParameterMap& parameters)
    : LeaseMgr(parameters) {
25
    LOG_WARN(dhcpsrv_logger, DHCPSRV_MEMFILE_WARNING);
26 27 28 29 30
}

Memfile_LeaseMgr::~Memfile_LeaseMgr() {
}

31
bool Memfile_LeaseMgr::addLease(const Lease4Ptr& lease) {
32 33
    LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
              DHCPSRV_MEMFILE_ADD_ADDR4).arg(lease->addr_.toText());
34

35 36 37 38 39 40
    if (getLease4(lease->addr_)) {
        // there is a lease with specified address already
        return (false);
    }
    storage4_.insert(lease);
    return (true);
41 42
}

Tomek Mrugalski's avatar
Tomek Mrugalski committed
43
bool Memfile_LeaseMgr::addLease(const Lease6Ptr& lease) {
44 45
    LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
              DHCPSRV_MEMFILE_ADD_ADDR6).arg(lease->addr_.toText());
46

47 48 49 50 51 52 53 54
    if (getLease6(lease->addr_)) {
        // there is a lease with specified address already
        return (false);
    }
    storage6_.insert(lease);
    return (true);
}

55
Lease4Ptr Memfile_LeaseMgr::getLease4(const isc::asiolink::IOAddress& addr) const {
56 57
    LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
              DHCPSRV_MEMFILE_GET_ADDR4).arg(addr.toText());
58

59 60 61
    typedef Lease4Storage::nth_index<0>::type SearchIndex;
    const SearchIndex& idx = storage4_.get<0>();
    Lease4Storage::iterator l = idx.find(addr);
62 63 64 65 66
    if (l == storage4_.end()) {
        return (Lease4Ptr());
    } else {
        return (*l);
    }
67 68
}

69
Lease4Collection Memfile_LeaseMgr::getLease4(const HWAddr& hwaddr) const {
70
    LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
71
              DHCPSRV_MEMFILE_GET_HWADDR).arg(hwaddr.toText());
72

73
    isc_throw(NotImplemented, "getLease4(HWaddr x) method not implemented yet");
74 75
}

76 77
Lease4Ptr Memfile_LeaseMgr::getLease4(const HWAddr& hwaddr,
                                      SubnetID subnet_id) const {
78 79
    LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
              DHCPSRV_MEMFILE_GET_SUBID_HWADDR).arg(subnet_id)
80 81
        .arg(hwaddr.toText());

82 83 84
    // We are going to use index #1 of the multi index container.
    // We define SearchIndex locally in this function because
    // currently only this function uses this index.
85
    typedef Lease4Storage::nth_index<1>::type SearchIndex;
86
    // Get the index.
87
    const SearchIndex& idx = storage4_.get<1>();
88 89 90 91
    // Try to find the lease using HWAddr and subnet id.
    SearchIndex::const_iterator lease =
        idx.find(boost::make_tuple(hwaddr.hwaddr_, subnet_id));
    // Lease was not found. Return empty pointer to the caller.
92 93 94 95
    if (lease == idx.end()) {
        return Lease4Ptr();
    }

96
    // Lease was found. Return it to the caller.
97
    return (*lease);
98 99
}

100
Lease4Collection Memfile_LeaseMgr::getLease4(const ClientId& clientid) const {
101 102
    LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
              DHCPSRV_MEMFILE_GET_CLIENTID).arg(clientid.toText());
103
    isc_throw(NotImplemented, "getLease4(ClientId) not implemented");
104
}
105

106
Lease4Ptr Memfile_LeaseMgr::getLease4(const ClientId& client_id,
107
                                      SubnetID subnet_id) const {
108 109
    LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
              DHCPSRV_MEMFILE_GET_SUBID_CLIENTID).arg(subnet_id)
110
              .arg(client_id.toText());
111

112 113 114
    // We are going to use index #2 of the multi index container.
    // We define SearchIndex locally in this function because
    // currently only this function uses this index.
115
    typedef Lease4Storage::nth_index<2>::type SearchIndex;
116
    // Get the index.
117
    const SearchIndex& idx = storage4_.get<2>();
118 119 120 121
    // Try to get the lease using client id and subnet id.
    SearchIndex::const_iterator lease =
        idx.find(boost::make_tuple(client_id.getClientId(), subnet_id));
    // Lease was not found. Return empty pointer to the caller.
122 123 124
    if (lease == idx.end()) {
        return Lease4Ptr();
    }
125
    // Lease was found. Return it to the caller.
126
    return (*lease);
127 128
}

129 130
Lease6Ptr Memfile_LeaseMgr::getLease6(
        const isc::asiolink::IOAddress& addr) const {
131 132
    LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
              DHCPSRV_MEMFILE_GET_ADDR6).arg(addr.toText());
133 134 135 136 137 138 139 140 141

    Lease6Storage::iterator l = storage6_.find(addr);
    if (l == storage6_.end()) {
        return (Lease6Ptr());
    } else {
        return (*l);
    }
}

142 143
Lease6Collection Memfile_LeaseMgr::getLease6(const DUID& duid,
                                             uint32_t iaid) const {
144 145
    LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
              DHCPSRV_MEMFILE_GET_IAID_DUID).arg(iaid).arg(duid.toText());
146

147 148 149
    return (Lease6Collection());
}

150 151
Lease6Ptr Memfile_LeaseMgr::getLease6(const DUID& duid, uint32_t iaid,
                                      SubnetID subnet_id) const {
152
    LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
153 154 155
              DHCPSRV_MEMFILE_GET_IAID_SUBID_DUID)
              .arg(iaid).arg(subnet_id).arg(duid.toText());

156 157 158 159 160 161 162 163 164 165 166 167
    // We are going to use index #1 of the multi index container.
    // We define SearchIndex locally in this function because
    // currently only this function uses this index.
    typedef Lease6Storage::nth_index<1>::type SearchIndex;
    // Get the index.
    const SearchIndex& idx = storage6_.get<1>();
    // Try to get the lease using the DUID, IAID and Subnet ID.
    SearchIndex::const_iterator lease =
        idx.find(boost::make_tuple(duid.getDuid(), iaid, subnet_id));
    // Lease was not found. Return empty pointer.
    if (lease == idx.end()) {
        return (Lease6Ptr());
168
    }
169 170
    // Lease was found, return it to the caller.
    return (*lease);
171 172
}

173
void Memfile_LeaseMgr::updateLease4(const Lease4Ptr& lease) {
174 175
    LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
              DHCPSRV_MEMFILE_UPDATE_ADDR4).arg(lease->addr_.toText());
176

177 178
}

179
void Memfile_LeaseMgr::updateLease6(const Lease6Ptr& lease) {
180 181
    LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
              DHCPSRV_MEMFILE_UPDATE_ADDR6).arg(lease->addr_.toText());
182

183 184 185

}

186
bool Memfile_LeaseMgr::deleteLease(const isc::asiolink::IOAddress& addr) {
187 188
    LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
              DHCPSRV_MEMFILE_DELETE_ADDR).arg(addr.toText());
189
    if (addr.isV4()) {
190 191 192 193 194 195 196 197 198
        // v4 lease
        Lease4Storage::iterator l = storage4_.find(addr);
        if (l == storage4_.end()) {
            // No such lease
            return (false);
        } else {
            storage4_.erase(l);
            return (true);
        }
199

200
    } else {
201
        // v6 lease
202 203 204 205 206 207 208 209
        Lease6Storage::iterator l = storage6_.find(addr);
        if (l == storage6_.end()) {
            // No such lease
            return (false);
        } else {
            storage6_.erase(l);
            return (true);
        }
210 211 212 213 214 215 216 217
    }
}

std::string Memfile_LeaseMgr::getDescription() const {
    return (std::string("This is a dummy memfile backend implementation.\n"
                        "It does not offer any useful lease management and its only\n"
                        "purpose is to test abstract lease manager API."));
}
218 219 220

void
Memfile_LeaseMgr::commit() {
221
    LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_MEMFILE_COMMIT);
222 223 224 225
}

void
Memfile_LeaseMgr::rollback() {
226 227
    LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
              DHCPSRV_MEMFILE_ROLLBACK);
228
}