void EnvironmentNode::setAttributeValue(const std::string &attrName, const std::string &value, Status &status, bool allowInvalid, bool forceCreate) { std::shared_ptr<EnvironmentValue> pEnvValue; auto it = m_attributes.find(attrName); if (it != m_attributes.end()) { pEnvValue = it->second; } // // Not found on this node. See if the configuration defines the attribute. If so, set the value and move on. // If not and the forceCreate flag is set, create it. else if (forceCreate) { std::shared_ptr<SchemaValue> pCfgValue = m_pSchemaItem->getAttribute(attrName); pEnvValue = std::make_shared<EnvironmentValue>(shared_from_this(), pCfgValue, attrName); addAttribute(attrName, pEnvValue); if (!pCfgValue->isDefined()) { status.addMsg(statusMsg::warning, getId(), attrName, "Undefined attribute did not exist in configuration, was created"); } } if (pEnvValue) { pEnvValue->setValue(value, &status, allowInvalid); } else { status.addMsg(statusMsg::error, getId(), attrName, "The attribute does not exist and was not created"); } }
std::shared_ptr<EnvironmentNode> EnvironmentMgr::addNewEnvironmentNode(const std::string &parentNodeId, const std::string &configType, Status &status) { std::shared_ptr<EnvironmentNode> pNewNode; std::shared_ptr<EnvironmentNode> pParentNode = getEnvironmentNode(parentNodeId); if (pParentNode) { std::shared_ptr<SchemaItem> pNewCfgItem; std::vector<std::shared_ptr<SchemaItem>> insertableItems; pParentNode->getInsertableItems(insertableItems); for (auto it = insertableItems.begin(); it != insertableItems.end(); ++it) { if ((*it)->getItemType() == configType) { pNewNode = addNewEnvironmentNode(pParentNode, *it, status); break; } if (pNewNode == nullptr) { status.addMsg(statusMsg::error, "Configuration type (" + configType + ") not found"); } } } else { status.addMsg(statusMsg::error, parentNodeId, "", "", "Unable to find indicated parent node"); } return pNewNode; }
void EnvironmentMgr::validate(Status &status) const { if (m_pRootNode) { m_pRootNode->validate(status, true); } else { status.addMsg(statusMsg::error, "No environment loaded"); } }
bool EnvironmentMgr::removeEnvironmentNode(const std::string &nodeId, Status &status) { bool rc = false; std::shared_ptr<EnvironmentNode> pNode = getEnvironmentNode(nodeId); if (pNode) { std::shared_ptr<EnvironmentNode> pParentNode = pNode->getParent(); if (pParentNode->removeChild(pNode)) { m_nodeIds.erase(nodeId); } else { status.addMsg(statusMsg::error, nodeId, "", "", "Unable to remove the node"); } } else { status.addMsg(statusMsg::error, nodeId, "", "", "Unable to find indicated node"); } return rc; }
void SchemaValue::validate(Status &status, const std::string &id, const EnvironmentValue *pEnvValue) const { bool isValid = true; if (pEnvValue == nullptr) { std::string msg = "Attempt to validate schema value w/o an environment value."; throw(std::runtime_error(msg)); } // // If we have an environment value, more specific information can be provided if (pEnvValue) { std::string curValue = pEnvValue->getValue(); isValid = m_pType->isValueValid(curValue); // // See if there is a dependency on another value being set. if (!m_requiredIf.empty() && isValid) { // // Required if string format is an xpath. Search this environment value's owning node // for a match. std::vector<std::shared_ptr<EnvironmentNode>> nodes; pEnvValue->getEnvironmentNode()->fetchNodes(m_requiredIf, nodes); if (!nodes.empty()) { // // Since here is a match for a requiredIf, this value MUST be set if (pEnvValue->getValue().empty()) { isValid = false; std::string msg = "Environment value required based on requiredIf rule " + m_requiredIf + " being set."; status.addMsg(statusMsg::error, pEnvValue->getNodeId(), pEnvValue->getName(), msg); } } } // // If not valid, provide the reason if (!isValid) { std::string msg; if (pEnvValue->wasForced()) msg = "Value was forced to an invalid value (" + curValue + ")."; else msg = "Value is invalid (" + curValue + ")."; msg += " Valid value (" + m_pType->getLimitString() + ")"; status.addMsg(pEnvValue->wasForced() ? statusMsg::warning : statusMsg::error, pEnvValue->getNodeId(), pEnvValue->getName(), msg); } // // Otherwise, the value is valid, but there could be a validate message else { const std::string &validateMsg = m_pType->getValidateMsg(); if (!validateMsg.empty()) { status.addMsg(status.getMsgLevelFromString(m_pType->getValidateMsgType()), pEnvValue->getNodeId(), pEnvValue->getName(), validateMsg); } } } }
void EnvironmentNode::validate(Status &status, bool includeChildren, bool includeHiddenNodes) const { if (!m_pSchemaItem->isHidden() || includeHiddenNodes) { // // Check node value if (m_pLocalValue) { m_pLocalValue->validate(status, m_id); } // // Check any attributes for (auto attrIt = m_attributes.begin(); attrIt != m_attributes.end(); ++attrIt) { attrIt->second->validate(status, m_id); // // If this value must be unique, make sure it is if (attrIt->second->getSchemaValue()->isUniqueValue()) { bool found = false; std::vector<std::string> allValues; attrIt->second->getAllValuesForSiblings(allValues); std::set<std::string> unquieValues; for (auto it = allValues.begin(); it != allValues.end() && !found; ++it) { auto ret = unquieValues.insert(*it); found = !ret.second; } if (found) { status.addUniqueMsg(statusMsg::error, m_id, attrIt->second->getName(), "Attribute value must be unique"); } } // // Does this value need to be from another set of values? if (attrIt->second->getSchemaValue()->isFromUniqueValueSet()) { bool found = false; std::vector<std::string> allValues; attrIt->second->getSchemaValue()->getAllKeyRefValues(allValues); for (auto it = allValues.begin(); it != allValues.end() && !found; ++it) found = *it == attrIt->second->getValue(); if (!found) { status.addMsg(statusMsg::error, m_id, attrIt->second->getName(), "Attribute value must be from a unique set"); } } } // // Now check all children if (includeChildren) { for (auto childIt = m_children.begin(); childIt != m_children.end(); ++childIt) { childIt->second->validate(status, includeChildren, includeHiddenNodes); } } } }