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