1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
|
// Copyright (C) 2018-2019 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <config.h>
#include <database/database_connection.h>
#include <test_config_backend_dhcp4.h>
#include <list>
using namespace isc::data;
using namespace isc::db;
namespace isc {
namespace dhcp {
namespace test {
bool
TestConfigBackendDHCPv4::registerBackendType(ConfigBackendDHCPv4Mgr& mgr,
const std::string& db_type) {
return(mgr.registerBackendFactory(db_type,
[](const db::DatabaseConnection::ParameterMap& params)
-> dhcp::ConfigBackendDHCPv4Ptr {
return (TestConfigBackendDHCPv4Ptr(new TestConfigBackendDHCPv4(params)));
})
);
}
void
TestConfigBackendDHCPv4::unregisterBackendType(ConfigBackendDHCPv4Mgr& mgr,
const std::string& db_type) {
mgr.unregisterBackendFactory(db_type);
}
Subnet4Ptr
TestConfigBackendDHCPv4::getSubnet4(const db::ServerSelector& /* server_selector */,
const std::string& subnet_prefix) const{
const auto& index = subnets_.get<SubnetPrefixIndexTag>();
auto subnet_it = index.find(subnet_prefix);
return ((subnet_it != index.cend()) ? (*subnet_it) : Subnet4Ptr());
}
Subnet4Ptr
TestConfigBackendDHCPv4::getSubnet4(const db::ServerSelector& /* server_selector */,
const SubnetID& subnet_id) const {
const auto& index = subnets_.get<SubnetSubnetIdIndexTag>();
auto subnet_it = index.find(subnet_id);
return ((subnet_it != index.cend()) ? (*subnet_it) : Subnet4Ptr());
}
Subnet4Collection
TestConfigBackendDHCPv4::getAllSubnets4(const db::ServerSelector& /* server_selector */) const {
return (subnets_);
}
Subnet4Collection
TestConfigBackendDHCPv4::getModifiedSubnets4(const db::ServerSelector& /* server_selector */,
const boost::posix_time::ptime& modification_time) const {
const auto& index = subnets_.get<SubnetModificationTimeIndexTag>();
Subnet4Collection subnets;
auto lb = index.lower_bound(modification_time);
for (auto subnet = lb; subnet != index.end(); ++subnet) {
subnets.push_back(*subnet);
}
return (subnets);
}
Subnet4Collection
TestConfigBackendDHCPv4::getSharedNetworkSubnets4(const db::ServerSelector& /* server_selector */,
const std::string& shared_network_name) const {
Subnet4Collection subnets;
// Subnet collection does not include the index by shared network name.
// We need to iterate over the subnets and pick those that are associated
// with a shared network.
for (auto subnet = subnets_.begin(); subnet != subnets_.end();
++subnet) {
// The subnet can be associated with a shared network instance or
// it may just point to the shared network name. The former is
// the case when the subnet belongs to the server configuration.
// The latter is the case when the subnet is fetched from the
// database.
SharedNetwork4Ptr network;
(*subnet)->getSharedNetwork(network);
if (((network && (network->getName() == shared_network_name)) ||
((*subnet)->getSharedNetworkName() == shared_network_name))) {
subnets.push_back(*subnet);
}
}
return (subnets);
}
SharedNetwork4Ptr
TestConfigBackendDHCPv4::getSharedNetwork4(const db::ServerSelector& /* server_selector */,
const std::string& name) const {
const auto& index = shared_networks_.get<SharedNetworkNameIndexTag>();
auto network_it = index.find(name);
return ((network_it != index.cend()) ? (*network_it) : SharedNetwork4Ptr());
}
SharedNetwork4Collection
TestConfigBackendDHCPv4::getAllSharedNetworks4(const db::ServerSelector& /* server_selector */) const{
return (shared_networks_);
}
SharedNetwork4Collection
TestConfigBackendDHCPv4::getModifiedSharedNetworks4(const db::ServerSelector& /* server_selector */,
const boost::posix_time::ptime& modification_time) const {
const auto& index = shared_networks_.get<SharedNetworkModificationTimeIndexTag>();
SharedNetwork4Collection shared_networks;
auto lb = index.lower_bound(modification_time);
for (auto shared_network = lb; shared_network != index.end(); ++shared_network) {
shared_networks.push_back(*shared_network);
}
return (shared_networks);
}
OptionDefinitionPtr
TestConfigBackendDHCPv4::getOptionDef4(const db::ServerSelector& /* server_selector */,
const uint16_t code,
const std::string& space) const {
const auto& index = option_defs_.get<1>();
auto option_def_it_pair = index.equal_range(code);
for (auto option_def_it = option_def_it_pair.first;
option_def_it != option_def_it_pair.second;
++option_def_it) {
if ((*option_def_it)->getOptionSpaceName() == space) {
return (*option_def_it);
}
}
return (OptionDefinitionPtr());
}
OptionDefContainer
TestConfigBackendDHCPv4::getAllOptionDefs4(const db::ServerSelector& /* server_selector */) const {
return (option_defs_);
}
OptionDefContainer
TestConfigBackendDHCPv4::getModifiedOptionDefs4(const db::ServerSelector& /* server_selector */,
const boost::posix_time::ptime& modification_time) const {
const auto& index = option_defs_.get<3>();
OptionDefContainer option_defs;
auto lb = index.lower_bound(modification_time);
for (auto option_def = lb; option_def != index.end(); ++option_def) {
option_defs.push_back(*option_def);
}
return (option_defs);
}
OptionDescriptorPtr
TestConfigBackendDHCPv4::getOption4(const db::ServerSelector& /* server_selector */,
const uint16_t code,
const std::string& space) const {
const auto& index = options_.get<1>();
auto option_it_pair = index.equal_range(code);
for (auto option_it = option_it_pair.first; option_it != option_it_pair.second;
++option_it) {
if (option_it->space_name_ == space) {
return (OptionDescriptorPtr(new OptionDescriptor(*option_it)));
}
}
return (OptionDescriptorPtr());
}
OptionContainer
TestConfigBackendDHCPv4::getAllOptions4(const db::ServerSelector& /* server_selector */) const {
return (options_);
}
OptionContainer
TestConfigBackendDHCPv4::getModifiedOptions4(const db::ServerSelector& /* server_selector */,
const boost::posix_time::ptime& modification_time) const {
const auto& index = options_.get<3>();
OptionContainer options;
auto lb = index.lower_bound(modification_time);
for (auto option = lb; option != index.end(); ++option) {
options.push_back(*option);
}
return (options);
}
StampedValuePtr
TestConfigBackendDHCPv4::getGlobalParameter4(const db::ServerSelector& server_selector,
const std::string& name) const {
const auto& index = globals_.get<StampedValueNameIndexTag>();
auto global_range = index.equal_range(name);
for (auto global_it = global_range.first; global_it != global_range.second;
++global_it) {
auto tags = server_selector.getTags();
for (auto tag : tags) {
if ((*global_it)->hasServerTag(ServerTag(tag))) {
return (*global_it);
}
}
}
auto global_all_it = index.find(name);
if ((global_all_it != index.end()) && ((*global_all_it)->hasAllServerTag())) {
return (*global_all_it);
}
return (StampedValuePtr());
}
StampedValueCollection
TestConfigBackendDHCPv4::getAllGlobalParameters4(const db::ServerSelector& /* server_selector */) const {
return (globals_);
}
StampedValueCollection
TestConfigBackendDHCPv4::getModifiedGlobalParameters4(const db::ServerSelector& /* server_selector */,
const boost::posix_time::ptime& modification_time) const {
const auto& index = globals_.get<StampedValueModificationTimeIndexTag>();
StampedValueCollection globals;
auto lb = index.lower_bound(modification_time);
for (auto global = lb; global != index.end(); ++global) {
globals.insert(*global);
}
return (globals);
}
AuditEntryCollection
TestConfigBackendDHCPv4::getRecentAuditEntries(const db::ServerSelector&,
const boost::posix_time::ptime&) const {
return (AuditEntryCollection());
}
ServerCollection
TestConfigBackendDHCPv4::getAllServers4() const {
return (servers_);
}
ServerPtr
TestConfigBackendDHCPv4::getServer4(const ServerTag& server_tag) const {
const auto& index = servers_.get<ServerTagIndexTag>();
auto server_it = index.find(server_tag.get());
return ((server_it != index.cend()) ? (*server_it) : ServerPtr());
}
void
TestConfigBackendDHCPv4::createUpdateSubnet4(const db::ServerSelector& server_selector,
const Subnet4Ptr& subnet) {
subnet->setServerTag(getServerTag(server_selector));
auto& index = subnets_.get<SubnetSubnetIdIndexTag>();
auto subnet_it = index.find(subnet->getID());
if (subnet_it != index.cend()) {
index.replace(subnet_it, subnet);
} else {
index.insert(subnet);
}
}
void
TestConfigBackendDHCPv4::createUpdateSharedNetwork4(const db::ServerSelector& server_selector,
const SharedNetwork4Ptr& shared_network) {
shared_network->setServerTag(getServerTag(server_selector));
auto& index = shared_networks_.get<SharedNetworkNameIndexTag>();
auto network_it = index.find(shared_network->getName());
if (network_it != index.cend()) {
index.replace(network_it, shared_network);
} else {
index.insert(shared_network);
}
}
void
TestConfigBackendDHCPv4::createUpdateOptionDef4(const db::ServerSelector& server_selector,
const OptionDefinitionPtr& option_def) {
option_def->setServerTag(getServerTag(server_selector));
auto& index = option_defs_.get<1>();
auto option_def_it = index.find(option_def->getCode());
if (option_def_it != index.cend()) {
index.replace(option_def_it, option_def);
} else {
index.insert(option_def);
}
}
void
TestConfigBackendDHCPv4::createUpdateOption4(const db::ServerSelector& server_selector,
const OptionDescriptorPtr& option) {
option->setServerTag(getServerTag(server_selector));
auto& index = options_.get<1>();
auto option_it = index.find(option->option_->getType());
if (option_it != index.end()) {
index.replace(option_it, *option);
} else {
index.insert(*option);
}
}
void
TestConfigBackendDHCPv4::createUpdateOption4(const db::ServerSelector& /* server_selector */,
const std::string& shared_network_name,
const OptionDescriptorPtr& option) {
auto& index = shared_networks_.get<SharedNetworkNameIndexTag>();
auto network_it = index.find(shared_network_name);
if (network_it != index.end()) {
auto shared_network = *network_it;
shared_network->getCfgOption()->del(option->space_name_, option->option_->getType());
shared_network->getCfgOption()->add(*option, option->space_name_);
} else {
isc_throw(BadValue, "attempted to create or update option in a non existing "
"shared network " << shared_network_name);
}
}
void
TestConfigBackendDHCPv4::createUpdateOption4(const db::ServerSelector& /* server_selector */,
const SubnetID& subnet_id,
const OptionDescriptorPtr& option) {
auto& index = subnets_.get<SubnetSubnetIdIndexTag>();
auto subnet_it = index.find(subnet_id);
if (subnet_it != index.cend()) {
auto subnet = *subnet_it;
subnet->getCfgOption()->del(option->space_name_, option->option_->getType());
subnet->getCfgOption()->add(*option, option->space_name_);
} else {
isc_throw(BadValue, "attempted to create or update option in a non existing "
"subnet ID " << subnet_id);
}
}
void
TestConfigBackendDHCPv4::createUpdateOption4(const db::ServerSelector& /* server_selector */,
const asiolink::IOAddress& pool_start_address,
const asiolink::IOAddress& pool_end_address,
const OptionDescriptorPtr& option) {
for (auto subnet = subnets_.begin(); subnet != subnets_.end(); ++subnet) {
auto pool = (*subnet)->getPool(Lease::TYPE_V4, pool_start_address);
if (pool) {
pool->getCfgOption()->del(option->space_name_, option->option_->getType());
pool->getCfgOption()->add(*option, option->space_name_);
return;
}
}
isc_throw(BadValue, "attempted to create or update option in a non existing "
"pool " << pool_start_address << " - " << pool_end_address);
}
void
TestConfigBackendDHCPv4::createUpdateGlobalParameter4(const db::ServerSelector& server_selector,
const data::StampedValuePtr& value) {
value->setServerTag(getServerTag(server_selector));
auto& index = globals_.get<StampedValueNameIndexTag>();
auto global_it_pair = index.equal_range(value->getName());
for (auto global_it = global_it_pair.first; global_it != global_it_pair.second;
++global_it) {
auto existing_value = *global_it;
if (existing_value->hasServerTag(ServerTag(getServerTag(server_selector)))) {
index.replace(global_it, value);
return;
}
}
index.insert(value);
}
void
TestConfigBackendDHCPv4::createUpdateServer4(const db::ServerPtr& server) {
auto& index = servers_.get<ServerTagIndexTag>();
auto server_it = index.find(server->getServerTagAsText());
if (server_it != index.end()) {
index.replace(server_it, server);
} else {
index.insert(server);
}
}
uint64_t
TestConfigBackendDHCPv4::deleteSubnet4(const db::ServerSelector& /* server_selector */,
const std::string& subnet_prefix) {
auto& index = subnets_.get<SubnetPrefixIndexTag>();
return (index.erase(subnet_prefix));
}
uint64_t
TestConfigBackendDHCPv4::deleteSubnet4(const db::ServerSelector& /* server_selector */,
const SubnetID& subnet_id) {
auto& index = subnets_.get<SubnetSubnetIdIndexTag>();
return (index.erase(subnet_id));
}
uint64_t
TestConfigBackendDHCPv4::deleteAllSubnets4(const db::ServerSelector& /* server_selector */) {
auto subnets_size = subnets_.size();
subnets_.clear();
return (subnets_size);
}
uint64_t
TestConfigBackendDHCPv4::deleteSharedNetworkSubnets4(const db::ServerSelector& /* server_selector */,
const std::string& shared_network_name) {
uint64_t cnt = 0;
auto& index = subnets_.get<SubnetRandomAccessIndexTag>();
for (auto subnet = index.begin(); subnet != index.end(); ++subnet) {
SharedNetwork4Ptr network;
(*subnet)->getSharedNetwork(network);
if (network && (network->getName() == shared_network_name)) {
network->del((*subnet)->getID());
}
if ((network && (network->getName() == shared_network_name)) ||
((*subnet)->getSharedNetworkName() == shared_network_name)) {
subnet = index.erase(subnet);
++cnt;
}
}
return (cnt);
}
uint64_t
TestConfigBackendDHCPv4::deleteSharedNetwork4(const db::ServerSelector& /* server_selector */,
const std::string& name) {
for (auto subnet = subnets_.begin(); subnet != subnets_.end(); ++subnet) {
if ((*subnet)->getSharedNetworkName() == name) {
(*subnet)->setSharedNetworkName("");
}
}
auto& index = shared_networks_.get<SharedNetworkNameIndexTag>();
auto network_it = index.find(name);
if (network_it != index.end()) {
(*network_it)->delAll();
}
return (index.erase(name));
}
uint64_t
TestConfigBackendDHCPv4::deleteAllSharedNetworks4(const db::ServerSelector& /* server_selector */) {
auto shared_networks_size = shared_networks_.size();
shared_networks_.clear();
return (shared_networks_size);
}
uint64_t
TestConfigBackendDHCPv4::deleteOptionDef4(const db::ServerSelector& /* server_selector */,
const uint16_t code,
const std::string& space) {
uint64_t erased = 0;
for (auto option_def_it = option_defs_.begin(); option_def_it != option_defs_.end();
++option_def_it) {
if (((*option_def_it)->getCode() == code) &&
((*option_def_it)->getOptionSpaceName() == space)) {
option_def_it = option_defs_.erase(option_def_it);
++erased;
}
}
return (erased);
}
uint64_t
TestConfigBackendDHCPv4::deleteAllOptionDefs4(const db::ServerSelector& /* server_selector */) {
auto option_defs_size = option_defs_.size();
option_defs_.clear();
return (option_defs_size);
}
uint64_t
TestConfigBackendDHCPv4::deleteOption4(const db::ServerSelector& /* server_selector */,
const uint16_t code,
const std::string& space) {
uint64_t erased = 0;
for (auto option_it = options_.begin(); option_it != options_.end();
++option_it) {
if ((option_it->option_->getType() == code) &&
(option_it->space_name_ == space)) {
option_it = options_.erase(option_it);
++erased;
}
}
return (erased);
}
uint64_t
TestConfigBackendDHCPv4::deleteOption4(const db::ServerSelector& /* server_selector */,
const std::string& shared_network_name,
const uint16_t code,
const std::string& space) {
auto& index = shared_networks_.get<SharedNetworkNameIndexTag>();
auto network_it = index.find(shared_network_name);
if (network_it != index.end()) {
auto shared_network = *network_it;
return (shared_network->getCfgOption()->del(space, code));
} else {
isc_throw(BadValue, "attempted to delete an option in a non existing "
"shared network " << shared_network_name);
}
}
uint64_t
TestConfigBackendDHCPv4::deleteOption4(const db::ServerSelector& /* server_selector */,
const SubnetID& subnet_id,
const uint16_t code,
const std::string& space) {
auto& index = subnets_.get<SubnetSubnetIdIndexTag>();
auto subnet_it = index.find(subnet_id);
if (subnet_it != index.cend()) {
auto subnet = *subnet_it;
return (subnet->getCfgOption()->del(space, code));
} else {
isc_throw(BadValue, "attempted to delete an option in a non existing "
"subnet ID " << subnet_id);
}
}
uint64_t
TestConfigBackendDHCPv4::deleteOption4(const db::ServerSelector& /* server_selector */,
const asiolink::IOAddress& pool_start_address,
const asiolink::IOAddress& pool_end_address,
const uint16_t code,
const std::string& space) {
for (auto subnet = subnets_.begin(); subnet != subnets_.end(); ++subnet) {
auto pool = (*subnet)->getPool(Lease::TYPE_V4, pool_start_address);
if (pool) {
return (pool->getCfgOption()->del(space, code));
}
}
isc_throw(BadValue, "attempted to delete an option in a non existing "
"pool " << pool_start_address << " - " << pool_end_address);
}
uint64_t
TestConfigBackendDHCPv4::deleteGlobalParameter4(const db::ServerSelector& server_selector,
const std::string& name) {
auto& index = globals_.get<StampedValueNameIndexTag>();
auto global_it_pair = index.equal_range(name);
for (auto global_it = global_it_pair.first; global_it != global_it_pair.second;
++global_it) {
auto value = *global_it;
if (value->hasServerTag(ServerTag(getServerTag(server_selector)))) {
index.erase(global_it);
return (1);
}
}
return (0);
}
uint64_t
TestConfigBackendDHCPv4::deleteAllGlobalParameters4(const db::ServerSelector& /* server_selector */) {
auto globals_size = globals_.size();
globals_.clear();
return (globals_size);
}
uint64_t
TestConfigBackendDHCPv4::deleteServer4(const ServerTag& server_tag) {
auto& index = servers_.get<ServerTagIndexTag>();
return (index.erase(server_tag.get()));
}
uint64_t
TestConfigBackendDHCPv4::deleteAllServers4() {
auto servers_size = servers_.size();
servers_.clear();
return (servers_size);
}
} // namespace test
} // namespace dhcp
} // namespace isc
|