aboutsummaryrefslogtreecommitdiffstats
path: root/libtransport/src/core/global_configuration.cc
blob: 9da37c2fa97c2bd4bb9179089e258f99bddf45cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * Copyright (c) 2021 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <core/global_configuration.h>
#include <glog/logging.h>
#include <hicn/transport/core/connector.h>

#include <libconfig.h++>
#include <map>

namespace transport {
namespace core {

GlobalConfiguration::GlobalConfiguration() {}

bool GlobalConfiguration::parseTransportConfig(const std::string& path) {
  using namespace libconfig;
  Config cfg;

  try {
    cfg.readFile(path.c_str());
  } catch (const FileIOException& fioex) {
    LOG(ERROR) << "I/O error while reading file.";
    return false;
  } catch (const ParseException& pex) {
    LOG(ERROR) << "Parse error at " << pex.getFile() << ":" << pex.getLine()
               << " - " << pex.getError();
    return false;
  }

  Setting& root = cfg.getRoot();

  /**
   * Iterate over sections. Best thing to do here would be to have other
   * components of the program registering a callback here, to parse their
   * section of the configuration file.
   */
  for (auto section = root.begin(); section != root.end(); section++) {
    std::string name = section->getName();
    std::error_code ec;
    VLOG(2) << "Parsing Section: " << name;

    auto it = configuration_parsers_.find(name);
    if (it != configuration_parsers_.end() && !it->second.first) {
      VLOG(2) << "Found valid configuration parser";
      it->second.second(*section, ec);
      it->second.first = true;
    }
  }

  return true;
}

void GlobalConfiguration::parseConfiguration(const std::string& path) {
  // Check if an environment variable with the configuration path exists. Conf
  // variable comes first.
  std::unique_lock<std::mutex> lck(cp_mtx_);
  if (const char* env_c = std::getenv(GlobalConfiguration::conf_file)) {
    parseTransportConfig(env_c);
  } else if (!path.empty()) {
    conf_file_path_ = path;
    parseTransportConfig(conf_file_path_);
  } else {
    LOG(ERROR)
        << "Called parseConfiguration but no configuration file was provided.";
  }
}

void GlobalConfiguration::registerConfigurationSetter(
    const std::string& key, const SetCallback& set_callback) {
  std::unique_lock<std::mutex> lck(cp_mtx_);
  if (configuration_setters_.find(key) != configuration_setters_.end()) {
    LOG(WARNING) << "Trying to register configuration setter " << key
                 << " twice. Ignoring second "
                    "registration attempt.";
  } else {
    configuration_setters_.emplace(key, set_callback);
  }
}

void GlobalConfiguration::registerConfigurationGetter(
    const std::string& key, const GetCallback& get_callback) {
  std::unique_lock<std::mutex> lck(cp_mtx_);
  if (configuration_getters_.find(key) != configuration_getters_.end()) {
    LOG(WARNING) << "Trying to register configuration setter " << key
                 << " twice. Ignoring second "
                    "registration attempt.";
  } else {
    configuration_getters_.emplace(key, get_callback);
  }
}

void GlobalConfiguration::registerConfigurationParser(
    const std::string& key, const ParserCallback& parser) {
  std::unique_lock<std::mutex> lck(cp_mtx_);
  if (configuration_parsers_.find(key) != configuration_parsers_.end()) {
    LOG(WARNING) << "Trying to register configuration setter " << key
                 << " twice. Ignoring second "
                    "registration attempt.";
  } else {
    configuration_parsers_.emplace(key, std::make_pair(false, parser));

    // Trigger a parsing of the configuration.
    if (!conf_file_path_.empty()) {
      parseTransportConfig(conf_file_path_);
    }
  }
}

void GlobalConfiguration::unregisterConfigurationParser(
    const std::string& key) {
  std::unique_lock<std::mutex> lck(cp_mtx_);
  auto it = configuration_parsers_.find(key);
  if (it != configuration_parsers_.end()) {
    configuration_parsers_.erase(it);
  }
}

void GlobalConfiguration::unregisterConfigurationSetter(
    const std::string& key) {
  std::unique_lock<std::mutex> lck(cp_mtx_);
  auto it = configuration_setters_.find(key);
  if (it != configuration_setters_.end()) {
    configuration_setters_.erase(it);
  }
}

void GlobalConfiguration::unregisterConfigurationGetter(
    const std::string& key) {
  std::unique_lock<std::mutex> lck(cp_mtx_);
  auto it = configuration_getters_.find(key);
  if (it != configuration_getters_.end()) {
    configuration_getters_.erase(it);
  }
}

void GlobalConfiguration::getConfiguration(
    interface::global_config::ConfigurationObject& configuration_object,
    std::error_code& ec) {
  auto it = configuration_getters_.find(configuration_object.getKey());

  if (it != configuration_getters_.end()) {
    it->second(configuration_object, ec);
  }
}

void GlobalConfiguration::setConfiguration(
    const interface::global_config::ConfigurationObject& configuration_object,
    std::error_code& ec) {
  auto it = configuration_setters_.find(configuration_object.getKey());

  if (it != configuration_setters_.end()) {
    it->second(configuration_object, ec);
  }
}

}  // namespace core
}  // namespace transport