SearchResult Tree::find(NodeRefPtr startNode, const ResourceName &policyRes, bool usePatterns = true) { am_resource_match_t compRes; std::list<NodeRefPtr>::const_iterator iter; const ResourceName& resName = startNode->getPolicyDecision()->getName(); compRes = compare(resName, policyRes, usePatterns); if(compRes != AM_SUB_RESOURCE_MATCH && compRes != AM_EXACT_PATTERN_MATCH) { if(compRes == AM_EXACT_MATCH) return SearchResult(AM_EXACT_MATCH, startNode); return SearchResult(compRes, Node::NULL_NODE); } for(iter = startNode->begin(); iter != startNode->end(); ++iter) { NodeRefPtr node = *iter; const PDRefCntPtr &element = node->getPolicyDecision(); switch((compare(element->getName(), policyRes, usePatterns))) { case AM_EXACT_MATCH: return SearchResult(AM_EXACT_MATCH, node); break; case AM_NO_MATCH: continue; break; case AM_SUPER_RESOURCE_MATCH: return SearchResult(AM_SUB_RESOURCE_MATCH, startNode); case AM_SUB_RESOURCE_MATCH: case AM_EXACT_PATTERN_MATCH: { SearchResult result = find(node, policyRes, usePatterns); switch(result.first) { // In case of the node below returns SUB_RESOURCE_MATCH, // EXACT_PATTERN_MATCH or EXACT_MATCH, // we just need to pass on the result upwards. case AM_SUB_RESOURCE_MATCH: case AM_EXACT_PATTERN_MATCH: case AM_EXACT_MATCH: // Bingo! got a match. return result; case AM_SUPER_RESOURCE_MATCH: case AM_NO_MATCH: return SearchResult(AM_SUB_RESOURCE_MATCH, startNode); } } break; } } // This is the level we have to add the node. if(iter == startNode->end()) { if(compRes == AM_SUB_RESOURCE_MATCH || compRes == AM_EXACT_PATTERN_MATCH) return SearchResult(compRes, startNode); else return SearchResult(compRes, Node::NULL_NODE); } return SearchResult(AM_NO_MATCH, Node::NULL_NODE); }
void Tree::search_recursive(const ResourceName &resName, const NodeRefPtr node, std::vector<PDRefCntPtr> &results, bool usePatterns) const { am_resource_match_t compRes; compRes = compare(node->getPolicyDecision()->getName(), resName, usePatterns); std::list<NodeRefPtr>::const_iterator iter; switch(compRes) { case AM_EXACT_MATCH: results.push_back(node->getPolicyDecision()); break; case AM_EXACT_PATTERN_MATCH: results.push_back(node->getPolicyDecision()); // fall through to the following case AM_SUB_RESOURCE_MATCH: case AM_NO_MATCH: for(iter = node->begin(); iter != node->end(); iter++) { search_recursive(resName, *iter, results, usePatterns); } break; default: break; } return; }
bool Tree::insertBelow(NodeRefPtr parent, PDRefCntPtr &elem) { std::list<NodeRefPtr>::iterator iter; NodeRefPtr newNode(new Node(parent, elem)); // add the subordinate nodes of newNode under // new node. In the next loop, we iterate nodes // under new node and remove them from parent. // Reason: if we remove nodes under parent in this // loop, iterator becomes invalid. ScopeLock myLock(treeLock); for(iter = parent->begin(); iter != parent->end(); iter++) { NodeRefPtr node = *iter; PDRefCntPtr data = node->getPolicyDecision(); switch(compare(data->getName(), elem->getName(), false)) { case AM_EXACT_MATCH: case AM_SUB_RESOURCE_MATCH: return false; case AM_NO_MATCH: break; case AM_SUPER_RESOURCE_MATCH: newNode->addNode(node); break; } } for(iter = newNode->begin(); iter != newNode->end(); iter++) { NodeRefPtr node = *iter; parent->remove(node); } parent->addNode(newNode); return true; }