9 #include <pugixml/pugixml.hpp>
13 #include "ifmap/ifmap_log_types.h"
20 static const char *
NodeName(
const xml_node &node) {
21 const char *name = node.name();
23 const char *dot = strchr(name,
':');
31 ModuleMap::iterator loc = module_map_.find(module);
32 if (loc != module_map_.end()) {
36 module_map_.insert(make_pair(module, parser));
41 ModuleMap::iterator loc = module_map_.find(module);
42 if (loc == module_map_.end()) {
47 module_map_.erase(loc);
52 pair<MetadataParseMap::iterator, bool> result =
53 metadata_map_.insert(make_pair(metadata, parser));
54 assert(result.second);
58 metadata_map_.clear();
59 DeleteInstance(module);
63 string id(node.attribute(
"name").value());
78 MetadataParseMap::const_iterator loc = metadata_map_.find(name);
79 if (loc == metadata_map_.end()) {
82 std::unique_ptr<AutogenProperty> pvalue;
83 bool success = (loc->second)(node, &pvalue);
89 result->
data.reset(data);
92 data->
content.reset(pvalue.release());
99 size_t ns =
id.find(
"contrail:");
100 size_t start = (ns == 0) ?
sizeof(
"contrail:") - 1: 0;
101 size_t loc =
id.find(
':', start);
102 if (loc != string::npos) {
103 *id_type = string(
id, start, loc - start);
104 *id_name = string(
id, loc + 1,
id.size() - (loc + 1));
115 if (src_key != NULL) {
120 request->
key.reset(dst_key);
130 request->
data.reset(dst_data);
136 const xml_node &parent,
bool add_change,
RequestList *list)
const {
137 unique_ptr<DBRequest> request(
new DBRequest);
143 bool has_meta =
false;
144 for (xml_node node = parent.first_child(); node;
145 node = node.next_sibling()) {
147 if (strcmp(name,
"identity") == 0) {
152 request->key.reset(key);
157 request->data.reset(data);
166 if (strcmp(name,
"metadata") == 0) {
171 for (xml_node meta = node.first_child(); meta;
172 meta = meta.next_sibling()) {
173 if (ParseMetadata(meta, request.get())) {
174 SetOrigin(request.get());
176 if (meta.next_sibling()) {
179 list->push_back(current);
185 if (idcount == 0 || !has_meta) {
192 const xml_document &xdoc,
RequestList *list)
const {
193 xml_node current = xdoc.first_child();
196 if (strcmp(
NodeName(current),
"updateResult") == 0 ||
197 strcmp(
NodeName(current),
"searchResult") == 0 ||
198 strcmp(
NodeName(current),
"deleteResult") == 0) {
200 if (strcmp(
NodeName(current),
"deleteResult") == 0) {
205 xml_node result = current;
206 for (xml_node node = result.first_child(); node;
207 node = node.next_sibling()) {
208 ParseResultItem(node, add_change, list);
210 current = current.next_sibling();
212 current = current.first_child();
219 uint64_t sequence_number) {
221 pugi::xml_parse_result result = xdoc.load_buffer(data, length);
223 IFMAP_WARN(IFMapXmlLoadError,
"Unable to load XML document", length);
228 ParseResults(xdoc, &requests);
230 while (!requests.empty()) {
231 unique_ptr<DBRequest> req(requests.front());
232 requests.pop_front();
bool Enqueue(DBRequest *req)
void MetadataRegister(const std::string &metadata, MetadataParseFn parser)
void SetOrigin(struct DBRequest *result) const
bool Receive(DB *db, const char *data, size_t length, uint64_t sequence_number)
static IFMapServerParser * GetInstance(const std::string &module)
void ParseResults(const pugi::xml_document &xdoc, RequestList *list) const
std::map< std::string, IFMapServerParser * > ModuleMap
boost::function< bool(const pugi::xml_node &, std::unique_ptr< AutogenProperty > *) > MetadataParseFn
static void DeleteInstance(const std::string &module)
void MetadataClear(const std::string &module)
bool ParseMetadata(const pugi::xml_node &node, struct DBRequest *result) const
static ModuleMap module_map_
bool ParseResultItem(const pugi::xml_node &parent, bool add_change, RequestList *list) const
std::list< struct DBRequest * > RequestList
static IFMapTable * FindTable(DB *db, const std::string &element_type)
#define IFMAP_TRACE(obj,...)
#define IFMAP_WARN(obj,...)
static DBRequest * IFMapServerRequestClone(const DBRequest *src)
static const char * NodeName(const xml_node &node)
static string ParseIdentifier(const xml_node &node)
static void IdentifierNsTypeName(const string &id, string *id_type, string *id_name)
std::unique_ptr< DBRequestKey > key
std::unique_ptr< DBRequestData > data
void set_origin(Origin in_origin)
std::unique_ptr< AutogenProperty > content