Line data Source code
1 : /*
2 : * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
3 : */
4 :
5 : #ifndef vnsw_agent_test_xml_test_xml_h
6 : #define vnsw_agent_test_xml_test_xml_h
7 :
8 : #include <iostream>
9 : #include <vector>
10 : #include <pugixml/pugixml.hpp>
11 : #include <boost/uuid/uuid.hpp>
12 :
13 : #include <base/util.h>
14 :
15 : class AgentUtXmlTestCase;
16 : class AgentUtXmlNode;
17 : class AgentUtXmlValidationNode;
18 :
19 : namespace AgentUtXmlUtils {
20 : bool GetStringAttribute(const pugi::xml_node &node, const std::string &name,
21 : std::string *value);
22 : bool GetUintAttribute(const pugi::xml_node &node, const std::string &name,
23 : uint16_t *value);
24 : bool GetIntAttribute(const pugi::xml_node &node, const std::string &name,
25 : int *value);
26 : bool GetBoolAttribute(const pugi::xml_node &node, const std::string &name,
27 : bool *value);
28 : void NovaIntfAdd(bool op_delete, const boost::uuids::uuid &id,
29 : const Ip4Address &ip, const boost::uuids::uuid &vm_uuid,
30 : const boost::uuids::uuid vn_uuid, const std::string &name,
31 : const std::string &mac, const std::string vm_name);
32 : void LinkXmlNode(pugi::xml_node *parent, const std::string <ype,
33 : const std::string lname, const std::string &rtype,
34 : const std::string rname);
35 : pugi::xml_node AddXmlNodeWithAttr(pugi::xml_node *parent, const char *attr);
36 : pugi::xml_node AddXmlNodeWithValue(pugi::xml_node *parent, const char *name,
37 : const std::string &value);
38 : pugi::xml_node AddXmlNodeWithIntValue(pugi::xml_node *parent, const char *name,
39 : int val);
40 : }
41 :
42 : class AgentUtXmlTest {
43 : private:
44 : typedef std::vector<AgentUtXmlTestCase *> AgentUtXmlTestList;
45 : public:
46 : typedef boost::function<AgentUtXmlNode *(const std::string type,
47 : const std::string &name,
48 : const boost::uuids::uuid &id,
49 : const pugi::xml_node &node,
50 : AgentUtXmlTestCase *test_case)>
51 : AgentUtXmlTestConfigCreateFn;
52 :
53 : typedef boost::function<AgentUtXmlValidationNode *
54 : (const std::string &type, const std::string &name,
55 : const boost::uuids::uuid &id, const pugi::xml_node &node)>
56 : AgentUtXmlTestValidateCreateFn;
57 :
58 : typedef std::map<std::string, AgentUtXmlTestConfigCreateFn>
59 : AgentUtXmlTestConfigFactory;
60 : typedef std::map<std::string, AgentUtXmlTestValidateCreateFn>
61 : AgentUtXmlTestValidateFactory;
62 :
63 : AgentUtXmlTest(const std::string &file_name);
64 : virtual ~AgentUtXmlTest();
65 :
66 : bool Load();
67 : bool ReadXml();
68 : void ToString(std::string *str);
69 : bool Run();
70 : bool Run(std::string test_case);
71 : void AddConfigEntry(const std::string &name,
72 : AgentUtXmlTestConfigCreateFn fn);
73 : void AddValidateEntry(const std::string &name,
74 : AgentUtXmlTestValidateCreateFn fn);
75 : AgentUtXmlTestConfigCreateFn GetConfigCreateFn(const std::string &name);
76 : AgentUtXmlTestValidateCreateFn GetValidateCreateFn(const std::string &name);
77 : private:
78 : std::string file_name_;
79 : std::string name_;
80 : pugi::xml_document doc_;
81 : AgentUtXmlTestList test_list_;
82 : AgentUtXmlTestConfigFactory config_factory_;
83 : AgentUtXmlTestValidateFactory validate_factory_;
84 : DISALLOW_COPY_AND_ASSIGN(AgentUtXmlTest);
85 : };
86 :
87 : class AgentUtXmlTestCase {
88 : private:
89 : typedef std::vector<AgentUtXmlNode *> AgentUtXmlNodeList;
90 : public:
91 : AgentUtXmlTestCase(const std::string &name, const pugi::xml_node &node,
92 : AgentUtXmlTest *test);
93 : virtual ~AgentUtXmlTestCase();
94 0 : AgentUtXmlTest *test() { return test_; }
95 :
96 : virtual bool ReadXml();
97 : virtual bool Run();
98 : virtual void ToString(std::string *str);
99 0 : void set_verbose(bool val) { verbose_ = val; }
100 : bool verbose() const { return verbose_; }
101 0 : const std::string &name() const { return name_; }
102 : private:
103 : std::string name_;
104 : pugi::xml_node xml_node_;
105 : AgentUtXmlNodeList node_list_;
106 : pugi::xml_document gen_doc_;
107 : AgentUtXmlTest *test_;
108 : bool verbose_;
109 : DISALLOW_COPY_AND_ASSIGN(AgentUtXmlTestCase);
110 : };
111 :
112 : class AgentUtXmlNode {
113 : public:
114 : AgentUtXmlNode(const std::string &name, const pugi::xml_node &node,
115 : AgentUtXmlTestCase *test_case);
116 : AgentUtXmlNode(const std::string &name, const pugi::xml_node &node,
117 : bool gen_xml, AgentUtXmlTestCase *test_case);
118 : virtual ~AgentUtXmlNode();
119 :
120 : virtual bool ReadXml() = 0;
121 : virtual bool ToXml(pugi::xml_node *parent) = 0;
122 : virtual std::string NodeType() = 0;
123 : virtual void ToString(std::string *str);
124 0 : virtual bool Run() { assert(0); return false; }
125 :
126 : void set_gen_xml(bool val) { gen_xml_ = val; }
127 0 : bool gen_xml() const { return gen_xml_; }
128 0 : bool op_delete() const { return op_delete_; }
129 0 : void set_op_delete(bool val) { op_delete_ = val; }
130 0 : const std::string &name() const { return name_; }
131 0 : const pugi::xml_node &node() const { return node_; }
132 0 : AgentUtXmlTestCase *test_case() { return test_case_; }
133 : protected:
134 : pugi::xml_node gen_node_;
135 : private:
136 : pugi::xml_node node_;
137 : std::string name_;
138 : bool op_delete_;
139 : bool gen_xml_;
140 : AgentUtXmlTestCase *test_case_;
141 : DISALLOW_COPY_AND_ASSIGN(AgentUtXmlNode);
142 : };
143 :
144 : /////////////////////////////////////////////////////////////////////////////
145 : // Task control nodes
146 : /////////////////////////////////////////////////////////////////////////////
147 : class AgentUtXmlTask : public AgentUtXmlNode {
148 : public:
149 : AgentUtXmlTask(const pugi::xml_node &node, AgentUtXmlTestCase *test_case);
150 : virtual ~AgentUtXmlTask();
151 :
152 : virtual bool ReadXml();
153 : virtual bool ToXml(pugi::xml_node *parent);
154 : virtual std::string NodeType();
155 : virtual void ToString(std::string *str);
156 : virtual bool Run();
157 :
158 : private:
159 : pugi::xml_node xml_;
160 : std::string stop_;
161 : DISALLOW_COPY_AND_ASSIGN(AgentUtXmlTask);
162 : };
163 :
164 : /////////////////////////////////////////////////////////////////////////////
165 : // Link nodes
166 : /////////////////////////////////////////////////////////////////////////////
167 : class AgentUtXmlLink : public AgentUtXmlNode {
168 : public:
169 : AgentUtXmlLink(const pugi::xml_node &node, AgentUtXmlTestCase *test_case);
170 : virtual ~AgentUtXmlLink();
171 :
172 : virtual bool ReadXml();
173 : virtual bool ToXml(pugi::xml_node *parent);
174 : virtual std::string NodeType();
175 : virtual void ToString(std::string *str);
176 :
177 : private:
178 : pugi::xml_node xml_;
179 : std::string l_node_;
180 : std::string l_name_;
181 : std::string r_node_;
182 : std::string r_name_;
183 : DISALLOW_COPY_AND_ASSIGN(AgentUtXmlLink);
184 : };
185 :
186 : /////////////////////////////////////////////////////////////////////////////
187 : // Config nodes
188 : /////////////////////////////////////////////////////////////////////////////
189 : class AgentUtXmlConfig : public AgentUtXmlNode {
190 : public:
191 : AgentUtXmlConfig(const std::string &name, const boost::uuids::uuid &uuid,
192 : const pugi::xml_node &node, AgentUtXmlTestCase *test_case);
193 : AgentUtXmlConfig(const std::string &name, const boost::uuids::uuid &uuid,
194 : const pugi::xml_node &node, bool gen_xml,
195 : AgentUtXmlTestCase *test_case);
196 : virtual ~AgentUtXmlConfig();
197 :
198 : virtual bool ReadXml();
199 : virtual void ToString(std::string *str);
200 0 : const boost::uuids::uuid &id() const { return id_; }
201 : protected:
202 : void AddIdPerms(pugi::xml_node *parent);
203 : private:
204 : boost::uuids::uuid id_;
205 : DISALLOW_COPY_AND_ASSIGN(AgentUtXmlConfig);
206 : };
207 :
208 : #endif //vnsw_agent_test_xml_test_xml_h
|