OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sandesh_uve.cc
Go to the documentation of this file.
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 
22 
23 
24 bool
26  const std::map<std::string, ds_conf_elem> &dsmap) {
27  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  for (map<string, ds_conf_elem>::const_iterator cit = dsmap.begin();
31  cit != dsmap.end(); cit++) {
32  uve_global_map::iterator it = map_->find(cit->first);
33  if (it==map_->end()) {
34  success = false;
35  } else {
36  if (!(it->second.second)->InitDerivedStats(cit->second))
37  success = false;
38  }
39  }
40  return success;
41 }
42 
43 uint32_t
44 SandeshUVETypeMaps::Clear(const std::string& proxy, int partition) {
45  int count=0;
46  for (uve_global_map::iterator it = map_->begin();
47  it != map_->end(); it++) {
48  uint32_t subcount = it->second.second->ClearUVEs(proxy, partition);
49  if (subcount !=0) {
50  SANDESH_LOG(INFO, __func__ << " for " << it->first << " partition " <<
51  partition << " proxy " << proxy << " cleared " <<
52  subcount);
53  count += subcount;
54  }
55  }
56  return count;
57 }
58 
59 void
60 SandeshUVETypeMaps::SyncAllMaps(const map<string,uint32_t> & inpMap, bool periodic) {
61  static uint64_t periodic_count = 0;
62  if (periodic) periodic_count++;
63  for (uve_global_map::iterator it = map_->begin();
64  it != map_->end(); it++) {
65  map<string,uint32_t>::const_iterator iit = inpMap.find(it->first);
66  uint32_t cycle = 0;
67  if (periodic) {
68  uint64_t target_count =
69  (it->second.first * 1000) / SandeshClientSM::kTickInterval;
70  if (target_count == 0) continue;
71  if ((periodic_count % target_count) != 0) continue;
72  cycle = (uint32_t) (periodic_count / target_count);
73  assert(inpMap.size() == 0);
74  }
76  if (iit == inpMap.end()) {
77  uint count = it->second.second->SyncUVE("", stype, 0, cycle, "");
78  SANDESH_LOG(INFO, __func__ << " for " << it->first << ":" << stype <<
79  " period " << it->second.first << " cycle " << cycle <<
80  " without seqno , total = " << count);
81  } else {
82  uint count = it->second.second->SyncUVE("", stype, iit->second, cycle, "");
83  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 }
90 
91 void
93  std::string tname, std::string table, std::string key) {
95  um.second->SendUVE(table, key, "");
96 }
97 
98 void
99 SandeshUVETypesReq::HandleRequest() const {
100  std::vector<SandeshUVETypeInfo> stv;
101 
102  SandeshUVETypeMaps::uve_global_map::const_iterator it =
104  for(; it!= SandeshUVETypeMaps::End(); it++) {
105  SandeshUVETypeInfo sti;
106  sti.set_type_name(it->first);
107  sti.set_seq_num(it->second.second->TypeSeq());
108  sti.set_period(it->second.first);
109  sti.set_dsconf(it->second.second->GetDSConf());
110  sti.set_timeout(it->second.second->GetTimeout());
111  stv.push_back(sti);
112  }
113  SandeshUVETypesResp *sur = new SandeshUVETypesResp();
114  sur->set_type_info(stv);
115  sur->set_context(context());
116  sur->Response();
117 }
118 void
119 SandeshUVEDSConfReq::HandleRequest() const {
121  SandeshUVETypeMaps::TypeMap(get_tname());
122 
123  map<string,string> dsconf;
124  bool success = false;
125  if (um.second) {
126  dsconf.insert(std::make_pair(get_dsattr(), get_dsconf()));
127  success = um.second->InitDerivedStats(dsconf);
128  }
129  SandeshUVEDSConfResp *sur = new SandeshUVEDSConfResp();
130  sur->set_success(success);
131  if (success) sur->set_dsconf(um.second->GetDSConf());
132  sur->set_context(context());
133  sur->Response();
134 }
135 
136 void
137 SandeshUVECacheReq::HandleRequest() const {
138  uint32_t returned = 0;
139 
141 
142  if (um.second) {
143  if (__isset.key) {
144  returned = um.second->SendUVE("", get_key(), context());
145  } else {
146  returned += um.second->SyncUVE("", SandeshUVE::ST_INTROSPECT, 0, 0, context());
147  }
148  }
149 
150  SandeshUVECacheResp *sur = new SandeshUVECacheResp();
151  sur->set_returned(returned);
152  if (um.second) sur->set_period(um.first);
153  sur->set_context(context());
154  sur->Response();
155 }
static bool InitDerivedStats(const std::map< std::string, ds_conf_elem > &)
Definition: sandesh_uve.cc:25
static const int kTickInterval
int PullSandeshUVE
Definition: sandesh_uve.cc:21
static void SyncIntrospect(std::string tname, std::string table, std::string key)
Definition: sandesh_uve.cc:92
static uve_global_map::const_iterator Begin()
Definition: sandesh_uve.h:58
#define SANDESH_LOG(_Level, _Msg)
Definition: p/sandesh.h:474
static void SyncAllMaps(const std::map< std::string, uint32_t > &, bool periodic=false)
Definition: sandesh_uve.cc:60
std::string context() const
Definition: p/sandesh.h:311
std::map< std::string, uve_global_elem > uve_global_map
Definition: sandesh_uve.h:36
static uint32_t Clear(const std::string &proxy, int partition)
Definition: sandesh_uve.cc:44
static uve_global_map * map_
Definition: sandesh_uve.h:62
std::pair< int, SandeshUVEPerTypeMap * > uve_global_elem
Definition: sandesh_uve.h:35
static uve_global_map::const_iterator End()
Definition: sandesh_uve.h:59
static const uve_global_elem TypeMap(const std::string &s)
Definition: sandesh_uve.h:44