Reimplement Schema::is* methods with Schema::nodeType

Change-Id: I00653c6ff387f7a9b20970afd7ceb9da0fbdb1b5
diff --git a/src/schema.cpp b/src/schema.cpp
index c390e31..2cfcbf2 100644
--- a/src/schema.cpp
+++ b/src/schema.cpp
@@ -9,3 +9,40 @@
 #include "schema.hpp"
 
 Schema::~Schema() = default;
+
+bool Schema::isList(const schemaPath_& location, const ModuleNodePair& node) const
+{
+    try {
+        return nodeType(location, node) == yang::NodeTypes::List;
+    } catch (InvalidNodeException&) {
+        return false;
+    }
+}
+
+bool Schema::isPresenceContainer(const schemaPath_& location, const ModuleNodePair& node) const
+{
+    try {
+        return nodeType(location, node) == yang::NodeTypes::PresenceContainer;
+    } catch (InvalidNodeException&) {
+        return false;
+    }
+}
+
+bool Schema::isContainer(const schemaPath_& location, const ModuleNodePair& node) const
+{
+    try {
+        auto type = nodeType(location, node);
+        return type == yang::NodeTypes::Container || type == yang::NodeTypes::PresenceContainer;
+    } catch (InvalidNodeException&) {
+        return false;
+    }
+}
+
+bool Schema::isLeaf(const schemaPath_& location, const ModuleNodePair& node) const
+{
+    try {
+        return nodeType(location, node) == yang::NodeTypes::Leaf;
+    } catch (InvalidNodeException&) {
+        return false;
+    }
+}
diff --git a/src/schema.hpp b/src/schema.hpp
index 6970fe9..a85961b 100644
--- a/src/schema.hpp
+++ b/src/schema.hpp
@@ -63,13 +63,13 @@
 public:
     virtual ~Schema();
 
-    virtual bool isContainer(const schemaPath_& location, const ModuleNodePair& node) const = 0;
-    virtual bool isLeaf(const schemaPath_& location, const ModuleNodePair& node) const = 0;
+    bool isContainer(const schemaPath_& location, const ModuleNodePair& node) const;
+    bool isLeaf(const schemaPath_& location, const ModuleNodePair& node) const;
+    bool isList(const schemaPath_& location, const ModuleNodePair& node) const;
+    bool isPresenceContainer(const schemaPath_& location, const ModuleNodePair& node) const;
     virtual yang::NodeTypes nodeType(const std::string& path) const = 0;
     virtual yang::NodeTypes nodeType(const schemaPath_& location, const ModuleNodePair& node) const = 0;
     virtual bool isModule(const std::string& name) const = 0;
-    virtual bool isList(const schemaPath_& location, const ModuleNodePair& node) const = 0;
-    virtual bool isPresenceContainer(const schemaPath_& location, const ModuleNodePair& node) const = 0;
     virtual bool leafEnumHasValue(const schemaPath_& location, const ModuleNodePair& node, const std::string& value) const = 0;
     virtual bool leafIdentityIsValid(const schemaPath_& location, const ModuleNodePair& node, const ModuleValuePair& value) const = 0;
     virtual bool listHasKey(const schemaPath_& location, const ModuleNodePair& node, const std::string& key) const = 0;
diff --git a/src/static_schema.cpp b/src/static_schema.cpp
index aae6b84..d2287dc 100644
--- a/src/static_schema.cpp
+++ b/src/static_schema.cpp
@@ -34,16 +34,6 @@
     return m_modules.find(name) != m_modules.end();
 }
 
-bool StaticSchema::isContainer(const schemaPath_& location, const ModuleNodePair& node) const
-{
-    std::string locationString = pathToSchemaString(location, Prefixes::Always);
-    auto fullName = fullNodeName(location, node);
-    if (!nodeExists(locationString, fullName))
-        return false;
-
-    return children(locationString).at(fullName).type() == typeid(yang::container);
-}
-
 void StaticSchema::addContainer(const std::string& location, const std::string& name, yang::ContainerTraits isPresence)
 {
     m_nodes.at(location).emplace(name, yang::container{isPresence});
@@ -73,19 +63,6 @@
     return list.m_keys;
 }
 
-bool StaticSchema::isList(const schemaPath_& location, const ModuleNodePair& node) const
-{
-    std::string locationString = pathToSchemaString(location, Prefixes::Always);
-    auto fullName = fullNodeName(location, node);
-    if (!nodeExists(locationString, fullName))
-        return false;
-    const auto& child = children(locationString).at(fullName);
-    if (child.type() != typeid(yang::list))
-        return false;
-
-    return true;
-}
-
 void StaticSchema::addList(const std::string& location, const std::string& name, const std::set<std::string>& keys)
 {
     m_nodes.at(location).emplace(name, yang::list{keys});
@@ -94,14 +71,6 @@
     m_nodes.emplace(key, std::unordered_map<std::string, NodeType>());
 }
 
-bool StaticSchema::isPresenceContainer(const schemaPath_& location, const ModuleNodePair& node) const
-{
-    if (!isContainer(location, node))
-        return false;
-    std::string locationString = pathToSchemaString(location, Prefixes::Always);
-    return boost::get<yang::container>(children(locationString).at(fullNodeName(location, node))).m_presence == yang::ContainerTraits::Presence;
-}
-
 void StaticSchema::addLeaf(const std::string& location, const std::string& name, const yang::LeafDataTypes& type)
 {
     m_nodes.at(location).emplace(name, yang::leaf{type, {}, {}, {}});
@@ -203,16 +172,6 @@
     return std::any_of(identities.begin(), identities.end(), [toFind = identModule + ":" + value.second](const auto& x) { return x == toFind; });
 }
 
-bool StaticSchema::isLeaf(const schemaPath_& location, const ModuleNodePair& node) const
-{
-    std::string locationString = pathToSchemaString(location, Prefixes::Always);
-    auto fullName = fullNodeName(location, node);
-    if (!nodeExists(locationString, fullName))
-        return false;
-
-    return children(locationString).at(fullName).type() == typeid(yang::leaf);
-}
-
 std::string lastNodeOfSchemaPath(const std::string& path)
 {
     std::string res = path;
diff --git a/src/static_schema.hpp b/src/static_schema.hpp
index 8ff3405..c2623a5 100644
--- a/src/static_schema.hpp
+++ b/src/static_schema.hpp
@@ -48,13 +48,9 @@
 public:
     StaticSchema();
 
-    bool isContainer(const schemaPath_& location, const ModuleNodePair& node) const override;
     yang::NodeTypes nodeType(const std::string& path) const override;
     yang::NodeTypes nodeType(const schemaPath_& location, const ModuleNodePair& node) const override;
     bool isModule(const std::string& name) const override;
-    bool isLeaf(const schemaPath_& location, const ModuleNodePair& node) const override;
-    bool isList(const schemaPath_& location, const ModuleNodePair& node) const override;
-    bool isPresenceContainer(const schemaPath_& location, const ModuleNodePair& node) const override;
     bool leafEnumHasValue(const schemaPath_& location, const ModuleNodePair& node, const std::string& value) const override;
     bool leafIdentityIsValid(const schemaPath_& location, const ModuleNodePair& node, const ModuleValuePair& value) const override;
     bool listHasKey(const schemaPath_& location, const ModuleNodePair& node, const std::string& key) const override;
diff --git a/src/yang_schema.cpp b/src/yang_schema.cpp
index 3470865..10565d3 100644
--- a/src/yang_schema.cpp
+++ b/src/yang_schema.cpp
@@ -93,31 +93,6 @@
     return set.find(name) != set.end();
 }
 
-bool YangSchema::isContainer(const schemaPath_& location, const ModuleNodePair& node) const
-{
-    const auto schemaNode = getSchemaNode(location, node);
-    return schemaNode && schemaNode->nodetype() == LYS_CONTAINER;
-}
-
-bool YangSchema::isLeaf(const schemaPath_& location, const ModuleNodePair& node) const
-{
-    const auto schemaNode = getSchemaNode(location, node);
-    return schemaNode && schemaNode->nodetype() == LYS_LEAF;
-}
-
-bool YangSchema::isList(const schemaPath_& location, const ModuleNodePair& node) const
-{
-    const auto schemaNode = getSchemaNode(location, node);
-    return schemaNode && schemaNode->nodetype() == LYS_LIST;
-}
-
-bool YangSchema::isPresenceContainer(const schemaPath_& location, const ModuleNodePair& node) const
-{
-    if (!isContainer(location, node))
-        return false;
-    return libyang::Schema_Node_Container(getSchemaNode(location, node)).presence();
-}
-
 bool YangSchema::leafEnumHasValue(const schemaPath_& location, const ModuleNodePair& node, const std::string& value) const
 {
     auto enums = enumValues(location, node);
@@ -427,7 +402,7 @@
     case LYS_LIST:
         return yang::NodeTypes::List;
     default:
-        throw std::runtime_error{"YangSchema::nodeType: unsupported type"};
+        throw InvalidNodeException(); // FIXME: Implement all types.
     }
 }
 }
diff --git a/src/yang_schema.hpp b/src/yang_schema.hpp
index dd88145..ea204e2 100644
--- a/src/yang_schema.hpp
+++ b/src/yang_schema.hpp
@@ -30,13 +30,9 @@
     YangSchema(std::shared_ptr<libyang::Context> lyCtx);
     ~YangSchema() override;
 
-    bool isContainer(const schemaPath_& location, const ModuleNodePair& node) const override;
-    bool isLeaf(const schemaPath_& location, const ModuleNodePair& node) const override;
     yang::NodeTypes nodeType(const std::string& path) const override;
     yang::NodeTypes nodeType(const schemaPath_& location, const ModuleNodePair& node) const override;
     bool isModule(const std::string& name) const override;
-    bool isList(const schemaPath_& location, const ModuleNodePair& node) const override;
-    bool isPresenceContainer(const schemaPath_& location, const ModuleNodePair& node) const override;
     bool leafEnumHasValue(const schemaPath_& location, const ModuleNodePair& node, const std::string& value) const override;
     bool leafIdentityIsValid(const schemaPath_& location, const ModuleNodePair& node, const ModuleValuePair& value) const override;
     bool listHasKey(const schemaPath_& location, const ModuleNodePair& node, const std::string& key) const override;