/src/admin/logic/Logic.cpp

https://gitlab.com/admin-github-cloud/cynara · C++ · 99 lines · 63 code · 14 blank · 22 comment · 3 complexity · 66579d2b45aeff3e378009b5838f75e1 MD5 · raw file

  1. /*
  2. * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License
  15. */
  16. /**
  17. * @file src/admin/logic/Logic.cpp
  18. * @author Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
  19. * @author Aleksander Zdyb <a.zdyb@samsung.com>
  20. * @version 1.0
  21. * @brief This file contains implementation of Logic class - main libcynara-admin class
  22. */
  23. #include <common.h>
  24. #include <config/PathConfig.h>
  25. #include <log/log.h>
  26. #include "Logic.h"
  27. #include "OfflineLogic.h"
  28. #include "OnlineLogic.h"
  29. namespace Cynara {
  30. Logic::Logic() : m_onlineLogic(new OnlineLogic()), m_offlineLogic(new OfflineLogic()),
  31. m_lockable(PathConfig::StoragePath::lockFile) {}
  32. Logic::~Logic() {
  33. delete m_onlineLogic;
  34. delete m_offlineLogic;
  35. }
  36. int Logic::setPolicies(const ApiInterface::PoliciesByBucket &insertOrUpdate,
  37. const ApiInterface::KeysByBucket &remove) {
  38. using std::placeholders::_1;
  39. return callApiFunction(std::bind(&ApiInterface::setPolicies, _1,
  40. std::cref(insertOrUpdate), std::cref(remove)));
  41. }
  42. int Logic::insertOrUpdateBucket(const PolicyBucketId &bucket,
  43. const PolicyResult &policyResult) {
  44. using std::placeholders::_1;
  45. auto f = std::bind(&ApiInterface::insertOrUpdateBucket, _1,
  46. std::cref(bucket), std::cref(policyResult));
  47. return callApiFunction(f);
  48. }
  49. int Logic::removeBucket(const PolicyBucketId &bucket) {
  50. using std::placeholders::_1;
  51. return callApiFunction(std::bind(&ApiInterface::removeBucket, _1, std::cref(bucket)));
  52. }
  53. int Logic::adminCheck(const PolicyBucketId &startBucket, bool recursive, const PolicyKey &key,
  54. PolicyResult &result) {
  55. using std::placeholders::_1;
  56. return callApiFunction(std::bind(&ApiInterface::adminCheck, _1, std::cref(startBucket),
  57. recursive, std::cref(key), std::ref(result)));
  58. }
  59. int Logic::listDescriptions(std::vector<PolicyDescription> &descriptions) {
  60. using std::placeholders::_1;
  61. return callApiFunction(std::bind(&ApiInterface::listDescriptions, _1, std::ref(descriptions)));
  62. }
  63. int Logic::listPolicies(const PolicyBucketId &bucket, const PolicyKey &filter,
  64. std::vector<Policy> &policies) {
  65. using std::placeholders::_1;
  66. return callApiFunction(std::bind(&ApiInterface::listPolicies, _1, std::cref(bucket),
  67. std::cref(filter), std::ref(policies)));
  68. }
  69. int Logic::erasePolicies(const PolicyBucketId &startBucket, bool recursive,
  70. const PolicyKey &filter) {
  71. using std::placeholders::_1;
  72. return callApiFunction(std::bind(&ApiInterface::erasePolicies, _1, std::cref(startBucket),
  73. recursive, std::cref(filter)));
  74. }
  75. int Logic::callApiFunction(std::function<int(ApiInterface *api)> apiCall) {
  76. FileLock lock(m_lockable);
  77. if (lock.tryLock() == true) {
  78. LOGI("Admin uses offline API");
  79. return apiCall(m_offlineLogic);
  80. } else {
  81. LOGI("Admin uses online API");
  82. return apiCall(m_onlineLogic);
  83. }
  84. }
  85. } // namespace Cynara