softwarecontainer  0.18.0-739e8d7 2017-05-04
gatewayconfig.cpp
1 /*
2  * Copyright (C) 2016-2017 Pelagicore AB
3  *
4  * Permission to use, copy, modify, and/or distribute this software for
5  * any purpose with or without fee is hereby granted, provided that the
6  * above copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
9  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
10  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
11  * BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
12  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
13  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
14  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15  * SOFTWARE.
16  *
17  * For further information see LICENSE
18  */
19 
20 #include "gatewayconfig.h"
21 
22 namespace softwarecontainer {
23 
25 {
26 }
27 
29 {
30  if (!append(gwConf)) {
31  throw SoftwareContainerError("Failed to create GatewayConfiguration");
32  }
33 }
34 
35 GatewayConfiguration& GatewayConfiguration::operator=(const GatewayConfiguration &gwConf)
36 {
37  // We need to indicate our intention to use the json data later by incref-ing it
38  m_configMap = gwConf.m_configMap;
39  for (auto item : m_configMap) {
40  json_incref(item.second);
41  }
42 
43  return *this;
44 }
45 
46 GatewayConfiguration::~GatewayConfiguration()
47 {
48  for (auto &it : m_configMap) {
49  json_decref(it.second);
50  }
51 }
52 
53 bool GatewayConfiguration::append(const std::string &id, const std::string &jsonConf)
54 {
55  json_error_t jsonError;
56  size_t flags = 0; // default flags
57 
58  json_t *json = json_loads(jsonConf.c_str(), flags, &jsonError);
59  if (nullptr == json) {
60  log_error() << "Could not parse given string to JSON. Due to: " << jsonError.text << " at line: " << jsonError.line;
61  return false;
62  }
63 
64  auto ret = append(id, json);
65  json_decref(json);
66  return ret;
67 }
68 
69 bool GatewayConfiguration::append(const std::string &id, json_t *sourceArray)
70 {
71  auto search = m_configMap.find(id);
72  if (search != m_configMap.end()) {
73  // Get the arrays
74  json_t *destArray = search->second;
75  json_t *backupArray = json_deep_copy(destArray);
76 
77  // Add all elements of the source array to the destination array
78  size_t index;
79  json_t *value;
80  json_array_foreach(sourceArray, index, value) {
81  json_t *copy = json_deep_copy(value);
82  if (json_array_append(destArray, copy) < 0) {
83  log_error() << "Could not add Gateway Config to json array: " << id;
84  m_configMap[id] = backupArray;
85  json_decref(destArray);
86  return false;
87  }
88  }
89  json_decref(backupArray);
90  } else {
91  m_configMap[id] = json_deep_copy(sourceArray);
92  // we do not need to decref the sourceArray since we did a deep copy
93  }
94 
95  return true;
96 }
97 
98 bool GatewayConfiguration::append(const GatewayConfiguration &source)
99 {
100  for (auto &conf : source.m_configMap) {
101  if (!append(conf.first, conf.second)) {
102  return false;
103  }
104  }
105  return true;
106 }
107 
108 json_t *GatewayConfiguration::config(const std::string &gatewayId) const
109 {
110  if (m_configMap.count(gatewayId) == 0) {
111  return nullptr;
112  }
113 
114  return json_deep_copy(m_configMap.at(gatewayId));
115 }
116 
117 bool GatewayConfiguration::empty()
118 {
119  return m_configMap.empty();
120 }
121 
122 std::vector<std::string> GatewayConfiguration::ids() const
123 {
124  std::vector<std::string> ids;
125  for (auto &pair : m_configMap) {
126  ids.push_back(pair.first);
127  }
128 
129  return ids;
130 }
131 
132 } // namespace softwarecontainer
GatewayConfiguration()
Creates a new GatewayConfiguration object.
Developers guide to adding a config item: