Line data Source code
1 : /* 2 : * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved. 3 : * Copyright Contributors to the OpenSDN project 4 : */ 5 : #include "base/os.h" 6 : #include <iostream> 7 : #include <fstream> 8 : #include <pugixml/pugixml.hpp> 9 : #include <boost/uuid/uuid.hpp> 10 : 11 : #include <test/test_cmn_util.h> 12 : #include <pkt/test/test_pkt_util.h> 13 : #include <pkt/flow_mgmt.h> 14 : #include "test_xml.h" 15 : #include "test_xml_validate.h" 16 : 17 : using namespace std; 18 : using namespace pugi; 19 : using namespace boost::uuids; 20 : using namespace AgentUtXmlUtils; 21 : 22 : ///////////////////////////////////////////////////////////////////////////// 23 : // AgentUtXmlValidate routines 24 : ///////////////////////////////////////////////////////////////////////////// 25 82 : static bool CheckValidateNode(const string &node_name, const xml_node &node, 26 : string *name) { 27 82 : if (strcmp(node.name(), node_name.c_str()) != 0) { 28 0 : return false; 29 : } 30 : 31 82 : xml_attribute attr = node.attribute("name"); 32 82 : if (!attr) { 33 : cout << "Attribute \"name\" not found for " << node_name 34 0 : << ". Skipping..." << endl; 35 0 : return false;; 36 : } 37 : 38 82 : *name = attr.as_string(); 39 82 : if (*name == "") { 40 0 : cout << "Invalid \"name\" for " << node_name << " Skipping" << endl; 41 0 : return false; 42 : } 43 : 44 82 : return true; 45 : } 46 : 47 82 : static bool CheckValidateNodeWithUuid(const string &node_name, 48 : const xml_node &node, 49 : uuid *id, 50 : string *name) { 51 82 : if (CheckValidateNode(node_name, node, name) == false) 52 0 : return false; 53 : 54 82 : xml_attribute attr = node.attribute("uuid"); 55 82 : if (!attr) { 56 : cout << "Attribute \"uuid\" not found for " << node_name 57 0 : << ". Skipping..." << endl; 58 0 : return false;; 59 : } 60 : 61 82 : int x = attr.as_uint(); 62 82 : if (x == 0) { 63 0 : cout << "Invalid \"uuid\" (0) for " << node_name << " Skipping" << endl; 64 0 : return false; 65 : } 66 : 67 82 : *id = MakeUuid(x); 68 : 69 82 : return true; 70 : } 71 : 72 33 : AgentUtXmlValidate::AgentUtXmlValidate(const string &name, 73 : const xml_node &node, 74 33 : AgentUtXmlTestCase *test_case) : 75 33 : AgentUtXmlNode(name, node, false, test_case) { 76 33 : } 77 : 78 66 : AgentUtXmlValidate::~AgentUtXmlValidate() { 79 33 : for (AgentUtXmlValidationList::iterator i = node_list_.begin(); 80 115 : i != node_list_.end(); i++) { 81 82 : delete *i; 82 : } 83 66 : } 84 : 85 33 : void AgentUtXmlValidate::ToString(string *str) { 86 33 : AgentUtXmlNode::ToString(str); 87 33 : *str += "\n"; 88 33 : return; 89 : } 90 : 91 33 : string AgentUtXmlValidate::NodeType() { 92 33 : return "Validate"; 93 : } 94 : 95 33 : bool AgentUtXmlValidate::ReadXml() { 96 33 : AgentUtXmlNode::ReadXml(); 97 115 : for (xml_node n = node().first_child(); n; n = n.next_sibling()) { 98 : uuid id; 99 82 : string name; 100 82 : AgentUtXmlValidationNode *val = NULL; 101 : 102 : AgentUtXmlTest::AgentUtXmlTestValidateCreateFn fn = 103 164 : test_case()->test()->GetValidateCreateFn(n.name()); 104 82 : if (CheckValidateNodeWithUuid(n.name(), n, &id, &name) == true) { 105 82 : if (fn.empty() == false) 106 82 : val = fn(n.name(), name, id, n); 107 : } 108 : 109 82 : if (val != NULL) { 110 82 : val->ReadCmnXml(); 111 82 : if (val->ReadXml()) 112 82 : node_list_.push_back(val); 113 82 : cout << "Validate node " << val->ToString() << endl; 114 : } else { 115 0 : cout << "Unknown node name <" << n.name() << ">. Ignoring" << endl; 116 : } 117 82 : } 118 : 119 33 : return true; 120 : } 121 : 122 0 : bool AgentUtXmlValidate::ToXml(xml_node *parent) { 123 0 : assert(0); 124 : return true; 125 : } 126 : 127 33 : bool AgentUtXmlValidate::Run() { 128 33 : cout << "Running validation" << " <" << name() << ">" << endl; 129 33 : for (AgentUtXmlValidationList::iterator it = node_list_.begin(); 130 115 : it != node_list_.end(); it++) { 131 82 : TestClient::WaitForIdle(); 132 82 : AgentUtXmlValidationNode *node = *it; 133 82 : cout << "Validating " << node->ToString() << endl; 134 82 : uint32_t i = 0; 135 82 : bool ret = false; 136 82 : while (i < node->wait_count()) { 137 82 : TestClient::WaitForIdle(); 138 82 : if (node->Validate() == true) { 139 82 : ret = true; 140 82 : break; 141 : } 142 0 : usleep(node->sleep_time()); 143 0 : i++; 144 : } 145 82 : EXPECT_TRUE(ret); 146 82 : if (ret == false) { 147 0 : cout << "Failed validation of " << node->ToString() << endl; 148 : } 149 : } 150 : 151 33 : return true; 152 : } 153 : 154 : ///////////////////////////////////////////////////////////////////////////// 155 : // AgentUtXmlValidationNode routines 156 : ///////////////////////////////////////////////////////////////////////////// 157 82 : AgentUtXmlValidationNode::AgentUtXmlValidationNode(const string &name, 158 82 : const xml_node &node) : 159 82 : name_(name), node_(node), present_(true), delete_marked_(false) { 160 82 : } 161 : 162 82 : AgentUtXmlValidationNode::~AgentUtXmlValidationNode() { 163 82 : } 164 : 165 82 : bool AgentUtXmlValidationNode::ReadCmnXml() { 166 82 : std::string str; 167 82 : if (GetStringAttribute(node_, "present", &str)) { 168 34 : if (str == "no" || str == "0") 169 5 : present_ = false; 170 : else 171 29 : present_ = true; 172 : } 173 : 174 82 : if (GetStringAttribute(node_, "deleted", &str)) { 175 1 : delete_marked_ = true; 176 : } 177 : 178 82 : if (GetStringAttribute(node_, "del", &str)) { 179 0 : delete_marked_ = true; 180 : } 181 : 182 82 : if (GetUintAttribute(node_, "id", &id_) == false) 183 79 : GetUintAttribute(node_, "uuid", &id_); 184 : 185 82 : return true; 186 82 : } 187 : 188 1 : bool AgentUtXmlValidationNode::ReadXml() { 189 1 : return true; 190 : } 191 : 192 0 : bool AgentUtXmlValidationNode::Validate() { 193 0 : return true; 194 : }