diff --git a/nanomind-connector/parameters-provisioning/src/main/java/esa/mo/nanomind/impl/parameters_provisioning/NanomindParameterValuesProvider.java b/nanomind-connector/parameters-provisioning/src/main/java/esa/mo/nanomind/impl/parameters_provisioning/NanomindParameterValuesProvider.java index 7bc6da5..1c4de74 100644 --- a/nanomind-connector/parameters-provisioning/src/main/java/esa/mo/nanomind/impl/parameters_provisioning/NanomindParameterValuesProvider.java +++ b/nanomind-connector/parameters-provisioning/src/main/java/esa/mo/nanomind/impl/parameters_provisioning/NanomindParameterValuesProvider.java @@ -29,12 +29,6 @@ import java.util.logging.Logger; import org.ccsds.moims.mo.mal.MALException; import org.ccsds.moims.mo.mal.MALInteractionException; -import org.ccsds.moims.mo.mal.structures.Attribute; -import org.ccsds.moims.mo.mal.structures.Identifier; -import org.ccsds.moims.mo.mal.structures.IdentifierList; -import org.ccsds.moims.mo.mal.structures.UInteger; -import org.ccsds.moims.mo.mal.structures.URI; - import esa.mo.helpertools.connections.SingleConnectionDetails; import esa.mo.helpertools.helpers.HelperAttributes; import esa.mo.nanomind.impl.consumer.ActionNanomindConsumerServiceImpl; @@ -45,6 +39,19 @@ import esa.opssat.nanomind.mc.action.structures.ActionInstanceDetails; import esa.opssat.nanomind.mc.structures.AttributeValue; import esa.opssat.nanomind.mc.structures.AttributeValueList; +import org.ccsds.moims.mo.mal.structures.Attribute; +import org.ccsds.moims.mo.mal.structures.Blob; +import org.ccsds.moims.mo.mal.structures.Duration; +import org.ccsds.moims.mo.mal.structures.FineTime; +import org.ccsds.moims.mo.mal.structures.Identifier; +import org.ccsds.moims.mo.mal.structures.IdentifierList; +import org.ccsds.moims.mo.mal.structures.Time; +import org.ccsds.moims.mo.mal.structures.UInteger; +import org.ccsds.moims.mo.mal.structures.ULong; +import org.ccsds.moims.mo.mal.structures.UOctet; +import org.ccsds.moims.mo.mal.structures.URI; +import org.ccsds.moims.mo.mal.structures.UShort; +import org.ccsds.moims.mo.mal.structures.Union; /** * Provides OBSW parameter values by consuming the Nanomind aggregation service. Fetched values are @@ -127,6 +134,76 @@ public NanomindParameterValuesProvider(HashMap parame } } + /** + * Converts most MAL Attribute data types to their Long bits representation. + * This method is designed to preserve the bit pattern of the original value where applicable, + * facilitating conversions between different numerical and data types. + * + * @param in The MAL Attribute data type to be converted. + * @return The converted Long bits representation. + * @throws IllegalArgumentException If the input is an unsupported MAL Attribute type, + * indicating that the conversion cannot be performed. Unsupported types might include + * complex objects or specific numerical ranges that cannot be accurately represented in a Long. + */ + private Long attributeToLongBits(Attribute in) throws IllegalArgumentException { + if (in == null) { + throw new IllegalArgumentException("The given value is null"); + } + + // Handle Union types with various forms + if (in instanceof Union) { + Union union = (Union) in; + if (union.getTypeShortForm().equals(Union.BOOLEAN_TYPE_SHORT_FORM)) { + return (long) (union.getBooleanValue() ? 1 : 0); + } else if (union.getTypeShortForm().equals(Union.FLOAT_TYPE_SHORT_FORM)) { + // Preserve the bit pattern, including the sign, of the float value + return (long) (Float.floatToIntBits(union.getFloatValue()) & 0xFFFFFFFFL); + } else if (union.getTypeShortForm().equals(Union.INTEGER_TYPE_SHORT_FORM)) { + return (long) union.getIntegerValue(); + } else if (union.getTypeShortForm().equals(Union.SHORT_TYPE_SHORT_FORM)) { + return (long) union.getShortValue(); + } else if (union.getTypeShortForm().equals(Union.LONG_TYPE_SHORT_FORM)) { + return union.getLongValue(); + } else if (union.getTypeShortForm().equals(Union.OCTET_TYPE_SHORT_FORM)) { + return (long) union.getOctetValue(); + } else if (union.getTypeShortForm().equals(Union.DOUBLE_TYPE_SHORT_FORM)) { + return Double.doubleToLongBits(union.getDoubleValue()); + } else if (union.getTypeShortForm().equals(Union.STRING_TYPE_SHORT_FORM)) { + throw new IllegalArgumentException("String values cannot be converted to a Long bit representation"); + } else { + throw new IllegalArgumentException("The given value cannot be converted to a Long bit representation"); + } + } + // Other specified types handling + else if (in instanceof Duration) { + return Double.doubleToLongBits(((Duration) in).getValue()); + } else if (in instanceof Time) { + return ((Time) in).getValue(); + } else if (in instanceof FineTime) { + return ((FineTime) in).getValue(); + } else if (in instanceof UOctet) { + return (long) ((UOctet) in).getValue(); + } else if (in instanceof UShort) { + return (long) ((UShort) in).getValue(); + } else if (in instanceof UInteger) { + return ((UInteger) in).getValue(); + } else if (in instanceof ULong) { + try { + return ((ULong) in).getValue().longValueExact(); + } catch (ArithmeticException e) { + throw new IllegalArgumentException("The ULong value cannot fit in a Long representation", e); + } + } else if (in instanceof Blob) { + throw new IllegalArgumentException("The Blob value cannot fit in a Long representation"); + } else if (in instanceof Identifier) { + throw new IllegalArgumentException("The Identifier value cannot fit in a Long representation"); + } else if (in instanceof URI) { + throw new IllegalArgumentException("The URI value cannot fit in a Long representation"); + } else { + throw new IllegalArgumentException("The given value could not be processed"); + } + } + /** * Load the system properties that we need. */ @@ -303,7 +380,7 @@ public Boolean setValue(Attribute rawValue, Identifier identifier) { Long longParameterID = null; try { - longValue = HelperAttributes.attributeToLongBits(rawValue); + longValue = this.attributeToLongBits(rawValue); longParameterID = parameterMap.get(identifier).getId(); LOGGER.log(Level.INFO, "Prepare to set parameter with id {0} to value {1}", new Object[]{longParameterID, longValue});