test_computer.cpp
Go to the documentation of this file.
1 
12 
13 #include <stdexcept>
14 
15 namespace
16 {
17 using namespace OpcUa;
18 
19 class TestEndpoints : public EndpointServices
20 {
21 public:
22  TestEndpoints(const std::string & url)
23  : Applications(GetApplications(url))
24  , Endpoints(GetEndpoints(url))
25  {
26  }
27 
28  virtual std::vector<ApplicationDescription> FindServers(const FindServersParameters & params) const
29  {
30  return Applications;
31  }
32 
33  virtual std::vector<EndpointDescription> GetEndpoints(const EndpointsFilter & filter) const
34  {
35  return Endpoints;
36  }
37 
38  virtual void RegisterServer(const ServerParameters & parameters)
39  {
40  throw std::logic_error("not implemented.");
41  }
42 
43 private:
44  std::vector<ApplicationDescription> GetApplications(const std::string & url) const
45  {
47  app.URI = "URI";
48  app.Type = ApplicationType::Client;
49  app.ProductURI = "ProductURI";
50  app.Name.Text = "Name";
51  app.GatewayServerURI = "GatewayServerURI";
52  app.DiscoveryProfileURI = "DiscoveryProfileURI";
53  app.DiscoveryURLs.push_back(url);
54  return std::vector<ApplicationDescription>(1, app);
55  }
56 
57  std::vector<EndpointDescription> GetEndpoints(const std::string & url) const
58  {
59  EndpointDescription endpoint;
60  endpoint.EndpointUrl = url;
61  endpoint.SecurityLevel = 1;
63  endpoint.SecurityPolicyUri = "SecurityPolicyUri";
64  endpoint.ServerCertificate.push_back(1);
65  endpoint.Server = GetApplications(url).front();
66  endpoint.TransportProfileUri = "TransportProfileURI";
67  endpoint.UserIdentityTokens = GetUserTokens();
68  return std::vector<EndpointDescription>(1, endpoint);
69  }
70 
71  std::vector<OpcUa::UserTokenPolicy> GetUserTokens() const
72  {
74  policy.IssuedTokenType = "IssuedTokenType";
75  policy.IssuerEndpointUrl = "IssuerEndpointUrl";
76  policy.PolicyId = "PolicyId";
77  policy.SecurityPolicyUri = "SecurityPolicyUri";
79  return std::vector<OpcUa::UserTokenPolicy>(1, policy);
80  }
81 
82 private:
83  std::vector<ApplicationDescription> Applications;
84  std::vector<EndpointDescription> Endpoints;
85 };
86 
87 void Assert(bool isTrue, const char * msg)
88 {
89  if (!isTrue)
90  {
91  throw std::logic_error(msg);
92  }
93 }
94 
95 class TestAttributes : public AttributeServices
96 {
97 public:
98  virtual std::vector<DataValue> Read(const OpcUa::ReadParameters & params) const
99  {
100  Assert(params.MaxAge == 1, "Invalid MaxAgeValue");
101  Assert(params.TimestampsType == TimestampsToReturn::BOTH, "Invalid value of TimestampsToReturn.");
102  Assert(params.AttributesToRead.size() == 1, "Invalid size of AttributesToRead.");
103 
104  OpcUa::ReadValueId id = params.AttributesToRead[0];
105  Assert(id.Attribute == AttributeId::Value, "Invalid value of Attribute Id.");
106  Assert(id.DataEncoding.NamespaceIndex == 3, "Invalid namespace index in DataEncoding.");
107  Assert(id.DataEncoding.Name == "binary", "Invalid name in DataEncoding.");
108  Assert(id.IndexRange == "1:2", "Invalid value of IndexRange.");
109  Assert(id.Node.IsInteger(), "Node id is not integer.");
110  Assert(id.Node.GetIntegerIdentifier() == 2, "Node id is not equal to 2.");
111  Assert(id.Node.GetNamespaceIndex() == 1, "NodeId's namespace index is not equal to 1.");
112 
113  DataValue data;
114  data.Encoding =
115  DATA_VALUE |
121  data.ServerPicoseconds = 1;
122  data.ServerTimestamp.Value = 2;
123  data.SourcePicoseconds = 3;
124  data.SourceTimestamp.Value = 4;
125  data.Status = StatusCode::BadNotReadable;
126  data.Value = std::string("value");
127 
128  return std::vector<DataValue>(1, data);
129  }
130 
131  virtual std::vector<StatusCode> Write(const std::vector<OpcUa::WriteValue> & data)
132  {
133  Assert(data.size() == 1, "Invalid number od data for write.");
134  const OpcUa::WriteValue & value = data[0];
135  Assert(value.Attribute == OpcUa::AttributeId::Value, "Invalid id of attribute.");
136  Assert(value.Node.Encoding == NodeIdEncoding::EV_STRING, "Invalid encoding of node.");
137  Assert(value.Node.StringData.NamespaceIndex == 1, "Invalid namespace of node.");
138  Assert(value.Node.StringData.Identifier == "node", "Invalid identifier of node.");
139  Assert(value.NumericRange == "1:2", "Invalid numeric range.");
140  Assert(value.Data.ServerPicoseconds == 1, "Invalid ServerPicoseconds.");
141  Assert(value.Data.ServerTimestamp.Value == 2, "Invalid ServerTimeStamp.");
142  Assert(value.Data.SourcePicoseconds == 3, "Invalid SourcePicoseconds.");
143  Assert(value.Data.SourceTimestamp.Value == 4, "Invalid SourceTimeStamp.");
144  Assert(value.Data.Status == StatusCode::BadNotReadable, "Invalid data status.");
145  Assert(value.Data.Value.Type() == VariantType::STRING, "Invalid data type.");
146  Assert(value.Data.Value.As<std::string>().size() == 1, "Invalid number of strings in variant.");
147  Assert(value.Data.Value == std::vector<std::string>(1, "value"), "Invalid data value.");
148 
149  const uint8_t encoding =
150  DATA_VALUE |
156 
157  Assert(value.Data.Encoding == encoding, "Invalid encoding mask.");
158 
159  return std::vector<StatusCode>(1, StatusCode::BadNotReadable);
160  }
161 };
162 
163 class TestViewServices : public ViewServices
164 {
165 public:
166  virtual std::vector<ReferenceDescription> Browse(const OpcUa::NodesQuery & query) const
167  {
169  ref.BrowseName.Name = "Name";
170  ref.BrowseName.NamespaceIndex = 1;
171  ref.DisplayName.Text = "Text";
172  ref.IsForward = true;
175  ref.ReferenceTypeId.StringData.Identifier = "Identifier";
183  return std::vector<ReferenceDescription>(1, ref);
184  }
185 
186  virtual std::vector<ReferenceDescription> BrowseNext() const
187  {
188  return std::vector<ReferenceDescription>();
189  }
190 
191  virtual std::vector<BrowsePathResult> TranslateBrowsePathsToNodeIds(const TranslateBrowsePathsParameters & params) const
192  {
193  throw std::logic_error("not implemented.");
194  }
195 };
196 
197 class TestComputer : public Services
198 {
199 public:
200  TestComputer(const std::string & url)
201  : EndpointsImpl(new TestEndpoints(url))
202  , ViewsImpl(new TestViewServices())
203  , AttributesImpl(new TestAttributes)
204  {
205  }
206 
207  virtual void CreateSession(const RemoteSessionParameters & parameters)
208  {
209  throw std::logic_error("not implemented.");
210  }
211 
212  virtual void ActivateSession()
213  {
214  throw std::logic_error("not implemented.");
215  }
216 
217  virtual void CloseSession()
218  {
219  throw std::logic_error("not implemented.");
220  }
221 
222  virtual std::shared_ptr<EndpointServices> Endpoints()
223  {
224  return EndpointsImpl;
225  }
226 
227  virtual std::shared_ptr<ViewServices> Views()
228  {
229  return ViewsImpl;
230  }
231 
232  virtual std::shared_ptr<AttributeServices> Attributes()
233  {
234  return AttributesImpl;
235  }
236 
237  virtual std::shared_ptr<SubscriptionServices> Subscriptions()
238  {
239  throw std::logic_error("not implemented.");
240  }
241 
242  virtual std::shared_ptr<NodeManagementServices> NodeManagement()
243  {
244  throw std::logic_error("not implemented.");
245  }
246 
247 private:
248  EndpointServices::SharedPtr EndpointsImpl;
249  ViewServices::SharedPtr ViewsImpl;
250  AttributeServices::SharedPtr AttributesImpl;
251 };
252 
253 }
254 
255 namespace OpcUa
256 {
257 
258 std::unique_ptr<Services> Connect(const std::string & url)
259 {
260  return std::unique_ptr<Services>(new TestComputer(url));
261 }
262 
263 }
264 
std::vector< OpcUa::ReadValueId > AttributesToRead
struct OpcUa::NodeId::NumericDataType NumericData
std::vector< OpcUa::UserTokenPolicy > UserIdentityTokens
std::string SecurityPolicyUri
void CloseSession(OpcUa::Binary::IOStream &stream, const OpcUa::Binary::CreateSessionResponse &session)
const uint8_t DATA_VALUE_STATUS_CODE
Definition: data_value.h:19
std::string IssuedTokenType
std::vector< T > Browse(const NodeId &node, NodeClass nodeClassMask, Services::SharedPtr services)
Definition: model_impl.h:31
uint16_t NamespaceIndex
Definition: types.h:73
const uint8_t DATA_VALUE_Server_TIMESTAMP
Definition: data_value.h:21
OpcUa::ApplicationDescription Server
OpcUa::Binary::CreateSessionResponse CreateSession(OpcUa::Binary::IOStream &stream)
const uint8_t DATA_VALUE_SOURCE_PICOSECONDS
Definition: data_value.h:22
struct OpcUa::NodeId::FourByteDataType FourByteData
const uint8_t DATA_VALUE_SOURCE_TIMESTAMP
Definition: data_value.h:20
OpcUa::MessageSecurityMode SecurityMode
OPC UA Address space part. GNU LGPL.
const uint8_t DATA_VALUE_Server_PICOSECONDS
Definition: data_value.h:23
void ActivateSession(OpcUa::Binary::IOStream &stream, const OpcUa::Binary::CreateSessionResponse &session)
const char Views[]
Definition: strings.h:208
struct OpcUa::NodeId::StringDataType StringData
OpcUa::ByteString ServerCertificate
A Node object represent an OPC-UA node. It is high level object intended for developper who want to e...
Definition: node.h:42
std::string Text
Definition: types.h:136
std::string Name
Definition: types.h:74
void Assert(bool condition, const char *file, int line)
NodeIdEncoding Encoding
Definition: nodeid.h:46
url
Definition: setup.py:46
OpcUa::UserTokenType TokenType
uint8_t Encoding
Definition: data_value.h:28
std::string IssuerEndpointUrl
std::unique_ptr< RemoteConnection > Connect(const std::string &host, unsigned port, const Common::Logger::SharedPtr &logger)


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