Skip to content

Commit

Permalink
Issue #1208 [parameter-provisioning] fix setValue to support most types
Browse files Browse the repository at this point in the history
  • Loading branch information
georgeslabreche committed Feb 27, 2024
1 parent 8038f8a commit f7dd91b
Showing 1 changed file with 84 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -127,6 +134,76 @@ public NanomindParameterValuesProvider(HashMap<Identifier, OBSWParameter> 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.
*/
Expand Down Expand Up @@ -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});
Expand Down

0 comments on commit f7dd91b

Please sign in to comment.