Improved support for default yang statements in configuration subsystem 30/3430/3
authorMaros Marsalek <mmarsale@cisco.com>
Wed, 4 Dec 2013 08:04:48 +0000 (09:04 +0100)
committerMaros Marsalek <mmarsale@cisco.com>
Mon, 9 Dec 2013 11:05:30 +0000 (12:05 +0100)
Generated code for configuration contains default values with correct constructor if specified for leaves
Fixed binary type (de)serialization

Change-Id: Ia1338013f8aacab38074e6c7af2de351c6b73835
Signed-off-by: Maros Marsalek <mmarsale@cisco.com>
25 files changed:
opendaylight/config/yang-jmx-generator-plugin/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/ftl/TemplateFactory.java
opendaylight/config/yang-jmx-generator-plugin/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/ftl/model/Field.java
opendaylight/config/yang-jmx-generator-plugin/src/test/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/JMXGeneratorTest.java
opendaylight/config/yang-jmx-generator-plugin/src/test/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/ModuleMXBeanEntryTemplatesTest.java
opendaylight/config/yang-jmx-generator/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/TypeProviderWrapper.java
opendaylight/config/yang-jmx-generator/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/attribute/JavaAttribute.java
opendaylight/config/yang-test/src/main/yang/config-test-impl.yang
opendaylight/netconf/config-netconf-connector/pom.xml
opendaylight/netconf/config-netconf-connector/src/main/java/org/opendaylight/controller/netconf/confignetconfconnector/mapping/attributes/AttributeIfcSwitchStatement.java
opendaylight/netconf/config-netconf-connector/src/main/java/org/opendaylight/controller/netconf/confignetconfconnector/mapping/attributes/fromxml/AbstractAttributeReadingStrategy.java
opendaylight/netconf/config-netconf-connector/src/main/java/org/opendaylight/controller/netconf/confignetconfconnector/mapping/attributes/fromxml/AttributeConfigElement.java
opendaylight/netconf/config-netconf-connector/src/main/java/org/opendaylight/controller/netconf/confignetconfconnector/mapping/attributes/fromxml/ObjectXmlReader.java
opendaylight/netconf/config-netconf-connector/src/main/java/org/opendaylight/controller/netconf/confignetconfconnector/mapping/attributes/fromxml/SimpleAttributeReadingStrategy.java
opendaylight/netconf/config-netconf-connector/src/main/java/org/opendaylight/controller/netconf/confignetconfconnector/mapping/attributes/fromxml/SimpleBinaryAttributeReadingStrategy.java [new file with mode: 0644]
opendaylight/netconf/config-netconf-connector/src/main/java/org/opendaylight/controller/netconf/confignetconfconnector/mapping/attributes/fromxml/SimpleCompositeAttributeReadingStrategy.java
opendaylight/netconf/config-netconf-connector/src/main/java/org/opendaylight/controller/netconf/confignetconfconnector/mapping/attributes/resolving/CompositeAttributeResolvingStrategy.java
opendaylight/netconf/config-netconf-connector/src/main/java/org/opendaylight/controller/netconf/confignetconfconnector/mapping/attributes/toxml/ObjectXmlWriter.java
opendaylight/netconf/config-netconf-connector/src/main/java/org/opendaylight/controller/netconf/confignetconfconnector/mapping/attributes/toxml/SimpleBinaryAttributeWritingStrategy.java [new file with mode: 0644]
opendaylight/netconf/config-netconf-connector/src/test/java/org/opendaylight/controller/netconf/confignetconfconnector/NetconfMappingTest.java
opendaylight/netconf/netconf-util/src/test/resources/netconfMessages/editConfig.xml
opendaylight/netconf/netconf-util/src/test/resources/netconfMessages/editConfig_none.xml
opendaylight/netconf/netconf-util/src/test/resources/netconfMessages/namespaces/editConfig_differentNamespaceTO.xml
opendaylight/netconf/netconf-util/src/test/resources/netconfMessages/namespaces/editConfig_typeNameConfigAttributeMatching.xml
opendaylight/netconf/netconf-util/src/test/resources/netconfMessages/unrecognised/editConfig_unrecognised7.xml
opendaylight/netconf/netconf-util/src/test/resources/netconfMessages/unrecognised/editConfig_unrecognised8.xml

