• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

WindhoverLabs / yamcs-opcua / #28

12 Jul 2024 10:55PM UTC coverage: 86.874% (+4.1%) from 82.728%
#28

push

lorenzo-gomez-windhover
-Update unit tests

642 of 739 relevant lines covered (86.87%)

0.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.62
/src/main/java/com/windhoverlabs/yamcs/opcua/OPCUALink.java
1
/****************************************************************************
2
 *
3
 *   Copyright (c) 2024 Windhover Labs, L.L.C. All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * 1. Redistributions of source code must retain the above copyright
10
 *    notice, this list of conditions and the following disclaimer.
11
 * 2. Redistributions in binary form must reproduce the above copyright
12
 *    notice, this list of conditions and the following disclaimer in
13
 *    the documentation and/or other materials provided with the
14
 *    distribution.
15
 * 3. Neither the name Windhover Labs nor the names of its
16
 *    contributors may be used to endorse or promote products derived
17
 *    from this software without specific prior written permission.
18
 *
19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
 * POSSIBILITY OF SUCH DAMAGE.
31
 *
32
 *****************************************************************************/
33

34
package com.windhoverlabs.yamcs.opcua;
35

36
import static com.google.common.collect.Lists.newArrayList;
37
import static org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint;
38
import static org.yamcs.xtce.NameDescription.qualifiedName;
39

40
import com.google.gson.JsonObject;
41
import java.io.BufferedWriter;
42
import java.io.IOException;
43
import java.nio.file.Files;
44
import java.nio.file.Paths;
45
import java.nio.file.StandardOpenOption;
46
import java.time.Instant;
47
import java.time.temporal.ChronoUnit;
48
import java.util.ArrayList;
49
import java.util.Arrays;
50
import java.util.HashMap;
51
import java.util.HashSet;
52
import java.util.List;
53
import java.util.Map;
54
import java.util.Objects;
55
import java.util.Set;
56
import java.util.concurrent.CompletableFuture;
57
import java.util.concurrent.ConcurrentHashMap;
58
import java.util.concurrent.ExecutionException;
59
import java.util.concurrent.atomic.AtomicLong;
60
import java.util.function.Supplier;
61
import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
62
import org.eclipse.milo.opcua.sdk.client.api.config.OpcUaClientConfig;
63
import org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaMonitoredItem;
64
import org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaSubscription;
65
import org.eclipse.milo.opcua.sdk.client.nodes.UaNode;
66
import org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedDataItem;
67
import org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedSubscription;
68
import org.eclipse.milo.opcua.stack.client.DiscoveryClient;
69
import org.eclipse.milo.opcua.stack.core.AttributeId;
70
import org.eclipse.milo.opcua.stack.core.Identifiers;
71
import org.eclipse.milo.opcua.stack.core.UaException;
72
import org.eclipse.milo.opcua.stack.core.types.builtin.ByteString;
73
import org.eclipse.milo.opcua.stack.core.types.builtin.DataValue;
74
import org.eclipse.milo.opcua.stack.core.types.builtin.DateTime;
75
import org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject;
76
import org.eclipse.milo.opcua.stack.core.types.builtin.LocalizedText;
77
import org.eclipse.milo.opcua.stack.core.types.builtin.NodeId;
78
import org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName;
79
import org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode;
80
import org.eclipse.milo.opcua.stack.core.types.builtin.Variant;
81
import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger;
82
import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UShort;
83
import org.eclipse.milo.opcua.stack.core.types.enumerated.IdType;
84
import org.eclipse.milo.opcua.stack.core.types.enumerated.MonitoringMode;
85
import org.eclipse.milo.opcua.stack.core.types.enumerated.NodeClass;
86
import org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn;
87
import org.eclipse.milo.opcua.stack.core.types.structured.BrowsePath;
88
import org.eclipse.milo.opcua.stack.core.types.structured.BrowsePathResult;
89
import org.eclipse.milo.opcua.stack.core.types.structured.ContentFilter;
90
import org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription;
91
import org.eclipse.milo.opcua.stack.core.types.structured.EventFilter;
92
import org.eclipse.milo.opcua.stack.core.types.structured.MonitoredItemCreateRequest;
93
import org.eclipse.milo.opcua.stack.core.types.structured.MonitoringParameters;
94
import org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId;
95
import org.eclipse.milo.opcua.stack.core.types.structured.RelativePath;
96
import org.eclipse.milo.opcua.stack.core.types.structured.RelativePathElement;
97
import org.eclipse.milo.opcua.stack.core.types.structured.SimpleAttributeOperand;
98
import org.eclipse.milo.opcua.stack.core.types.structured.TranslateBrowsePathsToNodeIdsResponse;
99
import org.slf4j.Logger;
100
import org.slf4j.LoggerFactory;
101
import org.yamcs.ConfigurationException;
102
import org.yamcs.Spec;
103
import org.yamcs.Spec.OptionType;
104
import org.yamcs.StandardTupleDefinitions;
105
import org.yamcs.ValidationException;
106
import org.yamcs.YConfiguration;
107
import org.yamcs.YamcsServer;
108
import org.yamcs.http.NotFoundException;
109
import org.yamcs.mdb.XtceAssembler;
110
import org.yamcs.parameter.ParameterValue;
111
import org.yamcs.parameter.SystemParametersProducer;
112
import org.yamcs.parameter.SystemParametersService;
113
import org.yamcs.protobuf.Event.EventSeverity;
114
import org.yamcs.protobuf.Yamcs.NamedObjectId;
115
import org.yamcs.protobuf.Yamcs.Value.Type;
116
import org.yamcs.tctm.AbstractLink;
117
import org.yamcs.tctm.Link;
118
import org.yamcs.tctm.LinkAction;
119
import org.yamcs.utils.ValueUtility;
120
import org.yamcs.xtce.BooleanParameterType;
121
import org.yamcs.xtce.EnumeratedParameterType;
122
import org.yamcs.xtce.FloatParameterType;
123
import org.yamcs.xtce.IntegerParameterType;
124
import org.yamcs.xtce.NameDescription;
125
import org.yamcs.xtce.Parameter;
126
import org.yamcs.xtce.ParameterType;
127
import org.yamcs.xtce.SpaceSystem;
128
import org.yamcs.xtce.StringParameterType;
129
import org.yamcs.xtce.XtceDb;
130
import org.yamcs.yarch.DataType;
131
import org.yamcs.yarch.Stream;
132
import org.yamcs.yarch.Tuple;
133
import org.yamcs.yarch.TupleDefinition;
134
import org.yamcs.yarch.YarchDatabase;
135
import org.yamcs.yarch.YarchDatabaseInstance;
136
import org.yamcs.yarch.protobuf.Db.Event;
137

138
/**
139
 * Implementation of the OPCUA protocol as a YAMCS link. Maps configured nodes(see docs for details)
140
 * to yamcs PVs and subscribes to OPCUA variables for realtime updates.
141
 *
142
 * @author Lorenzo Gomez
143
 */
