use of org.eclipse.milo.opcua.stack.core.serialization.SerializationContext in project milo by eclipse.
the class AbstractMethodInvocationHandler method invoke.
@Override
public final CallMethodResult invoke(AccessContext accessContext, CallMethodRequest request) {
try {
checkExecutableAttributes(accessContext);
Variant[] inputArgumentValues = request.getInputArguments();
if (inputArgumentValues == null)
inputArgumentValues = new Variant[0];
if (inputArgumentValues.length != getInputArguments().length) {
throw new UaException(StatusCodes.Bad_ArgumentsMissing);
}
StatusCode[] inputDataTypeCheckResults = new StatusCode[inputArgumentValues.length];
for (int i = 0; i < inputArgumentValues.length; i++) {
Argument argument = getInputArguments()[i];
Variant variant = inputArgumentValues[i];
Object value = variant.getValue();
// TODO this needs to be able to match when argument DataType is an alias type
// extract subtype logic from AttributeWriter...
boolean dataTypeMatch = value == null || variant.getDataType().flatMap(xni -> xni.toNodeId(node.getNodeContext().getNamespaceTable())).map(type -> {
if (type.equals(argument.getDataType())) {
return true;
} else {
if (Identifiers.Structure.equals(type) && value instanceof ExtensionObject) {
SerializationContext serializationContext = getNode().getNodeContext().getServer().getSerializationContext();
try {
Object decoded = ((ExtensionObject) value).decode(serializationContext);
if (decoded instanceof UaStructure) {
return ((UaStructure) decoded).getTypeId().toNodeId(node.getNodeContext().getNamespaceTable()).map(argument.getDataType()::equals).orElse(false);
}
} catch (UaSerializationException e) {
LoggerFactory.getLogger(getClass()).warn("Error decoding argument value", e);
}
}
return false;
}
}).orElse(false);
switch(argument.getValueRank()) {
case ValueRanks.Scalar:
if (value != null && value.getClass().isArray()) {
dataTypeMatch = false;
}
break;
case ValueRanks.OneDimension:
case ValueRanks.OneOrMoreDimensions:
if (value != null && !value.getClass().isArray()) {
dataTypeMatch = false;
}
break;
default:
break;
}
if (dataTypeMatch) {
inputDataTypeCheckResults[i] = StatusCode.GOOD;
} else {
inputDataTypeCheckResults[i] = new StatusCode(StatusCodes.Bad_TypeMismatch);
}
}
if (Arrays.stream(inputDataTypeCheckResults).anyMatch(StatusCode::isBad)) {
throw new InvalidArgumentException(inputDataTypeCheckResults);
}
validateInputArgumentValues(inputArgumentValues);
InvocationContext invocationContext = new InvocationContext() {
@Override
public OpcUaServer getServer() {
return node.getNodeContext().getServer();
}
@Override
public NodeId getObjectId() {
return request.getObjectId();
}
@Override
public UaMethodNode getMethodNode() {
return node;
}
@Override
public Optional<Session> getSession() {
return accessContext.getSession();
}
};
Variant[] outputValues = invoke(invocationContext, inputArgumentValues);
return new CallMethodResult(StatusCode.GOOD, new StatusCode[0], new DiagnosticInfo[0], outputValues);
} catch (InvalidArgumentException e) {
return new CallMethodResult(e.getStatusCode(), e.getInputArgumentResults(), e.getInputArgumentDiagnosticInfos(), new Variant[0]);
} catch (UaException e) {
return new CallMethodResult(e.getStatusCode(), new StatusCode[0], new DiagnosticInfo[0], new Variant[0]);
}
}
Aggregations