Search in sources :

Example 6 with AddressSpaceManager

use of org.eclipse.milo.opcua.sdk.server.api.AddressSpaceManager in project milo by eclipse.

the class NodeFactory method createNodeTree.

public Tree<UaNode> createNodeTree(NodeId rootNodeId, NodeId typeDefinitionId, InstantiationCallback instantiationCallback) throws UaException {
    AddressSpaceManager addressSpaceManager = context.getServer().getAddressSpaceManager();
    if (!addressSpaceManager.getManagedNode(typeDefinitionId).isPresent()) {
        throw new UaException(StatusCodes.Bad_NodeIdUnknown, "unknown type definition: " + typeDefinitionId);
    }
    NamespaceTable namespaceTable = context.getServer().getNamespaceTable();
    InstanceDeclarationHierarchy idh;
    try {
        idh = IDH_CACHE.get(typeDefinitionId, () -> {
            LoggerFactory.getLogger(NodeFactory.class).debug("InstanceDeclarationHierarchy cache " + "miss for typeDefinitionId={}", typeDefinitionId);
            return InstanceDeclarationHierarchy.create(addressSpaceManager, namespaceTable, typeDefinitionId);
        });
    } catch (ExecutionException e) {
        throw new UaException(StatusCodes.Bad_InternalError, e);
    }
    NodeTable nodeTable = idh.getNodeTable();
    ReferenceTable referenceTable = idh.getReferenceTable();
    Map<BrowsePath, UaNode> nodes = new HashMap<>();
    for (Map.Entry<BrowsePath, NodeId> entry : nodeTable.nodes.entrySet()) {
        BrowsePath browsePath = entry.getKey();
        NodeId nodeId = entry.getValue();
        UaNode node = addressSpaceManager.getManagedNode(nodeId).orElse(null);
        if (browsePath.parent == null) {
            if (node instanceof UaObjectTypeNode) {
                UaNode instance = instanceFromTypeDefinition(rootNodeId, (UaObjectTypeNode) node);
                nodes.put(browsePath, instance);
            } else if (node instanceof UaVariableTypeNode) {
                UaNode instance = instanceFromTypeDefinition(rootNodeId, (UaVariableTypeNode) node);
                nodes.put(browsePath, instance);
            } else {
                throw new UaException(StatusCodes.Bad_InternalError);
            }
        } else {
            // Non-root Nodes are all instance declarations
            NodeId instanceNodeId = instanceNodeId(rootNodeId, browsePath);
            if (node instanceof UaMethodNode) {
                UaMethodNode declaration = (UaMethodNode) node;
                UaMethodNode instance = new UaMethodNode(context, instanceNodeId, declaration.getBrowseName(), declaration.getDisplayName(), declaration.getDescription(), declaration.getWriteMask(), declaration.getUserWriteMask(), declaration.isExecutable(), declaration.isUserExecutable());
                nodes.put(browsePath, instance);
            } else if (node instanceof UaObjectNode) {
                UaObjectNode declaration = (UaObjectNode) node;
                ExpandedNodeId instanceTypeDefinitionId = getTypeDefinition(referenceTable, browsePath);
                UaNode typeDefinitionNode = addressSpaceManager.getManagedNode(instanceTypeDefinitionId).orElse(null);
                if (typeDefinitionNode instanceof ObjectTypeNode) {
                    boolean optional = isOptionalDeclaration(declaration);
                    if (!optional || instantiationCallback.includeOptionalNode(typeDefinitionNode.getNodeId(), declaration.getBrowseName())) {
                        UaObjectNode instance = instanceFromTypeDefinition(instanceNodeId, (ObjectTypeNode) typeDefinitionNode);
                        instance.setBrowseName(declaration.getBrowseName());
                        instance.setDisplayName(declaration.getDisplayName());
                        instance.setDescription(declaration.getDescription());
                        instance.setWriteMask(declaration.getWriteMask());
                        instance.setUserWriteMask(declaration.getUserWriteMask());
                        instance.setEventNotifier(declaration.getEventNotifier());
                        nodes.put(browsePath, instance);
                    }
                } else {
                    throw new UaException(StatusCodes.Bad_InternalError, "expected type definition for " + instanceTypeDefinitionId);
                }
            } else if (node instanceof UaVariableNode) {
                UaVariableNode declaration = (UaVariableNode) node;
                ExpandedNodeId instanceTypeDefinitionId = getTypeDefinition(referenceTable, browsePath);
                UaNode typeDefinitionNode = addressSpaceManager.getManagedNode(instanceTypeDefinitionId).orElse(null);
                if (typeDefinitionNode instanceof VariableTypeNode) {
                    boolean optional = isOptionalDeclaration(declaration);
                    if (!optional || instantiationCallback.includeOptionalNode(typeDefinitionNode.getNodeId(), declaration.getBrowseName())) {
                        UaVariableNode instance = instanceFromTypeDefinition(instanceNodeId, (VariableTypeNode) typeDefinitionNode);
                        instance.setBrowseName(declaration.getBrowseName());
                        instance.setDisplayName(declaration.getDisplayName());
                        instance.setDescription(declaration.getDescription());
                        instance.setWriteMask(declaration.getWriteMask());
                        instance.setUserWriteMask(declaration.getUserWriteMask());
                        instance.setValue(declaration.getValue());
                        instance.setDataType(declaration.getDataType());
                        instance.setValueRank(declaration.getValueRank());
                        instance.setArrayDimensions(declaration.getArrayDimensions());
                        instance.setAccessLevel(declaration.getAccessLevel());
                        instance.setUserAccessLevel(declaration.getUserAccessLevel());
                        nodes.put(browsePath, instance);
                    }
                } else {
                    throw new UaException(StatusCodes.Bad_InternalError, "expected type definition for " + instanceTypeDefinitionId);
                }
            } else {
                throw new UaException(StatusCodes.Bad_InternalError, "not an instance declaration: " + node);
            }
        }
    }
    nodes.forEach((browsePath, node) -> {
        List<ReferenceTable.RefRow> references = referenceTable.getReferences(browsePath);
        references.forEach(t -> {
            NodeId referenceTypeId = t.nodeId;
            ReferenceTable.RefTarget target = t.target;
            if (!Identifiers.HasModellingRule.equals(referenceTypeId)) {
                if (target.targetNodeId != null) {
                    node.addReference(new Reference(node.getNodeId(), referenceTypeId, target.targetNodeId, true));
                } else {
                    BrowsePath targetPath = target.targetPath;
                    UaNode targetNode = nodes.get(targetPath);
                    if (targetNode != null) {
                        node.addReference(new Reference(node.getNodeId(), referenceTypeId, targetNode.getNodeId().expanded(), true));
                    }
                }
            }
        });
        context.getNodeManager().addNode(node);
    });
    Tree<UaNode> nodeTree = nodeTable.getBrowsePathTree().map(nodes::get);
    notifyInstantiationCallback(nodeTree, instantiationCallback);
    return nodeTree;
}
Also used : HashMap(java.util.HashMap) UaException(org.eclipse.milo.opcua.stack.core.UaException) UaObjectTypeNode(org.eclipse.milo.opcua.sdk.server.nodes.UaObjectTypeNode) ObjectTypeNode(org.eclipse.milo.opcua.sdk.core.nodes.ObjectTypeNode) UaNode(org.eclipse.milo.opcua.sdk.server.nodes.UaNode) UaMethodNode(org.eclipse.milo.opcua.sdk.server.nodes.UaMethodNode) NamespaceTable(org.eclipse.milo.opcua.stack.core.NamespaceTable) UaVariableTypeNode(org.eclipse.milo.opcua.sdk.server.nodes.UaVariableTypeNode) AddressSpaceManager(org.eclipse.milo.opcua.sdk.server.api.AddressSpaceManager) ExecutionException(java.util.concurrent.ExecutionException) UaVariableTypeNode(org.eclipse.milo.opcua.sdk.server.nodes.UaVariableTypeNode) VariableTypeNode(org.eclipse.milo.opcua.sdk.core.nodes.VariableTypeNode) UaObjectTypeNode(org.eclipse.milo.opcua.sdk.server.nodes.UaObjectTypeNode) UaObjectNode(org.eclipse.milo.opcua.sdk.server.nodes.UaObjectNode) Reference(org.eclipse.milo.opcua.sdk.core.Reference) ExpandedNodeId(org.eclipse.milo.opcua.stack.core.types.builtin.ExpandedNodeId) UaVariableNode(org.eclipse.milo.opcua.sdk.server.nodes.UaVariableNode) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) ExpandedNodeId(org.eclipse.milo.opcua.stack.core.types.builtin.ExpandedNodeId) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with AddressSpaceManager