144
public class OPCUALink extends AbstractLink implements Runnable, SystemParametersProducer {
1✔
145

146
  class NodeIDAttrPair {
147
    NodeId nodeID;
148
    AttributeId attrID;
149

150
    public NodeIDAttrPair(NodeId newNodeID, AttributeId newAttrID) {
1✔
151
      this.nodeID = newNodeID;
1✔
152
      this.attrID = newAttrID;
1✔
153
    }
1✔
154

155
    public int hashCode() {
156
      return Objects.hash(this.nodeID, this.attrID);
1✔
157
    }
158

159
    public boolean equals(Object obj) {
160
      return (this.hashCode() == obj.hashCode());
1✔
161
    }
162
  }
163

164
  class NodePath {
1✔
165
    String path;
166

167
    HashMap<Object, Object> rootNodeID = new HashMap<Object, Object>();
1✔
168
  }
169

170
  /** Useful status for tracking initialization status of the link. */
171
  public enum OPCUAStatus {
1✔
172
    OPCUA_INIT_CONFIG,
1✔
173
    OPCUA_INIT_TREE,
1✔
174
    OPCUA_INIT_GENERATE_XTCE,
1✔
175
    OPCUA_INIT_EVENTS,
1✔
176
    OPCUA_INIT_DATA_SUBSCRIPTION,
1✔
177
    OPCUA_INIT_ALL_DATA_QUERY,
1✔
178
    OPCUA_OK
1✔
179
  }
180

181
  /* Configuration Defaults */
182
  static final String STREAM_NAME = "opcua_params";
183

184
  /* Internal member attributes. */
185
  protected Thread thread;
186

187
  private String opcuaStreamName;
188

189
  private String parametersNamespace;
190
  XtceDb mdb;
191

192
  Stream opcuaStream;
193

194
  private static TupleDefinition gftdef = StandardTupleDefinitions.PARAMETER.copy();
1✔
195

196
  private ManagedSubscription opcuaSubscription;
197

198
  private static final Logger internalLogger = LoggerFactory.getLogger(OPCUALink.class.getName());
1✔
199

200
  /**
201
   * @note ALWAYS re-use params as org.yamcs.parameter.ParameterRequestManager.param2RequestMap uses
202
   *     the object inside a map that was added to the mdb for the very fist time. If when
203
   *     publishing the PV, we create a new VariableParam object clients will NOT receive real-time
204
   *     updates as the new object VariableParam inside the new PV won't match the one inside
205
   *     org.yamcs.parameter.ParameterRequestManager.param2RequestMap since the object hashes do not
206
   *     match (since VariableParam does not override its hash function).
207
   */
208
  private ConcurrentHashMap<NodeIDAttrPair, VariableParam> nodeIDToParamsMap =
1✔
209
      new ConcurrentHashMap<NodeIDAttrPair, VariableParam>();
210

211
  private OpcUaClient client;
212

213
  protected AtomicLong inCount = new AtomicLong(0);
1✔
214

215
  private Status linkStatus = Status.OK;
1✔
216

217
  /* Configuration Parameters */
218

219
  private String discoverURL;
220
  private String endpointURL;
221
  private boolean queryAllNodesAtStartup;
222
  private String outputFile;
223
  private int publishInterval; // milliseconds
224

225
  private ArrayList<NodePath> relativeNodePaths = new ArrayList<NodePath>();
1✔
226

227
  private final AtomicLong clientHandles = new AtomicLong(1L);
1✔
228

229
  /* System parameters*/
230

231
  private Parameter OPCUAStatusParam;
232
  private OPCUAStatus currentOPCUAStatus;
233
  private Parameter OPCUAActiveSubsParam;
234
  private AtomicLong OPCUAActiveSubs = new AtomicLong(0);
1✔
235

236
  LinkAction startAction =
1✔
237
      new LinkAction("query_all", "Query All OPCUA Server Data") {
1✔
238
        @Override
239
        public JsonObject execute(Link link, JsonObject jsonObject) {
240

241
          internalLogger.info("Executing query_all action");
1✔
242
          CompletableFuture.supplyAsync(
1✔
243
                  (Supplier<Integer>)
244
                      () -> {
245
                        queryAllOPCUAData();
1✔
246

247
                        return 0;
1✔
248
                      })
249
              .whenComplete(
1✔
250
                  (vaue, e) -> {
251
                    internalLogger.info("query_all action Complete");
1✔
252
                  });
1✔
253

254
          return jsonObject;
1✔
255
        }
256
      };
257

258
  public OPCUAStatus getCurrentOPCUAStatus() {
259
    return currentOPCUAStatus;
1✔
260
  }
261

262
  @Override
263
  public Spec getSpec() {
264
    Spec spec = new Spec();
1✔
265

266
    /* Define our configuration parameters. */
267
    spec.addOption("name", OptionType.STRING).withRequired(true);
1✔
268
    spec.addOption("class", OptionType.STRING).withRequired(true);
1✔
269
    spec.addOption("opcuaStream", OptionType.STRING).withRequired(true);
1✔
270
    spec.addOption("endpointUrl", OptionType.STRING).withRequired(true);
1✔
271
    spec.addOption("discoveryUrl", OptionType.STRING).withRequired(true);
1✔
272
    spec.addOption("xtceOutputFile", OptionType.STRING).withRequired(true);
1✔
273
    spec.addOption("parametersNamespace", OptionType.STRING).withRequired(true);
1✔
274
    spec.addOption("publishInterval", OptionType.INTEGER).withRequired(true);
1✔
275
    spec.addOption("queryAllNodesAtStartup", OptionType.BOOLEAN).withRequired(false);
1✔
276

277
    Spec rootNodeIDSpec = new Spec();
1✔
278

279
    rootNodeIDSpec.addOption("namespaceIndex", OptionType.INTEGER).withRequired(true);
1✔
280
    rootNodeIDSpec.addOption("identifier", OptionType.STRING).withRequired(true);
1✔
281
    rootNodeIDSpec.addOption("identifierType", OptionType.STRING).withRequired(true);
1✔
282

283
    Spec nodePathSpec = new Spec();
1✔
284
    nodePathSpec.addOption("path", OptionType.STRING);
1✔
285
    nodePathSpec
1✔
286
        .addOption("rootNodeID", OptionType.MAP)
1✔
287
        .withRequired(true)
1✔
288
        .withSpec(rootNodeIDSpec);
1✔
289

290
    spec.addOption("nodePaths", OptionType.LIST)
1✔
291
        .withElementType(OptionType.MAP)
1✔
292
        .withRequired(true)
1✔
293
        .withSpec(nodePathSpec);
1✔
294

295
    return spec;
1✔
296
  }
297

298
  @Override
299
  public void init(String yamcsInstance, String serviceName, YConfiguration config)
300
      throws ConfigurationException {
301
    super.init(yamcsInstance, serviceName, config);
1✔
302

303
    /* Local variables */
304
    this.config = config;
1✔
305
    /* Validate the configuration that the user passed us. */
306
    try {
307
      config = getSpec().validate(config);
1✔
308
    } catch (ValidationException e) {
×
309
      log.error("Failed configuration validation.", e);
×
310
      notifyFailed(e);
×
311
    }
1✔
312
    YarchDatabaseInstance ydb = YarchDatabase.getInstance(yamcsInstance);
1✔
313

314
    this.opcuaStreamName = config.getString("opcuaStream");
1✔
315
    this.opcuaStream = getStream(ydb, opcuaStreamName);
1✔
316
    this.parametersNamespace = config.getString("parametersNamespace");
1✔
317
    this.mdb = YamcsServer.getServer().getInstance(yamcsInstance).getXtceDb();
1✔
318

319
    readOPCUAConfig(config);
1✔
320
    readNodePathsConfig(config);
1✔
321

322
    outputFile = config.getString("xtceOutputFile");
1✔
323
  }
1✔
324

325
  private void readOPCUAConfig(YConfiguration config) {
326
    this.endpointURL = config.getString("endpointUrl");
1✔
327
    this.discoverURL = config.getString("discoveryUrl");
1✔
328
    this.publishInterval = config.getInt("publishInterval");
1✔
329
    this.queryAllNodesAtStartup = config.getBoolean("queryAllNodesAtStartup", false);
1✔
330
  }
1✔
331

332
  private void readNodePathsConfig(YConfiguration config) {
333
    List<Map<Object, Object>> nodePaths = config.getList("nodePaths");
1✔
334

335
    for (Map<Object, Object> path : nodePaths) {
1✔
336
      NodePath nodePath = new NodePath();
1✔
337
      nodePath.path = (String) path.get("path");
1✔
338
      nodePath.rootNodeID = (HashMap<Object, Object>) path.get("rootNodeID");
1✔
339
      relativeNodePaths.add(nodePath);
1✔
340
    }
1✔
341
  }
1✔
342

343
  private static SpaceSystem verifySpaceSystem(XtceDb mdb, String pathName) {
344
    String namespace;
345
    String name;
346
    int lastSlash = pathName.lastIndexOf('/');
1✔
347
    if ("/".equals(pathName)) {
1✔
348
      namespace = "";
×
349
      name = "";
×
350
    } else if (lastSlash == -1 || lastSlash == pathName.length() - 1) {
1✔
351
      namespace = "";
×
352
      name = pathName;
×
353
    } else {
354
      namespace = pathName.substring(0, lastSlash);
1✔
355
      name = pathName.substring(lastSlash + 1);
1✔
356
    }
357

358
    // First try with a prefixed slash (should be the common case)
359
    NamedObjectId id =
360
        NamedObjectId.newBuilder().setNamespace("/" + namespace).setName(name).build();
1✔
361
    SpaceSystem spaceSystem = mdb.getSpaceSystem(id);
1✔
362
    if (spaceSystem != null) {
1✔
363
      return spaceSystem;
×
364
    }
365

366
    // Maybe some non-xtce namespace like MDB:OPS Name
367
    id = NamedObjectId.newBuilder().setNamespace(namespace).setName(name).build();
1✔
368
    spaceSystem = mdb.getSpaceSystem(id);
1✔
369
    if (spaceSystem != null) {
1✔
370
      return spaceSystem;
1✔
371
    }
372

373
    throw new NotFoundException("No such space system");
×
374
  }
375

376
  /**
377
   * Initializes all PV mappings to OPCUA nodes and realtime subscriptions(managed data items in
378
   * OPCUA terms).
379
   */
380
  private void opcuaInit() {
381
    try {
382

383
      currentOPCUAStatus = OPCUAStatus.OPCUA_INIT_TREE;
1✔
384
      browseOPCUATree(client);
1✔
385
      currentOPCUAStatus = OPCUAStatus.OPCUA_INIT_GENERATE_XTCE;
1✔
386
      exportXTCE();
1✔
387
      currentOPCUAStatus = OPCUAStatus.OPCUA_INIT_EVENTS;
1✔
388
      subscribeToEvents(client);
1✔
389

390
    } catch (Exception e) {
×
391
      e.printStackTrace();
×
392
      return;
×
393
    }
1✔
394
    try {
395
      currentOPCUAStatus = OPCUAStatus.OPCUA_INIT_DATA_SUBSCRIPTION;
1✔
396
      createOPCUASubscriptions();
1✔
397
    } catch (Exception e) {
×
398
      e.printStackTrace();
×
399
    }
1✔
400
  }
1✔
401

402
  private void exportXTCE() throws IOException {
403
    var spaceSystem = verifySpaceSystem(mdb, parametersNamespace);
1✔
404
    var xtce = new XtceAssembler().toXtce(mdb, spaceSystem.getQualifiedName(), fqn -> true);
1✔
405
    BufferedWriter writer = null;
1✔
406

407
    if (outputFile != null) {
1✔
408
      writer =
1✔
409
          Files.newBufferedWriter(
1✔
410
              Paths.get(outputFile),
1✔
411
              StandardOpenOption.CREATE,
412
              StandardOpenOption.TRUNCATE_EXISTING);
413

414
      writer.write(xtce);
1✔
415

416
      writer.flush();
1✔
417
      writer.close();
1✔
418
    }
419
  }
1✔
420

421
  private void opcuaClientConnect() throws Exception {
422
    client = configureClient();
1✔
423
    connectToOPCUAServer(client);
1✔
424
  }
1✔
425

426
  private static Stream getStream(YarchDatabaseInstance ydb, String streamName) {
427
    Stream stream = ydb.getStream(streamName);
1✔
428
    if (stream == null) {
1✔
429
      try {
430
        ydb.execute("create stream " + streamName + gftdef.getStringDefinition());
1✔
431
      } catch (Exception e) {
×
432
        throw new ConfigurationException(e);
×
433
      }
1✔
434

435
      stream = ydb.getStream(streamName);
1✔
436
    }
437
    return stream;
1✔
438
  }
439

440
  @Override
441
  public void doDisable() {
442
    /* If the thread is created, interrupt it. */
443
    if (thread != null) {
1✔
444
      thread.interrupt();
1✔
445
    }
446

447
    linkStatus = Status.DISABLED;
1✔
448
  }
1✔
449

450
  @Override
451
  public void doEnable() {
452
    linkStatus = Status.OK;
1✔
453
  }
1✔
454

455
  @Override
456
  public String getDetailedStatus() {
457
    if (isDisabled()) {
1✔
458
      return String.format("DISABLED");
1✔
459
    } else {
460
      return String.format("OK, received %d packets", inCount.get());
1✔
461
    }
462
  }
463

464
  @Override
465
  public Status connectionStatus() {
466
    return linkStatus;
1✔
467
  }
468

469
  @Override
470
  protected void doStart() {
471
    try {
472
      opcuaClientConnect();
1✔
473
    } catch (Exception e) {
×
474
      e.printStackTrace();
×
475
      linkStatus = Status.FAILED;
×
476
      notifyFailed(e);
×
477
      return;
×
478
    }
1✔
479
    if (!isDisabled()) {
1✔
480
      doEnable();
1✔
481
    }
482
    startAction.addChangeListener(
1✔
483
        () -> {
484
          /**
485
           * TODO:Might be useful if we want turn off any functionality when the action is disabled
486
           * for instance..
487
           */
488
        });
×
489

490
    /* Create and start the new thread. */
491
    thread = new Thread(this);
1✔
492
    thread.setName(this.getClass().getSimpleName() + "-" + linkName);
1✔
493
    thread.start();
1✔
494

495
    notifyStarted();
1✔
496
  }
1✔
497

498
  @Override
499
  protected void doStop() {
500
    try {
501
      client.disconnect().get();
1✔
502
    } catch (InterruptedException | ExecutionException e) {
×
503
      // TODO Auto-generated catch block
504
      e.printStackTrace();
×
505
    }
1✔
506
    if (thread != null) {
1✔
507
      thread.interrupt();
1✔
508
    }
509

510
    notifyStopped();
1✔
511
  }
1✔
512

513
  @Override
514
  public void run() {
515
    opcuaInit();
1✔
516
    if (queryAllNodesAtStartup) {
1✔
517
      //            NOTE:I'm not sure if queryAllOPCUAData should block...
518
      currentOPCUAStatus = OPCUAStatus.OPCUA_INIT_ALL_DATA_QUERY;
×
519
      queryAllOPCUAData();
×
520
    }
521
    /* Enter our main loop */
522
    while (isRunningAndEnabled()) {
1✔
523
      currentOPCUAStatus = OPCUAStatus.OPCUA_OK;
1✔
524
    }
525
  }
1✔
526

527
  /**
528
   * Reads all attributes of all configured Value nodes and updates their corresponding PV. Useful
529
   * for querying data from the OPCUA server once, data such as browse names, NodeIds, etc.
530
   */
531
  private void queryAllOPCUAData() {
532
    TupleDefinition tdef = gftdef.copy();
1✔
533
    List<Object> cols = new ArrayList<>(4 + nodeIDToParamsMap.keySet().size());
1✔
534

535
    tdef = gftdef.copy();
1✔
536
    long gentime = timeService.getMissionTime();
1✔
537
    cols.add(gentime);
1✔
538
    cols.add(parametersNamespace);
1✔
539
    cols.add(0);
1✔
540
    cols.add(gentime);
1✔
541

542
    int columnCount = 0;
1✔
543

544
    Set<NodeId> nodeSet = new HashSet<NodeId>();
1✔
545
    /**
546
     * NOTE:This is super inefficient... The reason we collect these nodeIDs in a set is because
547
     * otherwise we will have redundant subscription(s) since there is more than 1 attribute per
548
     * nodeID given how nodeIDToParamsMap is designed
549
     */
550
    for (NodeIDAttrPair pair : nodeIDToParamsMap.keySet()) {
1✔
551
      nodeSet.add(pair.nodeID);
1✔
552
    }
1✔
553

554
    for (NodeId nId : nodeSet) {
1✔
555
      UaNode node;
556

557
      try {
558
        node = client.getAddressSpace().getNode(nId);
1✔
559

560
        DataValue nodeClass = node.readAttribute(AttributeId.NodeClass);
1✔
561

562
        switch (NodeClass.from((int) nodeClass.getValue().getValue())) {
1✔
563
          case Variable:
564
            for (AttributeId attr : AttributeId.VARIABLE_ATTRIBUTES) {
1✔
565
              VariableParam p = nodeIDToParamsMap.get(new NodeIDAttrPair(nId, attr));
1✔
566

567
              if (p.getParameterType() == null) {
1✔
568
                internalLogger.warn(
×
569
                    "{} ignored since it does not have a Parameter type",
570
                    p,
571
                    Character.toString(NameDescription.PATH_SEPARATOR));
×
572
                continue;
×
573
              }
574

575
              switch (p.getParameterType().getValueType()) {
1✔
576
                case BOOLEAN:
577
                  {
578
                    Boolean value = true;
1✔
579
                    if (node.readAttribute(attr).getValue().isNull()) {
1✔
580
                      //                      value = "NULL";
581
                    } else {
582
                      value = (Boolean) node.readAttribute(attr).getValue().getValue();
1✔
583
                    }
584

585
                    tdef.addColumn(p.getQualifiedName(), DataType.PARAMETER_VALUE);
1✔
586
                    cols.add(getPV(p, Instant.now().toEpochMilli(), value));
1✔
587
                  }
588
                  break;
1✔
589
                case DOUBLE:
590
                  {
591
                    Number value = 0;
1✔
592
                    if (node.readAttribute(attr).getValue().isNull()) {
1✔
593
                      internalLogger.warn("node {} has a Null variant.", node);
×
594
                    } else {
595
                      value = (Number) node.readAttribute(attr).getValue().getValue();
1✔
596
                    }
597

598
                    tdef.addColumn(p.getQualifiedName(), DataType.PARAMETER_VALUE);
1✔
599
                    cols.add(getPV(p, Instant.now().toEpochMilli(), value.doubleValue()));
1✔
600
                  }
601
                  break;
1✔
602
                case FLOAT:
603
                  {
604
                    Number value = 0;
1✔
605
                    if (node.readAttribute(attr).getValue().isNull()) {
1✔
606
                      internalLogger.warn("node {} has a Null variant.", node);
×
607
                    } else {
608
                      value = (Number) node.readAttribute(attr).getValue().getValue();
1✔
609
                    }
610

611
                    tdef.addColumn(p.getQualifiedName(), DataType.PARAMETER_VALUE);
1✔
612
                    cols.add(getPV(p, Instant.now().toEpochMilli(), value.floatValue()));
1✔
613
                  }
614
                  break;
1✔
615
                case SINT32:
616
                  {
617
                    Number value = 0;
1✔
618
                    if (node.readAttribute(attr).getValue().isNull()) {
1✔
619
                      internalLogger.warn("node {} has a Null variant.", node);
×
620
                    } else {
621
                      value = (Number) node.readAttribute(attr).getValue().getValue();
1✔
622
                    }
623

624
                    tdef.addColumn(p.getQualifiedName(), DataType.PARAMETER_VALUE);
1✔
625
                    cols.add(getPV(p, Instant.now().toEpochMilli(), value.intValue()));
1✔
626
                  }
627
                  break;
1✔
628
                case SINT64:
629
                  {
630
                    Number value = 0;
1✔
631
                    if (node.readAttribute(attr).getValue().isNull()) {
1✔
632
                      internalLogger.warn("node {} has a Null variant.", node);
×
633
                    } else {
634
                      value = (Number) node.readAttribute(attr).getValue().getValue();
1✔
635
                    }
636

637
                    tdef.addColumn(p.getQualifiedName(), DataType.PARAMETER_VALUE);
1✔
638
                    cols.add(getPV(p, Instant.now().toEpochMilli(), value.longValue()));
1✔
639
                  }
640
                  break;
1✔
641
                case STRING:
642
                  {
643
                    String value = "";
1✔
644
                    if (node.readAttribute(attr).getValue().isNull()) {
1✔
645
                      value = "NULL";
1✔
646
                    } else {
647
                      value = node.readAttribute(attr).getValue().getValue().toString();
1✔
648
                    }
649

650
                    tdef.addColumn(p.getQualifiedName(), DataType.PARAMETER_VALUE);
1✔
651
                    cols.add(getPV(p, Instant.now().toEpochMilli(), value));
1✔
652
                  }
653
                  break;
1✔
654
                case UINT32:
655
                  {
656
                    Number value = 0;
1✔
657
                    if (node.readAttribute(attr).getValue().isNull()) {
1✔
658
                      internalLogger.warn("node {} has a Null variant.", node);
×
659
                    } else {
660
                      value = (Number) node.readAttribute(attr).getValue().getValue();
1✔
661
                    }
662

663
                    tdef.addColumn(p.getQualifiedName(), DataType.PARAMETER_VALUE);
1✔
664
                    cols.add(getPV(p, Instant.now().toEpochMilli(), value.longValue()));
1✔
665
                  }
666
                  break;
1✔
667
                case UINT64:
668
                  {
669
                    Number value = 0;
1✔
670
                    if (node.readAttribute(attr).getValue().isNull()) {
1✔
671
                      internalLogger.warn("node {} has a Null variant.", node);
×
672
                    } else {
673
                      value = (Number) node.readAttribute(attr).getValue().getValue();
1✔
674
                    }
675

676
                    tdef.addColumn(p.getQualifiedName(), DataType.PARAMETER_VALUE);
1✔
677
                    cols.add(getPV(p, Instant.now().toEpochMilli(), value.longValue()));
1✔
678
                  }
679
                  break;
1✔
680
                default:
681
                  break;
682
              }
683

684
              log.debug("Pushing {} to stream", p.toString());
1✔
685

686
              columnCount++;
1✔
687
            }
1✔
688
            break;
1✔
689
          default:
690
            break;
691
        }
692

693
      } catch (UaException e) {
×
694
        // TODO Auto-generated catch block
695
        e.printStackTrace();
×
696
        continue;
×
697
      }
1✔
698
    }
1✔
699

700
    pushTuple(tdef, cols);
1✔
701

702
    inCount.getAndAdd(columnCount);
1✔
703
  }
1✔
704

705
  private synchronized void pushTuple(TupleDefinition tdef, List<Object> cols) {
706
    Tuple t;
707
    t = new Tuple(tdef, cols);
1✔
708
    opcuaStream.emitTuple(t);
1✔
709
  }
1✔
710

711
  private static ParameterType getOrCreateType(
712
      XtceDb mdb, String name, Supplier<ParameterType.Builder<?>> supplier) {
713

714
    String fqn = XtceDb.YAMCS_SPACESYSTEM_NAME + NameDescription.PATH_SEPARATOR + name;
1✔
715
    ParameterType ptype = mdb.getParameterType(fqn);
1✔
716
    if (ptype != null) {
1✔
717
      return ptype;
1✔
718
    }
719
    ParameterType.Builder<?> typeb = supplier.get().setName(name);
1✔
720

721
    ptype = typeb.build();
1✔
722
    ((NameDescription) ptype).setQualifiedName(fqn);
1✔
723

724
    return mdb.addSystemParameterType(ptype);
1✔
725
  }
726

727
  public static ParameterType getBasicType(XtceDb mdb, Type type) {
728
    ParameterType pType = null;
1✔
729
    switch (type) {
1✔
730
      case BOOLEAN:
731
        return getOrCreateType(mdb, "boolean", () -> new BooleanParameterType.Builder());
1✔
732
      case STRING:
733
        return getOrCreateType(mdb, "string", () -> new StringParameterType.Builder());
1✔
734

735
      case FLOAT:
736
        return getOrCreateType(
1✔
737
            mdb, "float32", () -> new FloatParameterType.Builder().setSizeInBits(32));
1✔
738
      case DOUBLE:
739
        return getOrCreateType(
1✔
740
            mdb, "float64", () -> new FloatParameterType.Builder().setSizeInBits(64));
1✔
741
      case SINT32:
742
        return getOrCreateType(
1✔
743
            mdb,
744
            "sint32",
745
            () -> new IntegerParameterType.Builder().setSizeInBits(32).setSigned(true));
×
746
      case SINT64:
747
        return getOrCreateType(
1✔
748
            mdb,
749
            "sint64",
750
            () -> new IntegerParameterType.Builder().setSizeInBits(64).setSigned(true));
1✔
751
      case UINT32:
752
        return getOrCreateType(
1✔
753
            mdb,
754
            "uint32",
755
            () -> new IntegerParameterType.Builder().setSizeInBits(32).setSigned(false));
×
756
      case UINT64:
757
        return getOrCreateType(
1✔
758
            mdb,
759
            "uint64",
760
            () -> new IntegerParameterType.Builder().setSizeInBits(64).setSigned(false));
×
761
      default:
762
        break;
763
    }
764

765
    return pType;
×
766
  }
767

768
  public static ParameterValue getNewPv(Parameter parameter, long time) {
769
    ParameterValue pv = new ParameterValue(parameter);
1✔
770
    pv.setAcquisitionTime(time);
1✔
771
    pv.setGenerationTime(time);
1✔
772
    return pv;
1✔
773
  }
774

775
  public static ParameterValue getPV(Parameter parameter, long time, String v) {
776
    ParameterValue pv = getNewPv(parameter, time);
1✔
777
    pv.setEngValue(ValueUtility.getStringValue(v));
1✔
778
    return pv;
1✔
779
  }
780

781
  public static ParameterValue getPV(Parameter parameter, long time, double v) {
782
    ParameterValue pv = getNewPv(parameter, time);
1✔
783
    pv.setEngValue(ValueUtility.getDoubleValue(v));
1✔
784
    return pv;
1✔
785
  }
786

787
  public static ParameterValue getPV(Parameter parameter, long time, float v) {
788
    ParameterValue pv = getNewPv(parameter, time);
1✔
789
    pv.setEngValue(ValueUtility.getFloatValue(v));
1✔
790
    return pv;
1✔
791
  }
792

793
  public static ParameterValue getPV(Parameter parameter, long time, boolean v) {
794
    ParameterValue pv = getNewPv(parameter, time);
1✔
795
    pv.setEngValue(ValueUtility.getBooleanValue(v));
1✔
796
    return pv;
1✔
797
  }
798

799
  public static ParameterValue getPV(Parameter parameter, long time, long v) {
800
    ParameterValue pv = getNewPv(parameter, time);
1✔
801
    pv.setEngValue(ValueUtility.getSint64Value(v));
1✔
802
    return pv;
1✔
803
  }
804

805
  @Override
806
  public Status getLinkStatus() {
807
    return linkStatus;
1✔
808
  }
809

810
  @Override
811
  public boolean isDisabled() {
812
    return linkStatus == Status.DISABLED;
1✔
813
  }
814

815
  @Override
816
  public long getDataInCount() {
817
    return inCount.get();
1✔
818
  }
819

820
  @Override
821
  public long getDataOutCount() {
822
    return 0;
1✔
823
  }
824

825
  @Override
826
  public void resetCounters() {
827
    inCount.set(0);
1✔
828
  }
1✔
829

830
  /**
831
   * Selects first non-secured endpoint from endpoints found at discover URL. At the moment secured
832
   * endpoints are not supported.
833
   *
834
   * @return
835
   * @throws Exception
836
   */
837
  private OpcUaClient configureClient() throws Exception {
838

839
    List<EndpointDescription> endpoints = DiscoveryClient.getEndpoints(discoverURL).get();
1✔
840

841
    // At the moment, we do not support certificates.
842
    EndpointDescription selectedEndpoint = null;
1✔
843
    for (var endpoint : endpoints) {
1✔
844
      switch (endpoint.getSecurityMode()) {
1✔
845
        case Invalid:
846
          internalLogger.warn("Endpoint mode {} is not supported.", endpoint.getSecurityMode());
×
847
          break;
×
848
        case None:
849
          selectedEndpoint = endpoint;
1✔
850
          break;
1✔
851
        case Sign:
852
          internalLogger.warn("Endpoint mode {} is not supported.", endpoint.getSecurityMode());
×
853
          break;
×
854
        case SignAndEncrypt:
855
          internalLogger.warn("Endpoint mode {} is not supported.", endpoint.getSecurityMode());
×
856
          break;
857
      }
858

859
      if (selectedEndpoint != null) {
1✔
860
        break;
1✔
861
      }
862
    }
×
863

864
    if (selectedEndpoint == null) {
1✔
865
      throw new Exception("No viable endpoint found from list:" + endpoints);
×
866
    }
867

868
    OpcUaClientConfig builder = OpcUaClientConfig.builder().setEndpoint(selectedEndpoint).build();
1✔
869

870
    return OpcUaClient.create(builder);
1✔
871
  }
872

873
  /**
874
   * Adds new PV with the name of node.
875
   *
876
   * @param client
877
   * @param node
878
   */
879
  private void addOPCUAPV(OpcUaClient client, UaNode node) {
880

881
    if (node.getBrowseName()
1✔
882
        .getName()
1✔
883
        .contains(Character.toString(NameDescription.PATH_SEPARATOR))) {
1✔
884
      internalLogger.info(
×
885
          "{} ignored since it contains a {} character",
886
          node.getBrowseName().getName(),
×
887
          Character.toString(NameDescription.PATH_SEPARATOR));
×
888

889
    } else {
890

891
      /**
892
       * NOTE:For now we'll just flatten all the attributes instead of using an aggregate type for
893
       * attributes
894
       */
895
      for (AttributeId attr : AttributeId.values()) {
1✔
896

897
        ParameterType ptype = OPCUAAttrTypeToParamType(attr, node);
1✔
898

899
        String opcuaTranslatedQName = translateNodeToParamQName(client, node, attr);
1✔
900
        Parameter p = VariableParam.getForFullyQualifiedName(opcuaTranslatedQName);
1✔
901

902
        p.setParameterType(ptype);
1✔
903

904
        if (mdb.getParameter(p.getQualifiedName()) == null) {
1✔
905
          log.debug("Adding OPCUA object as parameter to mdb:{}", p.getQualifiedName());
1✔
906
          mdb.addParameter(p, true);
1✔
907

908
          nodeIDToParamsMap.put(new NodeIDAttrPair(node.getNodeId(), attr), (VariableParam) p);
1✔
909
        }
910
      }
911
    }
912
  }
1✔
913

914
  /**
915
   * Map nodeID name to a qualified name that can be used for a YAMCS PV.
916
   *
917
   * @param client
918
   * @param node
919
   * @param attr
920
   * @return
921
   */
922
  private String translateNodeToParamQName(OpcUaClient client, UaNode node, AttributeId attr) {
923
    LocalizedText localizedDisplayName = null;
1✔
924
    try {
925

926
      localizedDisplayName =
1✔
927
          (LocalizedText) (node.readAttribute(AttributeId.DisplayName).getValue().getValue());
1✔
928
    } catch (UaException e) {
×
929
      // TODO Auto-generated catch block
930
      e.printStackTrace();
×
931
    }
1✔
932
    String opcuaTranslatedQName =
1✔
933
        qualifiedName(
1✔
934
            parametersNamespace
935
                + NameDescription.PATH_SEPARATOR
936
                + node.getNodeId().toParseableString().replace(";", "-")
1✔
937
                + NameDescription.PATH_SEPARATOR
938
                + localizedDisplayName.getText(),
1✔
939
            attr.toString());
1✔
940

941
    return opcuaTranslatedQName;
1✔
942
  }
943

944
  /**
945
   * Browse node at nodePath relative to browseRoot.
946
   *
947
   * @param indent
948
   * @param client
949
   * @param browseRoot
950
   * @param nodePath in the format of "0:Root,0:Objects,2:HelloWorld,2:MyObject,2:Bar"
951
   * @throws Exception
952
   */
953
  private void browsePath(String indent, OpcUaClient client, NodeId startingNode, String nodePath)
954
      throws Exception {
955
    internalLogger.info("Browsing at " + startingNode);
1✔
956
    ArrayList<String> rPathTokens = new ArrayList<String>();
1✔
957
    ArrayList<RelativePathElement> relaitivePathElements = new ArrayList<RelativePathElement>();
1✔
958

959
    for (var pathToken : nodePath.split(",")) {
1✔
960
      rPathTokens.add(nodePath);
1✔
961

962
      int namespaceIndex = 0;
1✔
963

964
      String namespaceName = "";
1✔
965

966
      namespaceIndex = Integer.parseInt(pathToken.split(":")[0]);
1✔
967

968
      namespaceName = pathToken.split(":")[1];
1✔
969

970
      relaitivePathElements.add(
1✔
971
          new RelativePathElement(
972
              Identifiers.HierarchicalReferences,
973
              false,
1✔
974
              true,
1✔
975
              new QualifiedName(namespaceIndex, namespaceName)));
976
    }
977

978
    ArrayList<BrowsePath> list = new ArrayList<BrowsePath>();
1✔
979

980
    RelativePathElement[] elements = new RelativePathElement[relaitivePathElements.size()];
1✔
981

982
    relaitivePathElements.toArray(elements);
1✔
983

984
    list.add(new BrowsePath(startingNode, new RelativePath(elements)));
1✔
985

986
    TranslateBrowsePathsToNodeIdsResponse response = null;
1✔
987
    try {
988
      response = client.translateBrowsePaths(list).get();
1✔
989
    } catch (InterruptedException e) {
×
990
      // TODO Auto-generated catch block
991
      e.printStackTrace();
×
992
    } catch (ExecutionException e) {
×
993
      // TODO Auto-generated catch block
994
      e.printStackTrace();
×
995
    }
1✔
996

997
    BrowsePathResult result = Arrays.asList(response.getResults()).get(0);
1✔
998
    StatusCode statusCode = result.getStatusCode();
1✔
999

1000
    if (statusCode.isBad()) {
1✔
1001
      log.warn("Bad status code:" + statusCode);
×
1002
      //      throw new Exception("Bad status code:" + statusCode);
1003
      //      FIXME:send error yamcs event.
1004
      org.yamcs.yarch.protobuf.Db.Event ev =
1005
          Event.newBuilder()
×
1006
              .setGenerationTime(YamcsServer.getTimeService(yamcsInstance).getMissionTime())
×
1007
              .setGenerationTime(YamcsServer.getTimeService(yamcsInstance).getMissionTime())
×
1008
              .setSource(this.linkName)
×
1009
              .setType(this.linkName)
×
1010
              .setMessage("Failed to find node:" + nodePath + ". Error code info:" + statusCode)
×
1011
              .setSeverity(EventSeverity.ERROR)
×
1012
              .build();
×
1013
      eventProducer.sendEvent(ev);
×
1014

1015
      throw new Exception("Bad status code:" + statusCode);
×
1016

1017
    } else if (statusCode.isUncertain()) {
1✔
1018
      log.warn("Uncertain status code:" + statusCode);
×
1019
      return;
×
1020
    }
1021

1022
    try {
1023
      UaNode node =
1✔
1024
          client
1025
              .getAddressSpace()
1✔
1026
              .getNode(
1✔
1027
                  result.getTargets()[0].getTargetId().toNodeId(client.getNamespaceTable()).get());
1✔
1028

1029
      addOPCUAPV(client, node);
1✔
1030
    } catch (UaException e) {
×
1031
      // TODO Auto-generated catch block
1032
      e.printStackTrace();
×
1033
    }
1✔
1034
  }
1✔
1035

1036
  private void createOPCUASubscriptions() {
1037
    createDataChangeListener();
1✔
1038
    Set<NodeId> nodeSet = new HashSet<NodeId>();
1✔
1039
    /**
1040
     * FIXME:This is super inefficient... The reason we collect these nodeIDs in a set is because
1041
     * otherwise we will have redundant subscription(s) since there is more than 1 attribute per
1042
     * nodeID given how nodeIDToParamsMap is designed
1043
     */
1044
    for (NodeIDAttrPair pair : nodeIDToParamsMap.keySet()) {
1✔
1045
      nodeSet.add(pair.nodeID);
1✔
1046
    }
1✔
1047

1048
    ArrayList<NodeId> variableNodes = new ArrayList<NodeId>();
1✔
1049
    for (NodeId id : nodeSet) {
1✔
1050
      Variant nodeClass = null;
1✔
1051
      try {
1052
        UaNode node = client.getAddressSpace().getNode(id);
1✔
1053

1054
        nodeClass = node.readAttribute(AttributeId.NodeClass).getValue();
1✔
1055

1056
      } catch (UaException e) {
×
1057
        // TODO Auto-generated catch block
1058
        e.printStackTrace();
×
1059
      }
1✔
1060
      if (nodeClass != null) {
1✔
1061
        //        try {
1062
        switch (NodeClass.from((int) nodeClass.getValue())) {
1✔
1063
            // As per the spec, the only thing we can subscribe to is Variables
1064
          case Variable:
1065
            variableNodes.add(id);
1✔
1066
            break;
1067
        }
1068
      }
1069
    }
1✔
1070

1071
    try {
1072
      List<ManagedDataItem> dataItems = opcuaSubscription.createDataItems(variableNodes);
1✔
1073
      for (var dataItem : dataItems) {
1✔
1074
        log.debug("Status code for dataItem:{}", dataItem.getStatusCode());
1✔
1075
        OPCUAActiveSubs.addAndGet(1);
1✔
1076
      }
1✔
1077
    } catch (UaException e) {
×
1078
      // TODO Auto-generated catch block
1079
      e.printStackTrace();
×
1080
    }
1✔
1081
  }
1✔
1082

1083
  public void connectToOPCUAServer(OpcUaClient client) throws Exception {
1084
    internalLogger.info("Connecting to OPCUA server...");
1✔
1085
    client.connect().get();
1✔
1086

1087
    addAction(startAction);
1✔
1088
    startAction.setEnabled(true);
1✔
1089
  }
1✔
1090

1091
  /**
1092
   * Browses the tree on the OPCUA server and maps them to YAMCS Parameters.
1093
   *
1094
   * @param client
1095
   * @throws Exception
1096
   */
1097
  private void browseOPCUATree(OpcUaClient client) throws Exception {
1098
    // start browsing at root folder
1099
    internalLogger.info("Browsing OPCUA...");
1✔
1100
    for (var p : relativeNodePaths) {
1✔
1101
      int namespaceIndex = (int) p.rootNodeID.get("namespaceIndex");
1✔
1102
      String identifier = (String) p.rootNodeID.get("identifier");
1✔
1103
      IdType identifierType = IdType.valueOf((String) p.rootNodeID.get("identifierType"));
1✔
1104

1105
      browsePath(
1✔
1106
          endpointURL, client, getNewNodeID(identifierType, namespaceIndex, identifier), p.path);
1✔
1107
    }
1✔
1108
  }
1✔
1109

1110
  /**
1111
   * Get new OPCUA-compliant NodeID object that is created from NamespaceIndex and Identifier. At
1112
   * the moment only String and Numeric node ids are supported.
1113
   *
1114
   * @param rootIdentifierType
1115
   * @param NamespaceIndex
1116
   * @param Identifier
1117
   * @return
1118
   */
1119
  private NodeId getNewNodeID(IdType rootIdentifierType, int NamespaceIndex, String Identifier) {
1120
    NodeId nodeID = null;
1✔
1121
    switch (rootIdentifierType) {
1✔
1122
      case Guid:
1123
        //                FIXME
1124
        break;
×
1125
      case Numeric:
1126
        nodeID = new NodeId(NamespaceIndex, Integer.parseInt(Identifier));
1✔
1127
        break;
1✔
1128
      case Opaque:
1129
        //                FIXME
1130
        break;
×
1131
      case String:
1132
        nodeID = new NodeId(NamespaceIndex, Identifier);
×
1133
        break;
×
1134
      default:
1135
        break;
1136
    }
1137
    return nodeID;
1✔
1138
  }
1139

1140
  /** Data listener for realtime OPCUA server updates. */
1141
  private void createDataChangeListener() {
1142
    try {
1143
      opcuaSubscription = ManagedSubscription.create(client, publishInterval);
1✔
1144
    } catch (UaException e) {
×
1145
      // TODO Auto-generated catch block
1146
      e.printStackTrace();
×
1147
    }
1✔
1148
    opcuaSubscription.addDataChangeListener(
1✔
1149
        (items, values) -> {
1150
          for (int i = 0; i < items.size(); i++) {
1✔
1151
            NodeIDAttrPair nodeAttrKey =
1✔
1152
                new NodeIDAttrPair(items.get(i).getNodeId(), AttributeId.Value);
1✔
1153
            log.debug(
1✔
1154
                "subscription value received: item={}, value={}",
1155
                items.get(i).getNodeId(),
1✔
1156
                values.get(i).getValue());
1✔
1157

1158
            log.debug(
1✔
1159
                "Pushing new PV for param name {} which is mapped to NodeID {}",
1160
                nodeIDToParamsMap.get(nodeAttrKey),
1✔
1161
                items.get(i).getNodeId());
1✔
1162

1163
            TupleDefinition tdef = gftdef.copy();
1✔
1164
            List<Object> cols = new ArrayList<>(4 + 1);
1✔
1165
            //            FIXME: Add leap seconds.... as config or get it from YAMCS API.
1166
            long gentime =
1✔
1167
                values
1168
                    .get(i)
1✔
1169
                    .getSourceTime()
1✔
1170
                    .getJavaInstant()
1✔
1171
                    .plus(37, ChronoUnit.SECONDS)
1✔
1172
                    .toEpochMilli();
1✔
1173
            cols.add(gentime);
1✔
1174
            cols.add(parametersNamespace);
1✔
1175
            cols.add(0);
1✔
1176
            cols.add(gentime);
1✔
1177

1178
            /**
1179
             * TODO:Not sure if this is the best way to do this since the aggregate values will be
1180
             * partially updated. Another potential approach might be to decouple the live OPCUA
1181
             * data(subscriptions) via namespaces. For example; have a "special" namespace called
1182
             * "subscriptions" that ONLY gets updated with items. And maybe another namespace for
1183
             * static data...maybe.
1184
             *
1185
             * <p>Another option is to flatten everything and have no aggregate types at all. That
1186
             * approach might even simplify the code quite a bit...
1187
             *
1188
             * <p>Another question worth answering before moving forward is to find out whether or
1189
             * not it is concrete in the OPCUA protocol what data can change in real time and which
1190
             * data is "static". Not sure if there is any "static" data given that clients have the
1191
             * ability of writing to values... might be worth a test.
1192
             */
1193
            log.debug(
1✔
1194
                "Data({}) chnage triggered for {}",
1195
                values.get(i).getValue(),
1✔
1196
                nodeIDToParamsMap.get(nodeAttrKey));
1✔
1197

1198
            if (nodeIDToParamsMap.get(nodeAttrKey) == null) {
1✔
1199
              log.debug("No parameter mapping found for {}", nodeAttrKey.nodeID);
×
1200
              continue;
×
1201
            } else {
1202
              log.debug(
1✔
1203
                  String.format(
1✔
1204
                      "parameter mapping found for {} and {}",
1205
                      nodeAttrKey.nodeID,
1206
                      nodeAttrKey.attrID));
1207
            }
1208

1209
            if (values.get(i).getValue() != null && values.get(i).getValue().getValue() != null) {
1✔
1210

1211
              switch (nodeIDToParamsMap.get(nodeAttrKey).getParameterType().getValueType()) {
1✔
1212
                case BOOLEAN:
1213
                  {
1214
                    boolean value = (boolean) values.get(i).getValue().getValue();
1✔
1215

1216
                    tdef.addColumn(
1✔
1217
                        nodeIDToParamsMap.get(nodeAttrKey).getQualifiedName(),
1✔
1218
                        DataType.PARAMETER_VALUE);
1219
                    cols.add(getPV(nodeIDToParamsMap.get(nodeAttrKey), gentime, value));
1✔
1220
                  }
1221
                  break;
1✔
1222
                case DOUBLE:
1223
                  {
1224
                    Number value = (Number) values.get(i).getValue().getValue();
1✔
1225

1226
                    tdef.addColumn(
1✔
1227
                        nodeIDToParamsMap.get(nodeAttrKey).getQualifiedName(),
1✔
1228
                        DataType.PARAMETER_VALUE);
1229
                    cols.add(
1✔
1230
                        getPV(nodeIDToParamsMap.get(nodeAttrKey), gentime, value.doubleValue()));
1✔
1231
                  }
1232
                  break;
1✔
1233
                case FLOAT:
1234
                  {
1235
                    Number value = (Number) values.get(i).getValue().getValue();
1✔
1236

1237
                    tdef.addColumn(
1✔
1238
                        nodeIDToParamsMap.get(nodeAttrKey).getQualifiedName(),
1✔
1239
                        DataType.PARAMETER_VALUE);
1240
                    cols.add(
1✔
1241
                        getPV(nodeIDToParamsMap.get(nodeAttrKey), gentime, value.floatValue()));
1✔
1242
                  }
1243
                  break;
1✔
1244
                case SINT32:
1245
                  {
1246
                    Number value = (Number) values.get(i).getValue().getValue();
1✔
1247
                    tdef.addColumn(
1✔
1248
                        nodeIDToParamsMap.get(nodeAttrKey).getQualifiedName(),
1✔
1249
                        DataType.PARAMETER_VALUE);
1250
                    cols.add(getPV(nodeIDToParamsMap.get(nodeAttrKey), gentime, value.intValue()));
1✔
1251
                  }
1252
                  break;
1✔
1253
                case SINT64:
1254
                  {
1255
                    Number value = (Number) values.get(i).getValue().getValue();
1✔
1256

1257
                    tdef.addColumn(
1✔
1258
                        nodeIDToParamsMap.get(nodeAttrKey).getQualifiedName(),
1✔
1259
                        DataType.PARAMETER_VALUE);
1260
                    cols.add(getPV(nodeIDToParamsMap.get(nodeAttrKey), gentime, value.longValue()));
1✔
1261
                  }
1262
                  break;
1✔
1263
                case STRING:
1264
                  {
1265
                    String value = (String) values.get(i).getValue().getValue().toString();
1✔
1266

1267
                    tdef.addColumn(
1✔
1268
                        nodeIDToParamsMap.get(nodeAttrKey).getQualifiedName(),
1✔
1269
                        DataType.PARAMETER_VALUE);
1270
                    cols.add(getPV(nodeIDToParamsMap.get(nodeAttrKey), gentime, value));
1✔
1271
                  }
1272
                  break;
1✔
1273
                case UINT32:
1274
                  {
1275
                    Number value = (Number) values.get(i).getValue().getValue();
1✔
1276
                    tdef.addColumn(
1✔
1277
                        nodeIDToParamsMap.get(nodeAttrKey).getQualifiedName(),
1✔
1278
                        DataType.PARAMETER_VALUE);
1279
                    cols.add(getPV(nodeIDToParamsMap.get(nodeAttrKey), gentime, value.longValue()));
1✔
1280
                  }
1281
                  break;
1✔
1282
                case UINT64:
1283
                  {
1284
                    Number value = (Number) values.get(i).getValue().getValue();
1✔
1285

1286
                    tdef.addColumn(
1✔
1287
                        nodeIDToParamsMap.get(nodeAttrKey).getQualifiedName(),
1✔
1288
                        DataType.PARAMETER_VALUE);
1289
                    cols.add(getPV(nodeIDToParamsMap.get(nodeAttrKey), gentime, value.longValue()));
1✔
1290
                  }
1291
                  break;
1✔
1292
                default:
1293
                  break;
1294
              }
1295

1296
              pushTuple(tdef, cols);
1✔
1297

1298
              inCount.getAndAdd(1);
1✔
1299
            } else {
1300
              // TODO:Add some type emptyValue count for OPS.
1301
              log.warn(
×
1302
                  "Data chnage triggered for {}, but it empty. This should not happen.",
1303
                  nodeIDToParamsMap.get(nodeAttrKey).getQualifiedName());
×
1304
            }
1305
          }
1306
        });
1✔
1307
  }
1✔
1308

1309
  /**
1310
   * Get new ParameterType for the specified attribute of the node. Particularly useful for Value
1311
   * attributes of nodes.
1312
   *
1313
   * @param attr
1314
   * @param node
1315
   * @return
1316
   */
1317
  private ParameterType OPCUAAttrTypeToParamType(AttributeId attr, UaNode node) {
1318
    ParameterType pType = null;
1✔
1319

1320
    switch (attr) {
1✔
1321
      case AccessLevel:
1322
        pType = getBasicType(mdb, Type.STRING);
1✔
1323
        break;
1✔
1324
      case ArrayDimensions:
1325
        pType = getBasicType(mdb, Type.STRING);
1✔
1326
        break;
1✔
1327
      case BrowseName:
1328
        pType = getBasicType(mdb, Type.STRING);
1✔
1329
        break;
1✔
1330
      case ContainsNoLoops:
1331
        pType = getBasicType(mdb, Type.STRING);
1✔
1332
        break;
1✔
1333
      case DataType:
1334
        pType = getBasicType(mdb, Type.STRING);
1✔
1335
        break;
1✔
1336
      case Description:
1337
        pType = getBasicType(mdb, Type.STRING);
1✔
1338
        break;
1✔
1339
      case DisplayName:
1340
        pType = getBasicType(mdb, Type.STRING);
1✔
1341
        break;
1✔
1342
      case EventNotifier:
1343
        pType = getBasicType(mdb, Type.STRING);
1✔
1344
        break;
1✔
1345
      case Executable:
1346
        pType = getBasicType(mdb, Type.STRING);
1✔
1347
        break;
1✔
1348
      case Historizing:
1349
        pType = getBasicType(mdb, Type.STRING);
1✔
1350
        break;
1✔
1351
      case InverseName:
1352
        pType = getBasicType(mdb, Type.STRING);
1✔
1353
        break;
1✔
1354
      case IsAbstract:
1355
        pType = getBasicType(mdb, Type.STRING);
1✔
1356
        break;
1✔
1357
      case MinimumSamplingInterval:
1358
        pType = getBasicType(mdb, Type.STRING);
1✔
1359
        break;
1✔
1360
      case NodeClass:
1361
        pType = getBasicType(mdb, Type.STRING);
1✔
1362
        break;
1✔
1363
      case NodeId:
1364
        pType = getBasicType(mdb, Type.STRING);
1✔
1365
        break;
1✔
1366
      case Symmetric:
1367
        pType = getBasicType(mdb, Type.STRING);
1✔
1368
        break;
1✔
1369
      case UserAccessLevel:
1370
        pType = getBasicType(mdb, Type.STRING);
1✔
1371
        break;
1✔
1372
      case UserExecutable:
1373
        pType = getBasicType(mdb, Type.STRING);
1✔
1374
        break;
1✔
1375
      case UserWriteMask:
1376
        pType = getBasicType(mdb, Type.STRING);
1✔
1377
        break;
1✔
1378
      case Value:
1379
        try {
1380

1381
          var value = node.readAttribute(attr).getValue();
1✔
1382

1383
          if (value.isNotNull()) {
1✔
1384
            NodeId valueObjectType =
1✔
1385
                value.getDataType().get().toNodeId(client.getNamespaceTable()).get();
1✔
1386

1387
            /** As per the spec:https://reference.opcfoundation.org/Core/Part6/v104/docs/5.1.2 */
1388
            if (valueObjectType.equals(Identifiers.SByte)) {
1✔
1389
              pType = getBasicType(mdb, Type.SINT32);
×
1390
            } else if (valueObjectType.equals(Identifiers.Byte)) {
1✔
1391
              pType = getBasicType(mdb, Type.SINT32);
×
1392
            } else if (valueObjectType.equals(Identifiers.Int16)) {
1✔
1393
              pType = getBasicType(mdb, Type.SINT32);
×
1394
            } else if (valueObjectType.equals(Identifiers.UInt16)) {
1✔
1395
              pType = getBasicType(mdb, Type.SINT32);
×
1396
            } else if (valueObjectType.equals(Identifiers.Int32)) {
1✔
1397
              pType = getBasicType(mdb, Type.SINT32);
1✔
1398
            } else if (valueObjectType.equals(Identifiers.UInt32)) {
1✔
1399
              pType = getBasicType(mdb, Type.UINT32);
1✔
1400
            } else if (valueObjectType.equals(Identifiers.Int64)) {
1✔
1401
              pType = getBasicType(mdb, Type.SINT64);
1✔
1402
            } else if (valueObjectType.equals(Identifiers.UInt64)) {
1✔
1403
              pType = getBasicType(mdb, Type.UINT64);
1✔
1404
            } else if (valueObjectType.equals(Identifiers.Float)) {
1✔
1405
              pType = getBasicType(mdb, Type.FLOAT);
1✔
1406
            } else if (valueObjectType.equals(Identifiers.Double)) {
1✔
1407
              pType = getBasicType(mdb, Type.DOUBLE);
1✔
1408
            } else if (valueObjectType.equals(Identifiers.String)) {
1✔
1409
              pType = getBasicType(mdb, Type.STRING);
1✔
1410
            } else if (valueObjectType.equals(Identifiers.Boolean)) {
1✔
1411
              pType = getBasicType(mdb, Type.BOOLEAN);
1✔
1412
            }
1413
          } else {
1✔
1414
            pType = getBasicType(mdb, Type.STRING);
×
1415
          }
1416

1417
        } catch (UaException e) {
×
1418
          e.printStackTrace();
×
1419
          //                        FIXME:Add log message
1420
        }
1✔
1421
        break;
×
1422
      case ValueRank:
1423
        pType = getBasicType(mdb, Type.STRING);
1✔
1424
        break;
1✔
1425
      case WriteMask:
1426
        pType = getBasicType(mdb, Type.STRING);
1✔
1427
        break;
1✔
1428
      default:
1429
        break;
1430
    }
1431

1432
    return pType;
1✔
1433
  }
1434

1435
  private void subscribeToEvents(OpcUaClient client)
1436
      throws InterruptedException, ExecutionException {
1437
    // create a subscription and a monitored item
1438
    UaSubscription subscription = client.getSubscriptionManager().createSubscription(1000.0).get();
1✔
1439

1440
    ReadValueId readValueId =
1✔
1441
        new ReadValueId(
1442
            Identifiers.Server, AttributeId.EventNotifier.uid(), null, QualifiedName.NULL_VALUE);
1✔
1443

1444
    // client handle must be unique per item
1445
    UInteger clientHandle = uint(clientHandles.getAndIncrement());
1✔
1446

1447
    EventFilter eventFilter =
1✔
1448
        new EventFilter(
1449
            new SimpleAttributeOperand[] {
1450
              new SimpleAttributeOperand(
1451
                  Identifiers.BaseEventType,
1452
                  new QualifiedName[] {new QualifiedName(0, "EventId")},
1453
                  AttributeId.Value.uid(),
1✔
1454
                  null),
1455
              new SimpleAttributeOperand(
1456
                  Identifiers.BaseEventType,
1457
                  new QualifiedName[] {new QualifiedName(0, "EventType")},
1458
                  AttributeId.Value.uid(),
1✔
1459
                  null),
1460
              new SimpleAttributeOperand(
1461
                  Identifiers.BaseEventType,
1462
                  new QualifiedName[] {new QualifiedName(0, "Severity")},
1463
                  AttributeId.Value.uid(),
1✔
1464
                  null),
1465
              new SimpleAttributeOperand(
1466
                  Identifiers.BaseEventType,
1467
                  new QualifiedName[] {new QualifiedName(0, "Time")},
1468
                  AttributeId.Value.uid(),
1✔
1469
                  null),
1470
              new SimpleAttributeOperand(
1471
                  Identifiers.BaseEventType,
1472
                  new QualifiedName[] {new QualifiedName(0, "Message")},
1473
                  AttributeId.Value.uid(),
1✔
1474
                  null)
1475
            },
1476
            new ContentFilter(null));
1477

1478
    MonitoringParameters parameters =
1✔
1479
        new MonitoringParameters(
1480
            clientHandle,
1481
            0.0,
1✔
1482
            ExtensionObject.encode(client.getStaticSerializationContext(), eventFilter),
1✔
1483
            uint(10),
1✔
1484
            true);
1✔
1485

1486
    MonitoredItemCreateRequest request =
1✔
1487
        new MonitoredItemCreateRequest(readValueId, MonitoringMode.Reporting, parameters);
1488

1489
    List<UaMonitoredItem> items =
1✔
1490
        subscription.createMonitoredItems(TimestampsToReturn.Both, newArrayList(request)).get();
1✔
1491

1492
    // do something with the value updates
1493
    UaMonitoredItem monitoredItem = items.get(0);
1✔
1494

1495
    monitoredItem.setEventConsumer(
1✔
1496
        (item, vs) -> {
1497
          internalLogger.info("Event Received from {}", item.getReadValueId().getNodeId());
1✔
1498

1499
          StringBuilder eventText = new StringBuilder();
1✔
1500

1501
          ByteString eventId;
1502
          NodeId eventType;
1503
          UShort eventSeverity;
1504
          DateTime eventTime;
1505
          LocalizedText eventMessage;
1506

1507
          for (int i = 0; i < vs.length; i++) {
1✔
1508
            internalLogger.info("\tvariant[{}]: {}", i, vs[i].getValue());
1✔
1509
          }
1510

1511
          eventId = (ByteString) vs[0].getValue();
1✔
1512
          eventType = (NodeId) vs[1].getValue();
1✔
1513
          eventSeverity = (UShort) vs[2].getValue();
1✔
1514
          eventTime = (DateTime) vs[3].getValue();
1✔
1515
          eventMessage = (LocalizedText) vs[4].getValue();
1✔
1516

1517
          //          FIXME:Map these values to YAMCS API
1518
          eventText.append("eventId:" + eventId);
1✔
1519
          eventText.append("\n");
1✔
1520
          eventText.append("eventType:" + eventType);
1✔
1521
          eventText.append("\n");
1✔
1522
          eventText.append("eventSeverity:" + eventSeverity);
1✔
1523
          eventText.append("\n");
1✔
1524
          eventText.append("eventTime:" + eventTime);
1✔
1525
          eventText.append("\n");
1✔
1526
          eventText.append("eventMessage:" + eventMessage);
1✔
1527
          org.yamcs.yarch.protobuf.Db.Event ev =
1528
              Event.newBuilder()
1✔
1529
                  .setGenerationTime(YamcsServer.getTimeService(yamcsInstance).getMissionTime())
1✔
1530
                  .setGenerationTime(YamcsServer.getTimeService(yamcsInstance).getMissionTime())
1✔
1531
                  .setSource(this.linkName)
1✔
1532
                  .setType(this.linkName)
1✔
1533
                  .setMessage(eventText.toString())
1✔
1534
                  .setSeverity(EventSeverity.INFO)
1✔
1535
                  .build();
1✔
1536
          eventProducer.sendEvent(ev);
1✔
1537
        });
1✔
1538
  }
1✔
1539

1540
  @Override
1541
  public void setupSystemParameters(SystemParametersService sysParamService) {
1542
    super.setupSystemParameters(sysParamService);
1✔
1543
    OPCUAStatusParam =
1✔
1544
        sysParamService.createEnumeratedSystemParameter(
1✔
1545
            linkName + "/OPCUAStatusParam",
1546
            OPCUAStatus.class,
1547
            "The current status of OPCUA client");
1548
    EnumeratedParameterType spLinkStatusType =
1✔
1549
        (EnumeratedParameterType) OPCUAStatusParam.getParameterType();
1✔
1550
    spLinkStatusType
1✔
1551
        .enumValue(OPCUAStatus.OPCUA_INIT_CONFIG.name())
1✔
1552
        .setDescription(
1✔
1553
            "This link is in the configuration stage(Configuring OPCUA parameters such as certificates)");
1554
    spLinkStatusType
1✔
1555
        .enumValue(OPCUAStatus.OPCUA_INIT_TREE.name())
1✔
1556
        .setDescription(
1✔
1557
            "The link is parsing the OPCUA Tree and mapping them to PVs."
1558
                + " Depending on configuration, this can take a while.");
1559
    spLinkStatusType
1✔
1560
        .enumValue(OPCUAStatus.OPCUA_INIT_EVENTS.name())
1✔
1561
        .setDescription("The link is configuring and subscribing to OPCUA events");
1✔
1562
    spLinkStatusType
1✔
1563
        .enumValue(OPCUAStatus.OPCUA_INIT_DATA_SUBSCRIPTION.name())
1✔
1564
        .setDescription(
1✔
1565
            "The link is creating subscriptions for each node that was parsed from the tree"
1566
                + "that has a Value attribute.");
1567
    spLinkStatusType
1✔
1568
        .enumValue(OPCUAStatus.OPCUA_INIT_ALL_DATA_QUERY.name())
1✔
1569
        .setDescription(
1✔
1570
            "The link is querying all attributes of all parsed nodes."
1571
                + "This is can be configured to be done at startup.");
1572
    spLinkStatusType
1✔
1573
        .enumValue(OPCUAStatus.OPCUA_OK.name())
1✔
1574
        .setDescription(
1✔
1575
            "The link is done with all OPCUA initialization. It is in an usable state.");
1576

1577
    OPCUAActiveSubsParam =
1✔
1578
        sysParamService.createSystemParameter(
1✔
1579
            linkName + "/OPCUAActiveSubs",
1580
            Type.UINT64,
1581
            "The total number of active opcua subscriptions");
1582
  }
1✔
1583

1584
  @Override
1585
  public List<ParameterValue> getSystemParameters() {
1586
    long time = getCurrentTime();
1✔
1587
    ArrayList<ParameterValue> list = new ArrayList<>();
1✔
1588

1589
    list.add(
1✔
1590
        org.yamcs.parameter.SystemParametersService.getPV(
1✔
1591
            OPCUAStatusParam, time, currentOPCUAStatus));
1592
    list.add(
1✔
1593
        org.yamcs.parameter.SystemParametersService.getPV(
1✔
1594
            OPCUAActiveSubsParam, time, OPCUAActiveSubs.get()));
1✔
1595
    try {
1596
      super.collectSystemParameters(time, list);
1✔
1597
    } catch (Exception e) {
×
1598
      log.error("Exception caught when collecting link system parameters", e);
×
1599
    }
1✔
1600
    return list;
1✔
1601
  }
1602
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc