Line data Source code
1 : /* 2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. 3 : */ 4 : 5 : #include <sandesh/request_pipeline.h> 6 : 7 : #include "ifmap/ifmap_table.h" 8 : 9 : #include <boost/algorithm/string.hpp> 10 : #include "db/db.h" 11 : #include "db/db_table.h" 12 : #include "db/db_table_partition.h" 13 : #include "ifmap/autogen.h" 14 : #include "ifmap/ifmap_node.h" 15 : #include "ifmap/ifmap_server_show_types.h" 16 : 17 : using namespace std; 18 : 19 1224637 : IFMapTable::IFMapTable(DB *db, const std::string &name, DBGraph *graph) 20 1224637 : : DBGraphTable(db, name, graph) { 21 1224637 : } 22 : 23 272482 : IFMapNode *IFMapTable::FindNode(const std::string &name) { 24 : DBTablePartition *partition = 25 272482 : static_cast<DBTablePartition *>(GetTablePartition(0)); 26 272482 : IFMapTable::RequestKey reqkey; 27 272482 : reqkey.id_name = name; 28 544964 : return static_cast<IFMapNode *>(partition->Find(&reqkey)); 29 272482 : } 30 : 31 0 : IFMapNode *IFMapTable::FindNextNode(const std::string &name) { 32 : DBTablePartition *partition = 33 0 : static_cast<DBTablePartition *>(GetTablePartition(0)); 34 0 : IFMapTable::RequestKey reqkey; 35 0 : reqkey.id_name = name; 36 0 : return static_cast<IFMapNode *>(partition->FindNext(&reqkey)); 37 0 : } 38 : 39 532208 : IFMapTable *IFMapTable::FindTable(DB *db, const std::string &element_type) { 40 532208 : string idtype = element_type; 41 532207 : std::replace(idtype.begin(), idtype.end(), '-', '_'); 42 532204 : string name = "__ifmap__." + idtype + ".0"; 43 1064403 : return static_cast<IFMapTable *>(db->FindTable(name)); 44 532199 : } 45 : 46 8214 : void IFMapTable::ClearTables(DB *db) { 47 8214 : for (DB::iterator iter = db->lower_bound("__ifmap__."); 48 1230131 : iter != db->end(); ++iter) { 49 1230131 : if (iter->first.find("__ifmap__.") != 0) { 50 8214 : break; 51 : } 52 1221917 : IFMapTable *table = static_cast<IFMapTable *>(iter->second); 53 1221917 : table->Clear(); 54 : } 55 8214 : } 56 : 57 0 : void IFMapTable::FillNodeTableList(DB *db, 58 : std::vector<IFMapNodeTableListShowEntry> *table_list) { 59 0 : for (DB::const_iterator iter = db->const_lower_bound("__ifmap__"); 60 0 : iter != db->end(); ++iter) { 61 0 : DBTable *table = static_cast<DBTable *>(iter->second); 62 0 : if (table->name().compare("__ifmap_metadata__.0") == 0) { 63 : // Ignore the link-table in this api 64 0 : continue; 65 : } 66 0 : if (!table->Size()) 67 0 : continue; 68 : // Create a name that can be passed to IFMapTable::FindTable() 69 0 : size_t first = table->name().find_first_of("."); 70 0 : size_t second = table->name().find_first_of(".", first + 1); 71 0 : std::string name = table->name().substr(first + 1, second - first - 1); 72 0 : std::replace(name.begin(), name.end(), '_', '-'); 73 : 74 0 : IFMapNodeTableListShowEntry entry; 75 0 : entry.table_name = name; 76 0 : entry.size = table->Size(); 77 : 78 0 : table_list->push_back(entry); 79 0 : if (table->name().find("__ifmap__") != 0) { 80 0 : break; 81 : } 82 0 : } 83 0 : }