Line data Source code
1 : /* 2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. 3 : * Copyright Contributors to the OpenSDN project 4 : */ 5 : 6 : #include "ifmap/ifmap_agent_parser.h" 7 : 8 : #include <vector> 9 : #include <pugixml/pugixml.hpp> 10 : #include "base/logging.h" 11 : #include "db/db_entry.h" 12 : #include "ifmap/ifmap_agent_table.h" 13 : 14 : using namespace std; 15 : using namespace pugi; 16 : 17 149 : void IFMapAgentParser::NodeRegister(const string &node, NodeParseFn parser) { 18 : pair<NodeParseMap::iterator, bool> result = 19 149 : node_map_.insert(make_pair(node, parser)); 20 149 : assert(result.second); 21 149 : } 22 : 23 0 : void IFMapAgentParser::NodeClear() { 24 0 : node_map_.clear(); 25 0 : } 26 : 27 167 : void IFMapAgentParser::NodeParse(xml_node &node, DBRequest::DBOperation oper, uint64_t seq) { 28 : 29 167 : const char *name = node.attribute("type").value(); 30 : int msg_type; 31 167 : if (oper == DBRequest::DB_ENTRY_ADD_CHANGE) 32 92 : msg_type = UPDATE; 33 : else 34 75 : msg_type = DEL; 35 : 36 : IFMapTable *table; 37 167 : table = IFMapTable::FindTable(db_, name); 38 167 : if(!table) { 39 0 : node_parse_errors_[msg_type]++; 40 0 : return; 41 : } 42 : 43 : // Locate the decode function using id_name 44 167 : NodeParseMap::const_iterator loc = node_map_.find(name); 45 167 : if (loc == node_map_.end()) { 46 0 : node_parse_errors_[msg_type]++; 47 0 : return; 48 : } 49 : 50 : IFMapObject *obj; 51 167 : IFMapTable::RequestKey *req_key = new IFMapTable::RequestKey; 52 : 53 : // Invoke the decode routine 54 167 : req_key->id_type = name; 55 167 : req_key->id_seq_num = seq; 56 167 : obj = loc->second(node, db_, &req_key->id_name); 57 167 : if (!obj) { 58 0 : node_parse_errors_[msg_type]++; 59 0 : delete req_key; 60 0 : return; 61 : } 62 : 63 167 : IFMapAgentTable::IFMapAgentData *req_data = new IFMapAgentTable::IFMapAgentData; 64 167 : req_data->content.reset(obj); 65 : 66 167 : unique_ptr<DBRequest> request(new DBRequest); 67 167 : request->oper = oper; 68 167 : request->data.reset(req_data); 69 167 : request->key.reset(req_key); 70 167 : table->Enqueue(request.get()); 71 167 : } 72 : 73 187 : void IFMapAgentParser::LinkParse(xml_node &link, DBRequest::DBOperation oper, uint64_t seq) { 74 : 75 187 : xml_node first_node; 76 187 : xml_node second_node; 77 187 : xml_node name_node1; 78 187 : xml_node name_node2; 79 : const char *name1; 80 : const char *name2; 81 : IFMapTable *table; 82 : IFMapAgentLinkTable *link_table; 83 : 84 : int msg_type; 85 187 : if (oper == DBRequest::DB_ENTRY_ADD_CHANGE) 86 96 : msg_type = UPDATE; 87 : else 88 91 : msg_type = DEL; 89 : 90 : link_table = static_cast<IFMapAgentLinkTable *>( 91 187 : db_->FindTable(IFMAP_AGENT_LINK_DB_NAME)); 92 : 93 187 : assert(link_table); 94 : 95 : // Get both first and second node and its corresponding tables 96 187 : first_node = link.first_child(); 97 187 : if (!first_node) { 98 0 : link_parse_errors_[msg_type]++; 99 0 : return; 100 : } 101 : 102 187 : second_node = first_node.next_sibling(); 103 187 : if (!second_node) { 104 0 : link_parse_errors_[msg_type]++; 105 0 : return; 106 : } 107 : 108 187 : name1 = first_node.attribute("type").value(); 109 187 : table = IFMapTable::FindTable(db_, name1); 110 187 : if(!table) { 111 0 : link_parse_errors_[msg_type]++; 112 0 : return; 113 : } 114 : 115 187 : name2 = second_node.attribute("type").value(); 116 187 : table = IFMapTable::FindTable(db_, name2); 117 187 : if(!table) { 118 0 : link_parse_errors_[msg_type]++; 119 0 : return; 120 : } 121 : 122 : // Get id_name of both the nodes 123 187 : name_node1 = first_node.first_child(); 124 187 : if (!name_node1) { 125 0 : link_parse_errors_[msg_type]++; 126 0 : return; 127 : } 128 : 129 187 : if (strcmp(name_node1.name(), "name") != 0) { 130 0 : link_parse_errors_[msg_type]++; 131 0 : return; 132 : } 133 : 134 187 : name_node2 = second_node.first_child(); 135 187 : if (!name_node2) { 136 0 : link_parse_errors_[msg_type]++; 137 0 : return; 138 : } 139 : 140 187 : if (strcmp(name_node2.name(), "name") != 0) { 141 0 : link_parse_errors_[msg_type]++; 142 0 : return; 143 : } 144 : 145 : // Create both the request keys 146 187 : unique_ptr <IFMapAgentLinkTable::RequestKey> req_key (new IFMapAgentLinkTable::RequestKey); 147 187 : req_key->left_key.id_name = name_node1.child_value(); 148 187 : req_key->left_key.id_type = name1; 149 187 : req_key->left_key.id_seq_num = seq; 150 : 151 187 : req_key->right_key.id_name = name_node2.child_value(); 152 187 : req_key->right_key.id_type = name2; 153 187 : req_key->right_key.id_seq_num = seq; 154 : 155 187 : xml_node metadata = link.child("metadata"); 156 187 : if (metadata) { 157 187 : req_key->metadata = metadata.attribute("type").value(); 158 : } 159 : 160 187 : unique_ptr <DBRequest> req (new DBRequest); 161 187 : req->oper = oper; 162 187 : req->key = std::move(req_key); 163 : 164 187 : link_table->Enqueue(req.get()); 165 187 : } 166 : 167 165 : void IFMapAgentParser::ConfigParse(const xml_node config, const uint64_t seq) { 168 : 169 : DBRequest::DBOperation oper; 170 : 171 330 : for (xml_node node = config.first_child(); node; 172 165 : node = node.next_sibling()) { 173 : 174 : int msg_type; 175 165 : if (strcmp(node.name(), "update") == 0) { 176 90 : oper = DBRequest::DB_ENTRY_ADD_CHANGE; 177 90 : msg_type = UPDATE; 178 75 : } else if (strcmp(node.name(), "delete") == 0) { 179 75 : oper = DBRequest::DB_ENTRY_DELETE; 180 75 : msg_type = DEL; 181 : } else { 182 0 : continue; 183 : } 184 : 185 519 : for(xml_node chld = node.first_child(); chld; chld = chld.next_sibling()) { 186 : 187 : // Handle the links between the nodes 188 354 : if (strcmp(chld.name(), "link") == 0) { 189 187 : links_processed_[msg_type]++; 190 187 : LinkParse(chld, oper, seq); 191 187 : continue; 192 : } 193 : 194 167 : if (strcmp(chld.name(), "node") == 0) { 195 167 : nodes_processed_[msg_type]++; 196 167 : NodeParse(chld, oper, seq); 197 : } 198 : } 199 : } 200 165 : }