OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ifmap_uuid_mapper.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  */
4 
6 
7 #include "db/db.h"
9 #include "ifmap/ifmap_node.h"
10 #include "ifmap/ifmap_server.h"
12 #include "ifmap/ifmap_table.h"
13 #include "schema/vnc_cfg_types.h"
14 
15 void IFMapUuidMapper::SetUuid(uint64_t ms_long, uint64_t ls_long,
16  boost::uuids::uuid &uu_id) {
17  for (int i = 0; i < 8; i++) {
18  uu_id.data[7 - i] = ms_long & 0xFF;
19  ms_long = ms_long >> 8;
20  }
21 
22  for (int i = 0; i < 8; i++) {
23  uu_id.data[15 - i] = ls_long & 0xFF;
24  ls_long = ls_long >> 8;
25  }
26 }
27 
29  std::stringstream uuid_str;
30  uuid_str << id;
31  return uuid_str.str();
32 }
33 
34 std::string IFMapUuidMapper::Add(uint64_t ms_long, uint64_t ls_long,
35  IFMapNode *node) {
36  boost::uuids::uuid uu_id;
37  SetUuid(ms_long, ls_long, uu_id);
38  std::string uuid_str = UuidToString(uu_id);
39  uuid_node_map_.insert(std::make_pair(uuid_str, node));
40  return uuid_str;
41 }
42 
43 void IFMapUuidMapper::Delete(const std::string &uuid_str) {
44  uuid_node_map_.erase(uuid_str);
45 }
46 
47 IFMapNode *IFMapUuidMapper::Find(const std::string &uuid_str) {
48  UuidNodeMap::iterator loc = uuid_node_map_.find(uuid_str);
49  if (loc != uuid_node_map_.end()) {
50  return loc->second;;
51  }
52  return NULL;
53 }
54 
55 bool IFMapUuidMapper::Exists(const std::string &uuid_str) {
56  UuidNodeMap::iterator loc = uuid_node_map_.find(uuid_str);
57  if (loc != uuid_node_map_.end()) {
58  return true;
59  }
60  return false;
61 }
62 
64  std::cout << "Printing all UUID mapper entries - UUID : type:NODE-FQN\n";
65  for (UuidNodeMap::iterator iter = uuid_node_map_.begin();
66  iter != uuid_node_map_.end(); ++iter) {
67  IFMapNode *node = iter->second;
68  std::cout << iter->first << " : " << node->ToString() << std::endl;
69  }
70 }
71 
72 // Routines for class IFMapVmUuidMapper
73 
75  : db_(db), ifmap_server_(server), vm_table_(NULL), registered(false) {
76 }
77 
79  Shutdown();
80 }
81 
83  vm_table_ = static_cast<IFMapServerTable *>(
84  db_->FindTable("__ifmap__.virtual_machine.0"));
85  assert(vm_table_ != NULL);
87  this, _1, _2));
88  registered = true;
89 }
90 
92  if (registered) {
94  registered = false;
95  }
96 }
97 
99  DBEntryBase *entry) {
100  IFMapNode *vm_node = static_cast<IFMapNode *>(entry);
101  std::string tname = vm_node->table()->Typename();
102  assert(tname.compare("virtual-machine") == 0);
103 
104  if (!IsFeasible(vm_node)) {
105  std::string vm_uuid;
106  bool val = NodeToUuid(vm_node, &vm_uuid);
107 
108  // Its possible that the add came without any properties i.e no object
109  // and hence no entry in node_uuid_map_
110  if (val) {
111  node_uuid_map_.erase(vm_node);
112  uuid_mapper_.Delete(vm_uuid);
113  }
114  return;
115  }
116 
117  // Ignore 'change' if the 'add' has already been processed.
118  if (NodeProcessed(vm_node)) {
119  return;
120  }
121 
122  IFMapObject *object = vm_node->Find(IFMapOrigin(IFMapOrigin::CASSANDRA));
123  if (object) {
124  // Insert into the uuid-node-mapper
125  autogen::VirtualMachine *vm = static_cast<autogen::VirtualMachine *>
126  (object);
127  if (vm->IsPropertySet(autogen::VirtualMachine::ID_PERMS)) {
128  autogen::UuidType uuid = vm->id_perms().uuid;
129  std::string vm_uuid =
130  uuid_mapper_.Add(uuid.uuid_mslong, uuid.uuid_lslong, vm_node);
131 
132  // Insert into the node-uuid-map
133  node_uuid_map_.insert(make_pair(vm_node, vm_uuid));
134 
135  // Check if there were any vm-reg's for this VM whose processing we
136  // had deferred since the vm-node did not exist then.
137  std::string vr_name;
138  bool exists = PendingVmRegExists(vm_uuid, &vr_name);
139  if (exists) {
140  bool subscribe = true;
141  ifmap_server_->ProcessVmSubscribe(vr_name, vm_uuid, subscribe);
142  pending_vmreg_map_.erase(vm_uuid);
143  }
144  }
145  }
146 }
147 
148 IFMapNode *IFMapVmUuidMapper::GetVmNodeByUuid(const std::string &vm_uuid) {
149  return uuid_mapper_.Find(vm_uuid);
150 }
151 
152 bool IFMapVmUuidMapper::VmNodeExists(const std::string &vm_uuid) {
153  return uuid_mapper_.Exists(vm_uuid);
154 }
155 
158 }
159 
161  std::string vr_name, bool subscribe) {
162  if (subscribe) {
163  pending_vmreg_map_.insert(make_pair(vm_uuid, vr_name));
164  } else {
165  pending_vmreg_map_.erase(vm_uuid);
166  }
167 }
168 
169 bool IFMapVmUuidMapper::PendingVmRegExists(const std::string &vm_uuid,
170  std::string *vr_name) {
171  PendingVmRegMap::iterator loc = pending_vmreg_map_.find(vm_uuid);
172  if (loc != pending_vmreg_map_.end()) {
173  *vr_name = loc->second;
174  return true;
175  }
176  return false;
177 }
178 
180  std::cout << "Printing all pending vm-reg entries - VM-UUID : VR-FQN\n";
181  for (PendingVmRegMap::iterator iter = pending_vmreg_map_.begin();
182  iter != pending_vmreg_map_.end(); ++iter) {
183  std::cout << iter->first << " : " << iter->second << std::endl;
184  }
185 }
186 
188  if (node->IsDeleted()) {
189  return false;
190  }
191  return true;
192 }
193 
194 bool IFMapVmUuidMapper::NodeToUuid(IFMapNode *vm_node, std::string *vm_uuid) {
195  NodeUuidMap::iterator loc = node_uuid_map_.find(vm_node);
196  if (loc != node_uuid_map_.end()) {
197  *vm_uuid = loc->second;
198  return true;
199  }
200  return false;
201 }
202 
204  NodeUuidMap::iterator loc = node_uuid_map_.find(vm_node);
205  if (loc != node_uuid_map_.end()) {
206  return true;
207  }
208  return false;
209 }
210 
212  std::cout << "Printing all node-UUID entries - type:NODE-FQN : uuid\n";
213  for (NodeUuidMap::iterator iter = node_uuid_map_.begin();
214  iter != node_uuid_map_.end(); ++iter) {
215  IFMapNode *node = iter->first;
216  std::cout << node->ToString() << " : " << iter->second << std::endl;
217  }
218 }
219 
void ProcessVmRegAsPending(std::string vm_uuid, std::string vr_name, bool subscribe)
UuidNodeMap uuid_node_map_
std::string UuidToString(const boost::uuids::uuid &id)
NodeUuidMap node_uuid_map_
virtual std::string ToString() const
Definition: ifmap_node.cc:31
DBTable::ListenerId vm_lid_
bool IsDeleted() const
Definition: db_entry.h:49
virtual const char * Typename() const =0
IFMapNode * GetVmNodeByUuid(const std::string &vm_uuid)
boost::uuids::uuid uuid
IFMapTable * table()
Definition: ifmap_node.h:29
bool VmNodeExists(const std::string &vm_uuid)
IFMapVmUuidMapper(DB *db, IFMapServer *server)
void Unregister(ListenerId listener)
Definition: db_table.cc:186
void PrintAllNodeUuidMappedEntries()
void SetUuid(uint64_t ms_long, uint64_t ls_long, boost::uuids::uuid &uu_id)
ListenerId Register(ChangeCallback callback, const std::string &name="unspecified")
Definition: db_table.cc:181
Definition: db.h:24
bool PendingVmRegExists(const std::string &vm_uuid, std::string *vr_name)
bool NodeToUuid(IFMapNode *vm_node, std::string *vm_uuid)
PendingVmRegMap pending_vmreg_map_
IFMapObject * Find(IFMapOrigin origin)
Definition: ifmap_node.cc:38
bool IsFeasible(IFMapNode *node)
IFMapServerTable * vm_table_
std::string Add(uint64_t ms_long, uint64_t ls_long, IFMapNode *node)
void ProcessVmSubscribe(std::string vr_name, std::string vm_uuid, bool subscribe, bool has_vms)
bool NodeProcessed(IFMapNode *node)
bool Exists(const std::string &uuid_str)
IFMapNode * Find(const std::string &uuid_str)
void VmNodeProcess(DBTablePartBase *partition, DBEntryBase *entry)
IFMapUuidMapper uuid_mapper_
DBTableBase * FindTable(const std::string &name)
Definition: db.cc:68
IFMapServer * ifmap_server_
void Delete(const std::string &uuid_str)