Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include "endpoints_parameters.h"
00013
00014 #include <iostream>
00015 #include <stdexcept>
00016
00017 namespace
00018 {
00019
00020 using namespace OpcUa;
00021 using namespace OpcUa::Server;
00022
00023 class OpcUaParameters
00024 {
00025 public:
00026 OpcUaParameters(bool debug)
00027 : Debug(debug)
00028 {
00029 }
00030
00031 std::vector<ApplicationData> GetApplications(const std::vector<Common::ParametersGroup>& applicationGroups) const
00032 {
00033 std::vector<ApplicationData> applications;
00034 for (const Common::ParametersGroup subGroup : applicationGroups)
00035 {
00036 Log("Found group : ", subGroup.Name);
00037 if (subGroup.Name == "application")
00038 {
00039 const ApplicationData application = GetApplicationData(subGroup);
00040 applications.push_back(application);
00041 }
00042 else
00043 {
00044 Log("Unknown parameters group: ", subGroup.Name);
00045 }
00046 }
00047 return applications;
00048 }
00049
00050 std::vector<Common::ParametersGroup> GetAddonParameters(const std::vector<ApplicationData>& endpoints) const
00051 {
00052 std::vector<Common::ParametersGroup> result(endpoints.size());
00053 std::transform(endpoints.begin(), endpoints.end(), result.begin(), [this](const ApplicationData& app){
00054 return ApplicationToParametersGroup(app);
00055 });
00056 return result;
00057 }
00058
00059 private:
00060 UserTokenType GetTokenType(const std::string& typeName) const
00061 {
00062 if (typeName == "anonymous" || typeName.empty())
00063 return UserTokenType::Anonymous;
00064 else if (typeName == "user_name")
00065 return UserTokenType::UserName;
00066 else if (typeName == "certificate")
00067 return UserTokenType::Certificate;
00068 else if (typeName == "issued_token")
00069 return UserTokenType::IssuedToken;
00070
00071 throw std::logic_error("Unknown token type '" + typeName + "'");
00072 }
00073
00074 std::string GetTokenType(OpcUa::UserTokenType type) const
00075 {
00076 switch (type)
00077 {
00078 case UserTokenType::Anonymous:
00079 return "anonymous";
00080 case UserTokenType::UserName:
00081 return "user_name";
00082 case UserTokenType::Certificate:
00083 return "certificate";
00084 case UserTokenType::IssuedToken:
00085 return "issued_token";
00086 default:
00087 throw std::logic_error("Unknown token type '" + std::to_string((unsigned)type) + "'");
00088 }
00089 }
00090
00091 ApplicationType GetApplicationType(const std::string& typeName) const
00092 {
00093 if (typeName == "client" || typeName.empty())
00094 return ApplicationType::Client;
00095 else if (typeName == "server")
00096 return ApplicationType::Server;
00097 else if (typeName == "client_and_server")
00098 return ApplicationType::ClientAndServer;
00099 else if (typeName == "discovery_server")
00100 return ApplicationType::DiscoveryServer;
00101
00102 throw std::logic_error("Invalid name of type application type: " + typeName);
00103 }
00104
00105 std::string GetApplicationType(ApplicationType type) const
00106 {
00107 switch (type)
00108 {
00109 case ApplicationType::Server:
00110 return "server";
00111 case ApplicationType::Client:
00112 return "client";
00113 case ApplicationType::ClientAndServer:
00114 return "client_and_server";
00115 case ApplicationType::DiscoveryServer:
00116 return "discovery_server";
00117 default:
00118 throw std::logic_error("Unknown application type: " + std::to_string((unsigned)type));
00119 }
00120 }
00121
00122 MessageSecurityMode GetSecurityMode(const std::string& modeName) const
00123 {
00124 if (modeName == "none" || modeName.empty())
00125 return MessageSecurityMode::None;
00126 else if (modeName == "sign")
00127 return MessageSecurityMode::Sign;
00128 else if (modeName == "sign_encrypt")
00129 return MessageSecurityMode::SignAndEncrypt;
00130
00131 throw std::logic_error("Unknown security mode name: " + modeName);
00132 }
00133
00134 std::string GetSecurityMode(MessageSecurityMode mode) const
00135 {
00136 switch (mode)
00137 {
00138 case MessageSecurityMode::None:
00139 return "none";
00140 case MessageSecurityMode::Sign:
00141 return "sign";
00142 case MessageSecurityMode::SignAndEncrypt:
00143 return "sign_encrypt";
00144 default:
00145 throw std::logic_error("Unknown security mode: " + std::to_string((unsigned)mode));
00146 }
00147 }
00148
00149 UserTokenPolicy GetUserTokenPolicy(const std::vector<Common::Parameter>& params) const
00150 {
00151 Log("Parsing user token policy.");
00152 UserTokenPolicy tokenPolicy;
00153 for (const Common::Parameter& param : params)
00154 {
00155 if (param.Name == "id")
00156 tokenPolicy.PolicyId = param.Value;
00157 else if (param.Name == "type")
00158 tokenPolicy.TokenType = GetTokenType(param.Value);
00159 else if (param.Name == "uri")
00160 tokenPolicy.SecurityPolicyUri = param.Value;
00161 else if (param.Name == "issued_token_type")
00162 tokenPolicy.IssuedTokenType = param.Value;
00163 else if (param.Name == "issuer_endpoint_url")
00164 tokenPolicy.IssuerEndpointUrl = param.Value;
00165 else
00166 Log("Unknown policy token field", param.Name, param.Value);
00167 }
00168 return tokenPolicy;
00169 }
00170
00171 Common::ParametersGroup GetUserTokenPolicy(const UserTokenPolicy& policy) const
00172 {
00173 Log("Parsing user token policy.");
00174 Common::ParametersGroup policyGroup("user_token_policy");
00175 policyGroup.Parameters.push_back(Common::Parameter("id", policy.PolicyId));
00176 policyGroup.Parameters.push_back(Common::Parameter("type", GetTokenType(policy.TokenType)));
00177 policyGroup.Parameters.push_back(Common::Parameter("uri", policy.SecurityPolicyUri));
00178 policyGroup.Parameters.push_back(Common::Parameter("issuer_endpoint_url", policy.IssuerEndpointUrl));
00179 policyGroup.Parameters.push_back(Common::Parameter("issued_token_type", policy.IssuedTokenType));
00180 return policyGroup;
00181 }
00182
00183 EndpointDescription GetEndpointDescription(const Common::ParametersGroup& group) const
00184 {
00185 Log("Parsing endpoint parameters.");
00186 EndpointDescription endpoint;
00187 for (const Common::Parameter param : group.Parameters)
00188 {
00189 Log("Param is: ", param.Name, " = ", param.Value);
00190 if (param.Name == "security_mode")
00191 endpoint.SecurityMode = GetSecurityMode(param.Value);
00192 if (param.Name == "security_level")
00193 endpoint.SecurityLevel = std::stoi(param.Value);
00194 else if (param.Name == "security_policy_uri")
00195 endpoint.SecurityPolicyUri = param.Value;
00196 else if (param.Name == "transport_profile_uri")
00197 endpoint.TransportProfileUri = param.Value;
00198 else if (param.Name == "url")
00199 endpoint.EndpointUrl = param.Value;
00200 else
00201 Log("Unknown endpoint parameter: ", param.Name, "=", param.Value);
00202 }
00203
00204 for (const Common::ParametersGroup subGroup : group.Groups)
00205 {
00206 if (subGroup.Name == "user_token_policy")
00207 {
00208 const UserTokenPolicy tokenPolicy = GetUserTokenPolicy(subGroup.Parameters);
00209 endpoint.UserIdentityTokens.push_back(tokenPolicy);
00210 }
00211 else
00212 {
00213 Log("Unknown group of endpoint parameters: ", subGroup.Name);
00214 }
00215 }
00216 return endpoint;
00217 }
00218
00219 Common::ParametersGroup GetEndpointDescription(const EndpointDescription& endpoint) const
00220 {
00221 Common::ParametersGroup ed("endpoint");
00222 ed.Parameters.push_back(Common::Parameter("security_level", std::to_string(endpoint.SecurityLevel)));
00223 ed.Parameters.push_back(Common::Parameter("security_mode", GetSecurityMode(endpoint.SecurityMode)));
00224 ed.Parameters.push_back(Common::Parameter("security_policy_uri", endpoint.SecurityPolicyUri));
00225 ed.Parameters.push_back(Common::Parameter("transport_profile_uri", endpoint.TransportProfileUri));
00226 ed.Parameters.push_back(Common::Parameter("url", endpoint.EndpointUrl));
00227
00228 for (const UserTokenPolicy& policy : endpoint.UserIdentityTokens)
00229 {
00230 ed.Groups.push_back(GetUserTokenPolicy(policy));
00231 }
00232 return ed;
00233 }
00234
00235 ApplicationData GetApplicationData(const Common::ParametersGroup& applicationGroup) const
00236 {
00237 Log("Parsing application parameters.");
00238 ApplicationData data;
00239 for (const Common::Parameter param : applicationGroup.Parameters)
00240 {
00241 Log("Parsing app paramter ", param.Name, " = ", param.Value);
00242 if (param.Name == "uri")
00243 {
00244 data.Application.ApplicationUri = param.Value;
00245 }
00246 else if (param.Name == "product_uri")
00247 {
00248 data.Application.ProductUri = param.Value;
00249 }
00250 else if (param.Name == "gateway_server_uri")
00251 {
00252 data.Application.GatewayServerUri = param.Value;
00253 }
00254 else if (param.Name == "discovery_profile")
00255 {
00256 data.Application.DiscoveryProfileUri = param.Value;
00257 }
00258 else if (param.Name == "name")
00259 {
00260 data.Application.ApplicationName = LocalizedText(param.Value);
00261 }
00262 else if (param.Name == "type")
00263 {
00264 data.Application.ApplicationType = GetApplicationType(param.Value);
00265 }
00266 else
00267 {
00268 Log("Unknown application parameter: ", param.Name, "=", param.Value);
00269 }
00270 }
00271
00272 for (const Common::ParametersGroup group : applicationGroup.Groups)
00273 {
00274 Log("parsing group ", group.Name);
00275 if (group.Name == "endpoint")
00276 {
00277 EndpointDescription endpoint = GetEndpointDescription(group);
00278 data.Application.DiscoveryUrls.push_back(endpoint.EndpointUrl);
00279 data.Endpoints.push_back(endpoint);
00280 }
00281 else
00282 {
00283 Log("Unknown group in the applications parameters: ", group.Name);
00284 }
00285 }
00286
00287 for (EndpointDescription& endpoint : data.Endpoints)
00288 {
00289 endpoint.Server = data.Application;
00290 }
00291
00292 return data;
00293 }
00294
00295 Common::ParametersGroup ApplicationToParametersGroup(const ApplicationData& app) const
00296 {
00297 Common::ParametersGroup result("application");
00298 result.Parameters.push_back(Common::Parameter("discovery_profile", app.Application.DiscoveryProfileUri));
00299 result.Parameters.push_back(Common::Parameter("uri", app.Application.ApplicationUri));
00300 result.Parameters.push_back(Common::Parameter("gateway_server_uri", app.Application.GatewayServerUri));
00301 result.Parameters.push_back(Common::Parameter("product_uri", app.Application.ProductUri));
00302 result.Parameters.push_back(Common::Parameter("name", app.Application.ApplicationName.Text));
00303 result.Parameters.push_back(Common::Parameter("type", GetApplicationType(app.Application.ApplicationType)));
00304
00305 for (const EndpointDescription& endpoint : app.Endpoints)
00306 {
00307 result.Groups.push_back(GetEndpointDescription(endpoint));
00308 }
00309 return result;
00310 }
00311
00312 private:
00313 template <typename T, typename... Args>
00314 void Log(T&& msg, Args... args) const
00315 {
00316 if (!Debug)
00317 {
00318 return;
00319 }
00320 std::clog << msg;
00321 Log(args...);
00322 }
00323
00324 void Log() const
00325 {
00326 if (!Debug)
00327 {
00328 return;
00329 }
00330 std::clog << std::endl;
00331 }
00332
00333 private:
00334 const bool Debug;
00335 };
00336 }
00337
00338 std::vector<OpcUa::Server::ApplicationData> OpcUa::ParseEndpointsParameters(const std::vector<Common::ParametersGroup>& applicationGroups, bool debug)
00339 {
00340 OpcUaParameters parser(debug);
00341 const std::vector<OpcUa::Server::ApplicationData>& data = parser.GetApplications(applicationGroups);
00342 return data;
00343 }
00344
00345 std::vector<Common::ParametersGroup> OpcUa::CreateCommonParameters(const std::vector<Server::ApplicationData>& endpoints, bool debug)
00346 {
00347 OpcUaParameters parser(debug);
00348 const std::vector<Common::ParametersGroup>& result = parser.GetAddonParameters(endpoints);
00349 return result;
00350 }