This repository has been archived by the owner on Dec 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #236 from boschresearch/feature/load_overlays
Feature/load overlays
- Loading branch information
Showing
5 changed files
with
128 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* ****************************************************************************** | ||
* Copyright (c) 2021 Robert Bosch GmbH. | ||
* | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/org/documents/epl-2.0/index.php | ||
* | ||
* ***************************************************************************** | ||
*/ | ||
|
||
#include <vector> | ||
#include <boost/filesystem.hpp> | ||
|
||
#include "ILogger.hpp" | ||
#include "IVssDatabase.hpp" | ||
|
||
|
||
/** Finding overlays to load. Returns an alphanumerically list of paths pointing | ||
* to *.json files in directory | ||
*/ | ||
std::vector<boost::filesystem::path> gatherOverlays(std::shared_ptr<ILogger> log, boost::filesystem::path directory); | ||
|
||
/** Iterates over paths in vector, tries to parse as JSON and merge with existing | ||
* structure in database | ||
*/ | ||
void applyOverlays(std::shared_ptr<ILogger> log, const std::vector<boost::filesystem::path> overlayfiles, std::shared_ptr<IVssDatabase> db); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* ****************************************************************************** | ||
* Copyright (c) 2021 Robert Bosch GmbH. | ||
* | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/org/documents/epl-2.0/index.php | ||
* | ||
* ***************************************************************************** | ||
*/ | ||
|
||
/* helper fuctions to load overlays during server startup */ | ||
|
||
#include <stdexcept> | ||
#include <jsoncons/json.hpp> | ||
|
||
#include "OverlayLoader.hpp" | ||
#include "kuksa.pb.h" | ||
|
||
using jsoncons::json; | ||
|
||
std::vector<boost::filesystem::path> gatherOverlays( | ||
std::shared_ptr<ILogger> logger, boost::filesystem::path overlaydir) { | ||
std::vector<boost::filesystem::path> overlayfiles; | ||
logger->Log(LogLevel::VERBOSE, | ||
"Searching overlays in \"" + overlaydir.generic_string() + "\""); | ||
if (!boost::filesystem::is_directory(overlaydir)) { | ||
throw std::runtime_error("Overlays need to be a directory. \"" + | ||
overlaydir.generic_string() + "\" is not."); | ||
} | ||
for (auto &p : boost::filesystem::directory_iterator(overlaydir)) { | ||
if (boost::filesystem::is_regular_file(p.path()) && | ||
p.path().extension() == ".json") { | ||
logger->Log(LogLevel::VERBOSE, "Found " + p.path().generic_string()); | ||
overlayfiles.push_back(p); | ||
} | ||
} | ||
|
||
// Sort according to filename | ||
std::sort(overlayfiles.begin(), overlayfiles.end(), | ||
[](boost::filesystem::path a, boost::filesystem::path b) { | ||
return a.filename() < b.filename(); | ||
}); | ||
|
||
return overlayfiles; | ||
} | ||
|
||
void applyOverlays(std::shared_ptr<ILogger> log, | ||
const std::vector<boost::filesystem::path> overlayfiles, | ||
std::shared_ptr<IVssDatabase> db) { | ||
// Mock a channel with permissions to change VSS tree | ||
auto mockChannel = std::make_shared<kuksa::kuksaChannel>(); | ||
mockChannel->set_modifytree(true); | ||
|
||
for (auto const &p : overlayfiles) { | ||
log->Log(LogLevel::INFO, "Loading overlay \"" + p.generic_string() + "\""); | ||
; | ||
try { | ||
std::ifstream is(p.generic_string()); | ||
jsoncons::json overlay = json::parse(is); | ||
db->updateJsonTree(*mockChannel, overlay); | ||
} catch (std::exception &e) { | ||
throw std::runtime_error("Error loading \"" + p.generic_string() + | ||
"\": " + e.what()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters