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