Commit 32987202 authored by Cedric Caffy's avatar Cedric Caffy
Browse files

DiskSystem jsonified and sent to stdin of the script

parent 9f227007
...@@ -105,7 +105,8 @@ set (COMMON_LIB_SRC_FILES ...@@ -105,7 +105,8 @@ set (COMMON_LIB_SRC_FILES
exception/TimeOut.cpp exception/TimeOut.cpp
exception/UserError.cpp exception/UserError.cpp
exception/XrootCl.cpp exception/XrootCl.cpp
json/parser/JSONCParser.cpp json/object/JSONObject.cpp
json/object/JSONCObject.cpp
log/DummyLogger.cpp log/DummyLogger.cpp
log/FileLogger.cpp log/FileLogger.cpp
log/LogContext.cpp log/LogContext.cpp
...@@ -204,7 +205,8 @@ set (COMMON_UNIT_TESTS_LIB_SRC_FILES ...@@ -204,7 +205,8 @@ set (COMMON_UNIT_TESTS_LIB_SRC_FILES
utils/UtilsTest.cpp utils/UtilsTest.cpp
optionalTest.cpp optionalTest.cpp
rangeTest.cpp rangeTest.cpp
json/parser/JSONCParserTest.cpp json/test/JSONCTestObject.cpp
json/test/JSONCObjectTest.cpp
) )
add_library (ctacommonunittests SHARED add_library (ctacommonunittests SHARED
......
...@@ -16,100 +16,89 @@ ...@@ -16,100 +16,89 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <json-c/json.h> #include "JSONCObject.hpp"
#include <json/json_object.h>
#include "JSONCParser.hpp" #include "JSONObjectException.hpp"
#include "common/json/object/SchedulerHints.hpp"
#include "common/json/object/TestObject.hpp"
#include "common/exception/Exception.hpp"
#include "JSONParserException.hpp"
namespace cta { namespace utils { namespace json { namespace parser { namespace cta { namespace utils { namespace json { namespace object {
JSONCParser::JSONCParser() { JSONCObject::JSONCObject():JSONObject() {
m_jsonObject = json_object_new_object(); initializeJSONCObject();
} }
void JSONCParser::setJSONToBeParsed(const std::string& json){ void JSONCObject::setAttributesFromJSON(const std::string& json){
//DO JSON_C deinitialization //DO JSON_C deinitialization
if(m_jsonObject != nullptr){ if(m_jsonObject != nullptr){
json_object_put(m_jsonObject); destroyJSONCObject();
m_jsonObject = nullptr;
} }
m_jsonObject = json_tokener_parse(json.c_str()); m_jsonObject = json_tokener_parse(json.c_str());
} }
std::string JSONCParser::getJSON() const { std::string JSONCObject::getJSON() {
return std::string(json_object_to_json_string_ext(m_jsonObject, JSON_C_TO_STRING_PLAIN)); return std::string(json_object_to_json_string_ext(m_jsonObject, JSON_C_TO_STRING_PLAIN));
} }
JSONCParser::~JSONCParser() { void JSONCObject::initializeJSONCObject() {
//Free the JSON object if initialized m_jsonObject = json_object_new_object();
if(m_jsonObject != nullptr){ }
json_object_put(m_jsonObject);
m_jsonObject = nullptr; void JSONCObject::destroyJSONCObject() {
} json_object_put(m_jsonObject);
m_jsonObject = nullptr;
} }
json_object * JSONCParser::getJSONObject(const std::string& key){ void JSONCObject::reinitializeJSONCObject() {
destroyJSONCObject();
initializeJSONCObject();
}
json_object * JSONCObject::getJSONObject(const std::string& key){
json_object * objectRet; json_object * objectRet;
if(json_object_object_get_ex(m_jsonObject,key.c_str(),&objectRet)){ if(json_object_object_get_ex(m_jsonObject,key.c_str(),&objectRet)){
return objectRet; return objectRet;
} }
std::string errMsg = "In JSONCParser::getJSONObject(), the provided json does not contain any key named \""+key+"\"."; std::string errMsg = "In JSONCParser::getJSONObject(), the provided json does not contain any key named \""+key+"\".";
throw cta::exception::JSONParserException(errMsg); throw cta::exception::JSONObjectException(errMsg);
} }
////////////////////////////////////////////////////////////////////
// JSONCParser::getValue() implementation START
////////////////////////////////////////////////////////////////////
template<> template<>
std::string JSONCParser::getValue(const std::string& key){ std::string JSONCObject::jsonGetValue(const std::string& key){
json_object * jsonObj = getJSONObject(key); json_object * jsonObj = getJSONObject(key);
return std::string(json_object_get_string(jsonObj)); return std::string(json_object_get_string(jsonObj));
} }
template<> template<>
uint64_t JSONCParser::getValue(const std::string & key){ uint64_t JSONCObject::jsonGetValue(const std::string & key){
json_object * jsonObj = getJSONObject(key); json_object * jsonObj = getJSONObject(key);
return json_object_get_int64(jsonObj); return json_object_get_int64(jsonObj);
} }
template<> template<>
double JSONCParser::getValue(const std::string & key){ double JSONCObject::jsonGetValue(const std::string & key){
json_object * jsonObj = getJSONObject(key); json_object * jsonObj = getJSONObject(key);
return json_object_get_double(jsonObj); return json_object_get_double(jsonObj);
} }
////////////////////////////////////////////////////////////////////
// JSONCParser::getValue() implementation END
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
// json::object::TestObject implementation START
////////////////////////////////////////////////////////////////////
template<> template<>
void JSONCParser::generateJSONFromObject(const object::TestObject & value){ void JSONCObject::jsonSetValue(const std::string& key, const std::string & value){
json_object_object_add(m_jsonObject,"integer_number",json_object_new_int64(value.integer_number)); json_object_object_add(m_jsonObject,key.c_str(),json_object_new_string(value.c_str()));
json_object_object_add(m_jsonObject,"str",json_object_new_string(value.str.c_str()));
json_object_object_add(m_jsonObject,"double_number",json_object_new_double(value.double_number));
} }
template<> template<>
object::TestObject JSONCParser::getObjectFromJSON(){ void JSONCObject::jsonSetValue(const std::string& key, const double & value){
object::TestObject ret; json_object_object_add(m_jsonObject,key.c_str(),json_object_new_double(value));
ret.str = getValue<std::string>("str");
ret.integer_number = getValue<uint64_t>("integer_number");
ret.double_number = getValue<double>("double_number");
return ret;
} }
////////////////////////////////////////////////////////////////////
// json::object::TestObject implementation END
////////////////////////////////////////////////////////////////////
template <> template<>
void JSONCParser::generateJSONFromObject(const object::SchedulerHints & value){ void JSONCObject::jsonSetValue(const std::string& key, const uint64_t & value){
json_object_object_add(m_jsonObject,key.c_str(),json_object_new_int64(value));
}
JSONCObject::~JSONCObject() {
//Free the JSON object if initialized
if(m_jsonObject != nullptr){
destroyJSONCObject();
}
} }
}}}} }}}}
\ No newline at end of file
...@@ -18,36 +18,32 @@ ...@@ -18,36 +18,32 @@
#pragma once #pragma once
#include <string> #include "JSONObject.hpp"
#include <json-c/json.h> #include <json-c/json.h>
#include <memory>
namespace cta { namespace utils { namespace json { namespace parser { namespace cta { namespace utils { namespace json { namespace object {
class JSONCParser { class JSONCObject : public JSONObject {
public: public:
JSONCParser(); JSONCObject();
virtual void setAttributesFromJSON(const std::string & json);
void setJSONToBeParsed(const std::string & json); virtual std::string getJSON();
virtual ~JSONCObject();
protected:
json_object * m_jsonObject = nullptr;
std::string getJSON() const; void initializeJSONCObject();
void destroyJSONCObject();
void reinitializeJSONCObject();
template<typename T> template<typename T>
void generateJSONFromObject(const T & value); T jsonGetValue(const std::string & key);
template<typename T> template<typename T>
T getObjectFromJSON(); void jsonSetValue(const std::string & key, const T & value);
virtual ~JSONCParser();
private:
json_object * m_jsonObject = nullptr;
template<typename T>
T getValue(const std::string & key);
json_object * getJSONObject(const std::string & key); json_object * getJSONObject(const std::string & key);
}; };
}}}}
}}}} \ No newline at end of file
...@@ -16,15 +16,11 @@ ...@@ -16,15 +16,11 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#pragma once #include "JSONObject.hpp"
#include <string>
namespace cta { namespace utils { namespace json { namespace object { namespace cta { namespace utils { namespace json { namespace object {
struct SchedulerHints { JSONObject::~JSONObject() {
int test; }
std::string test2;
}; }}}}
\ No newline at end of file
}}}}
/*
* The CERN Tape Archive (CTA) project
* Copyright (C) 2019 CERN
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <string>
namespace cta { namespace utils { namespace json { namespace object {
class JSONObject {
public:
virtual std::string getJSON() = 0;
virtual void setAttributesFromJSON(const std::string & json) = 0;
virtual ~JSONObject();
private:
};
}}}}
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
namespace cta { namespace exception { namespace cta { namespace exception {
class JSONParserException : public Exception { class JSONObjectException : public Exception {
using Exception::Exception; using Exception::Exception;
}; };
......
...@@ -18,52 +18,38 @@ ...@@ -18,52 +18,38 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "common/json/parser/JSONCParser.hpp" #include "JSONCTestObject.hpp"
#include "common/json/object/TestObject.hpp" #include "common/json/object/JSONObjectException.hpp"
#include "JSONParserException.hpp"
namespace unitTests { namespace unitTests {
using namespace cta::utils; using namespace cta::utils;
TEST(JSONCParserTest, testJSONGenerationFromObject) { TEST(JSONCObjectTest, testJSONGenerationFromObject) {
json::object::TestObject to; JSONCTestObject to;
to.double_number = 42.0; to.double_number = 42.0;
to.integer_number = 42; to.integer_number = 42;
to.str = "forty two"; to.str = "forty two";
json::parser::JSONCParser parser; ASSERT_EQ("{\"integer_number\":42,\"str\":\"forty two\",\"double_number\":42.000000}",to.getJSON());
parser.generateJSONFromObject(to);
ASSERT_EQ("{\"integer_number\":42,\"str\":\"forty two\",\"double_number\":42.000000}",parser.getJSON());
} }
TEST(JSONCParserTest, testObjectGenerationFromJSON){ TEST(JSONCObjectTest, testObjectGenerationFromJSON){
std::string json = "{\"integer_number\":42,\"str\":\"forty two\",\"double_number\":42.000000}"; std::string json = "{\"integer_number\":42,\"str\":\"forty two\",\"double_number\":42.000000}";
json::parser::JSONCParser parser; JSONCTestObject to;
parser.setJSONToBeParsed(json); to.setAttributesFromJSON(json);
json::object::TestObject to = parser.getObjectFromJSON<decltype(to)>();
ASSERT_EQ(42,to.integer_number); ASSERT_EQ(42,to.integer_number);
ASSERT_EQ("forty two",to.str); ASSERT_EQ("forty two",to.str);
ASSERT_EQ(42.000000,to.double_number); ASSERT_EQ(42.000000,to.double_number);
} }
TEST(JSONCParserTest, testJSONCParserGetObjectFromUninitializedJSON){ TEST(JSONCObjectTest, testJSONCParserGetJSONShouldReturnDefaultValues){
json::parser::JSONCParser parser; JSONCTestObject to;
ASSERT_THROW(parser.getObjectFromJSON<json::object::TestObject>(),cta::exception::JSONParserException); ASSERT_EQ("{\"integer_number\":0,\"str\":\"\",\"double_number\":0.000000}",to.getJSON());
} }
TEST(JSONCParserTest, testJSONCParserGetJSONShouldReturnEmptyJSON){ TEST(JSONCObjectTest, testJSONCParserSetJSONToBeParsedWrongJSONFormat){
json::parser::JSONCParser parser; JSONCTestObject to;
ASSERT_EQ("{}",parser.getJSON()); ASSERT_THROW(to.setAttributesFromJSON("WRONG_JSON_STRING"),cta::exception::JSONObjectException);
}
TEST(JSONCParserTest, testJSONCParserSetJSONToBeParsedWrongJSONFormat){
json::parser::JSONCParser parser;
parser.setJSONToBeParsed("WRONG_JSON_STRING");
ASSERT_EQ("null",parser.getJSON());
parser.setJSONToBeParsed("{\"test");
ASSERT_EQ("null",parser.getJSON());
ASSERT_THROW(parser.getObjectFromJSON<json::object::TestObject>(),cta::exception::JSONParserException);
} }
} }
\ No newline at end of file
/*
* The CERN Tape Archive (CTA) project
* Copyright (C) 2019 CERN
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include "JSONCTestObject.hpp"
namespace unitTests {
JSONCTestObject::JSONCTestObject():JSONCObject(), TestObject() {
}
void JSONCTestObject::setAttributesFromJSON(const std::string & json){
JSONCObject::setAttributesFromJSON(json);
double_number = jsonGetValue<double>("double_number");
integer_number = jsonGetValue<uint64_t>("integer_number");
str = jsonGetValue<std::string>("str");
}
std::string JSONCTestObject::getJSON() {
reinitializeJSONCObject();
jsonSetValue("integer_number",integer_number);
jsonSetValue("str",str);
jsonSetValue("double_number",double_number);
return JSONCObject::getJSON();
}
JSONCTestObject::~JSONCTestObject() {
}
}
\ No newline at end of file
/*
* The CERN Tape Archive (CTA) project
* Copyright (C) 2019 CERN
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "common/json/object/JSONCObject.hpp"
#include "TestObject.hpp"
using namespace cta::utils::json;
namespace unitTests {
class JSONCTestObject : public object::JSONCObject, public TestObject {
public:
JSONCTestObject();
void setAttributesFromJSON(const std::string & json) override;
std::string getJSON() override;
virtual ~JSONCTestObject();
private:
};
}
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
#include <string> #include <string>
namespace cta { namespace utils { namespace json { namespace object { namespace unitTests {
struct TestObject { struct TestObject {
uint64_t integer_number; uint64_t integer_number;
...@@ -28,4 +28,4 @@ namespace cta { namespace utils { namespace json { namespace object { ...@@ -28,4 +28,4 @@ namespace cta { namespace utils { namespace json { namespace object {
double double_number; double double_number;
}; };
}}}} }
\ No newline at end of file \ No newline at end of file
...@@ -28,6 +28,7 @@ add_library(ctadisk SHARED ...@@ -28,6 +28,7 @@ add_library(ctadisk SHARED
DiskFile.cpp DiskFile.cpp
RadosStriperPool.cpp RadosStriperPool.cpp
DiskSystem.cpp DiskSystem.cpp
JSONDiskSystem.cpp
) )
target_link_libraries (ctadisk XrdCl cryptopp radosstriper) target_link_libraries (ctadisk XrdCl cryptopp radosstriper)
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
*/ */
#include "DiskSystem.hpp" #include "DiskSystem.hpp"
#include "JSONDiskSystem.hpp"
#include <algorithm> #include <algorithm>
#include "common/exception/Exception.hpp" #include "common/exception/Exception.hpp"
#include "common/threading/SubProcess.hpp" #include "common/threading/SubProcess.hpp"
...@@ -37,9 +37,9 @@ const DiskSystem& DiskSystemList::at(const std::string& name) const { ...@@ -37,9 +37,9 @@ const DiskSystem& DiskSystemList::at(const std::string& name) const {
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// DiskSystemList::getFSNAme() // DiskSystemList::getDSName()
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
std::string DiskSystemList::getDSNAme(const std::string& fileURL) const { std::string DiskSystemList::getDSName(const std::string& fileURL) const {
// First if the regexes have not been created yet, do so. // First if the regexes have not been created yet, do so.
if (m_pointersAndRegexes.empty() && size()) { if (m_pointersAndRegexes.empty() && size()) {
for (const auto &ds: *this) { for (const auto &ds: *this) {
...@@ -58,6 +58,17 @@ std::string DiskSystemList::getDSNAme(const std::string& fileURL) const { ...@@ -58,6 +58,17 @@ std::string DiskSystemList::getDSNAme(const std::string& fileURL) const {
throw std::out_of_range("In DiskSystemList::getDSNAme(): not match for fileURL"); throw std::out_of_range("In DiskSystemList::getDSNAme(): not match for fileURL");
} }
//------------------------------------------------------------------------------
// DiskSystemList::setFetchEosFreeSpaceScript()
//------------------------------------------------------------------------------
void DiskSystemList::setFetchEosFreeSpaceScript(const std::string& path){
m_fetchEosFreeSpaceScript = path;
}
std::string DiskSystemList::getFetchEosFreeSpaceScript() const{
return m_fetchEosFreeSpaceScript;
}
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// DiskSystemFreeSpaceList::fetchFileSystemFreeSpace() // DiskSystemFreeSpaceList::fetchFileSystemFreeSpace()
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
...@@ -72,8 +83,16 @@ void DiskSystemFreeSpaceList::fetchDiskSystemFreeSpace(const std::set<std::strin ...@@ -72,8 +83,16 @@ void DiskSystemFreeSpaceList::fetchDiskSystemFreeSpace(const std::set<std::strin
uint64_t freeSpace = 0; uint64_t freeSpace = 0;
try { try {
std::vector<std::string> regexResult; std::vector<std::string> regexResult;