index 6da6801..115bb85 100644 (file)
@@ -129,7 +129,7 @@ public class TemplateFactory {
                 for (JavaAttribute ja : rpc.getParameters()) {
                     Field field = new Field(Collections.<String> emptyList(),
                             ja.getType().getFullyQualifiedName(),
-                            ja.getLowerCaseCammelCase());
+                            ja.getLowerCaseCammelCase(), ja.getNullableDefaultWrappedForCode());
                     fields.add(field);
                 }
                 MethodDeclaration operation = new MethodDeclaration(
@@ -433,15 +433,18 @@ public class TemplateFactory {
                     String varName = BindingGeneratorUtil
                             .parseToValidParamName(attrEntry.getKey());
 
-                    String fullyQualifiedName;
+                    String fullyQualifiedName, nullableDefault = null;
                     if (attrEntry.getValue() instanceof TypedAttribute) {
                         Type type = ((TypedAttribute) attrEntry.getValue()).getType();
                         fullyQualifiedName = serializeType(type);
+                        if(attrEntry.getValue() instanceof JavaAttribute) {
+                            nullableDefault = ((JavaAttribute)attrEntry.getValue()).getNullableDefaultWrappedForCode();
+                        }
                     } else {
                         fullyQualifiedName = FullyQualifiedNameHelper
                                 .getFullyQualifiedName(packageName, attrEntry.getValue().getUpperCaseCammelCase());
                     }
-                    fields.add(new Field(fullyQualifiedName, varName));
+                    fields.add(new Field(fullyQualifiedName, varName, nullableDefault));
 
                     String getterName = "get" + innerName;
                     MethodDefinition getter = new MethodDefinition(
@@ -531,6 +534,7 @@ public class TemplateFactory {
                 String packageName) {
             for (Entry<String, AttributeIfc> attrEntry : attributes.entrySet()) {
                 String type;
+                String nullableDefaultWrapped = null;
                 AttributeIfc attributeIfc = attrEntry.getValue();
 
                 if (attributeIfc instanceof TypedAttribute) {
@@ -548,6 +552,7 @@ public class TemplateFactory {
                     if (innerAttr instanceof JavaAttribute) {
                         fullyQualifiedName = ((JavaAttribute) innerAttr)
                                 .getType().getFullyQualifiedName();
+                        nullableDefaultWrapped = ((JavaAttribute) innerAttr).getNullableDefaultWrappedForCode();
                     } else if (innerAttr instanceof TOAttribute) {
                         fullyQualifiedName = FullyQualifiedNameHelper
                                 .getFullyQualifiedName(packageName, innerAttr.getUpperCaseCammelCase());
@@ -563,7 +568,7 @@ public class TemplateFactory {
                 }
 
                 fields.add(new Field(type, attributeIfc
-                        .getUpperCaseCammelCase()));
+                        .getUpperCaseCammelCase(), nullableDefaultWrapped));
             }
         }
 
@@ -582,12 +587,16 @@ public class TemplateFactory {
         void processAttributes(Map<String, AttributeIfc> attributes,
                 String packageName) {
             for (Entry<String, AttributeIfc> attrEntry : attributes.entrySet()) {
-                String type;
+                String type, nullableDefaultWrapped = null;
                 AttributeIfc attributeIfc = attrEntry.getValue();
 
                 if (attributeIfc instanceof TypedAttribute) {
                     TypedAttribute typedAttribute = (TypedAttribute) attributeIfc;
                     type = serializeType(typedAttribute.getType());
+                    if (attributeIfc instanceof JavaAttribute) {
+                        nullableDefaultWrapped = ((JavaAttribute) attributeIfc).getNullableDefaultWrappedForCode();
+                    }
+
                 } else if (attributeIfc instanceof TOAttribute) {
                     String fullyQualifiedName = FullyQualifiedNameHelper
                             .getFullyQualifiedName(packageName, attributeIfc.getUpperCaseCammelCase());
@@ -600,6 +609,7 @@ public class TemplateFactory {
                     if (innerAttr instanceof JavaAttribute) {
                         fullyQualifiedName = ((JavaAttribute) innerAttr)
                                 .getType().getFullyQualifiedName();
+                        nullableDefaultWrapped = ((JavaAttribute) innerAttr).getNullableDefaultWrappedForCode();
                     } else if (innerAttr instanceof TOAttribute) {
                         fullyQualifiedName = FullyQualifiedNameHelper
                                 .getFullyQualifiedName(packageName, innerAttr.getUpperCaseCammelCase());
@@ -631,8 +641,7 @@ public class TemplateFactory {
                 String varName = BindingGeneratorUtil
                         .parseToValidParamName(attrEntry.getKey());
                 moduleFields.add(new ModuleField(type, varName, attributeIfc
-                        .getUpperCaseCammelCase(), attributeIfc
-                        .getNullableDefault(), isDependency, dependency));
+                        .getUpperCaseCammelCase(), nullableDefaultWrapped, isDependency, dependency));
 
                 String getterName = "get"
                         + attributeIfc.getUpperCaseCammelCase();
index fe9e885..0857ec6 100644 (file)
@@ -7,10 +7,10 @@
  */
 package org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model;
 
-import java.util.List;
-
 import com.google.common.collect.Lists;
 
+import java.util.List;
+
 public class Field {
     private final String type;
     private final String name;
@@ -21,6 +21,10 @@ public class Field {
         this(Lists.<String> newArrayList(), type, name, null);
     }
 
+    public Field(String type, String name, String definition) {
+        this(Lists.<String> newArrayList(), type, name, definition);
+    }
+
     public Field(List<String> modifiers, String type, String name) {
         this(modifiers, type, name, null);
     }
index 2f14374..1945cac 100644 (file)
@@ -534,7 +534,7 @@ public class JMXGeneratorTest extends AbstractGeneratorTest {
         assertDeclaredField(fieldDeclarations,
                 "private java.util.concurrent.ThreadFactory threadfactoryDependency");
         assertDeclaredField(fieldDeclarations,
-                "private java.lang.Long keepAlive=10");
+                "private java.lang.Long keepAlive=new java.lang.Long(\"10\")");
         assertDeclaredField(fieldDeclarations,
                 "private java.lang.Long coreSize");
         assertDeclaredField(fieldDeclarations, "private byte[] binary");
index 3c47931..48d5b30 100644 (file)
@@ -7,14 +7,7 @@
  */
 package org.opendaylight.controller.config.yangjmxgenerator.plugin;
 
-import static org.junit.Assert.assertNotNull;
-import static org.mockito.Matchers.anyString;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.mock;
-
-import java.util.Collections;
-import java.util.Map;
-
+import com.google.common.collect.Maps;
 import org.junit.Test;
 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
 import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc;
@@ -23,7 +16,13 @@ import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.AbstractFa
 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.TemplateFactory;
 import org.opendaylight.yangtools.sal.binding.model.api.Type;
 
-import com.google.common.collect.Maps;
+import java.util.Collections;
+import java.util.Map;
+
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
 
 public class ModuleMXBeanEntryTemplatesTest {
 
@@ -61,6 +60,7 @@ public class ModuleMXBeanEntryTemplatesTest {
         doReturn("package.type").when(typeA).getFullyQualifiedName();
         doReturn(typeA).when(attr).getType();
         doReturn("Type").when(attr).getUpperCaseCammelCase();
+        doReturn("new Default()").when(attr).getNullableDefault();
         return attr;
     }
 
index a2238d1..764ecd3 100644 (file)
@@ -26,6 +26,10 @@ public class TypeProviderWrapper {
         return getType(leaf, type);
     }
 
+    public String getDefault(LeafSchemaNode node) {
+        return typeProvider.getTypeDefaultConstruction(node);
+    }
+
     public Type getType(SchemaNode leaf, TypeDefinition<?> type) {
         Type javaType;
         try {
index 325ca9e..3e20e4a 100644 (file)
@@ -22,7 +22,7 @@ import javax.management.openmbean.SimpleType;
 public class JavaAttribute extends AbstractAttribute implements TypedAttribute {
 
     private final Type type;
-    private final String nullableDescription, nullableDefault;
+    private final String nullableDescription, nullableDefault, nullableDefaultWrappedForCode;
     private final TypeProviderWrapper typeProviderWrapper;
     private final TypeDefinition<?> typeDefinition;
 
@@ -33,6 +33,7 @@ public class JavaAttribute extends AbstractAttribute implements TypedAttribute {
         this.typeDefinition = leaf.getType();
         this.typeProviderWrapper = typeProviderWrapper;
         this.nullableDefault = leaf.getDefault();
+        this.nullableDefaultWrappedForCode = leaf.getDefault() == null ? null : typeProviderWrapper.getDefault(leaf);
         this.nullableDescription = leaf.getDescription();
     }
 
@@ -42,10 +43,14 @@ public class JavaAttribute extends AbstractAttribute implements TypedAttribute {
         this.type = typeProviderWrapper.getType(leaf);
         this.typeDefinition = leaf.getType();
         this.typeProviderWrapper = typeProviderWrapper;
-        this.nullableDefault = null;
+        this.nullableDefault = nullableDefaultWrappedForCode = null;
         this.nullableDescription = leaf.getDescription();
     }
 
+    public TypeDefinition<?> getTypeDefinition() {
+        return typeDefinition;
+    }
+
     /**
      * Returns the most base type
      */
@@ -56,6 +61,10 @@ public class JavaAttribute extends AbstractAttribute implements TypedAttribute {
         return baseType;
     }
 
+    public String getNullableDefaultWrappedForCode() {
+        return nullableDefaultWrappedForCode;
+    }
+
     @Override
     public Type getType() {
         return type;
index bd83a4c..9ad7a44 100644 (file)
@@ -48,28 +48,40 @@ module config-test-impl {
            container dto-a {
                 leaf simple-arg {
                     type uint32;
+                    default 1;
                 }
 
                 leaf port {
                     type inet:port-number;
+                    default 8080;
                 }
 
+                leaf ip4 {
+                    type inet:ipv4-address;
+                    default 127.0.0.1;
+                }
+
+                leaf ip {
+                    type inet:ip-address;
+                    // TODO defaults for union default 0:0:0:0:0:0:0:1;
+                }
             }
 
             leaf as-number {
-                mandatory true;
                 type inet:as-number;
+                default 44;
             }
 
 
             leaf simpleInt {
                 type uint32;
-                default 99L;
+                default 99;
             }
 
             container dto_b {
                 leaf simple-int1 {
                     type uint32;
+                    default 32;
                 }
 
                 leaf simple-int2 {
@@ -101,28 +113,34 @@ module config-test-impl {
             when "/config:modules/config:module/config:type = 'impl-netconf'";
             leaf binaryLeaf {
                 type binary;
+                default ZGVmYXVsdEJpbg==;
             }
 
             leaf type {
                 type string;
+                default "default-string";
             }
 
             leaf extended {
                 type tt:extend-once;
+                default 1;
             }
 
             leaf extended-twice {
                 type tt:extend-twice;
+                default 2;
             }
 
             leaf extended-enum {
                 type tt:extend-enum;
+                default ONE;
             }
 
             leaf sleep-factor {
                 type decimal64 {
                     fraction-digits 2;
                 }
+                default 2.00;
             }
 
            container dto-c {
@@ -153,23 +171,28 @@ module config-test-impl {
             }
 
             leaf simple-long {
-                type int64  ;
+                type int64;
+                default -45;
             }
 
             leaf simple-long-2 {
                 type uint32;
+                default 445;
             }
 
             leaf simple-BigInteger {
                 type uint64;
+                default 545454;
             }
 
             leaf simple-byte {
                 type int8;
+                default -4;
             }
 
             leaf simple-short {
                 type uint8;
+                default 45;
             }
 
             leaf simple-test {
@@ -209,6 +232,7 @@ module config-test-impl {
                     container deep {
                         leaf simple-int3 {
                             type uint16;
+                            default 0;
                         }
                     }
                 }
@@ -393,6 +417,7 @@ module config-test-impl {
             container retValContainer {
                 leaf v1 {
                     type string;
+                    default "from rpc";
                 }
 
                 leaf v2 {
index d855f15..888f13c 100644 (file)
                             org.osgi.framework,
                             org.osgi.util.tracker,
                             org.slf4j,
-                            org.w3c.dom
+                            org.w3c.dom,
+                            com.google.common.io,
+                            org.opendaylight.yangtools.yang.model.api.type
                         </Import-Package>
                         <Export-Package>
                         </Export-Package>
index 2b6f862..697b811 100644 (file)
@@ -14,6 +14,7 @@ import org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribu
 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListAttribute;
 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListDependenciesAttribute;
 import org.opendaylight.controller.config.yangjmxgenerator.attribute.TOAttribute;
+import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
 
 import javax.management.openmbean.ArrayType;
 import javax.management.openmbean.CompositeType;
@@ -30,7 +31,10 @@ public abstract class AttributeIfcSwitchStatement<T> {
 
         if (attributeIfc instanceof JavaAttribute) {
             try {
-                return caseJavaAttribute(attributeIfc.getOpenType());
+                if(((JavaAttribute)attributeIfc).getTypeDefinition() instanceof BinaryTypeDefinition) {
+                    return caseJavaBinaryAttribute(attributeIfc.getOpenType());
+                } else
+                    return caseJavaAttribute(attributeIfc.getOpenType());
             } catch (UnknownOpenTypeException e) {
                 throw getIllegalArgumentException(attributeIfc);
             }
@@ -48,6 +52,9 @@ public abstract class AttributeIfcSwitchStatement<T> {
         throw getIllegalArgumentException(attributeIfc);
     }
 
+    protected T caseJavaBinaryAttribute(OpenType<?> openType) {
+        return caseJavaAttribute(openType);
+    }
 
     private IllegalArgumentException getIllegalArgumentException(AttributeIfc attributeIfc) {
         return new IllegalArgumentException("Unknown attribute type " + attributeIfc.getClass() + ", " + attributeIfc
index 867d94e..7939112 100644 (file)
@@ -27,11 +27,14 @@ public abstract class AbstractAttributeReadingStrategy implements AttributeReadi
     @Override
     public AttributeConfigElement readElement(List<XmlElement> configNodes) {
         if (configNodes.size() == 0)
-            return AttributeConfigElement.createNullValue(nullableDefault);
+            return AttributeConfigElement.createNullValue(postprocessNullableDefault(nullableDefault));
 
         return readElementHook(configNodes);
     }
 
     abstract AttributeConfigElement readElementHook(List<XmlElement> configNodes);
 
+    protected Object postprocessNullableDefault(String nullableDefault) {
+        return nullableDefault;
+    }
 }
index 598935a..a1f46dd 100644 (file)
@@ -9,7 +9,6 @@
 package org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml;
 
 import com.google.common.base.Optional;
-import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc;
 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.resolving.AttributeResolvingStrategy;
 
 import javax.management.openmbean.OpenType;
@@ -47,24 +46,14 @@ public class AttributeConfigElement {
 
     }
 
-    public static AttributeConfigElement create(AttributeIfc attributeIfc, Object value) {
-        String nullableDefault = attributeIfc.getNullableDefault();
-        return create(nullableDefault, value);
-    }
-
-    public static AttributeConfigElement create(String nullableDefault, Object value) {
+    public static AttributeConfigElement create(Object nullableDefault, Object value) {
         return new AttributeConfigElement(nullableDefault, value);
     }
 
-    public static AttributeConfigElement createNullValue(AttributeIfc attributeIfc) {
-        return new AttributeConfigElement(attributeIfc.getNullableDefault(), null);
-    }
-
-    public static AttributeConfigElement createNullValue(String nullableDefault) {
+    public static AttributeConfigElement createNullValue(Object nullableDefault) {
         return new AttributeConfigElement(nullableDefault, null);
     }
 
-
     public Object getValue() {
         return value;
     }
index e2ea404..a2691f2 100644 (file)
@@ -18,6 +18,7 @@ import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attrib
 
 import javax.management.openmbean.ArrayType;
 import javax.management.openmbean.CompositeType;
+import javax.management.openmbean.OpenType;
 import javax.management.openmbean.SimpleType;
 import java.util.Map;
 import java.util.Map.Entry;
@@ -41,6 +42,11 @@ public class ObjectXmlReader extends AttributeIfcSwitchStatement<AttributeReadin
         return switchAttribute(attributeIfc);
     }
 
+    @Override
+    protected AttributeReadingStrategy caseJavaBinaryAttribute(OpenType<?> openType) {
+        return new SimpleBinaryAttributeReadingStrategy(lastAttribute.getNullableDefault());
+    }
+
     @Override
     public AttributeReadingStrategy caseJavaSimpleAttribute(SimpleType<?> openType) {
         return new SimpleAttributeReadingStrategy(lastAttribute.getNullableDefault());
index be86a2a..a860524 100644 (file)
@@ -28,7 +28,13 @@ public class SimpleAttributeReadingStrategy extends AbstractAttributeReadingStra
         String textContent = xmlElement.getTextContent();
 
         Preconditions.checkNotNull(textContent, "This element should contain text %s", xmlElement);
-        return AttributeConfigElement.create(getNullableDefault(), postprocessParsedValue(textContent));
+        return AttributeConfigElement.create(postprocessNullableDefault(getNullableDefault()),
+                postprocessParsedValue(textContent));
+    }
+
+    @Override
+    protected Object postprocessNullableDefault(String nullableDefault) {
+        return nullableDefault;
     }
 
     protected Object postprocessParsedValue(String textContent) {
diff --git a/opendaylight/netconf/config-netconf-connector/src/main/java/org/opendaylight/controller/netconf/confignetconfconnector/mapping/attributes/fromxml/SimpleBinaryAttributeReadingStrategy.java b/opendaylight/netconf/config-netconf-connector/src/main/java/org/opendaylight/controller/netconf/confignetconfconnector/mapping/attributes/fromxml/SimpleBinaryAttributeReadingStrategy.java
new file mode 100644 (file)
index 0000000..2cac902
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml;
+
+import com.google.common.collect.Lists;
+import com.google.common.io.BaseEncoding;
+
+import java.util.List;
+
+public class SimpleBinaryAttributeReadingStrategy extends SimpleAttributeReadingStrategy {
+
+    public SimpleBinaryAttributeReadingStrategy(String nullableDefault) {
+        super(nullableDefault);
+    }
+
+    protected Object postprocessParsedValue(String textContent) {
+        BaseEncoding en = BaseEncoding.base64();
+        byte[] decode = en.decode(textContent);
+        List<String> parsed = Lists.newArrayListWithCapacity(decode.length);
+        for (byte b : decode) {
+            parsed.add(Byte.toString(b));
+        }
+        return parsed;
+    }
+
+    @Override
+    protected Object postprocessNullableDefault(String nullableDefault) {
+        return nullableDefault == null ? null : postprocessParsedValue(nullableDefault);
+    }
+}
index 9249ac9..22c9e01 100644 (file)
@@ -14,7 +14,6 @@ import java.util.HashMap;
 
 public class SimpleCompositeAttributeReadingStrategy extends SimpleAttributeReadingStrategy {
 
-
     private final String key;
 
     public SimpleCompositeAttributeReadingStrategy(String nullableDefault, String key) {
@@ -28,4 +27,8 @@ public class SimpleCompositeAttributeReadingStrategy extends SimpleAttributeRead
         return map;
     }
 
+    @Override
+    protected Object postprocessNullableDefault(String nullableDefault) {
+        return nullableDefault == null ? null : postprocessParsedValue(nullableDefault);
+    }
 }
index a85f306..c477821 100644 (file)
@@ -70,7 +70,7 @@ final class CompositeAttributeResolvingStrategy extends
                     parsedInnerValue.isPresent() ? parsedInnerValue.get() : null);
         }
 
-        CompositeDataSupport parsedValue = null;
+        CompositeDataSupport parsedValue;
         try {
             parsedValue = new CompositeDataSupport(getOpenType(), items);
         } catch (OpenDataException e) {
index ad587ea..a174e9a 100644 (file)
@@ -19,6 +19,7 @@ import org.w3c.dom.Document;
 
 import javax.management.openmbean.ArrayType;
 import javax.management.openmbean.CompositeType;
+import javax.management.openmbean.OpenType;
 import javax.management.openmbean.SimpleType;
 import java.util.Map;
 import java.util.Map.Entry;
@@ -51,6 +52,11 @@ public class ObjectXmlWriter extends AttributeIfcSwitchStatement<AttributeWritin
         return switchAttribute(expectedAttr);
     }
 
+    @Override
+    protected AttributeWritingStrategy caseJavaBinaryAttribute(OpenType<?> openType) {
+        return new SimpleBinaryAttributeWritingStrategy(document, key);
+    }
+
     @Override
     protected AttributeWritingStrategy caseJavaSimpleAttribute(SimpleType<?> openType) {
         return new SimpleAttributeWritingStrategy(document, key);
diff --git a/opendaylight/netconf/config-netconf-connector/src/main/java/org/opendaylight/controller/netconf/confignetconfconnector/mapping/attributes/toxml/SimpleBinaryAttributeWritingStrategy.java b/opendaylight/netconf/config-netconf-connector/src/main/java/org/opendaylight/controller/netconf/confignetconfconnector/mapping/attributes/toxml/SimpleBinaryAttributeWritingStrategy.java
new file mode 100644 (file)
index 0000000..c159e46
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.toxml;
+
+import com.google.common.base.Preconditions;
+import com.google.common.io.BaseEncoding;
+import org.opendaylight.controller.netconf.confignetconfconnector.util.Util;
+import org.w3c.dom.Document;
+
+import java.util.List;
+
+public class SimpleBinaryAttributeWritingStrategy extends SimpleAttributeWritingStrategy {
+
+    /**
+     * @param document
+     * @param key
+     */
+    public SimpleBinaryAttributeWritingStrategy(Document document, String key) {
+        super(document, key);
+    }
+
+    protected Object preprocess(Object value) {
+        Util.checkType(value, List.class);
+        BaseEncoding en = BaseEncoding.base64();
+
+        List<?> list = (List<?>) value;
+        byte[] decoded = new byte[list.size()];
+        int i = 0;
+        for (Object bAsStr : list) {
+            Preconditions.checkArgument(bAsStr instanceof String, "Unexpected inner value for %s, expected string", value);
+            byte b = Byte.parseByte((String) bAsStr);
+            decoded[i++] = b;
+        }
+
+        return en.encode(decoded);
+    }
+
+}
index 35fa042..f8916ec 100644 (file)
@@ -55,7 +55,6 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
-import org.w3c.dom.NodeList;
 import org.xml.sax.SAXException;
 
 import javax.management.InstanceAlreadyExistsException;
@@ -125,6 +124,7 @@ public class NetconfMappingTest extends AbstractConfigTest {
         edit("netconfMessages/editConfig.xml");
         checkBinaryLeafEdited(getConfigCandidate());
 
+
         // default-operation:none, should not affect binary leaf
         edit("netconfMessages/editConfig_none.xml");
         checkBinaryLeafEdited(getConfigCandidate());
@@ -132,7 +132,6 @@ public class NetconfMappingTest extends AbstractConfigTest {
         // check after edit
         commit();
         Element response = getConfigRunning();
-        System.err.println(XmlUtil.toString(response));
 
         checkBinaryLeafEdited(response);
         checkTypeConfigAttribute(response);
@@ -165,6 +164,15 @@ public class NetconfMappingTest extends AbstractConfigTest {
         verifyNoMoreInteractions(netconfOperationRouter);
     }
 
+    private void checkBigDecimal(Element response) {
+        String responseTrimmed = XmlUtil.toString(response).replaceAll("\\s", "");
+
+        assertContainsString(responseTrimmed, "<sleep-factorxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2.58</sleep-factor>");
+        // Default
+        assertContainsString(responseTrimmed, "<sleep-factorxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2.00</sleep-factor>");
+
+    }
+
     private void closeSession() throws NetconfDocumentedException, ParserConfigurationException, SAXException,
             IOException {
         DefaultCloseSession closeOp = new DefaultCloseSession(NETCONF_SESSION_ID);
@@ -232,14 +240,9 @@ public class NetconfMappingTest extends AbstractConfigTest {
             edit("netconfMessages/namespaces/editConfig_sameAttrDifferentNamespaces.xml");
         } catch (NetconfDocumentedException e) {
             String message = e.getMessage();
-            assertThat(message,
-                    JUnitMatchers
-                            .containsString("Element simple-long-2 present multiple times with different namespaces"));
-            assertThat(message,
-                    JUnitMatchers.containsString("urn:opendaylight:params:xml:ns:yang:controller:test:impl"));
-            assertThat(message,
-                    JUnitMatchers
-                            .containsString(XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG));
+            assertContainsString(message, "Element simple-long-2 present multiple times with different namespaces");
+            assertContainsString(message, "urn:opendaylight:params:xml:ns:yang:controller:test:impl");
+            assertContainsString(message, XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
             throw e;
         }
     }
@@ -250,9 +253,9 @@ public class NetconfMappingTest extends AbstractConfigTest {
             edit("netconfMessages/namespaces/editConfig_differentNamespaceTO.xml");
         } catch (NetconfDocumentedException e) {
             String message = e.getMessage();
-            assertThat(message, JUnitMatchers.containsString("Unrecognised elements"));
-            assertThat(message, JUnitMatchers.containsString("simple-int2"));
-            assertThat(message, JUnitMatchers.containsString("dto_d"));
+            assertContainsString(message, "Unrecognised elements");
+            assertContainsString(message, "simple-int2");
+            assertContainsString(message, "dto_d");
             throw e;
         }
     }
@@ -263,13 +266,9 @@ public class NetconfMappingTest extends AbstractConfigTest {
             edit("netconfMessages/namespaces/editConfig_sameAttrDifferentNamespacesList.xml");
         } catch (NetconfDocumentedException e) {
             String message = e.getMessage();
-            assertThat(message,
-                    JUnitMatchers.containsString("Element binaryLeaf present multiple times with different namespaces"));
-            assertThat(message,
-                    JUnitMatchers.containsString("urn:opendaylight:params:xml:ns:yang:controller:test:impl"));
-            assertThat(message,
-                    JUnitMatchers
-                            .containsString(XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG));
+            assertContainsString(message, "Element binaryLeaf present multiple times with different namespaces");
+            assertContainsString(message, "urn:opendaylight:params:xml:ns:yang:controller:test:impl");
+            assertContainsString(message, XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
             throw e;
         }
     }
@@ -306,8 +305,8 @@ public class NetconfMappingTest extends AbstractConfigTest {
             try {
                 edit(file);
             } catch (NetconfDocumentedException e) {
-                assertThat(e.getMessage(), JUnitMatchers.containsString("Unrecognised elements"));
-                assertThat(e.getMessage(), JUnitMatchers.containsString("unknownAttribute"));
+                assertContainsString(e.getMessage(), "Unrecognised elements");
+                assertContainsString(e.getMessage(), "unknownAttribute");
                 continue;
             }
             fail("Unrecognised test should throw exception " + file);
@@ -353,22 +352,37 @@ public class NetconfMappingTest extends AbstractConfigTest {
     }
 
     private void checkBinaryLeafEdited(final Element response) {
-        final NodeList children = response.getElementsByTagName("binaryLeaf");
-        assertEquals(3, children.getLength());
-        final StringBuffer buf = new StringBuffer();
-        for (int i = 0; i < 3; i++) {
-            final Element e = (Element) children.item(i);
-            buf.append(XmlElement.fromDomElement(e).getTextContent());
-        }
-        assertEquals("810", buf.toString());
+        String responseTrimmed = XmlUtil.toString(response).replaceAll("\\s", "");
+        String substring = "<binaryLeafxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">YmluYXJ5</binaryLeaf>";
+        assertContainsString(responseTrimmed, substring);
+        substring = "<binaryLeafxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">ZGVmYXVsdEJpbg==</binaryLeaf>";
+        assertContainsString(responseTrimmed, substring);
     }
 
     private void checkTypedefs(final Element response) {
-        NodeList children = response.getElementsByTagName("extended");
-        assertEquals(1, children.getLength());
+        String responseTrimmed = XmlUtil.toString(response).replaceAll("\\s", "");
+
+        String substring = "<extendedxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">10</extended>";
+        assertContainsString(responseTrimmed, substring);
+        // Default
+        assertContainsString(responseTrimmed,
+                "<extendedxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">1</extended>");
 
-        children = response.getElementsByTagName("extended-twice");
-        assertEquals(1, children.getLength());
+        assertContainsString(responseTrimmed,
+                "<extended-twicexmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">20</extended-twice>");
+        // Default
+        assertContainsString(responseTrimmed,
+                "<extended-twicexmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2</extended-twice>");
+
+        assertContainsString(responseTrimmed,
+                "<extended-enumxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">TWO</extended-enum>");
+        // Default
+        assertContainsString(responseTrimmed,
+                "<extended-enumxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">ONE</extended-enum>");
+    }
+
+    private void assertContainsString(String string, String substring) {
+        assertThat(string, JUnitMatchers.containsString(substring));
     }
 
     private void checkEnum(final Element response) {
@@ -396,12 +410,6 @@ public class NetconfMappingTest extends AbstractConfigTest {
         assertEquals(2, testingDepsSize);
     }
 
-    private void checkBigDecimal(Element response) {
-        int size = response.getElementsByTagName("sleep-factor").getLength();
-        assertEquals(1, size);
-    }
-
-
     private void checkTypeConfigAttribute(Element response) {
 
         XmlElement modulesElement = XmlElement.fromDomElement(response).getOnlyChildElement("data")
@@ -458,17 +466,17 @@ public class NetconfMappingTest extends AbstractConfigTest {
         RuntimeRpc netconf = new RuntimeRpc(yangStoreSnapshot, configRegistryClient, NETCONF_SESSION_ID);
 
         response = executeOp(netconf, "netconfMessages/rpc.xml");
-        assertThat(XmlUtil.toString(response), JUnitMatchers.containsString("testarg1".toUpperCase()));
+        assertContainsString(XmlUtil.toString(response), "testarg1".toUpperCase());
 
         response = executeOp(netconf, "netconfMessages/rpcInner.xml");
-        assertThat(XmlUtil.toString(response), JUnitMatchers.containsString("ok"));
+        assertContainsString(XmlUtil.toString(response), "ok");
 
         response = executeOp(netconf, "netconfMessages/rpcInnerInner.xml");
-        assertThat(XmlUtil.toString(response), JUnitMatchers.containsString("true"));
+        assertContainsString(XmlUtil.toString(response), "true");
 
         response = executeOp(netconf, "netconfMessages/rpcInnerInner_complex_output.xml");
-        assertThat(XmlUtil.toString(response), JUnitMatchers.containsString("1"));
-        assertThat(XmlUtil.toString(response), JUnitMatchers.containsString("2"));
+        assertContainsString(XmlUtil.toString(response), "1");
+        assertContainsString(XmlUtil.toString(response), "2");
     }
 
     private Element get() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
index dc522d2..94b73f4 100644 (file)
                     <name>test1</name>
 
                     <sleep-factor>
-                        2.00
+                        2.58
                     </sleep-factor>
 
                     <extended>
-                            1
+                            10
                     </extended>
 
                     <extended-twice>
-                            1
+                            20
                     </extended-twice>
 
                     <extended-enum>
@@ -48,9 +48,8 @@
                     </extended-enum>
 
                     <simple-long-2>44</simple-long-2>
-                    <binaryLeaf>8</binaryLeaf>
-                    <binaryLeaf>1</binaryLeaf>
-                    <binaryLeaf>0</binaryLeaf>
+                    <binaryLeaf>YmluYXJ5</binaryLeaf>
+
                     <type xmlns="urn:opendaylight:params:xml:ns:yang:controller:test:impl">configAttributeType</type>
                     <dto_d xmlns="urn:opendaylight:params:xml:ns:yang:controller:test:impl">
                         <simple-int1>444</simple-int1>
index 388aa4f..b48730d 100644 (file)
@@ -27,9 +27,7 @@
                     </type>
                     <name>test1</name>
                     <simple-long-2>44</simple-long-2>
-                    <binaryLeaf>8</binaryLeaf>
-                    <binaryLeaf>7</binaryLeaf>
-                    <binaryLeaf>9</binaryLeaf>
+                    <binaryLeaf>8ad1</binaryLeaf>
                     <dto_d>
                         <simple-int1>444</simple-int1>
                         <simple-int2>4444</simple-int2>
index 4d94d9e..df2a5e8 100644 (file)
@@ -32,9 +32,7 @@
                     <name>test1</name>
 
                     <simple-long-2>44</simple-long-2>
-                    <binaryLeaf>8</binaryLeaf>
-                    <binaryLeaf>1</binaryLeaf>
-                    <binaryLeaf>0</binaryLeaf>
+                    <binaryLeaf>8545649856</binaryLeaf>
                     <type xmlns="urn:opendaylight:params:xml:ns:yang:controller:test:impl">configAttributeType</type>
                     <dto_d>
                         <simple-int1>444</simple-int1>
index 0e70e13..02aca8d 100644 (file)
@@ -32,9 +32,7 @@
                     <name>test1</name>
 
                     <simple-long-2>44</simple-long-2>
-                    <binaryLeaf>8</binaryLeaf>
-                    <binaryLeaf>1</binaryLeaf>
-                    <binaryLeaf>0</binaryLeaf>
+                    <binaryLeaf>8545649856</binaryLeaf>
                     <dto_d xmlns="urn:opendaylight:params:xml:ns:yang:controller:test:impl">
                         <simple-int1>444</simple-int1>
                         <simple-int2>4444</simple-int2>
index d03bb08..825be6d 100644 (file)
@@ -32,9 +32,7 @@
                     <name>test1</name>
 
                     <simple-long-2>44</simple-long-2>
-                    <binaryLeaf>8</binaryLeaf>
-                    <binaryLeaf>1</binaryLeaf>
-                    <binaryLeaf>0</binaryLeaf>
+                    <binaryLeaf>8545649856</binaryLeaf>
                     <dto_d>
                         <unknownAttribute>error</unknownAttribute>
                         <simple-int1>444</simple-int1>
index 3722973..9ef2bed 100644 (file)
@@ -32,9 +32,7 @@
                     <name>test1</name>
 
                     <simple-long-2>44</simple-long-2>
-                    <binaryLeaf>8</binaryLeaf>
-                    <binaryLeaf>1</binaryLeaf>
-                    <binaryLeaf>0</binaryLeaf>
+                    <binaryLeaf>8545649856</binaryLeaf>
                     <dto_d>
                         <simple-int1>444</simple-int1>
                         <simple-int2>4444</simple-int2>