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