Fledge
An open source edge computing platform for industrial users
configuration_manager.h
1 #ifndef _CONFIGURATION_MANAGER_H
2 #define _CONFIGURATION_MANAGER_H
3 
4 /*
5  * Fledge Configuration management.
6  *
7  * Copyright (c) 2018 Dianomic Systems
8  *
9  * Released under the Apache 2.0 Licence
10  *
11  * Author: Massimiliano Pinto
12  */
13 
14 #include <storage_client.h>
15 #include <config_category.h>
16 #include <string>
17 
19  public:
20  static ConfigurationManager* getInstance(const std::string&, short unsigned int);
21  // Called by microservice management API or the admin API:
22  // GET /fledge/service/category
23  // GET /fledge//category
25  // Called by microservice management API or the admin API:
26  // GET /fledge/service/category/{category_name}
27  // GET /fledge/category/{category_name}
28  ConfigCategory getCategoryAllItems(const std::string& categoryName) const;
29  // Called by microservice management API or the admin API:
30  // POST /fledge/service/category
31  // POST /fledge/category
32  ConfigCategory createCategory(const std::string& categoryName,
33  const std::string& categoryDescription,
34  const std::string& categoryItems,
35  bool keepOriginalIterms = false) const;
36  // Called by microservice management API or the admin API:
37  // GET /fledge/service/category/{categoryName}/{configItem}
38  // GET /fledge/category/{categoryName}/{configItem}
39  std::string getCategoryItem(const std::string& categoryName,
40  const std::string& itemName) const;
41  // Called by microservice management API or the admin API:
42  // PUT /fledge/service/category/{categoryName}/{configItem}
43  // PUT /fledge/service/{categoryName}/{configItem}
44  bool setCategoryItemValue(const std::string& categoryName,
45  const std::string& itemName,
46  const std::string& newValue) const;
47  // Called by microservice management API or the admin API:
48  // POST /fledge/service/category/{categoryName}/children
49  // POST /fledge/category/{categoryName}/children
50  std::string addChildCategory(const std::string& parentCategoryName,
51  const std::string& childCategories) const;
52  // Called by microservice management API or the admin API:
53  // GET /fledge/service/category/{categoryName}/children
54  // GET /fledge/category/{categoryName}/children
55  ConfigCategories getChildCategories(const std::string& parentCategoryName) const;
56  // Called by microservice management API or the admin API:
57  // DELETE /fledge/service/category/{CategoryName}/children/{ChildCategory}
58  // DELETE /fledge/category/{CategoryName}/children/{ChildCategory}
59  std::string deleteChildCategory(const std::string& parentCategoryName,
60  const std::string& childCategory) const;
61  // Called by microservice management API or the admin API:
62  // DELETE /fledge/service/category/{categoryName}/{configItem}/value
63  // DELETE /fledge/category/{categoryName}/{configItem}/value
64  std::string deleteCategoryItemValue(const std::string& categoryName,
65  const std::string& itemName) const;
66  // Called by microservice management API or the admin API:
67  // DELETE /fledge/service/category/{categoryName}
68  // DELETE /fledge/category/{categoryName}
69  ConfigCategories deleteCategory(const std::string& categoryName) const;
70  // Internal usage
71  std::string getCategoryItemValue(const std::string& categoryName,
72  const std::string& itemName) const;
73 
74  private:
75  ConfigurationManager(const std::string& host,
76  unsigned short port);
78  void mergeCategoryValues(rapidjson::Value& inputValues,
79  const rapidjson::Value* storedValues,
80  rapidjson::Document::AllocatorType& allocator,
81  bool keepOriginalitems) const;
82  // Internal usage
83  std::string fetchChildCategories(const std::string& parentCategoryName) const;
84  std::string getCategoryDescription(const std::string& categoryName) const;
85 
86  private:
87  static ConfigurationManager* m_instance;
88  StorageClient* m_storage;
89 };
90 
94 class NoSuchCategory : public std::exception {
95  public:
96  virtual const char* what() const throw()
97  {
98  return "Config category does not exist";
99  }
100 };
101 
105 class NoSuchCategoryItemValue : public std::exception {
106  public:
107  virtual const char* what() const throw()
108  {
109  return "Failure while fetching config category item value";
110  }
111 };
112 
116 class NoSuchCategoryItem : public std::exception {
117  public:
118  NoSuchCategoryItem(const std::string& message)
119  {
120  m_error = message;
121  }
122 
123  virtual const char* what() const throw()
124  {
125  return m_error.c_str();
126  }
127 
128  private:
129  std::string m_error;
130 };
131 
135 class CategoryDetailsEx : public std::exception {
136  public:
137  virtual const char* what() const throw()
138  {
139  return "Cannot access category informations";
140  }
141 };
142 
146 class StorageOperation : public std::exception {
147  public:
148  virtual const char* what() const throw()
149  {
150  return "Failure while performing insert or update operation";
151  }
152 };
153 
157 class NotSupportedDataType : public std::exception {
158  public:
159  virtual const char* what() const throw()
160  {
161  return "Data type not supported";
162  }
163 };
164 
168 class AllCategoriesEx : public std::exception {
169  public:
170  virtual const char* what() const throw()
171  {
172  return "Failure while fetching all config categories";
173  }
174 };
175 
179 class ConfigCategoryDefaultWithValue : public std::exception {
180  public:
181  virtual const char* what() const throw()
182  {
183  return "The config category being inserted/updated has both default and value properties for items";
184  }
185 };
186 
190 class ConfigCategoryEx : public std::exception {
191  public:
192  virtual const char* what() const throw()
193  {
194  return "Failure while setting/fetching a config category";
195  }
196 };
197 
201 class ChildCategoriesEx : public std::exception {
202  public:
203  virtual const char* what() const throw()
204  {
205  return "Failure while setting/fetching child categories";
206  }
207 };
208 
212 class ExistingChildCategories : public std::exception {
213  public:
214  virtual const char* what() const throw()
215  {
216  return "Requested child categories are already set for the given parent category";
217  }
218 };
219 #endif
CategoryDetailsEx.
Definition: configuration_manager.h:135
Definition: config_category.h:56
std::string addChildCategory(const std::string &parentCategoryName, const std::string &childCategories) const
Add child categories under a given (parent) category.
Definition: configuration_manager.cpp:667
std::string getCategoryItemValue(const std::string &categoryName, const std::string &itemName) const
Get the value of a given item within a given category.
Definition: configuration_manager.cpp:563
static ConfigurationManager * getInstance(const std::string &, short unsigned int)
Return the singleton instance of the configuration manager.
Definition: configuration_manager.cpp:45
std::string deleteCategoryItemValue(const std::string &categoryName, const std::string &itemName) const
Unset the category item value.
Definition: configuration_manager.cpp:1031
ConfigCategories deleteCategory(const std::string &categoryName) const
Delete a category from database.
Definition: configuration_manager.cpp:1063
Definition: config_category.h:37
Definition: configuration_manager.h:18
NoSuchItem.
Definition: configuration_manager.h:116
ConfigCategoryEx.
Definition: configuration_manager.h:190
NoSuchCategoryItemValue.
Definition: configuration_manager.h:105
NotSupportedDataType.
Definition: configuration_manager.h:157
ConfigCategory getCategoryAllItems(const std::string &categoryName) const
Return all the items of a specific category from the storage layer.
Definition: configuration_manager.cpp:142
ConfigCategoryDefaultWithValue.
Definition: configuration_manager.h:179
bool setCategoryItemValue(const std::string &categoryName, const std::string &itemName, const std::string &newValue) const
Set the "value" entry of a given item within a given category.
Definition: configuration_manager.cpp:596
AllCategoriesEx.
Definition: configuration_manager.h:168
std::string deleteChildCategory(const std::string &parentCategoryName, const std::string &childCategory) const
Remove the link between a child category and its parent.
Definition: configuration_manager.cpp:992
NoSuchCategory.
Definition: configuration_manager.h:94
ExistingChildCategories.
Definition: configuration_manager.h:212
Client for accessing the storage service.
Definition: storage_client.h:43
ConfigCategories getAllCategoryNames() const
Return all Fledge categories from storage layer.
Definition: configuration_manager.cpp:62
ConfigCategory createCategory(const std::string &categoryName, const std::string &categoryDescription, const std::string &categoryItems, bool keepOriginalIterms=false) const
Create or update a new category.
Definition: configuration_manager.cpp:243
ConfigCategories getChildCategories(const std::string &parentCategoryName) const
Get all the child categories of a given category name.
Definition: configuration_manager.cpp:905
ChildCategoriesEx.
Definition: configuration_manager.h:201
std::string getCategoryItem(const std::string &categoryName, const std::string &itemName) const
Get a given item within a given category.
Definition: configuration_manager.cpp:549
StorageOperation.
Definition: configuration_manager.h:146