X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fconfig-netconf-connector%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Fconfignetconfconnector%2Fmapping%2Fattributes%2Fresolving%2FSimpleAttributeResolvingStrategy.java;h=26709dbe6b97e2b8fcb37cde22526053debe51df;hb=87837c5398976e1f44418e9f161efea9d5fa4e7c;hp=3b881579f1b4547ecdebbd287387a9b2b3f68aee;hpb=a92d9d6a21a0f6ca8d2153795721f500eaf29ee9;p=controller.git diff --git a/opendaylight/netconf/config-netconf-connector/src/main/java/org/opendaylight/controller/netconf/confignetconfconnector/mapping/attributes/resolving/SimpleAttributeResolvingStrategy.java b/opendaylight/netconf/config-netconf-connector/src/main/java/org/opendaylight/controller/netconf/confignetconfconnector/mapping/attributes/resolving/SimpleAttributeResolvingStrategy.java index 3b881579f1..26709dbe6b 100644 --- a/opendaylight/netconf/config-netconf-connector/src/main/java/org/opendaylight/controller/netconf/confignetconfconnector/mapping/attributes/resolving/SimpleAttributeResolvingStrategy.java +++ b/opendaylight/netconf/config-netconf-connector/src/main/java/org/opendaylight/controller/netconf/confignetconfconnector/mapping/attributes/resolving/SimpleAttributeResolvingStrategy.java @@ -10,12 +10,16 @@ package org.opendaylight.controller.netconf.confignetconfconnector.mapping.attri import com.google.common.base.Optional; import com.google.common.collect.Maps; +import java.lang.reflect.InvocationTargetException; +import java.text.ParseException; +import org.opendaylight.controller.netconf.api.NetconfDocumentedException; import org.opendaylight.controller.netconf.confignetconfconnector.util.Util; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.management.openmbean.SimpleType; import java.lang.reflect.Method; +import java.math.BigDecimal; import java.math.BigInteger; import java.util.Date; import java.util.Map; @@ -34,7 +38,7 @@ final class SimpleAttributeResolvingStrategy extends AbstractAttributeResolvingS } @Override - public Optional parseAttribute(String attrName, Object value) { + public Optional parseAttribute(String attrName, Object value) throws NetconfDocumentedException { if (value == null) { return Optional.absent(); } @@ -65,35 +69,46 @@ final class SimpleAttributeResolvingStrategy extends AbstractAttributeResolvingS resolverPlugins.put(Date.class.getCanonicalName(), new DateResolver()); resolverPlugins.put(Character.class.getCanonicalName(), new CharResolver()); resolverPlugins.put(BigInteger.class.getCanonicalName(), new BigIntegerResolver()); + resolverPlugins.put(BigDecimal.class.getCanonicalName(), new BigDecimalResolver()); } static interface Resolver { - Object resolveObject(Class type, String attrName, String value); + Object resolveObject(Class type, String attrName, String value) throws NetconfDocumentedException; } static class DefaultResolver implements Resolver { @Override - public Object resolveObject(Class type, String attrName, String value) { + public Object resolveObject(Class type, String attrName, String value) throws NetconfDocumentedException { try { - Object parsedValue = parseObject(type, value); - return parsedValue; + return parseObject(type, value); } catch (Exception e) { - throw new RuntimeException("Unable to resolve attribute " + attrName + " from " + value, e); + throw new NetconfDocumentedException("Unable to resolve attribute " + attrName + " from " + value, + NetconfDocumentedException.ErrorType.application, + NetconfDocumentedException.ErrorTag.operation_failed, + NetconfDocumentedException.ErrorSeverity.error); } } - protected Object parseObject(Class type, String value) throws Exception { - Method method = type.getMethod("valueOf", String.class); - Object parsedValue = method.invoke(null, value); - return parsedValue; + protected Object parseObject(Class type, String value) throws NetconfDocumentedException { + Method method = null; + try { + method = type.getMethod("valueOf", String.class); + return method.invoke(null, value); + } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { + logger.trace("Error parsing object {}",e); + throw new NetconfDocumentedException("Error parsing object.", + NetconfDocumentedException.ErrorType.application, + NetconfDocumentedException.ErrorTag.operation_failed, + NetconfDocumentedException.ErrorSeverity.error); + } } } static class StringResolver extends DefaultResolver { @Override - protected Object parseObject(Class type, String value) throws Exception { + protected Object parseObject(Class type, String value) { return value; } } @@ -101,24 +116,39 @@ final class SimpleAttributeResolvingStrategy extends AbstractAttributeResolvingS static class BigIntegerResolver extends DefaultResolver { @Override - protected Object parseObject(Class type, String value) throws Exception { + protected Object parseObject(Class type, String value) { return new BigInteger(value); } } + static class BigDecimalResolver extends DefaultResolver { + + @Override + protected Object parseObject(Class type, String value) { + return new BigDecimal(value); + } + } + static class CharResolver extends DefaultResolver { @Override - protected Object parseObject(Class type, String value) throws Exception { + protected Object parseObject(Class type, String value) { return new Character(value.charAt(0)); } } static class DateResolver extends DefaultResolver { - @Override - protected Object parseObject(Class type, String value) throws Exception { - return Util.readDate(value); + protected Object parseObject(Class type, String value) throws NetconfDocumentedException { + try { + return Util.readDate(value); + } catch (ParseException e) { + logger.trace("Unable parse value {} due to {}",value, e); + throw new NetconfDocumentedException("Unable to parse value "+value+" as date.", + NetconfDocumentedException.ErrorType.application, + NetconfDocumentedException.ErrorTag.operation_failed, + NetconfDocumentedException.ErrorSeverity.error); + } } }