-
Notifications
You must be signed in to change notification settings - Fork 424
/
DefaultViewServiceSet.java
316 lines (247 loc) · 12.5 KB
/
DefaultViewServiceSet.java
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
* Copyright (c) 2024 the Eclipse Milo Authors
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.milo.opcua.sdk.server.servicesets.impl;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import org.eclipse.milo.opcua.sdk.server.AddressSpace.RegisterNodesContext;
import org.eclipse.milo.opcua.sdk.server.AddressSpace.UnregisterNodesContext;
import org.eclipse.milo.opcua.sdk.server.ContinuationPoint;
import org.eclipse.milo.opcua.sdk.server.OpcUaServer;
import org.eclipse.milo.opcua.sdk.server.Session;
import org.eclipse.milo.opcua.sdk.server.servicesets.ViewServiceSet;
import org.eclipse.milo.opcua.sdk.server.servicesets.impl.helpers.BrowseHelper;
import org.eclipse.milo.opcua.sdk.server.servicesets.impl.helpers.BrowsePathsHelper;
import org.eclipse.milo.opcua.stack.core.StatusCodes;
import org.eclipse.milo.opcua.stack.core.UaException;
import org.eclipse.milo.opcua.stack.core.types.builtin.ByteString;
import org.eclipse.milo.opcua.stack.core.types.builtin.DiagnosticInfo;
import org.eclipse.milo.opcua.stack.core.types.builtin.NodeId;
import org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode;
import org.eclipse.milo.opcua.stack.core.types.structured.BrowseDescription;
import org.eclipse.milo.opcua.stack.core.types.structured.BrowseNextRequest;
import org.eclipse.milo.opcua.stack.core.types.structured.BrowseNextResponse;
import org.eclipse.milo.opcua.stack.core.types.structured.BrowseRequest;
import org.eclipse.milo.opcua.stack.core.types.structured.BrowseResponse;
import org.eclipse.milo.opcua.stack.core.types.structured.BrowseResult;
import org.eclipse.milo.opcua.stack.core.types.structured.ReferenceDescription;
import org.eclipse.milo.opcua.stack.core.types.structured.RegisterNodesRequest;
import org.eclipse.milo.opcua.stack.core.types.structured.RegisterNodesResponse;
import org.eclipse.milo.opcua.stack.core.types.structured.ResponseHeader;
import org.eclipse.milo.opcua.stack.core.types.structured.TranslateBrowsePathsToNodeIdsRequest;
import org.eclipse.milo.opcua.stack.core.types.structured.TranslateBrowsePathsToNodeIdsResponse;
import org.eclipse.milo.opcua.stack.core.types.structured.UnregisterNodesRequest;
import org.eclipse.milo.opcua.stack.core.types.structured.UnregisterNodesResponse;
import org.eclipse.milo.opcua.stack.core.util.Lists;
import org.eclipse.milo.opcua.stack.transport.server.ServiceRequestContext;
import static org.eclipse.milo.opcua.sdk.server.servicesets.AbstractServiceSet.createResponseHeader;
import static org.eclipse.milo.opcua.stack.core.util.FutureUtils.failedUaFuture;
public class DefaultViewServiceSet implements ViewServiceSet {
private final OpcUaServer server;
public DefaultViewServiceSet(OpcUaServer server) {
this.server = server;
}
@Override
public BrowseResponse onBrowse(ServiceRequestContext context, BrowseRequest request) throws UaException {
Session session = server.getSessionManager()
.getSession(context, request.getRequestHeader());
try {
return browse(request, session).get();
} catch (Exception e) {
session.getSessionDiagnostics().getBrowseCount().incrementErrorCount();
session.getSessionDiagnostics().getTotalRequestCount().incrementErrorCount();
throw UaException.extract(e).orElse(new UaException(e));
} finally {
session.getSessionDiagnostics().getBrowseCount().incrementTotalCount();
session.getSessionDiagnostics().getTotalRequestCount().incrementTotalCount();
}
}
@Override
public BrowseNextResponse onBrowseNext(
ServiceRequestContext context,
BrowseNextRequest request
) throws UaException {
Session session = server.getSessionManager()
.getSession(context, request.getRequestHeader());
try {
return browseNext(request, session);
} catch (Exception e) {
session.getSessionDiagnostics().getBrowseNextCount().incrementErrorCount();
session.getSessionDiagnostics().getTotalRequestCount().incrementErrorCount();
throw UaException.extract(e).orElse(new UaException(e));
} finally {
session.getSessionDiagnostics().getBrowseNextCount().incrementTotalCount();
session.getSessionDiagnostics().getTotalRequestCount().incrementTotalCount();
}
}
@Override
public TranslateBrowsePathsToNodeIdsResponse onTranslateBrowsePaths(
ServiceRequestContext context,
TranslateBrowsePathsToNodeIdsRequest request
) throws UaException {
Session session = server.getSessionManager()
.getSession(context, request.getRequestHeader());
try {
var browsePathsHelper = new BrowsePathsHelper(() -> Optional.ofNullable(session), server);
return browsePathsHelper.translateBrowsePaths(request).get();
} catch (Exception e) {
session.getSessionDiagnostics().getTranslateBrowsePathsToNodeIdsCount().incrementErrorCount();
session.getSessionDiagnostics().getTotalRequestCount().incrementErrorCount();
throw UaException.extract(e).orElse(new UaException(e));
} finally {
session.getSessionDiagnostics().getTranslateBrowsePathsToNodeIdsCount().incrementTotalCount();
session.getSessionDiagnostics().getTotalRequestCount().incrementTotalCount();
}
}
@Override
public RegisterNodesResponse onRegisterNodes(
ServiceRequestContext context,
RegisterNodesRequest request
) throws UaException {
Session session = server.getSessionManager()
.getSession(context, request.getRequestHeader());
try {
return registerNodes(request, session);
} catch (Exception e) {
session.getSessionDiagnostics().getRegisterNodesCount().incrementErrorCount();
session.getSessionDiagnostics().getTotalRequestCount().incrementErrorCount();
throw UaException.extract(e).orElse(new UaException(e));
} finally {
session.getSessionDiagnostics().getRegisterNodesCount().incrementTotalCount();
session.getSessionDiagnostics().getTotalRequestCount().incrementTotalCount();
}
}
@Override
public UnregisterNodesResponse onUnregisterNodes(
ServiceRequestContext context,
UnregisterNodesRequest request
) throws UaException {
Session session = server.getSessionManager()
.getSession(context, request.getRequestHeader());
try {
return unregisterNodes(request, session);
} catch (Exception e) {
session.getSessionDiagnostics().getUnregisterNodesCount().incrementErrorCount();
session.getSessionDiagnostics().getTotalRequestCount().incrementErrorCount();
throw UaException.extract(e).orElse(new UaException(e));
} finally {
session.getSessionDiagnostics().getUnregisterNodesCount().incrementTotalCount();
session.getSessionDiagnostics().getTotalRequestCount().incrementTotalCount();
}
}
private CompletableFuture<BrowseResponse> browse(BrowseRequest request, Session session) {
List<BrowseDescription> nodesToBrowse = Lists.ofNullable(request.getNodesToBrowse());
if (nodesToBrowse.isEmpty()) {
return failedUaFuture(StatusCodes.Bad_NothingToDo);
}
if (nodesToBrowse.size() > server.getConfig().getLimits().getMaxNodesPerBrowse().intValue()) {
return failedUaFuture(StatusCodes.Bad_TooManyOperations);
}
if (request.getView().getViewId().isNotNull() &&
!server.getRegisteredViews().contains(request.getView().getViewId())) {
return failedUaFuture(StatusCodes.Bad_ViewIdUnknown);
}
List<BrowseResult> results = BrowseHelper.browse(server, () -> Optional.of(session), request);
ResponseHeader header = createResponseHeader(request);
var response = new BrowseResponse(
header,
results.toArray(BrowseResult[]::new),
new DiagnosticInfo[0]
);
return CompletableFuture.completedFuture(response);
}
private BrowseNextResponse browseNext(
BrowseNextRequest request, Session session) throws UaException {
List<ByteString> continuationPoints = Lists.ofNullable(request.getContinuationPoints());
if (continuationPoints.isEmpty()) {
throw new UaException(StatusCodes.Bad_NothingToDo);
}
if (continuationPoints.size() >
server.getConfig().getLimits().getMaxBrowseContinuationPoints().intValue()) {
throw new UaException(StatusCodes.Bad_TooManyOperations);
}
var results = new ArrayList<BrowseResult>();
for (ByteString bs : continuationPoints) {
if (request.getReleaseContinuationPoints()) {
results.add(release(session, bs));
} else {
results.add(references(session, bs));
}
}
var header = createResponseHeader(request);
return new BrowseNextResponse(
header,
results.toArray(BrowseResult[]::new),
new DiagnosticInfo[0]
);
}
private static BrowseResult release(Session session, ByteString bs) {
ContinuationPoint c = session.getBrowseContinuationPoints().remove(bs);
return c != null ?
new BrowseResult(StatusCode.GOOD, null, null) :
new BrowseResult(new StatusCode(StatusCodes.Bad_ContinuationPointInvalid), null, null);
}
private static BrowseResult references(Session session, ByteString bs) {
ContinuationPoint c = session.getBrowseContinuationPoints().remove(bs);
if (c != null) {
int max = c.max();
List<ReferenceDescription> references = c.references();
if (references.size() > max) {
List<ReferenceDescription> subList = references.subList(0, max);
List<ReferenceDescription> current = List.copyOf(subList);
subList.clear();
session.getBrowseContinuationPoints().put(c.id(), c);
return new BrowseResult(
StatusCode.GOOD,
c.id(),
current.toArray(new ReferenceDescription[0])
);
} else {
return new BrowseResult(
StatusCode.GOOD,
null,
references.toArray(new ReferenceDescription[0])
);
}
} else {
return new BrowseResult(new StatusCode(StatusCodes.Bad_ContinuationPointInvalid), null, null);
}
}
private RegisterNodesResponse registerNodes(
RegisterNodesRequest request, Session session) throws UaException {
List<NodeId> nodeIds = Lists.ofNullable(request.getNodesToRegister());
if (nodeIds.isEmpty()) {
throw new UaException(StatusCodes.Bad_NothingToDo);
}
if (nodeIds.size() > server.getConfig().getLimits().getMaxNodesPerRegisterNodes().intValue()) {
throw new UaException(StatusCodes.Bad_TooManyOperations);
}
var registerNodesContext = new RegisterNodesContext(server, session);
List<NodeId> registeredNodeIds =
server.getAddressSpaceManager().registerNodes(registerNodesContext, nodeIds);
ResponseHeader header = createResponseHeader(request);
return new RegisterNodesResponse(header, registeredNodeIds.toArray(new NodeId[0]));
}
private UnregisterNodesResponse unregisterNodes(
UnregisterNodesRequest request, Session session) throws UaException {
List<NodeId> nodeIds = Lists.ofNullable(request.getNodesToUnregister());
if (nodeIds.isEmpty()) {
throw new UaException(StatusCodes.Bad_NothingToDo);
}
if (nodeIds.size() > server.getConfig().getLimits().getMaxNodesPerRegisterNodes().intValue()) {
throw new UaException(StatusCodes.Bad_TooManyOperations);
}
var unregisterNodesContext = new UnregisterNodesContext(server, session);
server.getAddressSpaceManager().unregisterNodes(unregisterNodesContext, nodeIds);
ResponseHeader header = createResponseHeader(request);
return new UnregisterNodesResponse(header);
}
}