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