package org.oic.simulator;
-import org.oic.simulator.ResourceAttribute;
+import java.util.HashMap;
import java.util.Map;
/**
- * This class represents the resource model of a resource and it provides a set
- * of native methods for accessing the resource model.
+ * This class represents the resource model of a resource.
*/
public class SimulatorResourceModel {
+ private Map<String, AttributeValue> mValues = null;
+ private Map<String, AttributeProperty> mProperties = null;
+
/**
- * Constructor for creating a native resource model object. Client requests
- * such as PUT and POST uses this method for passing the new/updated
- * resource model.
+ * Constructs new {@SimulatorResourceModel} object.
*/
public SimulatorResourceModel() {
- create();
+ mValues = new HashMap<>();
+ mProperties = new HashMap<>();
}
/**
- * API to add an attribute of type integer.
+ * API to add an attribute to resource model.
*
- * @param name
- * Name of the attribute
+ * @param attrName
+ * Name of the attribute.
* @param value
- * Value of the attribute
+ * Value of the attribute.
*
* @throws InvalidArgsException
- * This exception will be thrown if the attribute name is
- * invalid.
- * @throws SimulatorException
- * This exception will be thrown either if the resource model is
- * not found or for some general errors.
+ * This exception will be thrown on invalid input.
*/
- public native void addAttributeInt(String name, int value)
- throws InvalidArgsException, SimulatorException;
+ public void addAttribute(String attrName, AttributeValue value)
+ throws InvalidArgsException {
+ if (null == attrName || attrName.isEmpty())
+ throw new InvalidArgsException("Invalid attribute name!");
+
+ if (null == value)
+ throw new InvalidArgsException("Attribute value is null!");
+
+ mValues.put(attrName, value);
+ }
/**
- * API to add an attribute of type double.
+ * API to add an attribute to resource model.
*
- * @param name
- * Name of the attribute
- * @param value
- * Value of the attribute
+ * @param attribute
+ * {@link SimulatorResourceAttribute} to be add to resource
+ * model.
*
* @throws InvalidArgsException
- * This exception will be thrown if the attribute name is
- * invalid.
- * @throws SimulatorException
- * This exception will be thrown either if the resource model is
- * not found or for some general errors.
+ * This exception will be thrown on invalid input.
*/
- public native void addAttributeDouble(String name, double value)
- throws InvalidArgsException, SimulatorException;
+ public void addAttribute(SimulatorResourceAttribute attribute)
+ throws InvalidArgsException {
+ if (null == attribute || null == attribute.name()
+ || attribute.name().isEmpty() || null == attribute.value())
+ throw new InvalidArgsException("Invalid attribute!");
+
+ mValues.put(attribute.name(), attribute.value());
+ }
/**
- * API to add an attribute of type boolean.
+ * API to set attribute's property.
*
- * @param name
- * Name of the attribute
- * @param value
- * Value of the attribute
+ * @param attrName
+ * Name of the attribute.
+ * @param property
+ * {@link AttributeProperty} to be set for attribute.
*
* @throws InvalidArgsException
- * This exception will be thrown if the attribute name is
- * invalid.
- * @throws SimulatorException
- * This exception will be thrown either if the resource model is
- * not found or for some general errors.
+ * This exception will be thrown on invalid input.
*/
- public native void addAttributeBoolean(String name, boolean value)
- throws InvalidArgsException, SimulatorException;
+ public void setAttributeProperty(String attrName, AttributeProperty property)
+ throws InvalidArgsException {
+ if (null == attrName || attrName.isEmpty())
+ throw new InvalidArgsException("Invalid attribute!");
+
+ if (null == property)
+ throw new InvalidArgsException("Invalid attribute property!");
+
+ mProperties.put(attrName, property);
+ }
/**
- * API to add an attribute of type string.
+ * API to set attribute's value.
*
- * @param name
- * Name of the attribute
+ * @param attrName
+ * Name of the attribute.
* @param value
- * Value of the attribute
+ * {@link AttributeValue} to be set for attribute.
*
* @throws InvalidArgsException
- * This exception will be thrown if the attribute name is
- * invalid.
- * @throws SimulatorException
- * This exception will be thrown either if the resource model is
- * not found or for some general errors.
+ * This exception will be thrown on invalid input.
*/
- public native void addAttributeString(String name, String value)
- throws InvalidArgsException, SimulatorException;
+ public void setAttributeValue(String attrName, AttributeValue value)
+ throws InvalidArgsException {
+ if (null == attrName || attrName.isEmpty())
+ throw new InvalidArgsException("Invalid attribute name!");
+
+ if (null == value)
+ throw new InvalidArgsException("Attribute value is null!");
+
+ mValues.put(attrName, value);
+ }
/**
- * API to get number of attributes for this model.
+ * API to get all the attributes of resource model.
*
- * @return Number of attributes.
- *
- * @throws SimulatorException
- * This exception will be thrown either if the resource model is
- * not found or for some general errors.
+ * @return Map of attributes with attribute name as key and its
+ * corresponding {@link SimulatorResourceAttribute} as value.
*/
- public native int size() throws SimulatorException;
+ public Map<String, SimulatorResourceAttribute> getAttributes() {
+ if (mValues.size() == 0)
+ return null;
+
+ Map<String, SimulatorResourceAttribute> attributes = new HashMap<>();
+ for (Map.Entry<String, AttributeValue> entry : mValues.entrySet()) {
+ SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
+ entry.getKey(), entry.getValue(), mProperties.get(entry
+ .getKey()));
+ attributes.put(entry.getKey(), attribute);
+ }
+
+ return attributes;
+ }
/**
- * API for getting all attributes.
+ * API to get attribute by name.
*
- * @return Map of attributes with attribute name as the key and its
- * corresponding {@link ResourceAttribute} object as the value.
+ * @param attrName
+ * Name of the attribute.
*
- * @throws SimulatorException
- * This exception will be thrown either if the resource model is
- * not found or for some general errors.
+ * @return {@link SimulatorResourceAttribute}.
*/
- public native Map<String, ResourceAttribute> getAttributes()
- throws SimulatorException;
+ public SimulatorResourceAttribute getAttribute(String attrName) {
+ if (mValues.containsKey(attrName)) {
+ SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
+ attrName, mValues.get(attrName), mProperties.get(attrName));
+ return attribute;
+ }
+
+ return null;
+ }
/**
- * API to get attribute by its name.
+ * API to check whether resource model has attribute.
*
- * @param name
- * Name of the attribute
+ * @param attrName
+ * Name of the attribute.
*
- * @return An object of {@link ResourceAttribute}.
+ * @return true if resource model has an attribute with given name,
+ * otherwise false.
+ */
+ public boolean containsAttribute(String attrName) {
+ if (mValues.containsKey(attrName))
+ return true;
+ return false;
+ }
+
+ /**
+ * API to get value type information of attribute.
*
- * @throws InvalidArgsException
- * This exception will be thrown if the attribute does not
- * exist.
+ * @param attrName
+ * Name of the attribute.
*
- * @throws SimulatorException
- * This exception will be thrown either if the resource model is
- * not found or for some general errors.
+ * @return Attribute value type information {@AttributeValue.TypeInfo}.
*/
- public native ResourceAttribute getAttribute(String name)
- throws InvalidArgsException, SimulatorException;
-
- private SimulatorResourceModel(long nativeHandle) {
- this.nativeHandle = nativeHandle;
+ public AttributeValue.TypeInfo getAttributeType(String attrName) {
+ if (mValues.containsKey(attrName))
+ return mValues.get(attrName).typeInfo();
+ return null;
}
- @Override
- protected void finalize() throws Throwable {
- try {
- dispose();
- } catch(Throwable t){
- throw t;
- } finally{
- System.out.println("Calling finalize of Super Class");
- super.finalize();
+ /**
+ * API to remove attribute from resource model.
+ *
+ * @param attrName
+ * Name of the attribute.
+ *
+ * @return true if resource model has attribute it is removed, otherwise
+ * false.
+ */
+ public boolean removeAttribute(String attrName) {
+ if (mValues.containsKey(attrName)) {
+ mValues.remove(attrName);
+ if (mProperties.containsKey(attrName))
+ mProperties.remove(attrName);
+ return true;
}
+
+ return false;
}
- private native void create();
+ /**
+ * API to get number of attributes present in resource model.
+ *
+ * @return Number of attributes present in resource model.
+ */
+ public int size() {
+ return mValues.size();
+ }
- private native void dispose();
+ /**
+ * API to update the attribute values from given
+ * {@link SimulatorResourceModel}.
+ */
+ public void update(SimulatorResourceModel resourceModel) {
+ if (null == resourceModel || 0 == resourceModel.size())
+ return;
+
+ for (Map.Entry<String, SimulatorResourceAttribute> entry : resourceModel
+ .getAttributes().entrySet()) {
+ SimulatorResourceAttribute newAttribute = entry.getValue();
+ SimulatorResourceAttribute attribute = getAttribute(entry.getKey());
+ if (null != newAttribute && null != attribute) {
+ if (null != attribute.property()) {
+ AttributeValueValidation validation = new AttributeValueValidation(
+ attribute.property());
+ if (!validation.validate(newAttribute.value())) {
+ mValues.put(entry.getKey(), newAttribute.value());
+ }
+ } else {
+ mValues.put(entry.getKey(), newAttribute.value());
+ }
+ }
+ }
+ }
- private long nativeHandle;
-}
\ No newline at end of file
+ // Methods used in native code
+ private SimulatorResourceModel(Map<String, AttributeValue> values,
+ Map<String, AttributeProperty> properties) {
+ mValues = values;
+ mProperties = properties;
+ }
+}