softwarecontainer  0.18.0-739e8d7 2017-05-04
baseconfigstore.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 "baseconfigstore.h"
21 
22 namespace softwarecontainer {
23 
24 BaseConfigStore::BaseConfigStore(std::unique_ptr<ServiceManifestLoader> loader)
25 {
26  std::vector<json_t *> content = loader->loadContent();
27 
28  for (json_t *serviceManifest : content) {
29  parseServiceManifest(serviceManifest);
30  }
31 }
32 
33 void BaseConfigStore::parseServiceManifest(json_t *serviceManifest)
34 {
35  size_t i;
36  json_t *capability;
37  std::string errorMessage;
38 
39  json_t *capabilities = json_object_get(serviceManifest, "capabilities");
40  if (nullptr == capabilities) {
41  errorMessage = "Could not parse the \"capability\" object";
42  log_error() << errorMessage;
43  throw CapabilityParseError(errorMessage);
44  }
45  if (!json_is_array(capabilities)) {
46  errorMessage = "The \"capability\" key does not point to an array";
47  log_error() << errorMessage;
48  throw CapabilityParseError(errorMessage);
49  }
50 
51  log_debug() << "Size of capabilities is " << std::to_string(json_array_size(capabilities));
52 
53  json_array_foreach(capabilities, i, capability) {
54  if (!json_is_object(capability)) {
55  errorMessage = "A \"capability\" in the Service Manifest is not a json object";
56  log_error() << errorMessage;
57  throw CapabilityParseError(errorMessage);
58  }
59 
60  std::string capName;
61  if (!JSONParser::read(capability, "name", capName)) {
62  errorMessage = "Could not read the name of the \"capability\" object";
63  log_error() << errorMessage;
64  throw CapabilityParseError(errorMessage);
65  }
66 
67  json_t *gateways = json_object_get(capability, "gateways");
68  if (nullptr == gateways) {
69  errorMessage = "Could not read the \"gateway\" objects in \"" + capName + "\"";
70  log_error() << errorMessage;
71  throw CapabilityParseError(errorMessage);
72  }
73  if (!json_is_array(gateways)) {
74  errorMessage = "The \"gateway\" object is not an array";
75  log_error() << errorMessage;
76  throw CapabilityParseError(errorMessage);
77  }
78 
79  log_debug() << "Found capability \"" << capName << "\"";
80 
81  parseGatewayConfigs(capName, gateways);
82  }
83 }
84 
85 void BaseConfigStore::parseGatewayConfigs(std::string capName, json_t *gateways)
86 {
87  if (m_capMap.count(capName) > 0) {
88  log_debug() << "Capability " << capName << " already loaded.";
89  return;
90  }
91 
92  size_t i;
93  json_t *gateway;
94  std::string errorMessage;
95 
96  GatewayConfiguration gwConf;
97  json_array_foreach(gateways, i, gateway) {
98  if (!json_is_object(gateway)) {
99  errorMessage = "A \"gateway\" in the Service Manifest is not a json object";
100  log_error() << errorMessage;
101  throw CapabilityParseError(errorMessage);
102  }
103 
104  std::string gwID;
105  if (!JSONParser::read(gateway, "id", gwID)) {
106  errorMessage = "Could not read the ID of the \"gateway\""
107  " object in the Service Manifest";
108  log_error() << errorMessage;
109  throw CapabilityParseError(errorMessage);
110  }
111 
112  json_t *confs = json_object_get(gateway, "config");
113  if (nullptr == confs) {
114  errorMessage = "Could not read the \"gateway\" object's configuration element "
115  "(" + gwID+ ")";
116  log_error() << errorMessage;
117  throw CapabilityParseError(errorMessage);
118  }
119  if (!json_is_array(confs)) {
120  errorMessage = "The \"gateway\" object's configuration is not an array (" + gwID+ ")";
121  log_error() << errorMessage;
122  throw CapabilityParseError(errorMessage);
123  }
124  gwConf.append(gwID, confs);
125  }
126  m_capMap[capName] = gwConf;
127 }
128 
129 } // namespace softwarecontainer
Contains the softwarecontainer::BaseConfigStore class.
An error occured in ConfigStore when parsing a Capability in a Service Manifest.
Developers guide to adding a config item:
static bool read(const json_t *element, const char *key, std::string &result)
Reads a string from a JSON Object.
Definition: jsonparser.cpp:51
BaseConfigStore(std::unique_ptr< ServiceManifestLoader > loader)
Creates a new BaseConfigStore object and searches for Service Manifests (of file type json) in the in...