1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright (C) 2018-2022 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#ifndef ISC_TRANSLATOR_TEST_H
#define ISC_TRANSLATOR_TEST_H 1

#include <yang/translator.h>

#include <vector><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <unordered_map><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

namespace isc {
namespace yang {
namespace test {

/// @brief Yang/sysrepo datastore textual representation class.
///
/// This class allows to store a configuration tree in YANG format.
/// It is used in tests to conduct operations on whole configurations.
class YangRepr {
public:
    /// @brief Constructor.
    ///
    /// @param model The model name.
    YangRepr(const std::string& model) : model_(model) {
    }

    /// @brief A struct representing a single entry.
    struct YangReprItem {
        /// @brief Constructor with content.
        ///
        /// @param xpath The xpath.
        /// @param value The textual value.
        /// @param type The type of the value.
        /// @param settable The settable flag.
        YangReprItem(std::string xpath, std::optional<std::string> value,
                     libyang::LeafBaseType type, bool settable)
            : xpath_(xpath), value_(value), type_(type), settable_(settable) {
        }

        /// @brief Retrieves configuration parameter from sysrepo.
        ///
        /// @param xpath The xpath of an element to be retrieved.
        /// @param session Sysrepo session.
        /// @return YangReprItem instance representing configuration parameter.
        static YangReprItem get(const std::string& xpath,
                                sysrepo::Session session);

        /// @brief The xpath.
        std::string xpath_;

        /// @brief The textual value.
        std::optional<std::string> value_;

        /// @brief The type of the value.
        libyang::LeafBaseType type_;

        /// @brief The settable flag.
        bool settable_;

        /// @brief The equal operator ignoring settable.
        ///
        /// @param other the other object to compare with.
        /// @return true if equal.
        bool operator==(const YangReprItem& other) const {
            return ((xpath_ == other.xpath_) &&
                    (value_ == other.value_) &&
                    (type_ == other.type_));
        }

        /// @brief The unequal operator ignoring settable.
        ///
        /// @param other the other object to compare with.
        /// @return false if equal.
        bool operator!=(const YangReprItem& other) const {
            return (!(*this == other));
        }

    private:
        /// @brief Gets the underlying type behind a union node.
        ///
        /// @param value the value obtained from the union node with ->asTerm().value()
        ///
        /// @return the underlying type
        static libyang::LeafBaseType getUnionType(libyang::Value const& value);
    };  // YangReprItem

    /// @brief Tree type.
    ///
    /// Indexed by xpath so that we can check against an entry received from
    /// sysrepo which has empirically proven to not come in a certain order
    /// starting with sysrepo 1.x. Relying on the order would have made a linear
    /// data structure more fitting.
    using Tree = std::unordered_map<std::string, YangReprItem>;

    /// @brief Get tree from session.
    ///
    /// @param session Sysrepo session.
    Tree get(sysrepo::Session session) const;

    /// @brief Verifies a tree.
    ///
    /// @param expected The expected value.
    /// @param session Sysrepo session.
    /// @param errs Error stream.
    /// @return true if verification succeeds, false with errors displayed.
    /// on errs if it fails.
    bool verify(const Tree& expected, sysrepo::Session session,
                std::ostream& errs) const;

    /// @brief Sets specified tree in a sysrepo.
    ///
    /// The actual access parameters are specified within session.
    ///
    /// @param tree The tree to install.
    /// @param session Sysrepo session.
    void set(const Tree& tree, sysrepo::Session session) const;

    /// @brief Convenience function that indexes a collection of items by xpath.
    ///
    /// @param v the input vector
    ///
    /// @return the output map
    static Tree buildTreeFromVector(std::vector<YangReprItem> const& v) {
        Tree tree;
        for (YangReprItem const& item : v) {
            if (tree.contains(item.xpath_)) {
                isc_throw(BadValue, "YangRepr::buildTreeFromVector(): duplicate " << item.xpath_);
            }
            tree.emplace(item.xpath_, item);
        }
        return tree;
    }

private:
    /// @brief The model name.
    std::string model_;
};  // YangRepr

/// @brief Alias for Items.
using YRItem = YangRepr::YangReprItem;

/// @brief Alias for Trees.
using YRTree = YangRepr::Tree;

/// @brief Overrides standard output operator for LeafBaseType.
std::ostream& operator<<(std::ostream& os, libyang::LeafBaseType type);

/// @brief Overrides standard output operator for @c YangReprItem object.
std::ostream& operator<<(std::ostream& os, const YRItem& item);

/// @brief Overrides standard output operator for @c Tree object.
std::ostream& operator<<(std::ostream& os, const YRTree& tree);

}  // namespace test
}  // namespace yang
}  // namespace isc

#endif  // ISC_TRANSLATOR_TEST_H