use of org.eclipse.milo.opcua.sdk.server.api.AddressSpaceManager in project milo by eclipse.

the class EventContentFilter method getParentTypeDefinition.

private static Optional<UaNode> getParentTypeDefinition(UaNode node, OpcUaServer server) {
    AddressSpaceManager addressSpaceManager = server.getAddressSpaceManager();
    NamespaceTable namespaceTable = server.getNamespaceTable();
    return addressSpaceManager.getManagedReferences(node.getNodeId()).stream().filter(Reference.SUBTYPE_OF).flatMap(r -> opt2stream(r.getTargetNodeId().toNodeId(namespaceTable))).findFirst().flatMap(addressSpaceManager::getManagedNode);
}
Also used : NamespaceTable(org.eclipse.milo.opcua.stack.core.NamespaceTable) AddressSpaceManager(org.eclipse.milo.opcua.sdk.server.api.AddressSpaceManager)

Aggregations

AddressSpaceManager (org.eclipse.milo.opcua.sdk.server.api.AddressSpaceManager)7 NamespaceTable (org.eclipse.milo.opcua.stack.core.NamespaceTable)6 NodeId (org.eclipse.milo.opcua.stack.core.types.builtin.NodeId)5 Reference (org.eclipse.milo.opcua.sdk.core.Reference)4 OpcUaServer (org.eclipse.milo.opcua.sdk.server.OpcUaServer)4 UaNode (org.eclipse.milo.opcua.sdk.server.nodes.UaNode)4 Identifiers (org.eclipse.milo.opcua.stack.core.Identifiers)4 List (java.util.List)3 Optional (java.util.Optional)3 UaNodeManager (org.eclipse.milo.opcua.sdk.server.UaNodeManager)3 NodeManager (org.eclipse.milo.opcua.sdk.server.api.NodeManager)3 NodeLoader (org.eclipse.milo.opcua.sdk.server.namespaces.loader.NodeLoader)3 ExpandedNodeId (org.eclipse.milo.opcua.stack.core.types.builtin.ExpandedNodeId)3 QualifiedName (org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName)3 Nullable (org.jetbrains.annotations.Nullable)3 Node (org.eclipse.milo.opcua.sdk.core.nodes.Node)2 ObjectTypeNode (org.eclipse.milo.opcua.sdk.core.nodes.ObjectTypeNode)2 VariableTypeNode (org.eclipse.milo.opcua.sdk.core.nodes.VariableTypeNode)2 StreamUtil.opt2stream (org.eclipse.milo.opcua.sdk.core.util.StreamUtil.opt2stream)2 ObjectTypeManager (org.eclipse.milo.opcua.sdk.server.ObjectTypeManager)2