OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
policy_config_parser.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #include "policy_config_parser.h"
6 
7 #include <sstream>
8 
9 #include <boost/ptr_container/ptr_list.hpp>
10 
11 #include "base/logging.h"
12 #include <pugixml/pugixml.hpp>
13 #include "schema/routing_policy_types.h"
14 
15 
16 using namespace std;
17 using namespace pugi;
18 
19 struct PolicyTerm {
20  std::string term_name;
21  autogen::PolicyMatch match;
22  autogen::PolicyAction action;
23 };
24 
25 typedef boost::ptr_list<PolicyTerm> PolicyTerms;
26 
27 typedef std::map<std::string, PolicyTerms> PolicyMap;
28 
29 static bool ParseTerm(const xml_node &xterm, PolicyTerm *term) {
30  xml_attribute name = xterm.attribute("name");
31  term->term_name = name.value();
32 
33  xml_node xaction;
34  xml_node xmatch = xterm.child("from");
35 
36  if (xmatch) {
37  term->match.XmlParse(xmatch);
38  xaction = xmatch.next_sibling("then");
39  } else {
40  xaction = xterm.child("then");
41  }
42 
43  term->action.XmlParse(xaction);
44 
45  return true;
46 }
47 
48 static bool ParsePolicy(const xml_node &node, PolicyMap *policy) {
49  PolicyTerms policy_terms;
50  xml_attribute name = node.attribute("name");
51  for (xml_node xterm = node.child("term"); xterm;
52  xterm = xterm.next_sibling("term")) {
53  PolicyTerm *term_data = new PolicyTerm();
54  ParseTerm(xterm, term_data);
55  policy_terms.push_back(term_data);
56  }
57 
58  policy->insert(std::make_pair(name.value(), policy_terms));
59  return true;
60 }
61 
63 }
64 
65 
66 bool PolicyConfigParser::Parse(const std::string &content) {
67  istringstream sstream(content);
68  xml_document xdoc;
69  xml_parse_result result = xdoc.load(sstream);
70  if (!result) {
71  LOG(WARN, "Unable to load XML document. (status="
72  << result.status << ", offset=" << result.offset << ")");
73  return false;
74  }
75 
76  PolicyMap policy;
77  for (xml_node node = xdoc.first_child(); node; node = node.next_sibling()) {
78  if (strcmp(node.name(), "policy") == 0) {
79  ParsePolicy(node, &policy);
80  }
81  }
82  return true;
83 }
static bool ParseTerm(const xml_node &xterm, PolicyTerm *term)
autogen::PolicyMatch match
bool Parse(const std::string &content)
static bool ParsePolicy(const xml_node &node, PolicyMap *policy)
std::string term_name
std::map< std::string, PolicyTerms > PolicyMap
#define LOG(_Level, _Msg)
Definition: logging.h:33
boost::ptr_list< PolicyTerm > PolicyTerms
autogen::PolicyAction action