endpoints_parameters.cpp
Go to the documentation of this file.
1 
11 
12 #include "endpoints_parameters.h"
13 
14 #include <iostream>
15 #include <stdexcept>
16 
17 namespace
18 {
19 
20 using namespace OpcUa;
21 using namespace OpcUa::Server;
22 
23 class OpcUaParameters
24 {
25 public:
26  OpcUaParameters(const Common::Logger::SharedPtr & logger)
27  : Logger(logger)
28  {
29  }
30 
31  std::vector<ApplicationData> GetApplications(const std::vector<Common::ParametersGroup> & applicationGroups) const
32  {
33  std::vector<ApplicationData> applications;
34 
35  for (const Common::ParametersGroup subGroup : applicationGroups)
36  {
37  Log("Found group : ", subGroup.Name);
38 
39  if (subGroup.Name == "application")
40  {
41  const ApplicationData application = GetApplicationData(subGroup);
42  applications.push_back(application);
43  }
44 
45  else
46  {
47  Log("Unknown parameters group: ", subGroup.Name);
48  }
49  }
50 
51  return applications;
52  }
53 
54  std::vector<Common::ParametersGroup> GetAddonParameters(const std::vector<ApplicationData> & endpoints) const
55  {
56  std::vector<Common::ParametersGroup> result(endpoints.size());
57  std::transform(endpoints.begin(), endpoints.end(), result.begin(), [this](const ApplicationData & app)
58  {
59  return ApplicationToParametersGroup(app);
60  });
61  return result;
62  }
63 
64 private:
65  UserTokenType GetTokenType(const std::string & typeName) const
66  {
67  if (typeName == "anonymous" || typeName.empty())
68  { return UserTokenType::Anonymous; }
69 
70  else if (typeName == "user_name")
71  { return UserTokenType::UserName; }
72 
73  else if (typeName == "certificate")
74  { return UserTokenType::Certificate; }
75 
76  else if (typeName == "issued_token")
77  { return UserTokenType::IssuedToken; }
78 
79  throw std::logic_error("Unknown token type '" + typeName + "'");
80  }
81 
82  std::string GetTokenType(OpcUa::UserTokenType type) const
83  {
84  switch (type)
85  {
87  return "anonymous";
88 
90  return "user_name";
91 
93  return "certificate";
94 
96  return "issued_token";
97 
98  default:
99  throw std::logic_error("Unknown token type '" + std::to_string((unsigned)type) + "'");
100  }
101  }
102 
103  ApplicationType GetApplicationType(const std::string & typeName) const
104  {
105  if (typeName == "client" || typeName.empty())
106  { return ApplicationType::Client; }
107 
108  else if (typeName == "server")
109  { return ApplicationType::Server; }
110 
111  else if (typeName == "client_and_server")
113 
114  else if (typeName == "discovery_server")
116 
117  throw std::logic_error("Invalid name of type application type: " + typeName);
118  }
119 
120  std::string GetApplicationType(ApplicationType type) const
121  {
122  switch (type)
123  {
125  return "server";
126 
128  return "client";
129 
131  return "client_and_server";
132 
134  return "discovery_server";
135 
136  default:
137  throw std::logic_error("Unknown application type: " + std::to_string((unsigned)type));
138  }
139  }
140 
141  MessageSecurityMode GetSecurityMode(const std::string & modeName) const
142  {
143  if (modeName == "none" || modeName.empty())
144  { return MessageSecurityMode::None; }
145 
146  else if (modeName == "sign")
147  { return MessageSecurityMode::Sign; }
148 
149  else if (modeName == "sign_encrypt")
151 
152  throw std::logic_error("Unknown security mode name: " + modeName);
153  }
154 
155  std::string GetSecurityMode(MessageSecurityMode mode) const
156  {
157  switch (mode)
158  {
160  return "none";
161 
163  return "sign";
164 
166  return "sign_encrypt";
167 
168  default:
169  throw std::logic_error("Unknown security mode: " + std::to_string((unsigned)mode));
170  }
171  }
172 
173  UserTokenPolicy GetUserTokenPolicy(const std::vector<Common::Parameter> & params) const
174  {
175  Log("Parsing user token policy.");
176  UserTokenPolicy tokenPolicy;
177 
178  for (const Common::Parameter & param : params)
179  {
180  if (param.Name == "id")
181  { tokenPolicy.PolicyId = param.Value; }//"Anonymous";
182 
183  else if (param.Name == "type")
184  { tokenPolicy.TokenType = GetTokenType(param.Value); }
185 
186  else if (param.Name == "uri")
187  { tokenPolicy.SecurityPolicyUri = param.Value; } //"http://opcfoundation.org/UA/SecurityPolicy#None";
188 
189  else if (param.Name == "issued_token_type")
190  { tokenPolicy.IssuedTokenType = param.Value; }
191 
192  else if (param.Name == "issuer_endpoint_url")
193  { tokenPolicy.IssuerEndpointUrl = param.Value; }
194 
195  else
196  { Log("Unknown policy token field", param.Name, param.Value); }
197  }
198 
199  return tokenPolicy;
200  }
201 
202  Common::ParametersGroup GetUserTokenPolicy(const UserTokenPolicy & policy) const
203  {
204  Log("Parsing user token policy.");
205  Common::ParametersGroup policyGroup("user_token_policy");
206  policyGroup.Parameters.push_back(Common::Parameter("id", policy.PolicyId));
207  policyGroup.Parameters.push_back(Common::Parameter("type", GetTokenType(policy.TokenType)));
208  policyGroup.Parameters.push_back(Common::Parameter("uri", policy.SecurityPolicyUri));
209  policyGroup.Parameters.push_back(Common::Parameter("issuer_endpoint_url", policy.IssuerEndpointUrl));
210  policyGroup.Parameters.push_back(Common::Parameter("issued_token_type", policy.IssuedTokenType));
211  return policyGroup;
212  }
213 
214  EndpointDescription GetEndpointDescription(const Common::ParametersGroup & group) const
215  {
216  Log("Parsing endpoint parameters.");
217  EndpointDescription endpoint;
218 
219  for (const Common::Parameter param : group.Parameters)
220  {
221  Log("Param is: ", param.Name, " = ", param.Value);
222 
223  if (param.Name == "security_mode")
224  { endpoint.SecurityMode = GetSecurityMode(param.Value); }
225 
226  if (param.Name == "security_level")
227  { endpoint.SecurityLevel = std::stoi(param.Value); }
228 
229  else if (param.Name == "security_policy_uri")
230  { endpoint.SecurityPolicyUri = param.Value; }
231 
232  else if (param.Name == "transport_profile_uri")
233  { endpoint.TransportProfileUri = param.Value; }//"http://opcfoundation.org/UA-Profile/Transport/uatcp-uasc-uabinary";
234 
235  else if (param.Name == "url")
236  { endpoint.EndpointUrl = param.Value; }
237 
238  else
239  { Log("Unknown endpoint parameter: ", param.Name, "=", param.Value); }
240  }
241 
242  for (const Common::ParametersGroup subGroup : group.Groups)
243  {
244  if (subGroup.Name == "user_token_policy")
245  {
246  const UserTokenPolicy tokenPolicy = GetUserTokenPolicy(subGroup.Parameters);
247  endpoint.UserIdentityTokens.push_back(tokenPolicy);
248  }
249 
250  else
251  {
252  Log("Unknown group of endpoint parameters: ", subGroup.Name);
253  }
254  }
255 
256  return endpoint;
257  }
258 
259  Common::ParametersGroup GetEndpointDescription(const EndpointDescription & endpoint) const
260  {
261  Common::ParametersGroup ed("endpoint");
262  ed.Parameters.push_back(Common::Parameter("security_level", std::to_string(endpoint.SecurityLevel)));
263  ed.Parameters.push_back(Common::Parameter("security_mode", GetSecurityMode(endpoint.SecurityMode)));
264  ed.Parameters.push_back(Common::Parameter("security_policy_uri", endpoint.SecurityPolicyUri));
265  ed.Parameters.push_back(Common::Parameter("transport_profile_uri", endpoint.TransportProfileUri)); //"http://opcfoundation.org/UA-Profile/Transport/uatcp-uasc-uabinary"
266  ed.Parameters.push_back(Common::Parameter("url", endpoint.EndpointUrl));
267 
268  for (const UserTokenPolicy & policy : endpoint.UserIdentityTokens)
269  {
270  ed.Groups.push_back(GetUserTokenPolicy(policy));
271  }
272 
273  return ed;
274  }
275 
276  ApplicationData GetApplicationData(const Common::ParametersGroup & applicationGroup) const
277  {
278  Log("Parsing application parameters.");
279  ApplicationData data;
280 
281  for (const Common::Parameter param : applicationGroup.Parameters)
282  {
283  Log("Parsing app paramter ", param.Name, " = ", param.Value);
284 
285  if (param.Name == "uri")
286  {
287  data.Application.ApplicationUri = param.Value;
288  }
289 
290  else if (param.Name == "product_uri")
291  {
292  data.Application.ProductUri = param.Value;
293  }
294 
295  else if (param.Name == "gateway_server_uri")
296  {
297  data.Application.GatewayServerUri = param.Value;
298  }
299 
300  else if (param.Name == "discovery_profile")
301  {
303  }
304 
305  else if (param.Name == "name")
306  {
308  }
309 
310  else if (param.Name == "type")
311  {
312  data.Application.ApplicationType = GetApplicationType(param.Value);
313  }
314 
315  else
316  {
317  Log("Unknown application parameter: ", param.Name, "=", param.Value);
318  }
319  }
320 
321  for (const Common::ParametersGroup group : applicationGroup.Groups)
322  {
323  Log("parsing group ", group.Name);
324 
325  if (group.Name == "endpoint")
326  {
327  EndpointDescription endpoint = GetEndpointDescription(group);
328  data.Application.DiscoveryUrls.push_back(endpoint.EndpointUrl);
329  data.Endpoints.push_back(endpoint);
330  }
331 
332  else
333  {
334  Log("Unknown group in the applications parameters: ", group.Name);
335  }
336  }
337 
338  for (EndpointDescription & endpoint : data.Endpoints)
339  {
340  endpoint.Server = data.Application;
341  }
342 
343  return data;
344  }
345 
346  Common::ParametersGroup ApplicationToParametersGroup(const ApplicationData & app) const
347  {
348  Common::ParametersGroup result("application");
349  result.Parameters.push_back(Common::Parameter("discovery_profile", app.Application.DiscoveryProfileUri));
350  result.Parameters.push_back(Common::Parameter("uri", app.Application.ApplicationUri));
351  result.Parameters.push_back(Common::Parameter("gateway_server_uri", app.Application.GatewayServerUri));
352  result.Parameters.push_back(Common::Parameter("product_uri", app.Application.ProductUri));
353  result.Parameters.push_back(Common::Parameter("name", app.Application.ApplicationName.Text));
354  result.Parameters.push_back(Common::Parameter("type", GetApplicationType(app.Application.ApplicationType)));
355 
356  for (const EndpointDescription & endpoint : app.Endpoints)
357  {
358  result.Groups.push_back(GetEndpointDescription(endpoint));
359  }
360 
361  return result;
362  }
363 
364 private:
365  template <typename T, typename... Args>
366  void Log(T && msg, Args... args) const
367  {
368  LOG_DEBUG(Logger, msg, args...);
369  /*
370  if (!Debug)
371  {
372  return;
373  }
374 
375  std::clog << msg;
376  Log(args...);
377  */
378  }
379 
380  /*
381  void Log() const
382  {
383  if (!Debug)
384  {
385  return;
386  }
387 
388  std::clog << std::endl;
389  }
390  */
391 
392 private:
393  Common::Logger::SharedPtr Logger;
394 };
395 }
396 
397 std::vector<OpcUa::Server::ApplicationData> OpcUa::ParseEndpointsParameters(const std::vector<Common::ParametersGroup> & applicationGroups, const Common::Logger::SharedPtr & logger)
398 {
399  OpcUaParameters parser(logger);
400  const std::vector<OpcUa::Server::ApplicationData> & data = parser.GetApplications(applicationGroups);
401  return data;
402 }
403 
404 std::vector<Common::ParametersGroup> OpcUa::CreateCommonParameters(const std::vector<Server::ApplicationData> & endpoints, const Common::Logger::SharedPtr & logger)
405 {
406  OpcUaParameters parser(logger);
407  const std::vector<Common::ParametersGroup> & result = parser.GetAddonParameters(endpoints);
408  return result;
409 }
ApplicationDescription Application
bool param(const std::string &param_name, T &param_val, const T &default_val)
std::vector< OpcUa::UserTokenPolicy > UserIdentityTokens
std::string SecurityPolicyUri
std::vector< Parameter > Parameters
std::vector< ParametersGroup > Groups
std::vector< EndpointDescription > Endpoints
#define LOG_DEBUG(__logger__,...)
Definition: common/logger.h:24
std::string IssuedTokenType
internal::ArgsMatcher< InnerMatcher > Args(const InnerMatcher &matcher)
OpcUa::ApplicationDescription Server
OpcUa::ApplicationType ApplicationType
GTEST_API_ void Log(LogSeverity severity, const string &message, int stack_frames_to_skip)
MessageSecurityMode
Definition: enums.h:68
OpcUa::MessageSecurityMode SecurityMode
OPC UA Address space part. GNU LGPL.
UserTokenType
Definition: enums.h:80
OpcUa::LocalizedText ApplicationName
std::vector< Server::ApplicationData > ParseEndpointsParameters(const std::vector< Common::ParametersGroup > &rootGroup, const Common::Logger::SharedPtr &logger)
std::string Text
Definition: types.h:136
OpcUa::UserTokenType TokenType
std::vector< std::string > DiscoveryUrls
std::string IssuerEndpointUrl
ApplicationType
Definition: enums.h:56
std::vector< Common::ParametersGroup > CreateCommonParameters(const std::vector< Server::ApplicationData > &endpoints, const Common::Logger::SharedPtr &logger)


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Tue Jan 19 2021 03:06:04