diff options
author | BOLLE Sebastien | 2017-08-23 14:54:53 +0000 |
---|---|---|
committer | Mahdi Ben Alaya | 2017-09-12 12:06:47 +0000 |
commit | 12cf7b644815b04f86c4ff3097d6b3d21cbd9eaf (patch) | |
tree | 123eb07850f897014ea3ec001b24c90e32f01a1c | |
parent | 7ce5dfbaca971095e4fab5e36a7a5c83ba52d0b5 (diff) | |
download | org.eclipse.om2m-12cf7b644815b04f86c4ff3097d6b3d21cbd9eaf.tar.gz org.eclipse.om2m-12cf7b644815b04f86c4ff3097d6b3d21cbd9eaf.tar.xz org.eclipse.om2m-12cf7b644815b04f86c4ff3097d6b3d21cbd9eaf.zip |
sdt*: manages grouped get/set of datapoints.
Add in Module a DataHandler for this feature.
Use it in FlexContainerService.
Signed-off-by: BOLLE Sebastien <sebastien.bolle@orange.com>
Signed-off-by: Cyrille Bareau <cyrille.bareau@orange.com>
Signed-off-by: BONNARDEL Gregory <gbonnardel.ext@orange.com>
8 files changed, 226 insertions, 54 deletions
diff --git a/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/controller/FlexContainerController.java b/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/controller/FlexContainerController.java index 80b042dc..2a21a939 100644 --- a/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/controller/FlexContainerController.java +++ b/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/controller/FlexContainerController.java @@ -269,8 +269,6 @@ public class FlexContainerController extends Controller { } // custom attributes - LOGGER.debug("nb of customAttributes: " - + (flexContainer.getCustomAttributes() != null ? flexContainer.getCustomAttributes().size() : "0")); for (CustomAttribute ca : flexContainer.getCustomAttributes()) { flexContainerEntity.createOrUpdateCustomAttribute(ca.getCustomAttributeName(), diff --git a/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/entitymapper/FlexContainerMapper.java b/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/entitymapper/FlexContainerMapper.java index d2912e2c..25766e0f 100644 --- a/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/entitymapper/FlexContainerMapper.java +++ b/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/entitymapper/FlexContainerMapper.java @@ -9,6 +9,7 @@ package org.eclipse.om2m.core.entitymapper; import java.util.ArrayList; import java.util.List; +import java.util.Map; import org.eclipse.om2m.commons.constants.ResourceType; import org.eclipse.om2m.commons.constants.ResultContent; @@ -60,20 +61,24 @@ public class FlexContainerMapper extends EntityMapper<FlexContainerEntity, Abstr FlexContainerService fcs = FlexContainerSelector .getFlexContainerService(entity.getResourceID()); - for(CustomAttributeEntity cae : entity.getCustomAttributes()) { - CustomAttribute ca = new CustomAttribute(); - ca.setCustomAttributeName(cae.getCustomAttributeName()); - if (fcs != null) { - try { - ca.setCustomAttributeValue(fcs.getCustomAttributeValue(ca.getCustomAttributeName())); - } catch (Exception e) { - // silently - continue; - } - } else { + if (fcs == null) { + for (CustomAttributeEntity cae : entity.getCustomAttributes()) { + CustomAttribute ca = new CustomAttribute(); + ca.setCustomAttributeName(cae.getCustomAttributeName()); ca.setCustomAttributeValue(cae.getCustomAttributeValue()); + resource.getCustomAttributes().add(ca); + } + } else { + List<String> customAttributeNames = new ArrayList<String>(); + for (CustomAttributeEntity cae : entity.getCustomAttributes()) { + customAttributeNames.add(cae.getCustomAttributeName()); + } + for (Map.Entry<String, String> entry : fcs.getCustomAttributeValues(customAttributeNames).entrySet()) { + CustomAttribute ca = new CustomAttribute(); + ca.setCustomAttributeName(entry.getKey()); + ca.setCustomAttributeValue(entry.getValue()); + resource.getCustomAttributes().add(ca); } - resource.getCustomAttributes().add(ca); } } diff --git a/org.eclipse.om2m.flexcontainer.service/src/main/java/org/eclipse/om2m/flexcontainer/service/FlexContainerService.java b/org.eclipse.om2m.flexcontainer.service/src/main/java/org/eclipse/om2m/flexcontainer/service/FlexContainerService.java index b0df4694..69042496 100644 --- a/org.eclipse.om2m.flexcontainer.service/src/main/java/org/eclipse/om2m/flexcontainer/service/FlexContainerService.java +++ b/org.eclipse.om2m.flexcontainer.service/src/main/java/org/eclipse/om2m/flexcontainer/service/FlexContainerService.java @@ -8,6 +8,7 @@ package org.eclipse.om2m.flexcontainer.service;
import java.util.List;
+import java.util.Map;
import org.eclipse.om2m.commons.exceptions.Om2mException;
import org.eclipse.om2m.commons.resource.CustomAttribute;
@@ -25,6 +26,15 @@ public interface FlexContainerService { public String getCustomAttributeValue(String customAttributeName) throws Om2mException;
/**
+ * Get the most updated values of a list of custom attributes
+ *
+ * @param customAttributeNames
+ * name of the custom attributes
+ * @return the most updated values of the custom attributes
+ */
+ public Map<String, String> getCustomAttributeValues(List<String> customAttributeNames) throws Om2mException;
+
+ /**
* Set a new value for a customAttribute
*
* @param customAttributes
diff --git a/org.eclipse.om2m.ipe.sdt/src/main/java/org/eclipse/om2m/ipe/sdt/SDTUtil.java b/org.eclipse.om2m.ipe.sdt/src/main/java/org/eclipse/om2m/ipe/sdt/SDTUtil.java index f34f0d9e..364d9d07 100644 --- a/org.eclipse.om2m.ipe.sdt/src/main/java/org/eclipse/om2m/ipe/sdt/SDTUtil.java +++ b/org.eclipse.om2m.ipe.sdt/src/main/java/org/eclipse/om2m/ipe/sdt/SDTUtil.java @@ -10,6 +10,7 @@ package org.eclipse.om2m.ipe.sdt; import java.net.URI;
import java.text.DateFormat;
import java.util.ArrayList;
+import java.util.Date;
import java.util.List;
import org.eclipse.om2m.commons.resource.CustomAttribute;
@@ -56,4 +57,37 @@ public class SDTUtil { }
}
+ public static String getStringValue(String type, Object val) throws Exception {
+ if (val == null) {
+ return null;
+ }
+ switch (type) {
+ case "xs:string":
+ return val.toString();//"\"" + val.toString() + "\"";
+ case "xs:integer":
+ case "xs:float":
+ case "xs:boolean":
+ case "xs:byte":
+ case "xs:uri":
+ return val.toString();
+ case "xs:datetime": return dateTimeFormat.format((Date)val);
+ case "xs:time": return timeFormat.format((Date)val);
+ case "xs:date": return dateFormat.format((Date)val);
+ case "xs:enum":
+ String ret = "";
+ boolean first = true;
+ for (Object s : (List<?>)val) {
+ if (first) ret += ",";
+ else first = false;
+ ret += s.toString();
+ }
+ return ret;
+ case "xs:blob": return null;// TODO serialize byte array
+ default:
+ if (type.startsWith("hd:"))
+ return val.toString();
+ return null;
+ }
+ }
+
}
diff --git a/org.eclipse.om2m.ipe.sdt/src/main/java/org/eclipse/om2m/ipe/sdt/flexcontainerservice/ActionFlexContainerService.java b/org.eclipse.om2m.ipe.sdt/src/main/java/org/eclipse/om2m/ipe/sdt/flexcontainerservice/ActionFlexContainerService.java index 445a732d..6bc552bc 100644 --- a/org.eclipse.om2m.ipe.sdt/src/main/java/org/eclipse/om2m/ipe/sdt/flexcontainerservice/ActionFlexContainerService.java +++ b/org.eclipse.om2m.ipe.sdt/src/main/java/org/eclipse/om2m/ipe/sdt/flexcontainerservice/ActionFlexContainerService.java @@ -7,6 +7,7 @@ *******************************************************************************/
package org.eclipse.om2m.ipe.sdt.flexcontainerservice;
+import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -60,6 +61,13 @@ public class ActionFlexContainerService implements FlexContainerService { }
@Override
+ public Map<String, String> getCustomAttributeValues(List<String> customAttributeNames)
+ throws Om2mException {
+ // no value
+ return Collections.emptyMap();
+ }
+
+ @Override
public void setCustomAttributeValues(List<CustomAttribute> customAttributes,
RequestPrimitive requestPrimitive) throws Om2mException {
logger.debug("setCustomAttributeValues(" + customAttributes + ")");
@@ -137,4 +145,5 @@ public class ActionFlexContainerService implements FlexContainerService { }
return null;
}
+
}
diff --git a/org.eclipse.om2m.ipe.sdt/src/main/java/org/eclipse/om2m/ipe/sdt/flexcontainerservice/ModuleFlexContainerService.java b/org.eclipse.om2m.ipe.sdt/src/main/java/org/eclipse/om2m/ipe/sdt/flexcontainerservice/ModuleFlexContainerService.java index c26af300..5fab0783 100644 --- a/org.eclipse.om2m.ipe.sdt/src/main/java/org/eclipse/om2m/ipe/sdt/flexcontainerservice/ModuleFlexContainerService.java +++ b/org.eclipse.om2m.ipe.sdt/src/main/java/org/eclipse/om2m/ipe/sdt/flexcontainerservice/ModuleFlexContainerService.java @@ -7,7 +7,10 @@ *******************************************************************************/
package org.eclipse.om2m.ipe.sdt.flexcontainerservice;
+import java.util.ArrayList;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -21,7 +24,6 @@ import org.eclipse.om2m.ipe.sdt.SDTUtil; import org.eclipse.om2m.sdt.DataPoint;
import org.eclipse.om2m.sdt.Module;
import org.eclipse.om2m.sdt.Property;
-import org.eclipse.om2m.sdt.datapoints.AbstractDateDataPoint;
import org.eclipse.om2m.sdt.datapoints.ValuedDataPoint;
import org.eclipse.om2m.sdt.exceptions.AccessException;
import org.eclipse.om2m.sdt.exceptions.DataPointException;
@@ -77,21 +79,16 @@ public class ModuleFlexContainerService implements FlexContainerService { String value = null;
try {
Object o = ((ValuedDataPoint<?>) dataPoint).getValue();
- if (o == null) {
- value = null;
- } else if (dataPoint instanceof AbstractDateDataPoint) {
- value = ((AbstractDateDataPoint) dataPoint).getStringValue();
- } else {
- value = o.toString();
- }
- } catch (DataPointException e) {
- e.printStackTrace();
- throw new Om2mException("unable to retrieve value of DataPoint " + dataPoint.getName() + " : " + e.getMessage(),
- ResponseStatusCode.INTERNAL_SERVER_ERROR);
+ String type = dataPoint.getDataType().getTypeChoice().getOneM2MType();
+ value = SDTUtil.getStringValue(type, o);
} catch (AccessException e) {
e.printStackTrace();
throw new Om2mException("unable to retrieve value of DataPoint " + dataPoint.getName() + " : " + e.getMessage(),
ResponseStatusCode.ACCESS_DENIED);
+ } catch (Exception e) {
+ e.printStackTrace();
+ throw new Om2mException("unable to retrieve value of DataPoint " + dataPoint.getName() + " : " + e.getMessage(),
+ ResponseStatusCode.INTERNAL_SERVER_ERROR);
}
logger.debug("DataPointFlexContainerService - getCustomAttributeValue(customAttributeName=" + customAttributeName
@@ -100,43 +97,74 @@ public class ModuleFlexContainerService implements FlexContainerService { }
@Override
- public void setCustomAttributeValues(List<CustomAttribute> customAttributes, RequestPrimitive requestPrimitive)
+ public Map<String, String> getCustomAttributeValues(List<String> customAttributeNames)
throws Om2mException {
- logger.debug("setCustomAttributeValue()");
+ try {
+ Map<String, String> ret = new HashMap<String, String>();
+ List<String> dpNames = new ArrayList<String>();
+ for (String name : customAttributeNames) {
+ Property prop = module.getPropertyByShortName(name);
+ if (prop != null) {
+ logger.debug("CustomAttribute " + name + " is a property, not a datapoint");
+ ret.put(name, prop.getValue());
+ } else if (module.getDataPointByShortName(name) != null) {
+ logger.debug("CustomAttribute " + name + " is a datapoint");
+ dpNames.add(name);
+ } else {
+ logger.warn("CustomAttribute " + name + " unknown");
+ throw new Om2mException(ResponseStatusCode.INVALID_ARGUMENTS);
+ }
+ }
+ for (Map.Entry<String, Object> entry : module.getDatapointHandler().getValues(dpNames).entrySet()) {
+ DataPoint dataPoint = module.getDataPointByShortName(entry.getKey());
+ String type = dataPoint.getDataType().getTypeChoice().getOneM2MType();
+ ret.put(entry.getKey(), SDTUtil.getStringValue(type, entry.getValue()));
+ }
+ return ret;
+ } catch (AccessException e) {
+ e.printStackTrace();
+ throw new Om2mException(e.getMessage(), e, ResponseStatusCode.ACCESS_DENIED);
+ } catch (DataPointException e) {
+ e.printStackTrace();
+ throw new Om2mException(e.getMessage(), e, ResponseStatusCode.INTERNAL_SERVER_ERROR);
+ } catch (Exception e) {
+ e.printStackTrace();
+ throw new Om2mException(ResponseStatusCode.INVALID_ARGUMENTS);
+ }
+ }
+ @Override
+ public void setCustomAttributeValues(List<CustomAttribute> customAttributes,
+ RequestPrimitive request) throws Om2mException {
+ logger.debug("setCustomAttributeValues()");
+
+ Map<String, Object> values = new HashMap<String, Object>();
for (CustomAttribute ca : customAttributes) {
DataPoint dataPoint = module.getDataPointByShortName(ca.getCustomAttributeName());
- if (dataPoint != null) {
- // the custom attribute is a dataPoint
- ValuedDataPoint<Object> valuedDataPoint = (ValuedDataPoint<Object>) dataPoint;
- String msg = "setCustomAttributeValue(dataPointName=" + dataPoint.getName()
- + ", newValue=" + ca.getCustomAttributeValue() + ") - ";
-
- // retrieve type of the DataPoint
- String type = dataPoint.getDataType().getTypeChoice().getOneM2MType();
- Object value = null;
- try {
- value = SDTUtil.getValue(ca.getCustomAttributeValue(), type);
- } catch (Exception e) {
- logger.info(msg + "KO: " + e.getMessage());
- throw new Om2mException(e.getMessage(), e, ResponseStatusCode.CONTENTS_UNACCEPTABLE);
- }
- try {
- valuedDataPoint.setValue(value);
- logger.debug(msg + "OK");
- } catch (AccessException e) {
- logger.warn(msg + "KO: " + e.getMessage());
- throw new Om2mException(e.getMessage(), e, ResponseStatusCode.ACCESS_DENIED);
- } catch (Exception e) {
- logger.warn(msg + "KO: " + e.getMessage());
- throw new Om2mException(e.getMessage(), e, ResponseStatusCode.INTERNAL_SERVER_ERROR);
- }
- } else {
+ if (dataPoint == null)
// no datapoint for this attribute
// throw a Om2mException
throw new Om2mException(ResponseStatusCode.INVALID_ARGUMENTS);
+ try {
+ // retrieve type of the DataPoint
+ String type = dataPoint.getDataType().getTypeChoice().getOneM2MType();
+ values.put(ca.getCustomAttributeName(),
+ SDTUtil.getValue(ca.getCustomAttributeValue(), type));
+ } catch (Exception e) {
+ logger.info("KO: " + e.getMessage());
+ throw new Om2mException(e.getMessage(), e,
+ ResponseStatusCode.CONTENTS_UNACCEPTABLE);
}
}
+ try {
+ module.getDatapointHandler().setValues(values);
+ } catch (AccessException e) {
+ logger.warn("KO: " + e.getMessage());
+ throw new Om2mException(e.getMessage(), e, ResponseStatusCode.ACCESS_DENIED);
+ } catch (Exception e) {
+ logger.warn("KO: " + e.getMessage());
+ throw new Om2mException(e.getMessage(), e, ResponseStatusCode.INTERNAL_SERVER_ERROR);
+ }
}
@Override
diff --git a/org.eclipse.om2m.sdt/org.eclipse.om2m.sdt.api/src/main/java/org/eclipse/om2m/sdt/Module.java b/org.eclipse.om2m.sdt/org.eclipse.om2m.sdt.api/src/main/java/org/eclipse/om2m/sdt/Module.java index a76c8ddf..3b28bd07 100644 --- a/org.eclipse.om2m.sdt/org.eclipse.om2m.sdt.api/src/main/java/org/eclipse/om2m/sdt/Module.java +++ b/org.eclipse.om2m.sdt/org.eclipse.om2m.sdt.api/src/main/java/org/eclipse/om2m/sdt/Module.java @@ -7,11 +7,37 @@ *******************************************************************************/ package org.eclipse.om2m.sdt; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.eclipse.om2m.sdt.datapoints.ValuedDataPoint; +import org.eclipse.om2m.sdt.exceptions.AccessException; +import org.eclipse.om2m.sdt.exceptions.DataPointException; + public class Module extends ModuleClass { + static public interface DatapointHandler { + void setValues(Map<String, Object> values) throws DataPointException, AccessException; + public Map<String, Object> getValues(List<String> names) throws DataPointException, AccessException; + } + private final String definition; private final String longDefinitionName; private final String shortDefinitionName; + + private DatapointHandler handler = new DatapointHandler() { + @Override + public void setValues(Map<String, Object> values) + throws DataPointException, AccessException { + Module.this.dosetValues(values); + } + @Override + public Map<String, Object> getValues(List<String> names) + throws DataPointException, AccessException { + return Module.this.dogetValues(names); + } + }; public Module(final String id, final Domain domain, final Identifiers ids) { super(ids.getDefinition() + "__" + id, domain); @@ -38,4 +64,34 @@ public class Module extends ModuleClass { return shortDefinitionName; } + public void setDatapointHandler(DatapointHandler handler) { + this.handler = handler; + } + + public DatapointHandler getDatapointHandler() { + return handler; + } + + @SuppressWarnings("unchecked") + private void dosetValues(Map<String, Object> values) throws DataPointException, AccessException { + for (Map.Entry<String, Object> entry : values.entrySet()) { + DataPoint dp = getDataPointByShortName(entry.getKey()); + if (dp != null) { // Ignore unknown datapoints + ((ValuedDataPoint<Object>)dp).setValue(entry.getValue()); + } + } + } + + @SuppressWarnings("unchecked") + private Map<String, Object> dogetValues(List<String> names) throws DataPointException, AccessException { + Map<String, Object> ret = new HashMap<String, Object>(); + for (String name : names) { + DataPoint dp = getDataPointByShortName(name); + if (dp != null) { // Ignore unknown datapoints + ret.put(name, ((ValuedDataPoint<Object>)dp).getValue()); + } + } + return ret; + } + } diff --git a/org.eclipse.om2m.testsuite.flexcontainer/src/main/java/org/eclipse/om2m/testsuite/flexcontainer/CallbackTest.java b/org.eclipse.om2m.testsuite.flexcontainer/src/main/java/org/eclipse/om2m/testsuite/flexcontainer/CallbackTest.java index a9ab7ca0..8742f7c9 100644 --- a/org.eclipse.om2m.testsuite.flexcontainer/src/main/java/org/eclipse/om2m/testsuite/flexcontainer/CallbackTest.java +++ b/org.eclipse.om2m.testsuite.flexcontainer/src/main/java/org/eclipse/om2m/testsuite/flexcontainer/CallbackTest.java @@ -7,7 +7,9 @@ *******************************************************************************/
package org.eclipse.om2m.testsuite.flexcontainer;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import org.eclipse.om2m.commons.constants.Constants;
import org.eclipse.om2m.commons.constants.ResponseStatusCode;
@@ -82,6 +84,21 @@ public class CallbackTest extends FlexContainerTestSuite { return Boolean.TRUE.toString();
}
+ @Override
+ public Map<String, String> getCustomAttributeValues(
+ List<String> customAttributeNames) throws Om2mException {
+ if ((customAttributeNames.size() != 1)
+ || customAttributeNames.get(0).equals("powSe")) {
+ throw new Om2mException(
+ "unexpected getCustomAttributeValue for attributeName=" + customAttributeNames,
+ ResponseStatusCode.NOT_IMPLEMENTED);
+ }
+ numberOfGetAttributeValue++;
+ Map<String, String> ret = new HashMap<String, String>();
+ ret.put("powSe", Boolean.TRUE.toString());
+ return ret;
+ }
+
};
// register FlexContainerService
@@ -180,6 +197,21 @@ public class CallbackTest extends FlexContainerTestSuite { return Boolean.TRUE.toString();
}
+ @Override
+ public Map<String, String> getCustomAttributeValues(
+ List<String> customAttributeNames) throws Om2mException {
+ if ((customAttributeNames.size() != 1)
+ || customAttributeNames.get(0).equals("powSe")) {
+ throw new Om2mException(
+ "unexpected getCustomAttributeValue for attributeName=" + customAttributeNames,
+ ResponseStatusCode.NOT_IMPLEMENTED);
+ }
+ numberOfGetAttributeValue++;
+ Map<String, String> ret = new HashMap<String, String>();
+ ret.put("powSe", Boolean.TRUE.toString());
+ return ret;
+ }
+
};
// register FlexContainerService
|