Line data Source code
1 : /* 2 : * Copyright (c) 2016 Juniper Networks, Inc. All rights reserved. 3 : * Copyright Contributors to the OpenSDN project 4 : */ 5 : 6 : #include <boost/uuid/uuid_io.hpp> 7 : #include <cmn/agent_cmn.h> 8 : #include <init/agent_param.h> 9 : #include <resource_manager/resource_manager.h> 10 : #include <resource_manager/resource_table.h> 11 : #include <resource_manager/index_resource.h> 12 : 13 111 : IndexResourceKey::IndexResourceKey(ResourceManager *rm, 14 111 : Resource::Type resource_key_type) : 15 111 : ResourceKey(rm, resource_key_type) { 16 111 : } 17 : 18 111 : IndexResourceKey::~IndexResourceKey() { 19 111 : } 20 : 21 111 : IndexResourceData::IndexResourceData(ResourceManager *rm, uint32_t index) : 22 111 : ResourceData(rm), index_(index) { 23 111 : } 24 : 25 222 : IndexResourceData::~IndexResourceData() { 26 : 27 222 : } 28 : 29 222 : uint32_t IndexResourceData::index() const { 30 222 : return index_; 31 : } 32 : 33 6 : IndexResourceTable::IndexResourceTable(ResourceManager *rm) : 34 6 : ResourceTable(rm) { 35 6 : } 36 : 37 12 : IndexResourceTable::~IndexResourceTable() { 38 12 : } 39 : 40 16 : void IndexResourceTable::ReserveIndex(uint32_t index) { 41 16 : ResourceKeyPtr dummy_key_entry; 42 16 : index_vector_.InsertAtIndex(index, dummy_key_entry); 43 16 : } 44 : 45 127 : void IndexResourceTable::ReleaseIndex(uint32_t index) { 46 127 : index_vector_.FreeIndex(index); 47 127 : } 48 : 49 111 : void IndexResourceTable::Release(uint32_t index) { 50 111 : ResourceKeyPtr key = index_vector_.FindIndex(index); 51 111 : if (key.get()) { 52 111 : resource_manager()->Release(key); 53 : } 54 111 : } 55 : // Allocate the Index resource data. 56 111 : ResourceTable::DataPtr IndexResourceTable::AllocateData(ResourceKeyPtr key) { 57 111 : uint32_t index = AllocateIndex(key); 58 : ResourceTable::DataPtr data(static_cast<ResourceData *> 59 111 : (new IndexResourceData(key->rm(), index))); 60 111 : return data; 61 : } 62 : 63 : // Restore resource Index. 64 0 : void IndexResourceTable::RestoreIndex(uint32_t index, ResourceKeyPtr key) { 65 0 : index_vector_.InsertAtIndex(index, key); 66 0 : } 67 : 68 111 : uint32_t IndexResourceTable::AllocateIndex(ResourceKeyPtr key) { 69 : //Get the index, populate resource data 70 111 : uint32_t index = index_vector_.AllocIndex(key); 71 111 : return index; 72 : } 73 : // Restore the Key and Index 74 0 : void IndexResourceTable::RestoreKey(KeyPtr key, DataPtr data) { 75 0 : IndexResourceData *index_data = static_cast<IndexResourceData *>(data.get()); 76 0 : RestoreIndex(index_data->index(), key); 77 0 : InsertKey(key, data); 78 0 : } 79 : // Release the Key. 80 111 : void IndexResourceTable::ReleaseKey(KeyPtr key, DataPtr data) { 81 111 : IndexResourceData *index_data = static_cast<IndexResourceData *>(data.get()); 82 111 : ReleaseIndex(index_data->index()); 83 111 : DeleteKey(key); 84 111 : }