import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;
-import org.opendaylight.controller.netconf.confignetconfconnector.util.Util;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
+import java.util.Map;
import javax.management.openmbean.CompositeDataSupport;
import javax.management.openmbean.CompositeType;
import javax.management.openmbean.OpenDataException;
import javax.management.openmbean.OpenType;
-import java.util.Map;
+import javax.management.openmbean.SimpleType;
+import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
+import org.opendaylight.controller.netconf.confignetconfconnector.util.Util;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
-final class CompositeAttributeResolvingStrategy extends
+class CompositeAttributeResolvingStrategy extends
AbstractAttributeResolvingStrategy<CompositeDataSupport, CompositeType> {
private final Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> innerTypes;
private final Map<String, String> yangToJavaAttrMapping;
- private static final Logger logger = LoggerFactory.getLogger(CompositeAttributeResolvingStrategy.class);
+ private static final Logger LOG = LoggerFactory.getLogger(CompositeAttributeResolvingStrategy.class);
CompositeAttributeResolvingStrategy(Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> innerTypes,
CompositeType openType, Map<String, String> yangToJavaAttrMapping) {
}
@Override
- public Optional<CompositeDataSupport> parseAttribute(String attrName, Object value) {
+ public Optional<CompositeDataSupport> parseAttribute(String attrName, Object value) throws NetconfDocumentedException {
if (value == null) {
return Optional.absent();
Util.checkType(value, Map.class);
Map<?, ?> valueMap = (Map<?, ?>) value;
+ valueMap = preprocessValueMap(valueMap);
Map<String, Object> items = Maps.newHashMap();
Map<String, OpenType<?>> openTypes = Maps.newHashMap();
+ final String[] names = new String[getOpenType().keySet().size()];
+ OpenType<?>[] itemTypes = new OpenType[names.length];
+ int i = 0;
+
for (Object innerAttrName : innerTypes.keySet()) {
Preconditions.checkState(innerAttrName instanceof String, "Attribute name must be string");
String innerAttrNameStr = (String) innerAttrName;
Optional<?> parsedInnerValue = attributeResolvingStrategy.parseAttribute(innerAttrNameStr, valueToParse);
- openTypes.put(innerAttrNameStr, attributeResolvingStrategy.getOpenType());
+ if(attributeResolvingStrategy instanceof EnumAttributeResolvingStrategy) {
+ // Open type for enum contain the class name necessary for its resolution, however in a DTO
+ // the open type need to be just SimpleType.STRING so that JMX is happy
+ // After the enum attribute is resolved, change its open type back to STRING
+ openTypes.put(innerAttrNameStr, SimpleType.STRING);
+ } else {
+ openTypes.put(innerAttrNameStr, attributeResolvingStrategy.getOpenType());
+ }
items.put(yangToJavaAttrMapping.get(innerAttrNameStr),
parsedInnerValue.isPresent() ? parsedInnerValue.get() : null);
+
+ // fill names + item types in order to reconstruct the open type for current attribute
+ names[i] = yangToJavaAttrMapping.get(innerAttrNameStr);
+ itemTypes[i] = openTypes.get(innerAttrNameStr);
+ i++;
}
CompositeDataSupport parsedValue;
try {
+ LOG.trace("Attribute {} with open type {}. Reconstructing open type.", attrName, getOpenType());
+ setOpenType(new CompositeType(getOpenType().getTypeName(), getOpenType().getDescription(), names, names, itemTypes));
+ LOG.debug("Attribute {} with open type {}. Open type reconstructed to {}", attrName, getOpenType(), getOpenType());
parsedValue = new CompositeDataSupport(getOpenType(), items);
} catch (OpenDataException e) {
- throw new RuntimeException("An error occured during restoration of composite type " + this
+ throw new IllegalStateException("An error occurred during restoration of composite type " + this
+ " for attribute " + attrName + " from value " + value, e);
}
- logger.debug("Attribute {} : {} parsed to type {} as {}", attrName, value, getOpenType(), parsedValue);
+ LOG.debug("Attribute {} : {} parsed to type {} as {}", attrName, value, getOpenType(), parsedValue);
return Optional.of(parsedValue);
}
+
+
+ protected Map<?, ?> preprocessValueMap(Map<?, ?> valueMap) {
+ return valueMap;
+ }
+
+
}