Line data Source code
1 : /* 2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. 3 : */ 4 : 5 : // 6 : // sandesh_uve.cc 7 : // 8 : // file to handle requests for Sending Sandesh UVEs 9 : // 10 : 11 : #include "sandesh_uve.h" 12 : #include "../common/sandesh_uve_types.h" 13 : #include "sandesh_session.h" 14 : #include "sandesh_http.h" 15 : #include "sandesh_client_sm.h" 16 : 17 : using std::string; 18 : using std::map; 19 : 20 : SandeshUVETypeMaps::uve_global_map* SandeshUVETypeMaps::map_ = NULL; 21 : int PullSandeshUVE = 0; 22 : 23 : 24 : bool 25 87 : SandeshUVETypeMaps::InitDerivedStats( 26 : const std::map<std::string, ds_conf_elem> &dsmap) { 27 87 : bool success = true; 28 : // For all (per UVE Struct) elements of the DerivedStats config, 29 : // call the InitDerivedStats functions on that UVE Struct's Cache 30 87 : for (map<string, ds_conf_elem>::const_iterator cit = dsmap.begin(); 31 87 : cit != dsmap.end(); cit++) { 32 0 : uve_global_map::iterator it = map_->find(cit->first); 33 0 : if (it==map_->end()) { 34 0 : success = false; 35 : } else { 36 0 : if (!(it->second.second)->InitDerivedStats(cit->second)) 37 0 : success = false; 38 : } 39 : } 40 87 : return success; 41 : } 42 : 43 : uint32_t 44 0 : SandeshUVETypeMaps::Clear(const std::string& proxy, int partition) { 45 0 : int count=0; 46 0 : for (uve_global_map::iterator it = map_->begin(); 47 0 : it != map_->end(); it++) { 48 0 : uint32_t subcount = it->second.second->ClearUVEs(proxy, partition); 49 0 : if (subcount !=0) { 50 0 : SANDESH_LOG(INFO, __func__ << " for " << it->first << " partition " << 51 : partition << " proxy " << proxy << " cleared " << 52 : subcount); 53 0 : count += subcount; 54 : } 55 : } 56 0 : return count; 57 : } 58 : 59 : void 60 92 : SandeshUVETypeMaps::SyncAllMaps(const map<string,uint32_t> & inpMap, bool periodic) { 61 : static uint64_t periodic_count = 0; 62 92 : if (periodic) periodic_count++; 63 92 : for (uve_global_map::iterator it = map_->begin(); 64 535 : it != map_->end(); it++) { 65 443 : map<string,uint32_t>::const_iterator iit = inpMap.find(it->first); 66 443 : uint32_t cycle = 0; 67 443 : if (periodic) { 68 : uint64_t target_count = 69 28 : (it->second.first * 1000) / SandeshClientSM::kTickInterval; 70 34 : if (target_count == 0) continue; 71 7 : if ((periodic_count % target_count) != 0) continue; 72 1 : cycle = (uint32_t) (periodic_count / target_count); 73 1 : assert(inpMap.size() == 0); 74 : } 75 416 : SandeshUVE::SendType stype = (periodic ? SandeshUVE::ST_PERIODIC : SandeshUVE::ST_SYNC); 76 416 : if (iit == inpMap.end()) { 77 416 : uint count = it->second.second->SyncUVE("", stype, 0, cycle, ""); 78 416 : SANDESH_LOG(INFO, __func__ << " for " << it->first << ":" << stype << 79 : " period " << it->second.first << " cycle " << cycle << 80 : " without seqno , total = " << count); 81 : } else { 82 0 : uint count = it->second.second->SyncUVE("", stype, iit->second, cycle, ""); 83 0 : SANDESH_LOG(INFO, __func__ << " for " << it->first << ":" << stype << 84 : " period " << it->second.first << " cycle " << cycle << 85 : " with seqno " << iit->second << 86 : ", total = " << count); 87 : } 88 : } 89 92 : } 90 : 91 : void 92 0 : SandeshUVETypeMaps::SyncIntrospect( 93 : std::string tname, std::string table, std::string key) { 94 0 : const SandeshUVETypeMaps::uve_global_elem um = TypeMap(tname); 95 0 : um.second->SendUVE(table, key, ""); 96 0 : } 97 : 98 : void 99 0 : SandeshUVETypesReq::HandleRequest() const { 100 0 : std::vector<SandeshUVETypeInfo> stv; 101 : 102 : SandeshUVETypeMaps::uve_global_map::const_iterator it = 103 0 : SandeshUVETypeMaps::Begin(); 104 0 : for(; it!= SandeshUVETypeMaps::End(); it++) { 105 0 : SandeshUVETypeInfo sti; 106 0 : sti.set_type_name(it->first); 107 0 : sti.set_seq_num(it->second.second->TypeSeq()); 108 0 : sti.set_period(it->second.first); 109 0 : sti.set_dsconf(it->second.second->GetDSConf()); 110 0 : sti.set_timeout(it->second.second->GetTimeout()); 111 0 : stv.push_back(sti); 112 0 : } 113 0 : SandeshUVETypesResp *sur = new SandeshUVETypesResp(); 114 0 : sur->set_type_info(stv); 115 0 : sur->set_context(context()); 116 0 : sur->Response(); 117 0 : } 118 : void 119 0 : SandeshUVEDSConfReq::HandleRequest() const { 120 : const SandeshUVETypeMaps::uve_global_elem um = 121 0 : SandeshUVETypeMaps::TypeMap(get_tname()); 122 : 123 0 : map<string,string> dsconf; 124 0 : bool success = false; 125 0 : if (um.second) { 126 0 : dsconf.insert(std::make_pair(get_dsattr(), get_dsconf())); 127 0 : success = um.second->InitDerivedStats(dsconf); 128 : } 129 0 : SandeshUVEDSConfResp *sur = new SandeshUVEDSConfResp(); 130 0 : sur->set_success(success); 131 0 : if (success) sur->set_dsconf(um.second->GetDSConf()); 132 0 : sur->set_context(context()); 133 0 : sur->Response(); 134 0 : } 135 : 136 : void 137 0 : SandeshUVECacheReq::HandleRequest() const { 138 0 : uint32_t returned = 0; 139 : 140 0 : const SandeshUVETypeMaps::uve_global_elem um = SandeshUVETypeMaps::TypeMap(get_tname()); 141 : 142 0 : if (um.second) { 143 0 : if (__isset.key) { 144 0 : returned = um.second->SendUVE("", get_key(), context()); 145 : } else { 146 0 : returned += um.second->SyncUVE("", SandeshUVE::ST_INTROSPECT, 0, 0, context()); 147 : } 148 : } 149 : 150 0 : SandeshUVECacheResp *sur = new SandeshUVECacheResp(); 151 0 : sur->set_returned(returned); 152 0 : if (um.second) sur->set_period(um.first); 153 0 : sur->set_context(context()); 154 0 : sur->Response(); 155 0 : }