RESTSensorBase.h 5.02 KB
Newer Older
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
//================================================================================
// Name        : RESTSensorBase.h
// Author      : Michael Ott
// Contact     : info@dcdb.it
// Copyright   : Leibniz Supercomputing Centre
// Description : Sensor base class for REST plugin.
//================================================================================

//================================================================================
// This file is part of DCDB (DataCenter DataBase)
// Copyright (C) 2018-2021 Leibniz Supercomputing Centre
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//================================================================================

/**
 * @defgroup rest REST plugin
 * @ingroup  pusherplugins
 *
 * @brief Collect data from REST APIs
 */

#ifndef RESTSENSORBASE_H_
#define RESTSENSORBASE_H_

#include "RESTUnit.h"
#include "sensorbase.h"
#include <sstream>

/**
 * @brief SensorBase specialization for this plugin.
 *
 * @ingroup rest
 */
class RESTSensorBase : public SensorBase {
      public:
	RESTSensorBase(const std::string &name)
	    : SensorBase(name) {}

	RESTSensorBase(const RESTSensorBase &other)
	    : SensorBase(other),
	      _xmlPath(other._xmlPath) {}

	virtual ~RESTSensorBase() {}

	RESTSensorBase &operator=(const RESTSensorBase &other) {
		SensorBase::operator=(other);
		_xmlPath = other._xmlPath;

		return *this;
	}

	const xmlPathVector_t &getXMLPath() const { return _xmlPath; }
	std::string            getXMLPathString() const {
                std::stringstream ss;
                for (const auto &i : _xmlPath) {
                        ss << "." << std::get<0>(i) << "." << std::get<1>(i);
                        for (const auto &j : std::get<2>(i)) {
                                ss << "(" << j.first << "=" << j.second << ")";
                        }
                }
                return ss.str();
	}
	void setXMLPath(const std::string &path) {
		std::vector<std::string> subStrings;

		std::stringstream pathStream(path);
		std::string       item;
		//split into parts if a attribute (indicated by '(' ')' ) was defined
		while (std::getline(pathStream, item, ')')) {
			subStrings.push_back(item);
		}

		for (auto subStr : subStrings) {
			//extract the attributes from the path-parts
			if (subStr.find('(') != std::string::npos) { //attribute specified
				std::stringstream pathWithAttributesSStream(subStr);

				//split into path and attributes string
				std::string subPath, attributeString;
				std::getline(pathWithAttributesSStream, subPath, '(');
				std::getline(pathWithAttributesSStream, attributeString);

				if (subPath.front() == '.') {
					subPath.erase(0, 1);
				}

				//now further split the attributes string as multiple attributes could be defined
				std::vector<std::string> attributes;
				std::stringstream        attributeStream(attributeString);
				while (std::getline(attributeStream, item, ',')) {
					attributes.push_back(item);
				}
				attributesVector_t attrs;
				for (auto att : attributes) {
					//part attributes into name and value
					if (att.find('=') != std::string::npos) {
						std::stringstream attStream(att);

						std::string attName, attVal;
						std::getline(attStream, attName, '=');
						std::getline(attStream, attVal);

						attrs.push_back(std::make_pair(attName, attVal));
					} else { //should not happen. If it does the path was malformed
						//LOG(error) << "  Could not parse XML-path!";
						return;
					}
				}
				//split of the last child in the path. Required to iterate over multiple nodes which only differ in the attributes with BOOST_FOREACH
				auto index = subPath.find_last_of('.');
				if (index != std::string::npos) {
					std::string subPathChild(subPath.substr(++index));
					subPath.erase(--index);
					_xmlPath.push_back(std::make_tuple(subPath, subPathChild, attrs));
				} else { //the path contained only one node
					_xmlPath.push_back(std::make_tuple("", subPath, attrs));
				}
			} else { //no attributes specified. Last (sub)path
				if (subStr.front() == '.') {
					subStr.erase(0, 1);
				}
				_xmlPath.push_back(std::make_tuple(subStr, "", attributesVector_t()));
				break;
			}
		}
	}

	void printConfig(LOG_LEVEL ll, LOGGER &lg, unsigned leadingSpaces = 16) {
		std::string leading(leadingSpaces, ' ');
		LOG_VAR(ll) << leading << "    XML Path: " << getXMLPathString();
	}

      protected:
	xmlPathVector_t _xmlPath;
};

#endif /* RESTSENSORBASE_H_ */