Merge "BUG-865: removed use of deprecated code in yang-parser-impl tests."
authorTony Tkacik <ttkacik@cisco.com>
Mon, 21 Jul 2014 09:15:25 +0000 (09:15 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Mon, 21 Jul 2014 09:15:25 +0000 (09:15 +0000)
43 files changed:
.gitignore
code-generator/binding-generator-impl/pom.xml
code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/BindingGeneratorImpl.java
code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/TransformerGenerator.xtend
code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/util/DefaultSourceCodeGenerator.java [new file with mode: 0644]
code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/util/JavassistUtils.java
code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/util/NullSourceCodeGenerator.java [new file with mode: 0644]
code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/util/SourceCodeGenerator.java [new file with mode: 0644]
code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/util/SourceCodeGeneratorFactory.java [new file with mode: 0644]
code-generator/binding-generator-util/src/main/java/org/opendaylight/yangtools/binding/generator/util/BindingGeneratorUtil.java
code-generator/binding-java-api-generator/pom.xml
code-generator/binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/BuilderTemplate.xtend
code-generator/binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/ClassTemplate.xtend
code-generator/binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/UnionTemplate.xtend
code-generator/binding-java-api-generator/src/test/java/org/opendaylight/yangtools/sal/java/api/generator/test/Bug1276Test.java [new file with mode: 0644]
code-generator/binding-java-api-generator/src/test/java/org/opendaylight/yangtools/sal/java/api/generator/test/Bug532Test.java [new file with mode: 0644]
code-generator/binding-java-api-generator/src/test/java/org/opendaylight/yangtools/sal/java/api/generator/test/CascadeUsesCompilationTest.java
code-generator/binding-java-api-generator/src/test/java/org/opendaylight/yangtools/sal/java/api/generator/test/CompilationTest.java
code-generator/binding-java-api-generator/src/test/java/org/opendaylight/yangtools/sal/java/api/generator/test/CompilationTestUtils.java
code-generator/binding-java-api-generator/src/test/java/org/opendaylight/yangtools/sal/java/api/generator/test/TypedefCompilationTest.java
code-generator/binding-java-api-generator/src/test/resources/compilation/bug1097/foo.yang [new file with mode: 0644]
code-generator/binding-java-api-generator/src/test/resources/compilation/bug1276/foo.yang [new file with mode: 0644]
code-generator/binding-java-api-generator/src/test/resources/compilation/bug1377/foo.yang [new file with mode: 0644]
code-generator/maven-sal-api-gen-plugin/pom.xml
code-generator/samples/maven-code-gen-sample/pom.xml
code-generator/samples/modeling-sample/pom.xml
common/features/src/main/resources/features.xml
common/parent/pom.xml
common/util/src/main/java/org/opendaylight/yangtools/util/MapAdaptor.java
model/pom.xml
yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/QName.java
yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/codec/xml/RandomPrefix.java
yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/codec/xml/XmlDocumentUtils.java
yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/codec/xml/XmlStreamUtils.java
yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/codec/xml/XmlUtils.java
yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/InMemoryDataTree.java
yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/codec/xml/RandomPrefixTest.java [new file with mode: 0644]
yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/TreeNodeUtilsTest.java [new file with mode: 0644]
yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/BaseTypes.java
yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/Leafref.java
yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/SchemaContextUtil.java
yang/yang-parser-impl/pom.xml
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/builder/impl/TypeUtils.java

index d4bca565221656f5c132aaafbbca22f079218dbc..3e8b88f5c0b6dae4377d6260df391bd2cc4a3ae1 100644 (file)
@@ -12,5 +12,5 @@ target
 *.iml
 .idea
 bin
-**/src/main/xtend-gen
+xtend-gen
 target
index fbf4098b0511f36d88e2e8595d5f2d339f8826e7..39a98ad1ef4b301259ebb139471da9e6763ed663 100644 (file)
             <plugin>
                 <groupId>org.eclipse.xtend</groupId>
                 <artifactId>xtend-maven-plugin</artifactId>
-                <executions>
-                    <execution>
-                        <goals>
-                            <goal>compile</goal>
-                        </goals>
-                        <configuration>
-                            <outputDirectory>${basedir}/src/main/xtend-gen</outputDirectory>
-                        </configuration>
-                    </execution>
-                </executions>
-            </plugin>
-            <plugin>
-                <artifactId>maven-clean-plugin</artifactId>
-                <configuration>
-                    <filesets>
-                        <fileset>
-                            <directory>${basedir}/src/main/xtend-gen</directory>
-                            <includes>
-                                <include>**</include>
-                            </includes>
-                        </fileset>
-                    </filesets>
-                </configuration>
             </plugin>
         </plugins>
     </build>
index 0e6a8e473cb217764e6d4ef7a712e69cbbc390ee..d7c5532af8fccec139ccf34df9d4d4730bc137e4 100644 (file)
@@ -25,9 +25,11 @@ import static org.opendaylight.yangtools.binding.generator.util.Types.VOID;
 import static org.opendaylight.yangtools.binding.generator.util.Types.typeForClass;
 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findDataSchemaNode;
 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findNodeInSchemaContext;
-import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findOriginal;
 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findParentModule;
 
+import com.google.common.base.Splitter;
+import com.google.common.collect.Iterables;
+import com.google.common.collect.Sets;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -36,7 +38,6 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
-
 import org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil;
 import org.opendaylight.yangtools.binding.generator.util.BindingTypes;
 import org.opendaylight.yangtools.binding.generator.util.ReferencedTypeImpl;
@@ -75,6 +76,7 @@ import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
@@ -101,10 +103,6 @@ import org.opendaylight.yangtools.yang.parser.util.ModuleDependencySort;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.base.Splitter;
-import com.google.common.collect.Iterables;
-import com.google.common.collect.Sets;
-
 public class BindingGeneratorImpl implements BindingGenerator {
     private static final Logger LOG = LoggerFactory.getLogger(BindingGeneratorImpl.class);
 
@@ -751,9 +749,11 @@ public class BindingGeneratorImpl implements BindingGenerator {
 
         targetSchemaNode = findDataSchemaNode(schemaContext, targetPath);
         if (targetSchemaNode instanceof DataSchemaNode && ((DataSchemaNode) targetSchemaNode).isAddedByUses()) {
-            targetSchemaNode = findOriginal((DataSchemaNode) targetSchemaNode, schemaContext);
+            if (targetSchemaNode instanceof DerivableSchemaNode) {
+                targetSchemaNode = ((DerivableSchemaNode) targetSchemaNode).getOriginal().orNull();
+            }
             if (targetSchemaNode == null) {
-                throw new NullPointerException("Failed to find target node from grouping in augmentation " + augSchema
+                throw new IllegalStateException("Failed to find target node from grouping in augmentation " + augSchema
                         + " in module " + module.getName());
             }
         }
@@ -1161,9 +1161,11 @@ public class BindingGeneratorImpl implements BindingGenerator {
                         SchemaNode targetSchemaNode = findDataSchemaNode(schemaContext, targetPath);
                         if (targetSchemaNode instanceof DataSchemaNode
                                 && ((DataSchemaNode) targetSchemaNode).isAddedByUses()) {
-                            targetSchemaNode = findOriginal((DataSchemaNode) targetSchemaNode, schemaContext);
+                            if (targetSchemaNode instanceof DerivableSchemaNode) {
+                                targetSchemaNode = ((DerivableSchemaNode) targetSchemaNode).getOriginal().orNull();
+                            }
                             if (targetSchemaNode == null) {
-                                throw new NullPointerException(
+                                throw new IllegalStateException(
                                         "Failed to find target node from grouping for augmentation " + augSchema
                                                 + " in module " + module.getName());
                             }
@@ -1174,6 +1176,9 @@ public class BindingGeneratorImpl implements BindingGenerator {
                         parent = findDataSchemaNode(schemaContext, sp.getParent());
                     }
                     GeneratedTypeBuilder childOfType = findChildNodeByPath(parent.getPath());
+                    if (childOfType == null) {
+                        childOfType = findGroupingByPath(parent.getPath());
+                    }
                     resolveDataSchemaNodes(module, basePackageName, caseTypeBuilder, childOfType, caseChildNodes);
                 }
             }
index 5d127d6c15cd2eaaf75b2e68fa47e82d4a83fd78..ab6b734c0bb540fdefb7bd5a94b1fa98c0cee7b8 100644 (file)
@@ -72,6 +72,9 @@ import static org.opendaylight.yangtools.sal.binding.generator.impl.CodecMapping
 
 import static extension org.opendaylight.yangtools.sal.binding.generator.util.YangSchemaUtils.*
 import java.util.ArrayList
+import org.opendaylight.yangtools.sal.binding.generator.util.DefaultSourceCodeGenerator
+import org.opendaylight.yangtools.sal.binding.generator.util.SourceCodeGeneratorFactory
+import org.opendaylight.yangtools.sal.binding.generator.util.SourceCodeGenerator
 
 class TransformerGenerator extends AbstractTransformerGenerator {
     private static val LOG = LoggerFactory.getLogger(TransformerGenerator)
@@ -90,6 +93,8 @@ class TransformerGenerator extends AbstractTransformerGenerator {
     val CtClass BINDING_CODEC
     val CtClass ctQName
 
+    val SourceCodeGeneratorFactory sourceCodeGeneratorFactory = new SourceCodeGeneratorFactory();
+
     public new(TypeResolver typeResolver, ClassPool pool) {
         super(typeResolver, pool)
 
@@ -328,17 +333,19 @@ class TransformerGenerator extends AbstractTransformerGenerator {
     private def generateKeyTransformerFor(Class<? extends Object> inputType, GeneratedType typeSpec, ListSchemaNode node) {
         try {
 
+            val SourceCodeGenerator sourceGenerator = sourceCodeGeneratorFactory.getInstance( null );
+
             //log.info("Generating DOM Codec for {} with {}", inputType, inputType.classLoader)
             val properties = typeSpec.allProperties;
             val ctCls = createClass(inputType.codecClassName) [
                 //staticField(Map,"AUGMENTATION_SERIALIZERS");
-                staticField(it, INSTANCE_IDENTIFIER_CODEC, BindingCodec)
-                staticField(it, IDENTITYREF_CODEC, BindingCodec)
-                staticQNameField(node.QName);
+                staticField(it, INSTANCE_IDENTIFIER_CODEC, BindingCodec, sourceGenerator)
+                staticField(it, IDENTITYREF_CODEC, BindingCodec, sourceGenerator)
+                staticQNameField(node.QName, sourceGenerator);
                 implementsType(BINDING_CODEC)
                 method(Object, "toDomStatic", #[QName, Object]) [
                     modifiers = PUBLIC + FINAL + STATIC
-                    bodyChecked = '''
+                    val body = '''
                         {
                             Â«QName.name» _resultName;
                             if($1 != null) {
@@ -357,10 +364,11 @@ class TransformerGenerator extends AbstractTransformerGenerator {
                             return ($r) java.util.Collections.singletonMap(_resultName,_childNodes);
                         }
                     '''
+                    setBodyChecked(body, sourceGenerator)
                 ]
                 method(Object, "fromDomStatic", #[QName, Object]) [
                     modifiers = PUBLIC + FINAL + STATIC
-                    bodyChecked = '''
+                    val body = '''
                         {
                             if($2 == null){
                                 return  null;
@@ -379,9 +387,10 @@ class TransformerGenerator extends AbstractTransformerGenerator {
                             return _value;
                         }
                     '''
+                    setBodyChecked(body, sourceGenerator)
                 ]
                 method(Object, "serialize", Object) [
-                    bodyChecked = '''
+                    val body = '''
                         {
                             java.util.Map.Entry _input =  (java.util.Map.Entry) $1;
                             Â«QName.name» _localQName = («QName.name») _input.getKey();
@@ -389,9 +398,10 @@ class TransformerGenerator extends AbstractTransformerGenerator {
                             return toDomStatic(_localQName,_keyValue);
                         }
                     '''
+                    setBodyChecked(body, sourceGenerator)
                 ]
                 method(Object, "deserialize", Object) [
-                    bodyChecked = '''
+                    val body = '''
                         {
                             Â«QName.name» _qname = QNAME;
                             if($1 instanceof java.util.Map.Entry) {
@@ -400,9 +410,11 @@ class TransformerGenerator extends AbstractTransformerGenerator {
                             return fromDomStatic(_qname,$1);
                         }
                     '''
+                    setBodyChecked(body, sourceGenerator)
                 ]
             ]
             val ret = ctCls.toClassImpl(inputType.classLoader, inputType.protectionDomain)
+            sourceGenerator.outputGeneratedSource( ctCls )
             LOG.debug("DOM Codec for {} was generated {}", inputType, ret)
             return ret as Class<? extends BindingCodec<Map<QName,Object>, ?>>;
         } catch (Exception e) {
@@ -414,17 +426,19 @@ class TransformerGenerator extends AbstractTransformerGenerator {
     private def Class<? extends BindingCodec<Object, Object>> generateCaseCodec(Class<?> inputType, GeneratedType type,
         ChoiceCaseNode node) {
         try {
+            val SourceCodeGenerator sourceGenerator = sourceCodeGeneratorFactory.getInstance( null );
+
             //log.info("Generating DOM Codec for {} with {}, TCCL is: {}", inputType, inputType.classLoader,Thread.currentThread.contextClassLoader)
             val ctCls = createClass(type.codecClassName) [
                 //staticField(Map,"AUGMENTATION_SERIALIZERS");
                 implementsType(BINDING_CODEC)
-                staticQNameField(node.QName);
-                staticField(it, INSTANCE_IDENTIFIER_CODEC, BindingCodec)
-                staticField(it, AUGMENTATION_CODEC, BindingCodec)
-                staticField(it, IDENTITYREF_CODEC, BindingCodec)
+                staticQNameField(node.QName, sourceGenerator);
+                staticField(it, INSTANCE_IDENTIFIER_CODEC, BindingCodec, sourceGenerator)
+                staticField(it, AUGMENTATION_CODEC, BindingCodec, sourceGenerator)
+                staticField(it, IDENTITYREF_CODEC, BindingCodec, sourceGenerator)
                 method(Object, "toDomStatic", #[QName, Object]) [
                     modifiers = PUBLIC + FINAL + STATIC
-                    bodyChecked = '''
+                    val body = '''
                         {
                             Â«QName.name» _resultName = Â«QName.name».create($1,QNAME.getLocalName());
                             java.util.List _childNodes = new java.util.ArrayList();
@@ -433,9 +447,10 @@ class TransformerGenerator extends AbstractTransformerGenerator {
                             return ($r) _childNodes;
                         }
                     '''
+                    setBodyChecked( body, sourceGenerator)
                 ]
                 method(Object, "serialize", Object) [
-                    bodyChecked = '''
+                    val body = '''
                         {
                             java.util.Map.Entry _input = (java.util.Map.Entry) $1;
                             Â«QName.name» _localName = QNAME;
@@ -445,23 +460,27 @@ class TransformerGenerator extends AbstractTransformerGenerator {
                             return toDomStatic(_localName,_input.getValue());
                         }
                     '''
+                    setBodyChecked( body, sourceGenerator)
                 ]
                 method(Object, "fromDomStatic", #[QName, Object, InstanceIdentifier]) [
                     modifiers = PUBLIC + FINAL + STATIC
-                    bodyChecked = deserializeBody(type, node, getBindingIdentifierByPath(node.path))
+                    setBodyChecked( deserializeBody(type, node, getBindingIdentifierByPath(node.path)),
+                                    sourceGenerator )
                 ]
                 method(Object, "deserialize", #[Object, InstanceIdentifier]) [
-                    bodyChecked = '''
+                    val body = '''
                         {
                             //System.out.println("«type.name»#deserialize: " +$1);
                             java.util.Map.Entry _input = (java.util.Map.Entry) $1;
                             return fromDomStatic((«QName.name»)_input.getKey(),_input.getValue(),$2);
                         }
                     '''
+                    setBodyChecked( body, sourceGenerator)
                 ]
             ]
 
             val ret = ctCls.toClassImpl(inputType.classLoader, inputType.protectionDomain)  as Class<? extends BindingCodec<Object, Object>>
+            sourceGenerator.outputGeneratedSource( ctCls )
             listener?.onDataContainerCodecCreated(inputType, ret);
             LOG.debug("DOM Codec for {} was generated {}", inputType, ret)
             return ret;
@@ -475,21 +494,24 @@ class TransformerGenerator extends AbstractTransformerGenerator {
         Class<?> inputType, GeneratedType typeSpec, SchemaNode node) {
         try {
 
+            val SourceCodeGenerator sourceGenerator = sourceCodeGeneratorFactory.getInstance( null );
+
             //log.info("Generating DOM Codec for {} with {}", inputType, inputType.classLoader)
             val ctCls = createClass(typeSpec.codecClassName) [
                 //staticField(Map,"AUGMENTATION_SERIALIZERS");
-                staticQNameField(node.QName);
-                staticField(it, INSTANCE_IDENTIFIER_CODEC, BindingCodec)
-                staticField(it, IDENTITYREF_CODEC, BindingCodec)
-                staticField(it, AUGMENTATION_CODEC, BindingCodec)
+                staticQNameField(node.QName, sourceGenerator);
+                staticField(it, INSTANCE_IDENTIFIER_CODEC, BindingCodec, sourceGenerator)
+                staticField(it, IDENTITYREF_CODEC, BindingCodec, sourceGenerator)
+                staticField(it, AUGMENTATION_CODEC, BindingCodec, sourceGenerator)
                 implementsType(BINDING_CODEC)
 
                 method(Object, "toDomStatic", #[QName, Object]) [
                     modifiers = PUBLIC + FINAL + STATIC
-                    bodyChecked = serializeBodyFacade(typeSpec, node)
+                    setBodyChecked( serializeBodyFacade(typeSpec, node), sourceGenerator )
                 ]
                 method(Object, "serialize", Object) [
-                    bodyChecked = '''
+                    val body = '''
                         {
                             java.util.Map.Entry _input = (java.util.Map.Entry) $1;
                             Â«QName.name» _localName = QNAME;
@@ -499,15 +521,17 @@ class TransformerGenerator extends AbstractTransformerGenerator {
                             return toDomStatic(_localName,_input.getValue());
                         }
                     '''
+                    setBodyChecked( body, sourceGenerator )
                 ]
 
                 method(Object, "fromDomStatic", #[QName, Object, InstanceIdentifier]) [
                     modifiers = PUBLIC + FINAL + STATIC
-                    bodyChecked = deserializeBody(typeSpec, node, getBindingIdentifierByPath(node.path))
+                    setBodyChecked( deserializeBody(typeSpec, node, getBindingIdentifierByPath(node.path)),
+                                    sourceGenerator )
                 ]
 
                 method(Object, "deserialize", #[Object, InstanceIdentifier]) [
-                    bodyChecked = '''
+                    val body = '''
                         {
                             Â«QName.name» _qname = QNAME;
                             if($1 instanceof java.util.Map.Entry) {
@@ -516,10 +540,14 @@ class TransformerGenerator extends AbstractTransformerGenerator {
                             return fromDomStatic(_qname,$1,$2);
                         }
                     '''
+                    setBodyChecked( body, sourceGenerator )
                 ]
             ]
 
             val ret = ctCls.toClassImpl(inputType.classLoader, inputType.protectionDomain) as Class<? extends BindingCodec<Map<QName,Object>, Object>>
+
+            sourceGenerator.outputGeneratedSource( ctCls )
+
             listener?.onDataContainerCodecCreated(inputType, ret);
             LOG.debug("DOM Codec for {} was generated {}", inputType, ret)
             return ret;
@@ -533,19 +561,21 @@ class TransformerGenerator extends AbstractTransformerGenerator {
         Class<?> inputType, GeneratedType type, AugmentationSchema node) {
         try {
 
+            val SourceCodeGenerator sourceGenerator = sourceCodeGeneratorFactory.getInstance( null );
+
             //log.info("Generating DOM Codec for {} with {}", inputType, inputType.classLoader)
             val properties = type.allProperties
             val ctCls = createClass(type.codecClassName) [
                 //staticField(Map,"AUGMENTATION_SERIALIZERS");
-                staticQNameField(node.augmentationQName);
-                staticField(it, INSTANCE_IDENTIFIER_CODEC, BindingCodec)
-                staticField(it, AUGMENTATION_CODEC, BindingCodec)
-                staticField(it, IDENTITYREF_CODEC, BindingCodec)
+                staticQNameField(node.augmentationQName, sourceGenerator);
+                staticField(it, INSTANCE_IDENTIFIER_CODEC, BindingCodec, sourceGenerator)
+                staticField(it, AUGMENTATION_CODEC, BindingCodec, sourceGenerator)
+                staticField(it, IDENTITYREF_CODEC, BindingCodec, sourceGenerator)
                 implementsType(BINDING_CODEC)
 
                 method(Object, "toDomStatic", #[QName, Object]) [
                     modifiers = PUBLIC + FINAL + STATIC
-                    bodyChecked = '''
+                    val body = '''
                         {
                             ////System.out.println("Qname " + $1);
                             ////System.out.println("Value " + $2);
@@ -560,23 +590,25 @@ class TransformerGenerator extends AbstractTransformerGenerator {
                             return ($r) _childNodes;
                         }
                     '''
+                    setBodyChecked( body, sourceGenerator )
                 ]
                 method(Object, "serialize", Object) [
-                    bodyChecked = '''
+                    val body = '''
                         {
-                        java.util.Map.Entry _input = (java.util.Map.Entry) $1;
-                        Â«QName.name» _localName = QNAME;
-                        if(_input.getKey() != null) {
-                            _localName = («QName.name») _input.getKey();
-                        }
-                        return toDomStatic(_localName,_input.getValue());
+                            java.util.Map.Entry _input = (java.util.Map.Entry) $1;
+                            Â«QName.name» _localName = QNAME;
+                            if(_input.getKey() != null) {
+                                _localName = («QName.name») _input.getKey();
+                            }
+                            return toDomStatic(_localName,_input.getValue());
                         }
                     '''
+                    setBodyChecked( body, sourceGenerator )
                 ]
 
                 method(Object, "fromDomStatic", #[QName, Object, InstanceIdentifier]) [
                     modifiers = PUBLIC + FINAL + STATIC
-                    bodyChecked = '''
+                    val body = '''
                         {
                             Â«QName.name» _localQName = QNAME;
 
@@ -598,16 +630,21 @@ class TransformerGenerator extends AbstractTransformerGenerator {
                             return _builder.build();
                         }
                     '''
+                    setBodyChecked( body, sourceGenerator )
                 ]
 
                 method(Object, "deserialize", #[Object, InstanceIdentifier]) [
-                    bodyChecked = '''
-                        return fromDomStatic(QNAME,$1,$2);
+                    val body = '''
+                        {
+                            return fromDomStatic(QNAME,$1,$2);
+                        }
                     '''
+                    setBodyChecked( body, sourceGenerator )
                 ]
             ]
 
             val ret = ctCls.toClassImpl(inputType.classLoader, inputType.protectionDomain) as Class<? extends BindingCodec<Map<QName,Object>, Object>>
+            sourceGenerator.outputGeneratedSource( ctCls )
             listener?.onDataContainerCodecCreated(inputType, ret);
             return ret;
         } catch (Exception e) {
@@ -620,18 +657,20 @@ class TransformerGenerator extends AbstractTransformerGenerator {
         Class<?> inputType, GeneratedType typeSpec, ChoiceNode node) {
         try {
 
+            val SourceCodeGenerator sourceGenerator = sourceCodeGeneratorFactory.getInstance( null );
+
             //log.info("Generating DOM Codec for {} with {}", inputType, inputType.classLoader)
             val ctCls = createClass(typeSpec.codecClassName) [
                 //staticField(Map,"AUGMENTATION_SERIALIZERS");
                 //staticQNameField(inputType);
-                staticField(it, INSTANCE_IDENTIFIER_CODEC, BindingCodec)
-                staticField(it, IDENTITYREF_CODEC, BindingCodec)
-                staticField(it, DISPATCH_CODEC, BindingCodec)
+                staticField(it, INSTANCE_IDENTIFIER_CODEC, BindingCodec, sourceGenerator)
+                staticField(it, IDENTITYREF_CODEC, BindingCodec, sourceGenerator)
+                staticField(it, DISPATCH_CODEC, BindingCodec, sourceGenerator)
                 //staticField(it,QNAME_TO_CASE_MAP,BindingCodec)
                 implementsType(BINDING_CODEC)
                 method(List, "toDomStatic", #[QName, Object]) [
                     modifiers = PUBLIC + FINAL + STATIC
-                    bodyChecked = '''
+                    val body = '''
                         {
                             if($2 == null) {
                                 return null;
@@ -645,15 +684,19 @@ class TransformerGenerator extends AbstractTransformerGenerator {
                             return («List.name») _ret;
                         }
                     '''
+                    setBodyChecked( body, sourceGenerator )
                 ]
                 method(Object, "serialize", Object) [
-                    bodyChecked = '''
-                        throw new Â«UnsupportedOperationException.name»("Direct invocation not supported.");
+                    val body = '''
+                        {
+                            throw new Â«UnsupportedOperationException.name»("Direct invocation not supported.");
+                        }
                     '''
+                    setBodyChecked( body, sourceGenerator )
                 ]
                 method(Object, "fromDomStatic", #[QName, Map, InstanceIdentifier]) [
                     modifiers = PUBLIC + FINAL + STATIC
-                    bodyChecked = '''
+                    val body = '''
                         {
                             if («DISPATCH_CODEC» == null) {
                                 throw new Â«IllegalStateException.name»("Implementation of codec was not initialized.");
@@ -661,15 +704,20 @@ class TransformerGenerator extends AbstractTransformerGenerator {
                             return Â«DISPATCH_CODEC».deserialize($2,$3);
                         }
                     '''
+                    setBodyChecked( body, sourceGenerator )
                 ]
                 method(Object, "deserialize", #[Object, InstanceIdentifier]) [
-                    bodyChecked = '''
-                        throw new Â«UnsupportedOperationException.name»("Direct invocation not supported.");
+                    val body = '''
+                        {
+                            throw new Â«UnsupportedOperationException.name»("Direct invocation not supported.");
+                        }
                     '''
+                    setBodyChecked( body, sourceGenerator )
                 ]
             ]
 
             val rawRet = ctCls.toClassImpl(inputType.classLoader, inputType.protectionDomain)
+            sourceGenerator.outputGeneratedSource( ctCls )
             val ret = rawRet as Class<? extends BindingCodec<Map<QName,Object>, Object>>;
             listener?.onChoiceCodecCreated(inputType, ret, node);
             LOG.debug("DOM Codec for {} was generated {}", inputType, ret)
@@ -908,10 +956,13 @@ class TransformerGenerator extends AbstractTransformerGenerator {
         Class<?> inputType, GeneratedTransferObject typeSpec, TypeDefinition<?> typeDef) {
         try {
 
+            val SourceCodeGenerator sourceGenerator = sourceCodeGeneratorFactory.getInstance( null );
+
             val returnType = typeSpec.valueReturnType;
             if (returnType == null) {
-                val ctCls = createDummyImplementation(inputType, typeSpec);
+                val ctCls = createDummyImplementation(inputType, typeSpec, sourceGenerator);
                 val ret = ctCls.toClassImpl(inputType.classLoader, inputType.protectionDomain)
+                sourceGenerator.outputGeneratedSource( ctCls )
                 return ret as Class<? extends BindingCodec<Map<QName,Object>, Object>>;
             }
 
@@ -919,14 +970,14 @@ class TransformerGenerator extends AbstractTransformerGenerator {
                 //staticField(Map,"AUGMENTATION_SERIALIZERS");
                 if (inputType.isYangBindingAvailable) {
                     implementsType(BINDING_CODEC)
-                    staticField(it, INSTANCE_IDENTIFIER_CODEC, BindingCodec)
-                    staticField(it, IDENTITYREF_CODEC, BindingCodec)
+                    staticField(it, INSTANCE_IDENTIFIER_CODEC, BindingCodec, sourceGenerator)
+                    staticField(it, IDENTITYREF_CODEC, BindingCodec, sourceGenerator)
                     implementsType(BindingDeserializer.asCtClass)
                 }
                 method(Object, "toDomValue", Object) [
                     modifiers = PUBLIC + FINAL + STATIC
                     val ctSpec = typeSpec.asCtClass;
-                    bodyChecked = '''
+                    val body = '''
                         {
                             ////System.out.println("«inputType.simpleName»#toDomValue: "+$1);
 
@@ -941,17 +992,19 @@ class TransformerGenerator extends AbstractTransformerGenerator {
                             return _domValue;
                         }
                     '''
+                    setBodyChecked( body, sourceGenerator )
                 ]
                 method(Object, "serialize", Object) [
-                    bodyChecked = '''
+                    val body = '''
                         {
                             return toDomValue($1);
                         }
                     '''
+                    setBodyChecked( body, sourceGenerator )
                 ]
                 method(Object, "fromDomValue", Object) [
                     modifiers = PUBLIC + FINAL + STATIC
-                    bodyChecked = '''
+                    val body = '''
                         {
                             ////System.out.println("«inputType.simpleName»#fromDomValue: "+$1);
 
@@ -963,16 +1016,20 @@ class TransformerGenerator extends AbstractTransformerGenerator {
                             return _value;
                         }
                     '''
+                    setBodyChecked( body, sourceGenerator )
                 ]
                 method(Object, "deserialize", Object) [
-                    bodyChecked = '''{
+                    val body = '''
+                        {
                             return fromDomValue($1);
-                    }
+                        }
                     '''
+                    setBodyChecked( body, sourceGenerator )
                 ]
             ]
 
             val ret = ctCls.toClassImpl(inputType.classLoader, inputType.protectionDomain)
+            sourceGenerator.outputGeneratedSource( ctCls )
             LOG.debug("DOM Codec for {} was generated {}", inputType, ret)
             return ret as Class<? extends BindingCodec<Map<QName,Object>, Object>>;
         } catch (Exception e) {
@@ -986,20 +1043,22 @@ class TransformerGenerator extends AbstractTransformerGenerator {
     private def dispatch Class<? extends BindingCodec<Map<QName, Object>, Object>> generateValueTransformer(
         Class<?> inputType, GeneratedTransferObject typeSpec, UnionTypeDefinition typeDef) {
         try {
+            val SourceCodeGenerator sourceGenerator = sourceCodeGeneratorFactory.getInstance( null );
+
             val ctCls = createClass(typeSpec.codecClassName) [
                 val properties = typeSpec.allProperties;
                 val getterToTypeDefinition = XtendHelper.getTypes(typeDef).toMap[type|type.QName.getterName];
                 //staticField(Map,"AUGMENTATION_SERIALIZERS");
                 if (inputType.isYangBindingAvailable) {
                     implementsType(BINDING_CODEC)
-                    staticField(it, INSTANCE_IDENTIFIER_CODEC, BindingCodec)
-                    staticField(it, IDENTITYREF_CODEC, BindingCodec)
+                    staticField(it, INSTANCE_IDENTIFIER_CODEC, BindingCodec, sourceGenerator)
+                    staticField(it, IDENTITYREF_CODEC, BindingCodec, sourceGenerator)
                     implementsType(BindingDeserializer.asCtClass)
                 }
                 method(Object, "toDomValue", Object) [
                     modifiers = PUBLIC + FINAL + STATIC
                     val ctSpec = inputType.asCtClass;
-                    bodyChecked = '''
+                    val body = '''
                         {
                             ////System.out.println("«inputType.simpleName»#toDomValue: "+$1);
 
@@ -1021,17 +1080,19 @@ class TransformerGenerator extends AbstractTransformerGenerator {
                             return null;
                         }
                     '''
+                    setBodyChecked( body, sourceGenerator )
                 ]
                 method(Object, "serialize", Object) [
-                    bodyChecked = '''
+                    val body = '''
                         {
                             return toDomValue($1);
                         }
                     '''
+                    setBodyChecked( body, sourceGenerator )
                 ]
                 method(Object, "fromDomValue", Object) [
                     modifiers = PUBLIC + FINAL + STATIC
-                    bodyChecked = '''
+                    val body = '''
                         {
                             ////System.out.println("«inputType.simpleName»#fromDomValue: "+$1);
 
@@ -1045,16 +1106,20 @@ class TransformerGenerator extends AbstractTransformerGenerator {
                             return null;
                         }
                     '''
+                    setBodyChecked( body, sourceGenerator )
                 ]
                 method(Object, "deserialize", Object) [
-                    bodyChecked = '''{
+                    val body = '''
+                        {
                             return fromDomValue($1);
-                    }
+                        }
                     '''
+                    setBodyChecked( body, sourceGenerator )
                 ]
             ]
 
             val ret = ctCls.toClassImpl(inputType.classLoader, inputType.protectionDomain)
+            sourceGenerator.outputGeneratedSource( ctCls )
             LOG.debug("DOM Codec for {} was generated {}", inputType, ret)
             return ret as Class<? extends BindingCodec<Map<QName,Object>, Object>>;
         } catch (Exception e) {
@@ -1068,18 +1133,20 @@ class TransformerGenerator extends AbstractTransformerGenerator {
     private def dispatch Class<? extends BindingCodec<Map<QName, Object>, Object>> generateValueTransformer(
         Class<?> inputType, GeneratedTransferObject typeSpec, BitsTypeDefinition typeDef) {
         try {
+            val SourceCodeGenerator sourceGenerator = sourceCodeGeneratorFactory.getInstance( null );
+
             val ctCls = createClass(typeSpec.codecClassName) [
                 //staticField(Map,"AUGMENTATION_SERIALIZERS");
                 if (inputType.isYangBindingAvailable) {
                     implementsType(BINDING_CODEC)
-                    staticField(it, INSTANCE_IDENTIFIER_CODEC, BindingCodec)
-                    staticField(it, IDENTITYREF_CODEC, BindingCodec)
+                    staticField(it, INSTANCE_IDENTIFIER_CODEC, BindingCodec, sourceGenerator)
+                    staticField(it, IDENTITYREF_CODEC, BindingCodec, sourceGenerator)
                     implementsType(BindingDeserializer.asCtClass)
                 }
                 method(Object, "toDomValue", Object) [
                     modifiers = PUBLIC + FINAL + STATIC
                     val ctSpec = typeSpec.asCtClass;
-                    bodyChecked = '''
+                    val body = '''
                         {
                             ////System.out.println("«inputType.simpleName»#toDomValue: "+$1);
 
@@ -1102,21 +1169,20 @@ class TransformerGenerator extends AbstractTransformerGenerator {
                             return _domValue;
                         }
                     '''
+                    setBodyChecked( body, sourceGenerator )
                 ]
                 method(Object, "serialize", Object) [
-                    bodyChecked = '''
+                    val body = '''
                         {
                             return toDomValue($1);
                         }
                     '''
+                    setBodyChecked( body, sourceGenerator )
                 ]
                 method(Object, "fromDomValue", Object) [
                     modifiers = PUBLIC + FINAL + STATIC
-                    val sortedBits = new ArrayList(typeDef.bits)
-                    Collections.sort(sortedBits, [o1, o2|
-                        o1.propertyName.compareTo(o2.propertyName)
-                    ])
-                    bodyChecked = '''
+                    val sortedBits = typeDef.bits.sort[o1, o2|o1.propertyName.compareTo(o2.propertyName)]
+                    val body = '''
                         {
                             //System.out.println("«inputType.simpleName»#fromDomValue: "+$1);
 
@@ -1131,16 +1197,20 @@ class TransformerGenerator extends AbstractTransformerGenerator {
                             return new Â«inputType.resolvedName»(«FOR bit : sortedBits SEPARATOR ","»«bit.propertyName»«ENDFOR»);
                         }
                     '''
+                    setBodyChecked( body, sourceGenerator )
                 ]
                 method(Object, "deserialize", Object) [
-                    bodyChecked = '''{
+                    val body = '''
+                        {
                             return fromDomValue($1);
-                    }
+                        }
                     '''
+                    setBodyChecked( body, sourceGenerator )
                 ]
             ]
 
             val ret = ctCls.toClassImpl(inputType.classLoader, inputType.protectionDomain)
+            sourceGenerator.outputGeneratedSource( ctCls )
             LOG.debug("DOM Codec for {} was generated {}", inputType, ret)
             return ret as Class<? extends BindingCodec<Map<QName,Object>, Object>>;
         } catch (Exception e) {
@@ -1170,42 +1240,48 @@ class TransformerGenerator extends AbstractTransformerGenerator {
         }
     }
 
-    private def createDummyImplementation(Class<?> object, GeneratedTransferObject typeSpec) {
+    private def createDummyImplementation(Class<?> object, GeneratedTransferObject typeSpec,
+                                          SourceCodeGenerator sourceGenerator ) {
         LOG.trace("Generating Dummy DOM Codec for {} with {}", object, object.classLoader)
         return createClass(typeSpec.codecClassName) [
             if (object.isYangBindingAvailable) {
                 implementsType(BINDING_CODEC)
-                staticField(it, INSTANCE_IDENTIFIER_CODEC, BindingCodec)
-                staticField(it, IDENTITYREF_CODEC, BindingCodec)
+                staticField(it, INSTANCE_IDENTIFIER_CODEC, BindingCodec, sourceGenerator)
+                staticField(it, IDENTITYREF_CODEC, BindingCodec, sourceGenerator)
                 implementsType(BindingDeserializer.asCtClass)
             }
             //implementsType(BindingDeserializer.asCtClass)
             method(Object, "toDomValue", Object) [
                 modifiers = PUBLIC + FINAL + STATIC
-                bodyChecked = '''{
-                    if($1 == null) {
-                        return null;
-                    }
-                    return $1.toString();
-
+                val body = '''
+                    {
+                        if($1 == null) {
+                            return null;
+                        }
+                        return $1.toString();
                     }'''
+                setBodyChecked( body, sourceGenerator )
             ]
             method(Object, "serialize", Object) [
-                bodyChecked = '''
+                val body = '''
                     {
                         return toDomValue($1);
                     }
                 '''
+                setBodyChecked( body, sourceGenerator )
             ]
             method(Object, "fromDomValue", Object) [
                 modifiers = PUBLIC + FINAL + STATIC
-                bodyChecked = '''return null;'''
+                val body = '''return null;'''
+                setBodyChecked( body, sourceGenerator )
             ]
             method(Object, "deserialize", Object) [
-                bodyChecked = '''{
+                val body = '''
+                    {
                         return fromDomValue($1);
                     }
                     '''
+                setBodyChecked( body, sourceGenerator )
             ]
         ]
     }
@@ -1233,13 +1309,16 @@ class TransformerGenerator extends AbstractTransformerGenerator {
         }
         val enumSchema = enumSchemaType;
         try {
+            val SourceCodeGenerator sourceGenerator = sourceCodeGeneratorFactory.getInstance( null );
+
             //log.info("Generating DOM Codec for {} with {}", inputType, inputType.classLoader)
             val ctCls = createClass(typeSpec.codecClassName) [
                 //staticField(Map,"AUGMENTATION_SERIALIZERS");
                 //implementsType(BINDING_CODEC)
                 method(Object, "toDomValue", Object) [
                     modifiers = PUBLIC + FINAL + STATIC
-                    bodyChecked = '''{
+                    val body = '''
+                        {
                             if($1 == null) {
                                 return null;
                             }
@@ -1252,15 +1331,19 @@ class TransformerGenerator extends AbstractTransformerGenerator {
                             return null;
                         }
                     '''
+                    setBodyChecked( body, sourceGenerator )
                 ]
                 method(Object, "serialize", Object) [
-                    bodyChecked = '''
-                        return toDomValue($1);
+                    val body = '''
+                        {
+                            return toDomValue($1);
+                        }
                     '''
+                    setBodyChecked( body, sourceGenerator )
                 ]
                 method(Object, "fromDomValue", Object) [
                     modifiers = PUBLIC + FINAL + STATIC
-                    bodyChecked = '''
+                    val body = '''
                         {
                             if($1 == null) {
                                 return null;
@@ -1274,15 +1357,20 @@ class TransformerGenerator extends AbstractTransformerGenerator {
                             return null;
                         }
                     '''
+                    setBodyChecked( body, sourceGenerator )
                 ]
                 method(Object, "deserialize", Object) [
-                    bodyChecked = '''
-                        return fromDomValue($1);
+                    val body = '''
+                        {
+                            return fromDomValue($1);
+                        }
                     '''
+                    setBodyChecked( body, sourceGenerator )
                 ]
             ]
 
             val ret = ctCls.toClassImpl(inputType.classLoader, inputType.protectionDomain)
+            sourceGenerator.outputGeneratedSource( ctCls )
             LOG.debug("DOM Codec for {} was generated {}", inputType, ret)
             return ret;
         } catch (CodeGenerationException e) {
@@ -1347,11 +1435,13 @@ class TransformerGenerator extends AbstractTransformerGenerator {
     */
     private def getBuilderName(GeneratedType type) '''«type.resolvedName»Builder'''
 
-    private def staticQNameField(CtClass it, QName node) {
+    private def staticQNameField(CtClass it, QName node, SourceCodeGenerator sourceGenerator) {
         val field = new CtField(ctQName, "QNAME", it);
         field.modifiers = PUBLIC + FINAL + STATIC;
-        addField(field,
-            '''«QName.asCtClass.name».create("«node.namespace»","«node.formattedRevision»","«node.localName»")''')
+        val code = '''«QName.asCtClass.name».create("«node.namespace»","«node.formattedRevision»","«node.localName»")'''
+        addField(field, code )
+
+        sourceGenerator.appendField( field, code );
     }
 
     private def String serializeBodyImpl(GeneratedType type, DataNodeContainer nodeContainer) '''
@@ -1605,9 +1695,11 @@ class TransformerGenerator extends AbstractTransformerGenerator {
         throw exception;
     }
 
-    private def setBodyChecked(CtMethod method, String body) {
+    private def setBodyChecked(CtMethod method, String body, SourceCodeGenerator sourceGenerator ) {
         try {
             method.setBody(body);
+
+            sourceGenerator.appendMethod( method, body );
         } catch (CannotCompileException e) {
             LOG.error("Cannot compile method: {}#{} {}, Reason: {} Body: {}", method.declaringClass, method.name,
                 method.signature, e.message, body)
diff --git a/code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/util/DefaultSourceCodeGenerator.java b/code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/util/DefaultSourceCodeGenerator.java
new file mode 100644 (file)
index 0000000..5df91cd
--- /dev/null
@@ -0,0 +1,139 @@
+/*
+ * Copyright (c) 2014 Brocade Communications 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.yangtools.sal.binding.generator.util;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javassist.CtClass;
+import javassist.CtField;
+import javassist.CtMethod;
+import javassist.Modifier;
+import javassist.NotFoundException;
+
+/**
+ * The default implementation of the SourceCodeGenerator interface that generates readable source code
+ * for a runtime generated class. The appendField/appendMethod methods output source code to a temporary
+ * StringBuilder. When outputGeneratedSource is called, the entire class source code is generated and
+ * written to a file under a specified directory.
+ *
+ * @author Thomas Pantelis
+ */
+public class DefaultSourceCodeGenerator implements SourceCodeGenerator {
+    private static final Logger LOG = LoggerFactory.getLogger(DefaultSourceCodeGenerator.class);
+
+    private static final String GENERATED_SOURCE_DIR_PROP = "org.opendaylight.yangtools.sal.generatedCodecSourceDir";
+
+    private final StringBuilder builder = new StringBuilder();
+    private final String generatedSourceDir;
+
+    /**
+     * Constructor.
+     *
+     * @param generatedSourceDir the directory in which to put generated source files. If null, the directory
+     *     is obtained from a system property (<i>org.opendaylight.yangtools.sal.generatedCodecSourceDir</i>) or
+     *     defaults to "generated-codecs".
+     */
+    public DefaultSourceCodeGenerator( String generatedSourceDir ) {
+        if( generatedSourceDir != null ) {
+            this.generatedSourceDir = generatedSourceDir;
+        }
+        else {
+            this.generatedSourceDir = System.getProperty( GENERATED_SOURCE_DIR_PROP, "generated-codecs" );
+        }
+    }
+
+    @Override
+    public void appendField(CtField field, String value) {
+        try {
+            builder.append('\n')
+                    .append(Modifier.toString(field.getModifiers()))
+                    .append(' ').append(field.getType().getName()).append(' ')
+                    .append(field.getName());
+            if (value != null) {
+                builder.append(" = ").append(value);
+            }
+
+            builder.append(";\n");
+        } catch (NotFoundException e) {
+            LOG.error("Error building field source for " + field.getName(), e);
+        }
+    }
+
+    @Override
+    public void appendMethod(CtMethod method, String code) {
+        try {
+            builder.append('\n')
+                    .append(Modifier.toString(method.getModifiers()))
+                    .append(' ').append(method.getReturnType().getName())
+                    .append(' ').append(method.getName()).append("( ");
+
+            CtClass[] paramTypes = method.getParameterTypes();
+            if (paramTypes != null) {
+                for (int i = 0; i < paramTypes.length; i++) {
+                    if (i > 0)
+                        builder.append(", ");
+                    builder.append(paramTypes[i].getName()).append(" $")
+                            .append(i + 1);
+                }
+            }
+
+            builder.append(" )\n").append(code).append("\n\n");
+        } catch (NotFoundException e) {
+            LOG.error("Error building method source for " + method.getName(), e);
+        }
+    }
+
+    @Override
+    public void outputGeneratedSource(CtClass ctClass) {
+        String name = ctClass.getName();
+
+        StringBuilder classBuilder = new StringBuilder();
+        classBuilder.append(Modifier.toString(ctClass.getModifiers()))
+                .append(" class ").append(ctClass.getSimpleName());
+
+        try {
+            CtClass superClass = ctClass.getSuperclass();
+            if (superClass != null) {
+                classBuilder.append(" extends ").append(superClass.getName());
+            }
+
+            CtClass[] interfaces = ctClass.getInterfaces();
+            if (interfaces.length > 0) {
+                classBuilder.append(" implements ");
+                for (int i = 0; i < interfaces.length; i++) {
+                    if (i > 0) {
+                        classBuilder.append(", ");
+                    }
+
+                    classBuilder.append(interfaces[i].getName());
+                }
+            }
+
+            classBuilder.append(" {\n").append(builder.toString())
+                    .append("\n}");
+        } catch (NotFoundException e) {
+            LOG.error("Error building class source for " + name, e);
+            return;
+        }
+
+        File dir = new File(generatedSourceDir);
+        dir.mkdir();
+        try (FileWriter writer = new FileWriter(new File(dir, name + ".java"))) {
+            writer.append(classBuilder.toString());
+            writer.flush();
+        } catch (IOException e) {
+            LOG.error("Error writing class source for " + name, e);
+        }
+    }
+}
index 252fca7107953dff0ff04cc49882d48e334ee076..7f92e704ee08c2214530751cc54a0a26c5266e5a 100644 (file)
@@ -23,7 +23,6 @@ import javassist.CtMethod;
 import javassist.LoaderClassPath;
 import javassist.Modifier;
 import javassist.NotFoundException;
-
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -149,9 +148,20 @@ public final class JavassistUtils {
     }
 
     public CtField staticField(final CtClass it, final String name, final Class<? extends Object> returnValue) throws CannotCompileException {
+        return staticField(it, name, returnValue, null);
+    }
+
+    public CtField staticField(final CtClass it, final String name,
+                               final Class<? extends Object> returnValue,
+                               SourceCodeGenerator sourceGenerator) throws CannotCompileException {
         final CtField field = new CtField(asCtClass(returnValue), name, it);
         field.setModifiers(Modifier.PUBLIC + Modifier.STATIC);
         it.addField(field);
+
+        if (sourceGenerator != null) {
+            sourceGenerator.appendField(field, null);
+        }
+
         return field;
     }
 
diff --git a/code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/util/NullSourceCodeGenerator.java b/code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/util/NullSourceCodeGenerator.java
new file mode 100644 (file)
index 0000000..f087d54
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ * Copyright (c) 2014 Brocade Communications 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.yangtools.sal.binding.generator.util;
+
+import javassist.CtClass;
+import javassist.CtField;
+import javassist.CtMethod;
+
+/**
+ * Implementation of the SourceCodeGenerator interface that does nothing.
+ *
+ * @author Thomas Pantelis
+ */
+public class NullSourceCodeGenerator implements SourceCodeGenerator {
+
+    @Override
+    public void appendField( CtField field, String value ) {
+    }
+
+    @Override
+    public void appendMethod( CtMethod method, String code ) {
+    }
+
+    @Override
+    public void outputGeneratedSource( CtClass ctClass ) {
+    }
+}
diff --git a/code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/util/SourceCodeGenerator.java b/code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/util/SourceCodeGenerator.java
new file mode 100644 (file)
index 0000000..bfbb031
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2014 Brocade Communications 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.yangtools.sal.binding.generator.util;
+
+import javassist.CtClass;
+import javassist.CtField;
+import javassist.CtMethod;
+
+/**
+ * Interface for a class that that generates readable source code for a runtime generated class.
+ * The appendField/appendMethod methods append source code to a temporary output. When outputGeneratedSource
+ * is called, the entire class source code is generated and outputted.
+ *
+ * @author Thomas Pantelis
+ */
+public interface SourceCodeGenerator {
+
+    /**
+     * Appends the given class field and value to the temporary output.
+     */
+    void appendField( CtField field, String value );
+
+    /**
+     * Appends the given method and source code body to the temporary output.
+     */
+    void appendMethod( CtMethod method, String code );
+
+    /**
+     * Generates the full source code for the given class and outputs it.
+     */
+    void outputGeneratedSource( CtClass ctClass );
+}
\ No newline at end of file
diff --git a/code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/util/SourceCodeGeneratorFactory.java b/code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/util/SourceCodeGeneratorFactory.java
new file mode 100644 (file)
index 0000000..23549c6
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2014 Brocade Communications 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.yangtools.sal.binding.generator.util;
+
+/**
+ * Factory class for creating SourceCodeGenerator instances.
+ *
+ * @author Thomas Pantelis
+ */
+public class SourceCodeGeneratorFactory {
+
+    private static final String GENERATE_CODEC_SOURCE_PROP = "org.opendaylight.yangtools.sal.generateCodecSource";
+
+    private static final SourceCodeGenerator NULL_GENERATOR = new NullSourceCodeGenerator();
+
+    /**
+     * Gets a SourceCodeGenerator instance.
+     * <p>
+     * Generation of source code is controlled by the <i>org.opendaylight.yangtools.sal.generateCodecSource</i>
+     * system property. If set to true, a DefaultSourceCodeGenerator instance is returned, otherwise a
+     * NullSourceCodeGenerator is returned.
+     *
+     * @param generatedSourceDir the directory in which to put generated source files. If null,
+     *     a default is used (see DefaultSourceCodeGenerator).
+     */
+    public SourceCodeGenerator getInstance( String generatedSourceDir ) {
+
+        boolean generateSource = Boolean.valueOf( System.getProperty( GENERATE_CODEC_SOURCE_PROP, "false") );
+        if( generateSource ) {
+            return new DefaultSourceCodeGenerator( generatedSourceDir );
+        }
+
+        return NULL_GENERATOR;
+    }
+}
index 249a58a1ee03e283fd3597348fc9a3bfbb6a1ac6..0b7e071fa8452696d8262416dc7f3f1136bb1b47 100644 (file)
@@ -15,6 +15,8 @@ import java.io.DataOutputStream;
 import java.io.IOException;
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
+import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.Iterator;
@@ -84,6 +86,20 @@ public final class BindingGeneratorUtil {
         }
     };
 
+    private static final Comparator<TypeMemberBuilder<?>> SUID_MEMBER_COMPARATOR = new Comparator<TypeMemberBuilder<?>>() {
+        @Override
+        public int compare(final TypeMemberBuilder<?> o1, final TypeMemberBuilder<?> o2) {
+            return o1.getName().compareTo(o2.getName());
+        }
+    };
+
+    private static final Comparator<Type> SUID_NAME_COMPARATOR = new Comparator<Type>() {
+        @Override
+        public int compare(final Type o1, final Type o2) {
+            return o1.getFullyQualifiedName().compareTo(o2.getFullyQualifiedName());
+        }
+    };
+
     /**
      * Converts <code>parameterName</code> to valid JAVA parameter name.
      *
@@ -312,6 +328,16 @@ public final class BindingGeneratorUtil {
         return sb.toString();
     }
 
+    private static <T> Iterable<T> sortedCollection(final Comparator<? super T> comparator, final Collection<T> input) {
+        if (input.size() > 1) {
+            final List<T> ret = new ArrayList<>(input);
+            Collections.sort(ret, comparator);
+            return ret;
+        } else {
+            return input;
+        }
+    }
+
     public static long computeDefaultSUID(final GeneratedTypeBuilderBase<?> to) {
         try {
             ByteArrayOutputStream bout = new ByteArrayOutputStream();
@@ -320,33 +346,15 @@ public final class BindingGeneratorUtil {
             dout.writeUTF(to.getName());
             dout.writeInt(to.isAbstract() ? 3 : 7);
 
-            List<Type> impl = to.getImplementsTypes();
-            Collections.sort(impl, new Comparator<Type>() {
-                @Override
-                public int compare(final Type o1, final Type o2) {
-                    return o1.getFullyQualifiedName().compareTo(o2.getFullyQualifiedName());
-                }
-            });
-            for (Type ifc : impl) {
+            for (Type ifc : sortedCollection(SUID_NAME_COMPARATOR, to.getImplementsTypes())) {
                 dout.writeUTF(ifc.getFullyQualifiedName());
             }
 
-            Comparator<TypeMemberBuilder<?>> comparator = new Comparator<TypeMemberBuilder<?>>() {
-                @Override
-                public int compare(final TypeMemberBuilder<?> o1, final TypeMemberBuilder<?> o2) {
-                    return o1.getName().compareTo(o2.getName());
-                }
-            };
-
-            List<GeneratedPropertyBuilder> props = to.getProperties();
-            Collections.sort(props, comparator);
-            for (GeneratedPropertyBuilder gp : props) {
+            for (GeneratedPropertyBuilder gp : sortedCollection(SUID_MEMBER_COMPARATOR, to.getProperties())) {
                 dout.writeUTF(gp.getName());
             }
 
-            List<MethodSignatureBuilder> methods = to.getMethodDefinitions();
-            Collections.sort(methods, comparator);
-            for (MethodSignatureBuilder m : methods) {
+            for (MethodSignatureBuilder m : sortedCollection(SUID_MEMBER_COMPARATOR, to.getMethodDefinitions())) {
                 if (!(m.getAccessModifier().equals(AccessModifier.PRIVATE))) {
                     dout.writeUTF(m.getName());
                     dout.write(m.getAccessModifier().ordinal());
@@ -355,17 +363,19 @@ public final class BindingGeneratorUtil {
 
             dout.flush();
 
-            MessageDigest md = MessageDigest.getInstance("SHA");
-            byte[] hashBytes = md.digest(bout.toByteArray());
-            long hash = 0;
-            for (int i = Math.min(hashBytes.length, 8) - 1; i >= 0; i--) {
-                hash = (hash << 8) | (hashBytes[i] & 0xFF);
+            try {
+                MessageDigest md = MessageDigest.getInstance("SHA");
+                byte[] hashBytes = md.digest(bout.toByteArray());
+                long hash = 0;
+                for (int i = Math.min(hashBytes.length, 8) - 1; i >= 0; i--) {
+                    hash = (hash << 8) | (hashBytes[i] & 0xFF);
+                }
+                return hash;
+            } catch (NoSuchAlgorithmException ex) {
+                throw new SecurityException(ex.getMessage());
             }
-            return hash;
         } catch (IOException ex) {
             throw new InternalError();
-        } catch (NoSuchAlgorithmException ex) {
-            throw new SecurityException(ex.getMessage());
         }
     }
 
index 5b16fa7eec7432b8ce85b6a1a2fab4869d09ec45..871e57b7ac6b7ebdd02ba8f11cdaeace31efdb13 100644 (file)
@@ -41,9 +41,6 @@
                 <groupId>org.eclipse.xtend</groupId>
                 <artifactId>xtend-maven-plugin</artifactId>
             </plugin>
-            <plugin>
-                <artifactId>maven-clean-plugin</artifactId>
-            </plugin>
 <!--      <plugin>
         <artifactId>maven-enforcer-plugin</artifactId>
         <executions>
             <groupId>org.codehaus.plexus</groupId>
             <artifactId>plexus-slf4j-logging</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-core</artifactId>
+        </dependency>
     </dependencies>
 
 </project>
index c97838ed02125b073a37d61ec53d00e2b72d5546..8b465758d0691ca67c386b5c7dbef63104b3cfb9 100644 (file)
@@ -31,6 +31,7 @@ import java.util.Collection
 import org.opendaylight.yangtools.yang.binding.Identifiable
 import com.google.common.collect.Range
 import org.opendaylight.yangtools.sal.binding.model.api.ConcreteType
+import com.google.common.collect.ImmutableList
 
 /**
  * Template for generating JAVA builder classes. 
@@ -213,6 +214,8 @@ class BuilderTemplate extends BaseTemplate {
 
             Â«generateConstructorsFromIfcs(type)»
 
+            Â«generateCopyConstructor(false)»
+
             Â«generateMethodFieldsFrom(type)»
 
             Â«generateGetters(false)»
@@ -231,7 +234,7 @@ class BuilderTemplate extends BaseTemplate {
 
                 Â«generateAugmentField(false)»
 
-                Â«generateConstructor»
+                Â«generateCopyConstructor(true)»
 
                 Â«generateGetters(true)»
 
@@ -501,13 +504,8 @@ class BuilderTemplate extends BaseTemplate {
         }
     '''
 
-    /**
-     * Template method which generate constructor for IMPL class.
-     * 
-     * @return string with IMPL class constructor
-     */
-    def private generateConstructor() '''
-        private Â«type.name»«IMPL»(«type.name»«BUILDER» builder) {
+    def private CharSequence generateCopyConstructor(boolean impl) '''
+        Â«IF impl»private«ELSE»public«ENDIF» Â«type.name»«IF impl»«IMPL»«ELSE»«BUILDER»«ENDIF»(«type.name»«IF impl»«BUILDER»«ENDIF» base) {
             Â«val allProps = new ArrayList(properties)»
             Â«val isList = implementsIfc(type, Types.parameterizedTypeFor(Types.typeForClass(Identifiable), type))»
             Â«val keyType = type.getKey»
@@ -522,37 +520,49 @@ class BuilderTemplate extends BaseTemplate {
                     Â«removeProperty(allProps, field.name)»
                 Â«ENDFOR»
                 Â«removeProperty(allProps, "key")»
-                if (builder.getKey() == null) {
+                if (base.getKey() == null) {
                     this._key = new Â«keyType.importedName»(
                         Â«FOR keyProp : keyProps SEPARATOR ", "»
-                            builder.«keyProp.getterMethodName»()
+                            base.«keyProp.getterMethodName»()
                         Â«ENDFOR»
                     );
                     Â«FOR field : keyProps»
-                        this.«field.fieldName» = builder.«field.getterMethodName»();
+                        this.«field.fieldName» = base.«field.getterMethodName»();
                     Â«ENDFOR»
                 } else {
-                    this._key = builder.getKey();
+                    this._key = base.getKey();
                     Â«FOR field : keyProps»
                            this.«field.fieldName» = _key.«field.getterMethodName»();
                     Â«ENDFOR»
                 }
             Â«ENDIF»
             Â«FOR field : allProps»
-                this.«field.fieldName» = builder.«field.getterMethodName»();
+                Â«IF List.canonicalName.equals(field.returnType.fullyQualifiedName)»
+                    if (base.«field.getterMethodName»() == null || base.«field.getterMethodName»().isEmpty()) {
+                        this.«field.fieldName» = Â«Collections.importedName».emptyList();
+                    } else {
+                        this.«field.fieldName» = Â«ImmutableList.importedName».copyOf(base.«field.getterMethodName»());
+                    }
+                Â«ELSE»
+                    this.«field.fieldName» = base.«field.getterMethodName»();
+                Â«ENDIF»
             Â«ENDFOR»
             Â«IF augmentField != null»
-               switch (builder.«augmentField.name».size()) {
-                case 0:
-                    this.«augmentField.name» = Â«Collections.importedName».emptyMap();
-                    break;
-                case 1:
-                    final Â«Map.importedName».Entry<«Class.importedName»<? extends Â«augmentField.returnType.importedName»>, Â«augmentField.returnType.importedName»> e = builder.«augmentField.name».entrySet().iterator().next();
-                    this.«augmentField.name» = Â«Collections.importedName».<«Class.importedName»<? extends Â«augmentField.returnType.importedName»>, Â«augmentField.returnType.importedName»>singletonMap(e.getKey(), e.getValue());
-                    break;
-                default :
-                    this.«augmentField.name» = new Â«HashMap.importedName»<>(builder.«augmentField.name»);
-                }
+                Â«IF !impl»if (base instanceof Â«type.name»«IMPL») {«ENDIF»
+                    Â«IF !impl»«type.name»«IMPL» _impl = («type.name»«IMPL») base;«ENDIF»
+                    Â«val prop = if (impl) "base" else "_impl"»
+                    switch («prop».«augmentField.name».size()) {
+                    case 0:
+                        this.«augmentField.name» = Â«Collections.importedName».emptyMap();
+                        break;
+                    case 1:
+                        final Â«Map.importedName».Entry<«Class.importedName»<? extends Â«augmentField.returnType.importedName»>, Â«augmentField.returnType.importedName»> e = Â«prop».«augmentField.name».entrySet().iterator().next();
+                        this.«augmentField.name» = Â«Collections.importedName».<«Class.importedName»<? extends Â«augmentField.returnType.importedName»>, Â«augmentField.returnType.importedName»>singletonMap(e.getKey(), e.getValue());
+                        break;
+                    default :
+                        this.«augmentField.name» = new Â«HashMap.importedName»<>(«prop».«augmentField.name»);
+                    }
+                Â«IF !impl»}«ENDIF»
             Â«ENDIF»
         }
     '''
@@ -733,4 +743,3 @@ class BuilderTemplate extends BaseTemplate {
     '''
 
 }
-
index fed70f50ea5beb0a2b7709694f2c557650be7877..eb5b94c5da742df982350567d99606116e351d2a 100644 (file)
@@ -368,12 +368,11 @@ class ClassTemplate extends BaseTemplate {
         Â«IF restrictions != null»
             Â«val prop = getPropByName("value")»
             Â«IF prop != null»
-                Â«val numberClass = prop.returnType.importedNumber»
                 Â«IF !(restrictions.lengthConstraints.empty)»
-                    private static Â«List.importedName»<«Range.importedName»<«numberClass»>> _length;
+                    private static Â«List.importedName»<«Range.importedName»<«prop.returnType.importedNumber»>> _length;
                 Â«ENDIF»
                 Â«IF !(restrictions.rangeConstraints.empty)»
-                    private static Â«List.importedName»<«Range.importedName»<«numberClass»>> _range;
+                    private static Â«List.importedName»<«Range.importedName»<«prop.returnType.importedNumber»>> _range;
                 Â«ENDIF»
             Â«ENDIF»
         Â«ENDIF»
@@ -384,7 +383,6 @@ class ClassTemplate extends BaseTemplate {
         Â«ENDIF»
     '''
 
-
     /**
      * Template method which generates the method <code>hashCode()</code>.
      *
index f9c27fe86529d2f8175dc2c1f45805b986f881b2..94cfe1e8d05cec8076711637a6da1129e06d8fed 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.yangtools.sal.java.api.generator
 
 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject
 import java.beans.ConstructorProperties
+import org.opendaylight.yangtools.sal.binding.model.api.Enumeration
 
 /**
  * Template for generating JAVA class. 
@@ -37,14 +38,15 @@ class UnionTemplate extends ClassTemplate {
 
     private def unionConstructors() '''
         Â«FOR property : finalProperties SEPARATOR "\n"»
-            Â«val isCharArray = "char[]".equals(property.returnType.name)»
+            Â«val propRet = property.returnType»
+            Â«val isCharArray = "char[]".equals(propRet.name)»
             Â«IF isCharArray»
                 /**
                  * Constructor provided only for using in JMX. Don't use it for
                  * construction new object of this union type. 
                  */
                 @«ConstructorProperties.importedName»("«property.name»")
-                public Â«type.name»(«property.returnType.importedName» Â«property.fieldName») {
+                public Â«type.name»(«propRet.importedName» Â«property.fieldName») {
                     Â«String.importedName» defVal = new Â«String.importedName»(«property.fieldName»);
                     Â«type.name» defInst = Â«type.name»Builder.getDefaultInstance(defVal);
                     Â«FOR other : finalProperties»
@@ -61,7 +63,28 @@ class UnionTemplate extends ClassTemplate {
                     super(«parentProperties.asArguments»);
                     this.«property.fieldName» = Â«property.fieldName»;
                     Â«FOR other : finalProperties»
-                        Â«IF property != other»this.«other.fieldName» = null;«ENDIF»
+                        Â«IF property != other»
+                            Â«IF "value".equals(other.name)»
+                                Â«IF "java.lang.String".equals(propRet.fullyQualifiedName)»
+                                    Â«Â«Â« type string
+                                    this.«other.fieldName» = Â«property.fieldName».toCharArray();
+                                Â«ELSEIF "byte[]".equals(propRet.name)»
+                                    Â«Â«Â« type binary
+                                    this.«other.fieldName» = new Â«String.importedName»(«property.fieldName»).toCharArray();
+                                Â«ELSEIF propRet.fullyQualifiedName.startsWith("java.lang") || propRet instanceof Enumeration»
+                                    Â«Â«Â« type int*, uint or enumeration*
+                                    this.«other.fieldName» = Â«property.fieldName».toString().toCharArray();
+                                Â«ELSEIF propRet instanceof GeneratedTransferObject && (propRet as GeneratedTransferObject).unionType»
+                                    Â«Â«Â« union type
+                                    this.«other.fieldName» = Â«property.fieldName».getValue();
+                                Â«ELSE»
+                                    Â«Â«Â« generated type
+                                    this.«other.fieldName» = Â«property.fieldName».getValue().toString().toCharArray();
+                                Â«ENDIF»
+                            Â«ELSE»
+                                this.«other.fieldName» = null;
+                            Â«ENDIF»
+                        Â«ENDIF»
                     Â«ENDFOR»
                 }
             Â«ENDIF»
diff --git a/code-generator/binding-java-api-generator/src/test/java/org/opendaylight/yangtools/sal/java/api/generator/test/Bug1276Test.java b/code-generator/binding-java-api-generator/src/test/java/org/opendaylight/yangtools/sal/java/api/generator/test/Bug1276Test.java
new file mode 100644 (file)
index 0000000..d2653f2
--- /dev/null
@@ -0,0 +1,105 @@
+/*
+ * 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.yangtools.sal.java.api.generator.test;
+
+import static org.junit.Assert.assertTrue;
+import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.BASE_PKG;
+import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.COMPILER_OUTPUT_PATH;
+import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.FS;
+import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.GENERATOR_OUTPUT_PATH;
+import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.assertContainsConstructor;
+import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.cleanUp;
+import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.getSourceFiles;
+import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.testCompilation;
+
+import java.io.File;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Method;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+import org.junit.Test;
+import org.opendaylight.yangtools.sal.binding.model.api.Type;
+import org.opendaylight.yangtools.sal.java.api.generator.GeneratorJavaFile;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+
+/**
+ * Previous construction of union constructor
+ *
+ * <code>
+ * public IpAddress(Arg1 _arg1) {
+ *     super();
+ *     this._arg1 = _arg1;
+ *     this._arg2 = null;
+ *     this._value = null;
+ * }
+ * </code>
+ *
+ * was incorrect and setting
+ *
+ * <code>this._value == null</code>
+ *
+ * was replaced with setting _value to correct value, for example:
+ *
+ * <code>this._value = arg1.getValue()</code> or
+ * <code>this._value = _arg1.getValue().toString().toCharArray()</code>
+ *
+ */
+public class Bug1276Test extends BaseCompilationTest {
+
+    @Test
+    public void test() throws Exception {
+        final File sourcesOutputDir = new File(GENERATOR_OUTPUT_PATH + FS + "bug1276");
+        assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
+        final File compiledOutputDir = new File(COMPILER_OUTPUT_PATH + FS + "bug1276");
+        assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
+
+        generateTestSources("/compilation/bug1276", sourcesOutputDir);
+
+        // Test if sources are compilable
+        testCompilation(sourcesOutputDir, compiledOutputDir);
+
+        ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
+        Class<?> ipAddressClass = Class.forName(BASE_PKG + ".test.yang.union.rev140715.IpAddress", true, loader);
+        Class<?> ipv4AddressClass = Class.forName(BASE_PKG + ".test.yang.union.rev140715.Ipv4Address", true, loader);
+        Class<?> hostClass = Class.forName(BASE_PKG + ".test.yang.union.rev140715.Host", true, loader);
+
+        Constructor<?> ipAddressConstructor = assertContainsConstructor(ipAddressClass, ipv4AddressClass);
+        Constructor<?> ipv4addressConstructor = assertContainsConstructor(ipv4AddressClass, String.class);
+        Constructor<?> hostConstructor = assertContainsConstructor(hostClass, ipAddressClass);
+
+        // test IpAddress with Ipv4Address argument
+        Object ipv4address = ipv4addressConstructor.newInstance("192.168.0.1");
+        Object ipAddress = ipAddressConstructor.newInstance(ipv4address);
+        Method getValue = ipAddressClass.getDeclaredMethod("getValue");
+        char[] expected = "192.168.0.1".toCharArray();
+        Object actual = getValue.invoke(ipAddress);
+        assertTrue(actual instanceof char[]);
+        assertTrue(Arrays.equals(expected, (char[]) actual));
+
+        // test Host with IpAddress argument
+        Object host = hostConstructor.newInstance(ipAddress);
+        getValue = hostClass.getDeclaredMethod("getValue");
+        actual = getValue.invoke(host);
+        assertTrue(actual instanceof char[]);
+        assertTrue(Arrays.equals(expected, (char[]) actual));
+
+        cleanUp(sourcesOutputDir, compiledOutputDir);
+    }
+
+    private void generateTestSources(String resourceDirPath, File sourcesOutputDir) throws Exception {
+        final List<File> sourceFiles = getSourceFiles(resourceDirPath);
+        final SchemaContext context = parser.parseFiles(sourceFiles);
+        final List<Type> types = bindingGenerator.generateTypes(context);
+        final GeneratorJavaFile generator = new GeneratorJavaFile(new HashSet<>(types));
+        generator.generateToFile(sourcesOutputDir);
+    }
+
+}
diff --git a/code-generator/binding-java-api-generator/src/test/java/org/opendaylight/yangtools/sal/java/api/generator/test/Bug532Test.java b/code-generator/binding-java-api-generator/src/test/java/org/opendaylight/yangtools/sal/java/api/generator/test/Bug532Test.java
new file mode 100644 (file)
index 0000000..e816a68
--- /dev/null
@@ -0,0 +1,118 @@
+/*
+ * 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.yangtools.sal.java.api.generator.test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.BASE_PKG;
+import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.COMPILER_OUTPUT_PATH;
+import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.FS;
+import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.GENERATOR_OUTPUT_PATH;
+import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.cleanUp;
+import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.getSourceFiles;
+import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.testCompilation;
+
+import com.google.common.collect.Lists;
+import java.io.File;
+import java.lang.reflect.Constructor;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.HashSet;
+import java.util.List;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.opendaylight.yangtools.sal.binding.model.api.Type;
+import org.opendaylight.yangtools.sal.java.api.generator.GeneratorJavaFile;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+
+/**
+ * Test correct functionality of copy constructor of generated builder classes.
+ */
+public class Bug532Test extends BaseCompilationTest {
+
+    @Test
+    public void test() throws Exception {
+        final File sourcesOutputDir = new File(GENERATOR_OUTPUT_PATH + FS + "bug532");
+        assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
+        final File compiledOutputDir = new File(COMPILER_OUTPUT_PATH + FS + "bug532");
+        assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
+
+        generateTestSources("/compilation/list-gen", sourcesOutputDir);
+
+        // Test if sources are compilable
+        testCompilation(sourcesOutputDir, compiledOutputDir);
+
+        ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
+        Class<?> linksKeyClass = Class.forName(BASE_PKG + ".urn.opendaylight.test.rev131008.LinksKey", true, loader);
+        Class<?> linksClass = Class.forName(BASE_PKG + ".urn.opendaylight.test.rev131008.Links", true, loader);
+        Class<?> linksBuilderClass = Class.forName(BASE_PKG + ".urn.opendaylight.test.rev131008.LinksBuilder", true,
+                loader);
+        Class<?> levelClass = Class.forName(BASE_PKG + ".urn.opendaylight.test.rev131008.links.Level", true, loader);
+        Class<?> nodeClass = Class.forName(BASE_PKG + ".urn.opendaylight.test.rev131008.links.Node", true, loader);
+        Class<?> nodeListClass = Class.forName(BASE_PKG + ".urn.opendaylight.test.rev131008.links.NodeList", true,
+                loader);
+
+        // init default values
+        Byte expectedId = Byte.valueOf("5");
+        String expectedName = "test-link";
+        Integer expectedSize = Integer.valueOf(10);
+        Object expectedLevel = Mockito.mock(levelClass);
+        Integer expectedLinksId = Integer.valueOf(11);
+        Object expectedNode = Mockito.mock(nodeClass);
+        List<?> expectedNodeList = Lists.newArrayList(Mockito.mock(nodeListClass), Mockito.mock(nodeListClass));
+        Constructor<?> keyConstructor = linksKeyClass.getDeclaredConstructor(Byte.class, String.class, Integer.class);
+        Object expectedKey = keyConstructor.newInstance(expectedId, expectedName, expectedSize);
+
+        // create Links object
+        Object linksBuilder = linksBuilderClass.newInstance();
+        linksBuilderClass.getDeclaredMethod("setKey", linksKeyClass).invoke(linksBuilder, expectedKey);
+        linksBuilderClass.getDeclaredMethod("setLevel", levelClass).invoke(linksBuilder, expectedLevel);
+        linksBuilderClass.getDeclaredMethod("setLinksId", Integer.class).invoke(linksBuilder, expectedLinksId);
+        linksBuilderClass.getDeclaredMethod("setNode", nodeClass).invoke(linksBuilder, expectedNode);
+        linksBuilderClass.getDeclaredMethod("setNodeList", List.class).invoke(linksBuilder, expectedNodeList);
+        Object links = linksBuilderClass.getDeclaredMethod("build").invoke(linksBuilder);
+
+        // create LinksBuilder object with constructor with Links object
+        // argument
+        Constructor<?> linksBuilderConstructor = linksBuilderClass.getDeclaredConstructor(linksClass);
+        assertNotNull(linksBuilderConstructor);
+        Object linksBuilderTested = linksBuilderConstructor.newInstance(links);
+
+        // get values from LinksBuilder
+        Object actualKey = linksBuilderClass.getDeclaredMethod("getKey").invoke(linksBuilderTested);
+        Object actualId = linksBuilderClass.getDeclaredMethod("getId").invoke(linksBuilderTested);
+        Object actualName = linksBuilderClass.getDeclaredMethod("getName").invoke(linksBuilderTested);
+        Object actualSize = linksBuilderClass.getDeclaredMethod("getSize").invoke(linksBuilderTested);
+        Object actualLevel = linksBuilderClass.getDeclaredMethod("getLevel").invoke(linksBuilderTested);
+        Object actualLinksId = linksBuilderClass.getDeclaredMethod("getLinksId").invoke(linksBuilderTested);
+        Object actualNode = linksBuilderClass.getDeclaredMethod("getNode").invoke(linksBuilderTested);
+        Object actualNodeList = linksBuilderClass.getDeclaredMethod("getNodeList").invoke(linksBuilderTested);
+
+        // test
+        assertEquals(expectedKey, actualKey);
+        assertEquals(expectedId, actualId);
+        assertEquals(expectedName, actualName);
+        assertEquals(expectedSize, actualSize);
+        assertEquals(expectedLevel, actualLevel);
+        assertEquals(expectedLinksId, actualLinksId);
+        assertEquals(expectedNode, actualNode);
+        assertEquals(expectedNodeList, actualNodeList);
+
+        cleanUp(sourcesOutputDir, compiledOutputDir);
+    }
+
+    private void generateTestSources(String resourceDirPath, File sourcesOutputDir) throws Exception {
+        final List<File> sourceFiles = getSourceFiles(resourceDirPath);
+        final SchemaContext context = parser.parseFiles(sourceFiles);
+        final List<Type> types = bindingGenerator.generateTypes(context);
+        final GeneratorJavaFile generator = new GeneratorJavaFile(new HashSet<>(types));
+        generator.generateToFile(sourcesOutputDir);
+    }
+
+}
index b49be322c6d9ea2292649f4c77334c47a428286b..58d213cbbbec98dc682582a2e0fe022162ffb70b 100644 (file)
@@ -97,7 +97,7 @@ public class CascadeUsesCompilationTest extends BaseCompilationTest {
         // test generated builder for 'container nodes'
         assertFalse(nodesBuilderClass.isInterface());
         Constructor<?>[] nodesBuilderConstructors = nodesBuilderClass.getConstructors();
-        assertEquals(5, nodesBuilderConstructors.length);
+        assertEquals(6, nodesBuilderConstructors.length);
 
         // test generation of builder constructors from uses in 'container nodes'
         Constructor<?> defaultConstructor = null;
index 5597486584d53d219d12217e75d44f6ae33bbca7..db2fa44ac5abcb9f0c1b05ceebdcfd330f2a34e4 100644 (file)
@@ -23,10 +23,13 @@ import static org.opendaylight.yangtools.sal.java.api.generator.test.Compilation
 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.assertContainsRestrictionCheck;
 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.assertFilesCount;
 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.assertImplementsIfc;
+import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.assertImplementsParameterizedIfc;
 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.cleanUp;
 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.getSourceFiles;
 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.testCompilation;
 
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Lists;
 import com.google.common.collect.Range;
 import java.io.File;
 import java.lang.annotation.Annotation;
@@ -39,11 +42,14 @@ import java.math.BigInteger;
 import java.net.URL;
 import java.net.URLClassLoader;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.HashSet;
 import java.util.List;
 import org.junit.Test;
+import org.mockito.Mockito;
 import org.opendaylight.yangtools.sal.binding.model.api.Type;
 import org.opendaylight.yangtools.sal.java.api.generator.GeneratorJavaFile;
+import org.opendaylight.yangtools.yang.binding.ChildOf;
 import org.opendaylight.yangtools.yang.binding.annotations.RoutingContext;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 
@@ -535,6 +541,80 @@ public class CompilationTest extends BaseCompilationTest {
         cleanUp(sourcesOutputDir, compiledOutputDir);
     }
 
+    /**
+     * Test if class generated for node from grouping implements ChildOf.
+     *
+     * @throws Exception
+     */
+    @Test
+    public void testBug1377() throws Exception {
+        final File sourcesOutputDir = new File(GENERATOR_OUTPUT_PATH + FS + "bug1377");
+        assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
+        final File compiledOutputDir = new File(COMPILER_OUTPUT_PATH + FS + "bug1377");
+        assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
+
+        generateTestSources("/compilation/bug1377", sourcesOutputDir);
+
+        // Test if sources are compilable
+        testCompilation(sourcesOutputDir, compiledOutputDir);
+
+        ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
+        Class<?> outputActionClass = Class.forName(BASE_PKG
+                + ".urn.test.foo.rev140717.action.action.output.action._case.OutputAction", true, loader);
+        Class<?> actionClass = Class.forName(BASE_PKG + ".urn.test.foo.rev140717.Action", true, loader);
+
+        // Test generated 'container output-action'
+        assertTrue(outputActionClass.isInterface());
+        assertImplementsParameterizedIfc(outputActionClass, ChildOf.class.toString(), actionClass.getCanonicalName());
+
+        cleanUp(sourcesOutputDir, compiledOutputDir);
+    }
+
+    @Test
+    public void testBug1097() throws Exception {
+        final File sourcesOutputDir = new File(GENERATOR_OUTPUT_PATH + FS + "bug1097");
+        assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
+        final File compiledOutputDir = new File(COMPILER_OUTPUT_PATH + FS + "bug1097");
+        assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
+
+        generateTestSources("/compilation/bug1097", sourcesOutputDir);
+
+        // Test if sources are compilable
+        testCompilation(sourcesOutputDir, compiledOutputDir);
+
+        ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
+        Class<?> linkClass = Class.forName(BASE_PKG + ".urn.test.foo.rev140717.Link", true, loader);
+        Class<?> linkBuilderClass = Class.forName(BASE_PKG + ".urn.test.foo.rev140717.LinkBuilder", true, loader);
+        Class<?> nodeClass = Class.forName(BASE_PKG + ".urn.test.foo.rev140717.link.Node", true, loader);
+
+        // Test LinkBuilder without setting any value
+        Object linkBuilder = linkBuilderClass.newInstance();
+        Method build = linkBuilderClass.getDeclaredMethod("build");
+        Object link = build.invoke(linkBuilder);
+        Method getNodes = linkClass.getDeclaredMethod("getNode");
+        Object node = getNodes.invoke(link);
+        assertEquals(Collections.emptyList(), node);
+
+        // Test LinkBuilder with setting an empty list
+        linkBuilder = linkBuilderClass.newInstance();
+        Method setNode = linkBuilderClass.getDeclaredMethod("setNode", List.class);
+        setNode.invoke(linkBuilder, Collections.emptyList());
+        link = build.invoke(linkBuilder);
+        node = getNodes.invoke(link);
+        assertEquals(Collections.emptyList(), node);
+
+        // Test LinkBuilder with setting list with values
+        linkBuilder = linkBuilderClass.newInstance();
+        List<?> testList = Lists.newArrayList(Mockito.mock(nodeClass), Mockito.mock(nodeClass));
+        setNode.invoke(linkBuilder, testList);
+        link = build.invoke(linkBuilder);
+        node = getNodes.invoke(link);
+        assertEquals(testList, node);
+        assertTrue(node instanceof ImmutableList);
+
+        cleanUp(sourcesOutputDir, compiledOutputDir);
+    }
+
     @Test
     public void classNamesColisionTest() throws Exception {
         final File sourcesOutputDir = new File(GENERATOR_OUTPUT_PATH + FS + "class-name-collision");
index 755b3af7b2febfab103ac907eb3637bd788e4ddb..d3ac8d7eec5bc0c2e0e3c127143f8fbb77751155 100644 (file)
@@ -40,6 +40,8 @@ public class CompilationTestUtils {
     static final String COMPILER_OUTPUT_PATH = TEST_PATH + FS + "bin";
     static final File COMPILER_OUTPUT_DIR = new File(COMPILER_OUTPUT_PATH);
 
+    static final String AUGMENTATION = "interface org.opendaylight.yangtools.yang.binding.Augmentation";
+
     static final String BASE_PATH = "org" + FS + "opendaylight" + FS + "yang" + FS + "gen" + FS + "v1";
     static final String NS_TEST = BASE_PATH + FS + "urn" + FS + "opendaylight" + FS + "test" + FS + "rev131008";
     static final String NS_FOO = BASE_PATH + FS + "urn" + FS + "opendaylight" + FS + "foo" + FS + "rev131008";
@@ -322,8 +324,7 @@ public class CompilationTestUtils {
      *            fully qualified name of expected parameter type
      */
     static void testAugmentation(Class<?> clazz, String genericTypeName) {
-        final String ifcName = "interface org.opendaylight.yangtools.yang.binding.Augmentation";
-        assertImplementsParameterizedIfc(clazz, ifcName, genericTypeName);
+        assertImplementsParameterizedIfc(clazz, AUGMENTATION, genericTypeName);
     }
 
     /**
index a80133d17368b05540179809d2bdab182248e729..9c739dfb266eaac28071c82dcd1fcad9fafaa379 100644 (file)
@@ -229,7 +229,8 @@ public class TypedefCompilationTest extends BaseCompilationTest {
 
         // typedef string-ext3
         assertFalse(stringExt3Class.isInterface());
-        assertContainsFieldWithValue(stringExt3Class, "serialVersionUID", Long.TYPE, -2751063130555484180L, String.class);
+        assertContainsFieldWithValue(stringExt3Class, "serialVersionUID", Long.TYPE, -2751063130555484180L,
+                String.class);
         assertEquals(1, stringExt3Class.getDeclaredFields().length);
         expectedConstructor = assertContainsConstructor(stringExt3Class, String.class);
         assertContainsConstructor(stringExt3Class, stringExt3Class);
@@ -245,7 +246,8 @@ public class TypedefCompilationTest extends BaseCompilationTest {
         assertFalse(myDecimalTypeClass.isInterface());
         assertContainsField(myDecimalTypeClass, VAL, BigDecimal.class);
         assertContainsField(myDecimalTypeClass, RANGE, List.class);
-        assertContainsFieldWithValue(myDecimalTypeClass, "serialVersionUID", Long.TYPE, 3143735729419861095L, BigDecimal.class);
+        assertContainsFieldWithValue(myDecimalTypeClass, "serialVersionUID", Long.TYPE, 3143735729419861095L,
+                BigDecimal.class);
         assertEquals(3, myDecimalTypeClass.getDeclaredFields().length);
         assertContainsMethod(myDecimalTypeClass, BigDecimal.class, "getValue");
         expectedConstructor = assertContainsConstructor(myDecimalTypeClass, BigDecimal.class);
@@ -293,7 +295,8 @@ public class TypedefCompilationTest extends BaseCompilationTest {
         assertFalse(unionExt1Class.isInterface());
         assertContainsField(unionExt1Class, "_int16", Short.class);
         assertContainsField(unionExt1Class, "_int32", Integer.class);
-        assertContainsFieldWithValue(unionExt1Class, "serialVersionUID", Long.TYPE, -5610530488718168882L, Short.class);
+        assertContainsFieldWithValue(unionExt1Class, "serialVersionUID", Long.TYPE, -5610530488718168882L,
+                new Class<?>[] { Short.class }, Short.valueOf("1"));
         assertEquals(4, unionExt1Class.getDeclaredFields().length);
         assertContainsMethod(unionExt1Class, Short.class, "getInt16");
         assertContainsMethod(unionExt1Class, Integer.class, "getInt32");
@@ -305,7 +308,8 @@ public class TypedefCompilationTest extends BaseCompilationTest {
 
         // typedef union-ext2
         assertFalse(unionExt2Class.isInterface());
-        assertContainsFieldWithValue(unionExt2Class, "serialVersionUID", Long.TYPE, -8833407459073585206L, Short.class);
+        assertContainsFieldWithValue(unionExt2Class, "serialVersionUID", Long.TYPE, -8833407459073585206L,
+                new Class<?>[] { Short.class }, Short.valueOf("1"));
         assertEquals(1, unionExt2Class.getDeclaredFields().length);
         assertEquals(0, unionExt2Class.getDeclaredMethods().length);
         assertContainsConstructor(unionExt2Class, Short.class);
@@ -318,8 +322,10 @@ public class TypedefCompilationTest extends BaseCompilationTest {
         assertFalse(unionExt3Class.isInterface());
         assertContainsField(unionExt3Class, "_string", String.class);
         assertContainsField(unionExt3Class, "_unionExt2", unionExt2Class);
-        assertContainsFieldWithValue(unionExt3Class, UNITS, String.class, "object id", String.class);
-        assertContainsFieldWithValue(unionExt3Class, "serialVersionUID", Long.TYPE, 4347887914884631036L, String.class);
+        assertContainsFieldWithValue(unionExt3Class, UNITS, String.class, "object id", new Class<?>[] { String.class },
+                "");
+        assertContainsFieldWithValue(unionExt3Class, "serialVersionUID", Long.TYPE, 4347887914884631036L,
+                new Class<?>[] { String.class }, "");
         assertEquals(5, unionExt3Class.getDeclaredFields().length);
         assertContainsMethod(unionExt3Class, String.class, "getString");
         assertContainsMethod(unionExt3Class, unionExt2Class, "getUnionExt2");
@@ -335,7 +341,8 @@ public class TypedefCompilationTest extends BaseCompilationTest {
         assertContainsField(unionExt4Class, "_int32Ext2", int32Ext2Class);
         assertContainsField(unionExt4Class, "_empty", Boolean.class);
         assertContainsField(unionExt4Class, "_myDecimalType", myDecimalTypeClass);
-        assertContainsFieldWithValue(unionExt4Class, "serialVersionUID", Long.TYPE, 4299836385615211130L, Boolean.class);
+        assertContainsFieldWithValue(unionExt4Class, "serialVersionUID", Long.TYPE, 4299836385615211130L,
+                new Class<?>[] { Boolean.class }, false);
         assertEquals(6, unionExt4Class.getDeclaredFields().length);
         assertContainsMethod(unionExt4Class, unionExt3Class, "getUnionExt3");
         assertContainsMethod(unionExt4Class, int32Ext2Class, "getInt32Ext2");
diff --git a/code-generator/binding-java-api-generator/src/test/resources/compilation/bug1097/foo.yang b/code-generator/binding-java-api-generator/src/test/resources/compilation/bug1097/foo.yang
new file mode 100644 (file)
index 0000000..fc0c4e2
--- /dev/null
@@ -0,0 +1,17 @@
+module foo {
+  namespace "urn:test:foo";
+  prefix foo;
+
+  revision 2014-07-17 {
+  }
+
+
+    container link {
+        list node {
+            leaf id {
+                type string;
+            }
+        }
+    }
+
+}
diff --git a/code-generator/binding-java-api-generator/src/test/resources/compilation/bug1276/foo.yang b/code-generator/binding-java-api-generator/src/test/resources/compilation/bug1276/foo.yang
new file mode 100644 (file)
index 0000000..9f77227
--- /dev/null
@@ -0,0 +1,62 @@
+ module foo {
+
+   namespace "test:yang:union";
+   prefix "foo";
+
+   revision 2014-07-15 {
+   }
+
+
+   typedef ip-address {
+     type union {
+       type ipv4-address;
+       type ipv6-address;
+     }
+   }
+
+   typedef ipv4-address {
+     type string {
+       pattern
+         '(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}'
+       +  '([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])'
+       + '(%[\p{N}\p{L}]+)?';
+     }
+   }
+
+   typedef ipv6-address {
+     type string {
+       pattern '((:|[0-9a-fA-F]{0,4}):)([0-9a-fA-F]{0,4}:){0,5}'
+             + '((([0-9a-fA-F]{0,4}:)?(:|[0-9a-fA-F]{0,4}))|'
+             + '(((25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])\.){3}'
+             + '(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])))'
+             + '(%[\p{N}\p{L}]+)?';
+       pattern '(([^:]+:){6}(([^:]+:[^:]+)|(.*\..*)))|'
+             + '((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)'
+             + '(%.+)?';
+     }
+   }
+
+   typedef domain-name {
+     type string {
+       pattern '((([a-zA-Z0-9_]([a-zA-Z0-9\-_]){0,61})?[a-zA-Z0-9]\.)*'
+            +  '([a-zA-Z0-9_]([a-zA-Z0-9\-_]){0,61})?[a-zA-Z0-9]\.?)'
+            +  '|\.';
+       length "1..253";
+     }
+   }
+
+   typedef host {
+     type union {
+       type ip-address;
+       type domain-name;
+     }
+   }
+
+    typedef int-type {
+        type union {
+            type binary;
+            type int8;
+        }
+    }
+
+ }
diff --git a/code-generator/binding-java-api-generator/src/test/resources/compilation/bug1377/foo.yang b/code-generator/binding-java-api-generator/src/test/resources/compilation/bug1377/foo.yang
new file mode 100644 (file)
index 0000000..b31a891
--- /dev/null
@@ -0,0 +1,21 @@
+module foo {
+  namespace "urn:test:foo";
+  prefix foo;
+
+  revision 2014-07-17 {
+  }
+
+
+    grouping action {
+        choice action {
+            case output-action-case {
+                container output-action {
+                    leaf id {
+                        type string;
+                    }
+                }
+            }
+        }
+    }
+
+}
index 9f8ac51670ef787187d4dccbf5c2f83e3441cd97..9f6f06ee639ff8810c22d986e5f00cbf1c296a3f 100644 (file)
@@ -89,9 +89,6 @@
                 <groupId>org.eclipse.xtend</groupId>
                 <artifactId>xtend-maven-plugin</artifactId>
             </plugin>
-            <plugin>
-                <artifactId>maven-clean-plugin</artifactId>
-            </plugin>
         </plugins>
     </build>
 
index e355f24a33c66c85e5119a0573cb6c1117d7ca17..bf2d1b7371c6d5c12137b864951cfc26c00ca521 100644 (file)
                     </dependency>
                 </dependencies>
             </plugin>
-            <plugin>
-                <groupId>org.codehaus.mojo</groupId>
-                <artifactId>build-helper-maven-plugin</artifactId>
-                <executions>
-                    <execution>
-                        <phase>generate-sources</phase>
-                        <goals>
-                            <goal>add-source</goal>
-                        </goals>
-                        <configuration>
-                            <sources>
-                                <source>target/generated-sources/sal</source>
-                            </sources>
-                        </configuration>
-                    </execution>
-                </executions>
-            </plugin>
         </plugins>
        </build>
     <dependencies>
index ab3bac3035472b5790dc8801f92c2482a8b350c7..f9ef1a7b0b67d757eaee2c5ac4b10464f8fe4693 100644 (file)
                     </dependency>
                 </dependencies>
             </plugin>
-            <plugin>
-                <groupId>org.codehaus.mojo</groupId>
-                <artifactId>build-helper-maven-plugin</artifactId>
-                <executions>
-                    <execution>
-                        <phase>generate-sources</phase>
-                        <goals>
-                            <goal>add-source</goal>
-                        </goals>
-                        <configuration>
-                            <sources>
-                                <source>target/generated-sources/sal</source>
-                            </sources>
-                        </configuration>
-                    </execution>
-                </executions>
-            </plugin>
         </plugins>
           </build>
     <dependencies>
index 6782698c32dd34aef65e8774ae9c0e0487ef557f..ee51ec6ef4ea9031f5c757b4b98aca739a977ee7 100644 (file)
@@ -15,6 +15,7 @@
         <feature version='${project.version}'>yangtools-common</feature>
         <feature version='${project.version}'>yangtools-concepts</feature>
         <feature version='${project.version}'>yangtools-binding-generator</feature>
+        <feature version='${project.version}'>yangtools-restconf</feature>
     </feature>
 
     <feature name='yangtools-models' version='${project.version}'>
         <bundle>mvn:org.opendaylight.yangtools/yang-model-util/${project.version}</bundle>
         <bundle>mvn:org.opendaylight.yangtools/yang-parser-api/${project.version}</bundle>
     </feature>
+    <feature name="yangtools-restconf" version='${project.version}'>
+        <bundle>mvn:org.opendaylight.yangtools/restconf-client-api/${project.version}</bundle>
+        <bundle>mvn:org.opendaylight.yangtools/restconf-client-impl/${project.version}</bundle>
+        <bundle>mvn:org.opendaylight.yangtools/restconf-common/${project.version}</bundle>
+     </feature>
+
 
 </features>
index 3f0675ba4d9fdf0dfbd16ae06c4792a0058b974b..84d4dfe9da7db6fc2d455dc6e66aeff0bdeb44d6 100644 (file)
                                         <execute />
                                     </action>
                                 </pluginExecution>
+                                <pluginExecution>
+                                    <pluginExecutionFilter>
+                                        <groupId>org.apache.maven.plugins</groupId>
+                                        <artifactId>maven-antrun-plugin</artifactId>
+                                        <versionRange>[1.0,)</versionRange>
+                                        <goals>
+                                            <goal>run</goal>
+                                        </goals>
+                                    </pluginExecutionFilter>
+                                    <action>
+                                        <execute/>
+                                    </action>
+                                </pluginExecution>
+                                <pluginExecution>
+                                    <pluginExecutionFilter>
+                                        <groupId>org.opendaylight.yangtools</groupId>
+                                        <artifactId>yang-maven-plugin</artifactId>
+                                        <versionRange>[0.5,)</versionRange>
+                                        <goals>
+                                            <goal>generate-sources</goal>
+                                        </goals>
+                                     </pluginExecutionFilter>
+                                     <action>
+                                         <ignore />
+                                     </action>
+                                 </pluginExecution>
+                                 <pluginExecution>
+                                    <pluginExecutionFilter>
+                                        <groupId>org.codehaus.mojo</groupId>
+                                        <artifactId>properties-maven-plugin</artifactId>
+                                        <versionRange>1.0-alpha-2</versionRange>
+                                        <goals>
+                                            <goal>write-project-properties</goal>
+                                        </goals>
+                                     </pluginExecutionFilter>
+                                     <action>
+                                         <ignore />
+                                     </action>
+                                 </pluginExecution>
+                                 <pluginExecution>
+                                    <pluginExecutionFilter>
+                                        <groupId>org.ops4j.pax.exam</groupId>
+                                        <artifactId>maven-paxexam-plugin</artifactId>
+                                        <versionRange>1.2.4</versionRange>
+                                        <goals>
+                                            <goal>generate-depends-file</goal>
+                                        </goals>
+                                     </pluginExecutionFilter>
+                                     <action>
+                                         <ignore />
+                                     </action>
+                                 </pluginExecution>
                             </pluginExecutions>
                         </lifecycleMappingMetadata>
                     </configuration>
                         </execution>
                     </executions>
                 </plugin>
+                <plugin>
+                    <groupId>org.codehaus.mojo</groupId>
+                    <artifactId>build-helper-maven-plugin</artifactId>
+                    <version>1.8</version>
+                    <executions>
+                        <execution>
+                            <phase>generate-sources</phase>
+                            <goals>
+                                <goal>add-source</goal>
+                            </goals>
+                            <configuration>
+                                <sources>
+                                    <source>target/generated-sources/parser</source>
+                                    <source>target/generated-sources/sal</source>
+                                    <source>${basedir}/src/main/xtend-gen</source>
+                                </sources>
+                            </configuration>
+                        </execution>
+                    </executions>
+                </plugin>
             </plugins>
         </pluginManagement>
 
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-javadoc-plugin</artifactId>
             </plugin>
+            <plugin>
+                <groupId>org.codehaus.mojo</groupId>
+                <artifactId>build-helper-maven-plugin</artifactId>
+            </plugin>
         </plugins>
     </build>
 
index 2ef038b99f8cf19c0928ad61702831a5745cd31e..d1d8b75806d1d54fcc684230680872b5a147580a 100644 (file)
@@ -19,7 +19,7 @@ import com.google.common.base.Preconditions;
 import com.google.common.collect.Iterables;
 import com.romix.scala.collection.concurrent.TrieMap;
 
-/*
+/**
  * A simple layer on top of maps, which performs snapshot mediation and optimization of
  * what the underlying implementation is.
  */
index 3dd76b81ecaddd085208653fa56c7c06d59eee34..1167a64fa65e40636868f465ca50f99d11131646 100644 (file)
                     </dependency>
                 </dependencies>
             </plugin>
-            <plugin>
-                <groupId>org.codehaus.mojo</groupId>
-                <artifactId>build-helper-maven-plugin</artifactId>
-                <executions>
-                    <execution>
-                        <phase>generate-sources</phase>
-                        <goals>
-                            <goal>add-source</goal>
-                        </goals>
-                        <configuration>
-                            <sources>
-                                <source>target/generated-sources/sal</source>
-                            </sources>
-                        </configuration>
-                    </execution>
-                </executions>
-            </plugin>
         </plugins>
     </build>
 
index 1ab33ade491179856532239d0582b53538c1f276..0ac89ae8da2c1d634d134bc07977877e5a9c4484 100644 (file)
@@ -117,59 +117,6 @@ public final class QName implements Immutable, Serializable, Comparable<QName> {
         return localName;
     }
 
-    /**
-     * QName Constructor.
-     *
-     * @param namespace
-     *            the namespace assigned to the YANG module
-     * @param revision
-     *            the revision of the YANG module
-     * @param localName
-     *            YANG schema identifier
-     *
-     * @deprecated Use {@link #create(URI, Date, String)} instead.
-     */
-    @Deprecated
-    public QName(final URI namespace, final Date revision, final String localName) {
-        this(QNameModule.create(namespace, revision), null, localName);
-    }
-
-    /**
-     * Construct new QName which reuses namespace, revision and prefix from
-     * base.
-     *
-     * @param base
-     * @param localName
-     * @deprecated Use {@link #create(QName, String)} instead.
-     */
-    @Deprecated
-    public QName(final QName base, final String localName) {
-        this(base.getNamespace(), base.getRevision(), base.getPrefix(), localName);
-    }
-
-    /**
-     * @deprecated Use {@link #create(String)} instead. This implementation is
-     *             broken.
-     */
-    @Deprecated
-    public QName(final String input) throws ParseException {
-        final String nsAndRev = input.substring(input.indexOf("(") + 1, input.indexOf(")"));
-        final Date revision;
-        final URI namespace;
-        if (nsAndRev.contains("?")) {
-            String[] splitted = nsAndRev.split("\\?");
-            namespace = URI.create(splitted[0]);
-            revision = getRevisionFormat().parse(splitted[1]);
-        } else {
-            namespace = URI.create(nsAndRev);
-            revision = null;
-        }
-
-        this.localName = checkLocalName(input.substring(input.indexOf(")") + 1));
-        this.prefix = null;
-        this.module = QNameModule.create(namespace, revision);
-    }
-
     public static QName create(final String input) {
         Matcher matcher = QNAME_PATTERN_FULL.matcher(input);
         if (matcher.matches()) {
@@ -281,7 +228,7 @@ public final class QName implements Immutable, Serializable, Comparable<QName> {
     }
 
     public static QName create(final QName base, final String localName) {
-        return new QName(base, localName);
+        return new QName(base.getModule(), base.getPrefix(), localName);
     }
 
     /**
index 82928944ec2c14e2fd0de6e0ed96bad40d83fd04..223157a223eee681231840c8b0d436dbe2ca0f83 100644 (file)
@@ -31,7 +31,7 @@ final class RandomPrefix {
                 do {
                     final StringBuilder sb = new StringBuilder();
                     for (int i = 0; i < 4; i++) {
-                        sb.append('a' + random.nextInt(25));
+                        sb.append((char)('a' + random.nextInt(25)));
                     }
 
                     prefix = sb.toString();
@@ -43,4 +43,4 @@ final class RandomPrefix {
 
         return prefix + ':' + qname.getLocalName();
     }
-}
\ No newline at end of file
+}
index 53f768c072e9bb1dcae93077c08b62bd2d117f77..ce2f036f16153e3e5383e32467304a00c5555fe6 100644 (file)
@@ -27,6 +27,10 @@ import javax.activation.UnsupportedDataTypeException;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.stream.XMLOutputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+import javax.xml.transform.dom.DOMResult;
 
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.AttributesContainer;
@@ -62,7 +66,6 @@ import org.w3c.dom.Element;
 import org.w3c.dom.NodeList;
 
 public class XmlDocumentUtils {
-
     private static class ElementWithSchemaContext {
         Element element;
         SchemaContext schemaContext;
@@ -81,16 +84,16 @@ public class XmlDocumentUtils {
         }
     }
 
+    public static final QName OPERATION_ATTRIBUTE_QNAME = QName.create(URI.create("urn:ietf:params:xml:ns:netconf:base:1.0"), null, "operation");
+    private static final Logger logger = LoggerFactory.getLogger(XmlDocumentUtils.class);
+    private static final XMLOutputFactory FACTORY = XMLOutputFactory.newFactory();
     private static final XmlCodecProvider DEFAULT_XML_VALUE_CODEC_PROVIDER = new XmlCodecProvider() {
-
         @Override
         public TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> codecFor(final TypeDefinition<?> baseType) {
             return TypeDefinitionAwareCodec.from(baseType);
         }
     };
 
-    private static final Logger logger = LoggerFactory.getLogger(XmlDocumentUtils.class);
-
     /**
      * Converts Data DOM structure to XML Document for specified XML Codec Provider and corresponding
      * Data Node Container schema. The CompositeNode data parameter enters as root of Data DOM tree and will
@@ -108,14 +111,19 @@ public class XmlDocumentUtils {
         Preconditions.checkNotNull(data);
         Preconditions.checkNotNull(schema);
 
-        Document doc = getDocument();
+        if (!(schema instanceof ContainerSchemaNode || schema instanceof ListSchemaNode)) {
+            throw new UnsupportedDataTypeException("Schema can be ContainerSchemaNode or ListSchemaNode. Other types are not supported yet.");
+        }
 
-        if (schema instanceof ContainerSchemaNode || schema instanceof ListSchemaNode) {
-            doc.appendChild(createXmlRootElement(doc, data, (SchemaNode) schema, codecProvider));
-            return doc;
-        } else {
-            throw new UnsupportedDataTypeException(
-                    "Schema can be ContainerSchemaNode or ListSchemaNode. Other types are not supported yet.");
+        final DOMResult result = new DOMResult();
+        try {
+            final XMLStreamWriter writer = FACTORY.createXMLStreamWriter(result);
+            XmlStreamUtils.create(codecProvider).writeDocument(writer, data, (SchemaNode)schema);
+            writer.close();
+            return (Document)result.getNode();
+        } catch (XMLStreamException e) {
+            logger.error("Failed to serialize data {}", data, e);
+            return null;
         }
     }
 
@@ -141,57 +149,17 @@ public class XmlDocumentUtils {
      * @return new instance of XML Document
      * @throws UnsupportedDataTypeException
      */
-    public static Document toDocument(final CompositeNode data, final XmlCodecProvider codecProvider)
-            throws UnsupportedDataTypeException {
-        Preconditions.checkNotNull(data);
-
-        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
-        dbf.setNamespaceAware(true);
-        Document doc = null;
+    public static Document toDocument(final CompositeNode data, final XmlCodecProvider codecProvider) {
+        final DOMResult result = new DOMResult();
         try {
-            DocumentBuilder bob = dbf.newDocumentBuilder();
-            doc = bob.newDocument();
-        } catch (ParserConfigurationException e) {
+            final XMLStreamWriter writer = FACTORY.createXMLStreamWriter(result);
+            XmlStreamUtils.create(codecProvider).writeDocument(writer, data);
+            writer.close();
+            return (Document)result.getNode();
+        } catch (XMLStreamException e) {
+            logger.error("Failed to serialize data {}", data, e);
             return null;
         }
-
-        doc.appendChild(createXmlRootElement(doc, data, null, codecProvider));
-        return doc;
-    }
-
-    private static Element createXmlRootElement(final Document doc, final Node<?> data, final SchemaNode schema,
-            final XmlCodecProvider codecProvider) throws UnsupportedDataTypeException {
-        Element itemEl = createElementFor(doc, data);
-        if (data instanceof SimpleNode<?>) {
-            if (schema instanceof LeafListSchemaNode) {
-                writeValueByType(itemEl, (SimpleNode<?>) data, ((LeafListSchemaNode) schema).getType(),
-                        (DataSchemaNode) schema, codecProvider);
-            } else if (schema instanceof LeafSchemaNode) {
-                writeValueByType(itemEl, (SimpleNode<?>) data, ((LeafSchemaNode) schema).getType(),
-                        (DataSchemaNode) schema, codecProvider);
-            } else {
-                Object value = data.getValue();
-                if (value != null) {
-                    itemEl.setTextContent(String.valueOf(value));
-                }
-            }
-        } else { // CompositeNode
-            for (Node<?> child : ((CompositeNode) data).getValue()) {
-                DataSchemaNode childSchema = null;
-                if (schema instanceof DataNodeContainer) {
-                    childSchema = findFirstSchema(child.getNodeType(), ((DataNodeContainer) schema).getChildNodes()).orNull();
-                    if (logger.isDebugEnabled()) {
-                        if (childSchema == null) {
-                            logger.debug("Probably the data node \""
-                                    + ((child == null) ? "" : child.getNodeType().getLocalName())
-                                    + "\" is not conform to schema");
-                        }
-                    }
-                }
-                itemEl.appendChild(createXmlRootElement(doc, child, childSchema, codecProvider));
-            }
-        }
-        return itemEl;
     }
 
     private static final Element createElementFor(final Document doc, final QName qname, final Object obj) {
@@ -376,8 +344,6 @@ public class XmlDocumentUtils {
         return ImmutableCompositeNode.create(qName, values, modifyAction.orNull());
     }
 
-    public static final QName OPERATION_ATTRIBUTE_QNAME = QName.create(URI.create("urn:ietf:params:xml:ns:netconf:base:1.0"), null, "operation");
-
     public static Optional<ModifyAction> getModifyOperationFromAttributes(final Element xmlElement) {
         Attr attributeNodeNS = xmlElement.getAttributeNodeNS(OPERATION_ATTRIBUTE_QNAME.getNamespace().toString(), OPERATION_ATTRIBUTE_QNAME.getLocalName());
         if(attributeNodeNS == null) {
index 64913914de515ad53f3d347c93ffc9b5def38149..1d319a5acec49db92d0fb909e266ebc85b313508 100644 (file)
@@ -1,10 +1,13 @@
 package org.opendaylight.yangtools.yang.data.impl.codec.xml;
 
+import com.google.common.annotations.Beta;
 import com.google.common.base.Preconditions;
 
 import java.net.URI;
 import java.util.Map.Entry;
 
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamWriter;
 
@@ -27,24 +30,61 @@ import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefi
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+/**
+ * Utility class for bridging JAXP Stream and YANG Data APIs. Note that the definition of this class
+ * by no means final and subject to change as more functionality is centralized here.
+ */
+@Beta
 public class XmlStreamUtils {
     private static final Logger LOG = LoggerFactory.getLogger(XmlStreamUtils.class);
+    private final XmlCodecProvider codecProvider;
 
-    public static void writeDataDocument(final XMLStreamWriter writer, final CompositeNode data, final SchemaNode schema, final XmlCodecProvider codecProvider) throws XMLStreamException {
-        //        final Boolean repairing = (Boolean) writer.getProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES);
-        //        Preconditions.checkArgument(repairing == true, "XML Stream Writer has to be repairing namespaces");
+    protected XmlStreamUtils(final XmlCodecProvider codecProvider) {
+        this.codecProvider = Preconditions.checkNotNull(codecProvider);
+    }
 
-        writer.writeStartDocument();
-        writeData(writer, data, schema, codecProvider);
-        writer.writeEndDocument();
-        writer.flush();
+    /**
+     * Create a new instance encapsulating a particular codec provider.
+     *
+     * @param codecProvider XML codec provider
+     * @return A new instance
+     */
+    public static XmlStreamUtils create(final XmlCodecProvider codecProvider) {
+        return new XmlStreamUtils(codecProvider);
     }
 
-    public static void writeDataDocument(final XMLStreamWriter writer, final CompositeNode data, final XmlCodecProvider codecProvider) throws XMLStreamException {
-        writeDataDocument(writer, data, null, codecProvider);
+    /**
+     * Check if a particular data element can be emitted as an empty element, bypassing value encoding. This
+     * functionality is optional, as valid XML stream is produced even if start/end element is produced unconditionally.
+     *
+     * @param data Data node
+     * @return True if the data node will result in empty element body.
+     */
+    public static boolean isEmptyElement(final Node<?> data) {
+        if (data == null) {
+            return true;
+        }
+
+        if (data instanceof CompositeNode) {
+            return ((CompositeNode) data).getValue().isEmpty();
+        }
+        if (data instanceof SimpleNode) {
+            return data.getValue() == null;
+        }
+
+        // Safe default
+        return false;
     }
 
-    public static void write(final XMLStreamWriter writer, final InstanceIdentifier id) throws XMLStreamException {
+    /**
+     * Write an InstanceIdentifier into the output stream. Calling corresponding {@link XMLStreamWriter#writeStartElement(String)}
+     * and {@link XMLStreamWriter#writeEndElement()} is the responsibility of the caller.
+     *
+     * @param writer XML Stream writer
+     * @param id InstanceIdentifier
+     * @throws XMLStreamException
+     */
+    public static void write(final @Nonnull XMLStreamWriter writer, final @Nonnull InstanceIdentifier id) throws XMLStreamException {
         Preconditions.checkNotNull(writer, "Writer may not be null");
         Preconditions.checkNotNull(id, "Variable should contain instance of instance identifier and can't be null");
 
@@ -57,14 +97,54 @@ public class XmlStreamUtils {
         writer.writeCharacters(str);
     }
 
-    public static void writeData(final XMLStreamWriter writer, final Node<?> data, final SchemaNode schema, final XmlCodecProvider codecProvider) throws XMLStreamException {
+    /**
+     * Write a full XML document corresponding to a CompositeNode into an XML stream writer.
+     *
+     * @param writer XML Stream writer
+     * @param data data node
+     * @param schema corresponding schema node, may be null
+     * @throws XMLStreamException if an encoding problem occurs
+     */
+    public void writeDocument(final @Nonnull XMLStreamWriter writer, final @Nonnull CompositeNode data, final @Nullable SchemaNode schema) throws XMLStreamException {
+        // final Boolean repairing = (Boolean) writer.getProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES);
+        // Preconditions.checkArgument(repairing == true, "XML Stream Writer has to be repairing namespaces");
+
+        writer.writeStartDocument();
+        writeElement(writer, data, schema);
+        writer.writeEndDocument();
+        writer.flush();
+    }
+
+    /**
+     * Short-hand for {@link #writeDataDocument(XMLStreamWriter, CompositeNode, SchemaNode, XmlCodecProvider)} with
+     * null SchemaNode.
+     *
+     * @param writer XML Stream writer
+     * @param data data node
+     * @param schema corresponding schema node, may be null
+     * @throws XMLStreamException if an encoding problem occurs
+     */
+    public void writeDocument(final XMLStreamWriter writer, final CompositeNode data) throws XMLStreamException {
+        writeDocument(writer, data, null);
+    }
+
+    /**
+     * Write an element into a XML stream writer. This includes the element start/end tags and
+     * the value of the element.
+     *
+     * @param writer XML Stream writer
+     * @param data data node
+     * @param schema Schema node
+     * @throws XMLStreamException if an encoding problem occurs
+     */
+    public void writeElement(final XMLStreamWriter writer, final @Nonnull Node<?> data, final SchemaNode schema) throws XMLStreamException {
         final QName qname = data.getNodeType();
         final String pfx = qname.getPrefix() != null ? qname.getPrefix() : "";
-        final String ns;
-        if (qname.getNamespace() != null) {
-            ns = qname.getNamespace().toString();
-        } else {
-            ns = "";
+        final String ns = qname.getNamespace() != null ? qname.getNamespace().toString() : "";
+
+        if (isEmptyElement(data)) {
+            writer.writeEmptyElement(pfx, qname.getLocalName(), ns);
+            return;
         }
 
         writer.writeStartElement(pfx, qname.getLocalName(), ns);
@@ -77,9 +157,9 @@ public class XmlStreamUtils {
         if (data instanceof SimpleNode<?>) {
             // Simple node
             if (schema instanceof LeafListSchemaNode) {
-                writeValue(writer, ((LeafListSchemaNode) schema).getType(), codecProvider, data.getValue());
+                writeValue(writer, ((LeafListSchemaNode) schema).getType(), data.getValue());
             } else if (schema instanceof LeafSchemaNode) {
-                writeValue(writer, ((LeafSchemaNode) schema).getType(), codecProvider, data.getValue());
+                writeValue(writer, ((LeafSchemaNode) schema).getType(), data.getValue());
             } else {
                 Object value = data.getValue();
                 if (value != null) {
@@ -99,62 +179,75 @@ public class XmlStreamUtils {
                     }
                 }
 
-                writeData(writer, child, childSchema, codecProvider);
+                writeElement(writer, child, childSchema);
             }
         }
 
         writer.writeEndElement();
     }
 
-    public static void writeValue(final XMLStreamWriter writer, final TypeDefinition<?> type, final XmlCodecProvider codecProvider, final Object nodeValue) throws XMLStreamException {
-        TypeDefinition<?> baseType = XmlUtils.resolveBaseTypeFrom(type);
+    /**
+     * Write a value into a XML stream writer. This method assumes the start and end of element is
+     * emitted by the caller.
+     *
+     * @param writer XML Stream writer
+     * @param data data node
+     * @param schema Schema node
+     * @throws XMLStreamException if an encoding problem occurs
+     */
+    public void writeValue(final @Nonnull XMLStreamWriter writer, final @Nonnull TypeDefinition<?> type, final Object value) throws XMLStreamException {
+        if (value == null) {
+            LOG.debug("Value of {}:{} is null, not encoding it", type.getQName().getNamespace(), type.getQName().getLocalName());
+            return;
+        }
+
+        final TypeDefinition<?> baseType = XmlUtils.resolveBaseTypeFrom(type);
         if (baseType instanceof IdentityrefTypeDefinition) {
-            if (nodeValue instanceof QName) {
-                QName value = (QName) nodeValue;
-                String prefix = "x";
-                if (value.getPrefix() != null && !value.getPrefix().isEmpty()) {
-                    prefix = value.getPrefix();
+            write(writer, (IdentityrefTypeDefinition) baseType, value);
+        } else if (baseType instanceof InstanceIdentifierTypeDefinition) {
+            write(writer, (InstanceIdentifierTypeDefinition) baseType, value);
+        } else {
+            final TypeDefinitionAwareCodec<Object, ?> codec = codecProvider.codecFor(baseType);
+            String text;
+            if (codec != null) {
+                try {
+                    text = codec.serialize(value);
+                } catch (ClassCastException e) {
+                    LOG.error("Provided node value {} did not have type {} required by mapping. Using stream instead.", value, baseType, e);
+                    text = String.valueOf(value);
                 }
-
-                writer.writeNamespace(prefix, value.getNamespace().toString());
-                writer.writeCharacters(prefix + ':' + value.getLocalName());
             } else {
-                Object value = nodeValue;
-                LOG.debug("Value of {}:{} is not instance of QName but is {}", baseType.getQName().getNamespace(),
-                        baseType.getQName().getLocalName(), value != null ? value.getClass() : "null");
-                if (value != null) {
-                    writer.writeCharacters(String.valueOf(value));
-                }
+                LOG.error("Failed to find codec for {}, falling back to using stream", baseType);
+                text = String.valueOf(value);
             }
-        } else if (baseType instanceof InstanceIdentifierTypeDefinition) {
-            if (nodeValue instanceof InstanceIdentifier) {
-                write(writer, (InstanceIdentifier)nodeValue);
+            writer.writeCharacters(text);
+        }
+    }
+
+    private static void write(final @Nonnull XMLStreamWriter writer, final @Nonnull IdentityrefTypeDefinition type, final @Nonnull Object value) throws XMLStreamException {
+        if (value instanceof QName) {
+            final QName qname = (QName) value;
+            final String prefix;
+            if (qname.getPrefix() != null && !qname.getPrefix().isEmpty()) {
+                prefix = qname.getPrefix();
             } else {
-                Object value = nodeValue;
-                LOG.debug("Value of {}:{} is not instance of InstanceIdentifier but is {}", baseType.getQName()
-                        .getNamespace(), //
-                        baseType.getQName().getLocalName(), value != null ? value.getClass() : "null");
-                if (value != null) {
-                    writer.writeCharacters(String.valueOf(value));
-                }
+                prefix = "x";
             }
+
+            writer.writeNamespace(prefix, qname.getNamespace().toString());
+            writer.writeCharacters(prefix + ':' + qname.getLocalName());
         } else {
-            if (nodeValue != null) {
-                final TypeDefinitionAwareCodec<Object, ?> codec = codecProvider.codecFor(baseType);
-                String text;
-                if (codec != null) {
-                    try {
-                        text = codec.serialize(nodeValue);
-                    } catch (ClassCastException e) {
-                        LOG.error("Provided node value {} did not have type {} required by mapping. Using stream instead.", nodeValue, baseType, e);
-                        text = String.valueOf(nodeValue);
-                    }
-                } else {
-                    LOG.error("Failed to find codec for {}, falling back to using stream", baseType);
-                    text = String.valueOf(nodeValue);
-                }
-                writer.writeCharacters(text);
-            }
+            LOG.debug("Value of {}:{} is not a QName but {}", type.getQName().getNamespace(), type.getQName().getLocalName(), value.getClass());
+            writer.writeCharacters(String.valueOf(value));
+        }
+    }
+
+    private static void write(final @Nonnull XMLStreamWriter writer, final @Nonnull InstanceIdentifierTypeDefinition type, final @Nonnull Object value) throws XMLStreamException {
+        if (value instanceof InstanceIdentifier) {
+            write(writer, (InstanceIdentifier)value);
+        } else {
+            LOG.debug("Value of {}:{} is not an InstanceIdentifier but {}", type.getQName().getNamespace(), type.getQName().getLocalName(), value.getClass());
+            writer.writeCharacters(String.valueOf(value));
         }
     }
 }
index dbdb6ff1b72909c4485bebdc11d1079f2d390ac7..052c250099ebae4a69857d70eb6c174aaf6ee472 100644 (file)
@@ -9,6 +9,8 @@ package org.opendaylight.yangtools.yang.data.impl.codec.xml;
 
 import java.util.Map;
 
+import javax.annotation.Nonnull;
+
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates;
@@ -26,7 +28,7 @@ final class XmlUtils {
 
     }
 
-    public static TypeDefinition<?> resolveBaseTypeFrom(final TypeDefinition<?> type) {
+    public static TypeDefinition<?> resolveBaseTypeFrom(final @Nonnull TypeDefinition<?> type) {
         TypeDefinition<?> superType = type;
         while (superType.getBaseType() != null) {
             superType = superType.getBaseType();
index 7013e55ad1fccdafcb4eb0c9e1550bfa9a4352df..668f7512c33f05e5a60749cbc540470b33465c0d 100644 (file)
@@ -50,7 +50,8 @@ final class InMemoryDataTree implements DataTree {
     public synchronized void setSchemaContext(final SchemaContext newSchemaContext) {
         Preconditions.checkNotNull(newSchemaContext);
 
-        LOG.info("Attempting to install schema context {}", newSchemaContext);
+        LOG.info("Attempting to install schema contexts");
+        LOG.debug("Following schema contexts will be attempted {}",newSchemaContext);
 
         /*
          * FIXME: we should walk the schema contexts, both current and new and see
diff --git a/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/codec/xml/RandomPrefixTest.java b/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/codec/xml/RandomPrefixTest.java
new file mode 100644 (file)
index 0000000..eb32e4d
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2014 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.yangtools.yang.data.impl.codec.xml;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.impl.codec.xml.RandomPrefix;
+
+/**
+ * @author tkubas
+ */
+public class RandomPrefixTest {
+    private RandomPrefix randomPrefix;
+
+    /**
+     * setup {@link #randomPrefix} instance
+     */
+    @Before
+    public void setUp() {
+        randomPrefix = new RandomPrefix();
+    }
+    /**
+     * Test method for {@link org.opendaylight.yangtools.yang.data.impl.codec.xml.RandomPrefix#encodeQName(QName)}.
+     */
+    @Test
+    public void testEncodeQName() {
+        QName node = QName.create("","2013-06-07","node");
+        String encodedQName = randomPrefix.encodeQName(node);
+        Assert.assertNotNull(encodedQName);
+        Assert.assertTrue("prefix is expected to contain 4 small letters as prefix but result is: "+encodedQName,
+                encodedQName.matches("[a-z]{4}:node"));
+    }
+
+}
diff --git a/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/TreeNodeUtilsTest.java b/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/TreeNodeUtilsTest.java
new file mode 100644 (file)
index 0000000..9098e32
--- /dev/null
@@ -0,0 +1,187 @@
+package org.opendaylight.yangtools.yang.data.impl.schema.tree;
+
+import com.google.common.base.Optional;
+import junit.framework.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
+import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNodeFactory;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
+import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Map;
+
+import static org.junit.Assert.*;
+import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.*;
+
+
+public class TreeNodeUtilsTest {
+    private static final Logger LOG = LoggerFactory.getLogger(TreeNodeUtilsTest.class);
+
+    private static final Short ONE_ID = 1;
+    private static final Short TWO_ID = 2;
+    private static final String TWO_ONE_NAME = "one";
+    private static final String TWO_TWO_NAME = "two";
+
+    private static final InstanceIdentifier OUTER_LIST_1_PATH = InstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
+            .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID) //
+            .build();
+
+    private static final InstanceIdentifier OUTER_LIST_2_PATH = InstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
+            .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID) //
+            .build();
+
+    private static final InstanceIdentifier TWO_TWO_PATH = InstanceIdentifier.builder(OUTER_LIST_2_PATH)
+            .node(TestModel.INNER_LIST_QNAME) //
+            .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_TWO_NAME) //
+            .build();
+
+    private static final MapEntryNode BAR_NODE = mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID) //
+            .withChild(mapNodeBuilder(TestModel.INNER_LIST_QNAME) //
+                    .withChild(mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_ONE_NAME)) //
+                    .withChild(mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_TWO_NAME)) //
+                    .build()) //
+            .build();
+
+    private SchemaContext schemaContext;
+    private RootModificationApplyOperation rootOper;
+
+    @Before
+    public void prepare() {
+        schemaContext = TestModel.createTestContext();
+        assertNotNull("Schema context must not be null.", schemaContext);
+        rootOper = RootModificationApplyOperation.from(SchemaAwareApplyOperation.from(schemaContext));
+    }
+
+    public NormalizedNode<?, ?> createDocumentOne() {
+        return ImmutableContainerNodeBuilder
+                .create()
+                .withNodeIdentifier(new InstanceIdentifier.NodeIdentifier(schemaContext.getQName()))
+                .withChild(createTestContainer()).build();
+
+    }
+
+    private ContainerNode createTestContainer() {
+        return ImmutableContainerNodeBuilder
+                .create()
+                .withNodeIdentifier(new InstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
+                .withChild(
+                        mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
+                                .withChild(mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID))
+                                .withChild(BAR_NODE).build()).build();
+    }
+
+    private static <T> T assertPresentAndType(final Optional<?> potential, final Class<T> type) {
+        assertNotNull(potential);
+        assertTrue(potential.isPresent());
+        assertTrue(type.isInstance(potential.get()));
+        return type.cast(potential.get());
+    }
+
+    @Test
+    public void findNodeTestNodeFound() {
+        InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
+                TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
+        TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
+        Optional<TreeNode> node = TreeNodeUtils.findNode(rootNode, OUTER_LIST_1_PATH);
+        assertPresentAndType(node, TreeNode.class);
+    }
+
+    @Test
+    public void findNodeTestNodeNotFound() {
+        InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
+                TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
+        TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
+        final InstanceIdentifier outerList1InvalidPath = InstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
+                .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 3) //
+                .build();
+        Optional<TreeNode> node = TreeNodeUtils.findNode(rootNode, outerList1InvalidPath);
+        Assert.assertFalse(node.isPresent());
+    }
+
+    @Test
+    public void findNodeCheckedTestNodeFound() {
+        InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
+                TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
+        TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
+        TreeNode foundNode = null;
+        try {
+            foundNode = TreeNodeUtils.findNodeChecked(rootNode, OUTER_LIST_1_PATH);
+        } catch (IllegalArgumentException e) {
+            fail("Illegal argument exception was thrown and should not have been" + e.getMessage());
+        }
+        Assert.assertNotNull(foundNode);
+    }
+
+    @Test
+    public void findNodeCheckedTestNodeNotFound() {
+        InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
+                TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
+        TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
+        final InstanceIdentifier outerList1InvalidPath = InstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
+                .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 3) //
+                .build();
+        try {
+            TreeNodeUtils.findNodeChecked(rootNode, outerList1InvalidPath);
+            fail("Illegal argument exception should have been thrown");
+        } catch (IllegalArgumentException e) {
+            LOG.debug("Illegal argument exception was thrown as expected: '{}' - '{}'", e.getClass(), e.getMessage());
+        }
+    }
+
+    @Test
+    public void findClosestOrFirstMatchTestNodeExists() {
+        InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
+                TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
+        TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
+        Optional<TreeNode> expectedNode = TreeNodeUtils.findNode(rootNode, TWO_TWO_PATH);
+        assertPresentAndType(expectedNode, TreeNode.class);
+        Map.Entry<InstanceIdentifier, TreeNode> actualNode = TreeNodeUtils.findClosest(rootNode, TWO_TWO_PATH);
+        Assert.assertEquals("Expected node and actual node are not the same", expectedNode.get(), actualNode.getValue());
+    }
+
+    @Test
+    public void findClosestOrFirstMatchTestNodeDoesNotExist() {
+        InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
+                TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
+        TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
+        final InstanceIdentifier outerListInnerListPath = InstanceIdentifier.builder(OUTER_LIST_2_PATH)
+                .node(TestModel.INNER_LIST_QNAME)
+                .build();
+        final InstanceIdentifier twoTwoInvalidPath = InstanceIdentifier.builder(OUTER_LIST_2_PATH)
+                .node(TestModel.INNER_LIST_QNAME) //
+                .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, "three") //
+                .build();
+        Optional<TreeNode> expectedNode = TreeNodeUtils.findNode(rootNode, outerListInnerListPath);
+        assertPresentAndType(expectedNode, TreeNode.class);
+        Map.Entry<InstanceIdentifier, TreeNode> actualNode = TreeNodeUtils.findClosest(rootNode, twoTwoInvalidPath);
+        Assert.assertEquals("Expected node and actual node are not the same", expectedNode.get(), actualNode.getValue());
+    }
+
+    @Test
+    public void getChildTestChildFound() {
+        InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
+                TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
+        TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
+        Optional<TreeNode> node = TreeNodeUtils.getChild(Optional.fromNullable(rootNode),
+                TestModel.TEST_PATH.getLastPathArgument());
+        assertPresentAndType(node, TreeNode.class);
+    }
+
+    @Test
+    public void getChildTestChildNotFound() {
+        InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
+                TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
+        TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
+        Optional<TreeNode> node = TreeNodeUtils.getChild(Optional.fromNullable(rootNode),
+                TestModel.OUTER_LIST_PATH.getLastPathArgument());
+        Assert.assertFalse(node.isPresent());
+    }
+}
index c4831ef3f2a56b49280ea1e29af96be1bccb6155..002746fa571d8b9616e4ca78ddfd406bcfd005ef 100644 (file)
@@ -123,7 +123,7 @@ public final class BaseTypes {
         }
         final List<QName> pathList = new ArrayList<QName>();
         for (final String path : actualPath) {
-            final QName qname = new QName(namespace, revision, path);
+            final QName qname = QName.create(namespace, revision, path);
             if (qname != null) {
                 pathList.add(qname);
             }
index 65fd92458f72564de1f9fd084490854fc4c50498..5a14d3097b7c2eb8fdf9c780e18fc3fb0582bf2e 100644 (file)
@@ -7,9 +7,9 @@
  */
 package org.opendaylight.yangtools.yang.model.util;
 
+import com.google.common.base.Preconditions;
 import java.util.Collections;
 import java.util.List;
-
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
@@ -17,8 +17,6 @@ import org.opendaylight.yangtools.yang.model.api.Status;
 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
 
-import com.google.common.base.Preconditions;
-
 /**
  * The <code>default</code> implementation of Instance Leafref Type Definition
  * interface.
@@ -27,7 +25,7 @@ import com.google.common.base.Preconditions;
  */
 public final class Leafref implements LeafrefTypeDefinition {
     private static final QName NAME = BaseTypes.constructQName("leafref");
-    private static final SchemaPath PATH = BaseTypes.schemaPath(NAME);
+    private static final SchemaPath PATH = SchemaPath.create(true, NAME);
     private static final String DESCRIPTION = "The leafref type is used to reference a particular leaf instance in the data tree.";
     private static final String REF = "https://tools.ietf.org/html/rfc6020#section-9.9";
 
index fbf4c4389b05f140dbc3452c367a18627f707109..57ed8fdd1da3ebdd47b89f690b6e1571114ef06b 100644 (file)
@@ -11,22 +11,14 @@ import com.google.common.base.Function;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Splitter;
 import com.google.common.collect.Iterables;
-import java.net.URI;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Date;
 import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Set;
 import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
-import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
@@ -38,7 +30,6 @@ import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
-import org.opendaylight.yangtools.yang.model.api.UsesNode;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -240,33 +231,6 @@ public final class SchemaContextUtil {
         return findNodeInModule(module, path);
     }
 
-    public static GroupingDefinition findGrouping(final SchemaContext context, final Module module, final Iterable<QName> path) {
-        Iterator<QName> iterator = path.iterator();
-        QName first = iterator.next();
-        Module m = context.findModuleByNamespace(first.getNamespace()).iterator().next();
-        DataNodeContainer currentParent = m;
-        for (QName qname : path) {
-            boolean found = false;
-            DataNodeContainer node = (DataNodeContainer) currentParent.getDataChildByName(qname.getLocalName());
-            if (node == null) {
-                Set<GroupingDefinition> groupings = currentParent.getGroupings();
-                for (GroupingDefinition gr : groupings) {
-                    if (gr.getQName().getLocalName().equals(qname.getLocalName())) {
-                        currentParent = gr;
-                        found = true;
-                    }
-                }
-            } else {
-                found = true;
-                currentParent = node;
-            }
-
-            Preconditions.checkArgument(found, "Failed to find referenced grouping: %s(%s)", path, qname.getLocalName());
-        }
-
-        return (GroupingDefinition) currentParent;
-    }
-
     private static SchemaNode findNodeInModule(final Module module, final Iterable<QName> path) {
         final QName current = path.iterator().next();
 
@@ -428,7 +392,7 @@ public final class SchemaContextUtil {
         return node;
     }
 
-    public static SchemaNode findNodeInCase(final ChoiceCaseNode parent, final Iterable<QName> path) {
+    private static SchemaNode findNodeInCase(final ChoiceCaseNode parent, final Iterable<QName> path) {
         final QName current = Iterables.getFirst(path, null);
         if (current == null) {
             return parent;
@@ -442,7 +406,7 @@ public final class SchemaContextUtil {
         return findNode(node, nextLevel(path));
     }
 
-    public static RpcDefinition getRpcByName(final Module module, final QName name) {
+    private static RpcDefinition getRpcByName(final Module module, final QName name) {
         for (RpcDefinition rpc : module.getRpcs()) {
             if (rpc.getQName().equals(name)) {
                 return rpc;
@@ -455,7 +419,7 @@ public final class SchemaContextUtil {
         return Iterables.skip(path, 1);
     }
 
-    public static NotificationDefinition getNotificationByName(final Module module, final QName name) {
+    private static NotificationDefinition getNotificationByName(final Module module, final QName name) {
         for (NotificationDefinition notification : module.getNotifications()) {
             if (notification.getQName().equals(name)) {
                 return notification;
@@ -464,7 +428,7 @@ public final class SchemaContextUtil {
         return null;
     }
 
-    public static GroupingDefinition getGroupingByName(final Module module, final QName name) {
+    private static GroupingDefinition getGroupingByName(final Module module, final QName name) {
         for (GroupingDefinition grouping : module.getGroupings()) {
             if (grouping.getQName().equals(name)) {
                 return grouping;
@@ -473,257 +437,6 @@ public final class SchemaContextUtil {
         return null;
     }
 
-    /**
-     * Utility method which search for original node defined in grouping.
-     *
-     * @param node
-     * @return
-     */
-    public static DataSchemaNode findOriginal(final DataSchemaNode node, final SchemaContext ctx) {
-        DataSchemaNode result = findCorrectTargetFromGrouping(node, ctx);
-        if (result == null) {
-            result = findCorrectTargetFromAugment(node, ctx);
-            if (result != null) {
-                if (result.isAddedByUses()) {
-                    result = findOriginal(result, ctx);
-                }
-            }
-        }
-        return result;
-    }
-
-    private static DataSchemaNode findCorrectImmediateTargetFromGrouping(final DataSchemaNode node, final SchemaContext ctx) {
-        // uses is under module statement
-        final Module m = findParentModule(ctx, node);
-        Preconditions.checkArgument(m != null, "Failed to find module for node {} in context {}", node, ctx);
-
-        for (final UsesNode u : m.getUses()) {
-            final SchemaNode targetGrouping = findNodeInSchemaContext(ctx, u.getGroupingPath().getPathFromRoot());
-            Preconditions.checkArgument(targetGrouping instanceof GroupingDefinition,
-                    "Failed to generate code for augment in %s", u);
-
-            LOG.trace("Checking grouping {} for node {}", targetGrouping, node);
-            final GroupingDefinition gr = (GroupingDefinition) targetGrouping;
-            final DataSchemaNode result = gr.getDataChildByName(node.getQName().getLocalName());
-            if (result != null) {
-                return result;
-            }
-
-            LOG.debug("Skipped grouping {}, no matching node found", gr);
-        }
-
-        throw new IllegalArgumentException(
-                String.format("Failed to find uses node matching {} in context {}", node, ctx));
-    }
-
-    private static DataSchemaNode findCorrectTargetFromGrouping(final DataSchemaNode node, final SchemaContext ctx) {
-        if (Iterables.size(node.getPath().getPathTowardsRoot()) != 1) {
-            QName currentName = node.getQName();
-            // tmpPath is used to track level of nesting
-            List<QName> tmpPath = new ArrayList<>();
-            Object parent = null;
-
-            // create schema path of parent node
-            SchemaPath sp = node.getPath().getParent();
-            parent = findDataSchemaNode(ctx, sp);
-
-            do {
-                tmpPath.add(currentName);
-
-                DataSchemaNode result = null;
-                // search parent node's used groupings for presence of wanted
-                // node
-                if (parent instanceof DataNodeContainer) {
-                    DataNodeContainer dataNodeParent = (DataNodeContainer) parent;
-                    for (UsesNode u : dataNodeParent.getUses()) {
-                        result = getResultFromUses(u, currentName.getLocalName(), ctx);
-                        if (result != null) {
-                            break;
-                        }
-                    }
-                }
-
-                // if node is not found in any of current parent's used
-                // groupings => parent is added by grouping too, so repeat same
-                // process for parent
-                if (result == null) {
-                    final SchemaNode sn = (SchemaNode) parent;
-
-                    // set current name to name of parent node
-                    currentName = sn.getQName();
-                    Preconditions.checkArgument(parent instanceof SchemaNode,
-                            "Failed to generate code for augmend node {} at parent {}", node, parent);
-
-                    // create schema path for parent of current parent
-                    final SchemaPath parentSp = sn.getPath().getParent();
-                    parent = parentSp.getPathFromRoot().iterator().hasNext() ? findDataSchemaNode(ctx, parentSp)
-                            : getParentModule(sn, ctx);
-                } else {
-                    // if wanted node was found in grouping, traverse this node
-                    // based on level of nesting
-                    return getTargetNode(tmpPath, result, ctx);
-                }
-            } while (!(parent instanceof Module));
-
-            return null;
-        } else {
-            return findCorrectImmediateTargetFromGrouping(node, ctx);
-        }
-    }
-
-    private static DataSchemaNode findCorrectTargetFromAugment(final DataSchemaNode node, final SchemaContext ctx) {
-        if (!node.isAugmenting()) {
-            return null;
-        }
-
-        QName currentName = node.getQName();
-        Object currentNode = node;
-        Object parent = node;
-        List<QName> tmpPath = new ArrayList<QName>();
-        List<SchemaNode> tmpTree = new ArrayList<SchemaNode>();
-
-        AugmentationSchema augment = null;
-        do {
-            SchemaPath sp = ((SchemaNode) parent).getPath();
-            parent = findDataSchemaNode(ctx, sp.getParent());
-            if (parent instanceof AugmentationTarget) {
-                tmpPath.add(currentName);
-                tmpTree.add((SchemaNode) currentNode);
-                augment = findNodeInAugment(((AugmentationTarget) parent).getAvailableAugmentations(), currentName);
-                if (augment == null) {
-                    currentName = ((DataSchemaNode) parent).getQName();
-                    currentNode = parent;
-                }
-            }
-        } while (((DataSchemaNode) parent).isAugmenting() && augment == null);
-
-        if (augment == null) {
-            return null;
-        } else {
-            Collections.reverse(tmpPath);
-            Collections.reverse(tmpTree);
-            Object actualParent = augment;
-            DataSchemaNode result = null;
-            for (QName name : tmpPath) {
-                if (actualParent instanceof DataNodeContainer) {
-                    result = ((DataNodeContainer) actualParent).getDataChildByName(name.getLocalName());
-                    actualParent = ((DataNodeContainer) actualParent).getDataChildByName(name.getLocalName());
-                } else {
-                    if (actualParent instanceof ChoiceNode) {
-                        result = ((ChoiceNode) actualParent).getCaseNodeByName(name.getLocalName());
-                        actualParent = ((ChoiceNode) actualParent).getCaseNodeByName(name.getLocalName());
-                    }
-                }
-            }
-
-            if (result.isAddedByUses()) {
-                result = findCorrectTargetFromAugmentGrouping(result, augment, tmpTree, ctx);
-            }
-
-            return result;
-        }
-    }
-
-    private static DataSchemaNode getResultFromUses(final UsesNode u, final String currentName, final SchemaContext ctx) {
-        SchemaNode targetGrouping = SchemaContextUtil.findNodeInSchemaContext(ctx, u.getGroupingPath()
-                .getPathFromRoot());
-        if (!(targetGrouping instanceof GroupingDefinition)) {
-            targetGrouping = findGrouping(ctx, getParentModule(targetGrouping, ctx), u.getGroupingPath()
-                    .getPathFromRoot());
-        }
-        Preconditions.checkArgument(targetGrouping instanceof GroupingDefinition,
-                "Failed to generate code for augment in %s", u);
-        GroupingDefinition gr = (GroupingDefinition) targetGrouping;
-        return gr.getDataChildByName(currentName);
-    }
-
-    private static Module getParentModule(final SchemaNode node, final SchemaContext ctx) {
-        QName qname = node.getPath().getPathFromRoot().iterator().next();
-        URI namespace = qname.getNamespace();
-        Date revision = qname.getRevision();
-        return ctx.findModuleByNamespaceAndRevision(namespace, revision);
-    }
-
-    private static DataSchemaNode getTargetNode(final List<QName> tmpPath, final DataSchemaNode node, final SchemaContext ctx) {
-        DataSchemaNode result = node;
-        if (tmpPath.size() == 1) {
-            if (result != null && result.isAddedByUses()) {
-                result = findOriginal(result, ctx);
-            }
-            return result;
-        } else {
-            DataSchemaNode newParent = result;
-            Collections.reverse(tmpPath);
-
-            tmpPath.remove(0);
-            for (QName name : tmpPath) {
-                // searching by local name is must, because node has different
-                // namespace in its original location
-                if (newParent == null) {
-                    break;
-                }
-                if (newParent instanceof DataNodeContainer) {
-                    newParent = ((DataNodeContainer) newParent).getDataChildByName(name.getLocalName());
-                } else {
-                    newParent = ((ChoiceNode) newParent).getCaseNodeByName(name.getLocalName());
-                }
-            }
-            if (newParent != null && newParent.isAddedByUses()) {
-                newParent = findOriginal(newParent, ctx);
-            }
-            return newParent;
-        }
-    }
-
-    private static AugmentationSchema findNodeInAugment(final Collection<AugmentationSchema> augments, final QName name) {
-        for (AugmentationSchema augment : augments) {
-            DataSchemaNode node = augment.getDataChildByName(name);
-            if (node != null) {
-                return augment;
-            }
-        }
-        return null;
-    }
-
-    private static DataSchemaNode findCorrectTargetFromAugmentGrouping(final DataSchemaNode node,
-            final AugmentationSchema parentNode, final List<SchemaNode> dataTree, final SchemaContext ctx) {
-
-        DataSchemaNode result = null;
-        QName currentName = node.getQName();
-        List<QName> tmpPath = new ArrayList<>();
-        tmpPath.add(currentName);
-        int i = 1;
-        Object parent = null;
-
-        do {
-            if (dataTree.size() < 2 || dataTree.size() == i) {
-                parent = parentNode;
-            } else {
-                parent = dataTree.get(dataTree.size() - (i + 1));
-                tmpPath.add(((SchemaNode) parent).getQName());
-            }
-
-            if (parent instanceof DataNodeContainer) {
-                DataNodeContainer dataNodeParent = (DataNodeContainer) parent;
-                for (UsesNode u : dataNodeParent.getUses()) {
-                    if (result == null) {
-                        result = getResultFromUses(u, currentName.getLocalName(), ctx);
-                    }
-                }
-            }
-
-            if (result == null) {
-                i = i + 1;
-                currentName = ((SchemaNode) parent).getQName();
-            }
-        } while (result == null);
-
-        if (result != null) {
-            result = getTargetNode(tmpPath, result, ctx);
-        }
-        return result;
-    }
-
     /**
      * Transforms string representation of XPath to Queue of QNames. The XPath
      * is split by "/" and for each part of XPath is assigned correct module in
index 2662bbb001fece80f80909260f6c669db3ee0e54..0c812a8c42163e2109b6717f334b198952711a6b 100644 (file)
                     <listener>true</listener>
                 </configuration>
             </plugin>
-            <plugin>
-                <groupId>org.codehaus.mojo</groupId>
-                <artifactId>build-helper-maven-plugin</artifactId>
-                <executions>
-                    <execution>
-                        <phase>generate-sources</phase>
-                        <goals>
-                            <goal>add-source</goal>
-                        </goals>
-                        <configuration>
-                            <sources>
-                                <source>target/generated-sources/parser</source>
-                            </sources>
-                        </configuration>
-                    </execution>
-                </executions>
-            </plugin>
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-javadoc-plugin</artifactId>
index 61b47f2cf5032585b10ce7533692857cd043e282..d59b0215a68cd9e1736b054802ff8a79e9e8dadb 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.yangtools.yang.parser.builder.impl;
 
+import static org.opendaylight.yangtools.yang.parser.builder.impl.BuilderUtils.findBaseIdentity;
+
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
@@ -86,6 +88,18 @@ public final class TypeUtils {
                 toRemove.add(unionType);
             }
         }
+        // special handling for identityref types under union
+        for (TypeDefinitionBuilder unionType : union.getTypedefs()) {
+            if (unionType instanceof IdentityrefTypeBuilder) {
+                IdentityrefTypeBuilder idref = (IdentityrefTypeBuilder) unionType;
+                IdentitySchemaNodeBuilder identity = findBaseIdentity(modules, module, idref.getBaseString(),
+                        idref.getLine());
+                if (identity == null) {
+                    throw new YangParseException(module.getName(), idref.getLine(), "Failed to find base identity");
+                }
+                idref.setBaseIdentity(identity);
+            }
+        }
         unionTypes.removeAll(toRemove);
     }
 
@@ -93,6 +107,10 @@ public final class TypeUtils {
             final Map<String, TreeMap<Date, ModuleBuilder>> modules, final ModuleBuilder module) {
         final QName utQName = ut.getQName();
         final ModuleBuilder dependentModuleBuilder = BuilderUtils.getModuleByPrefix(module, utQName.getPrefix());
+        if (dependentModuleBuilder == null) {
+            throw new YangParseException(module.getName(), union.getLine(), "No module found with prefix "
+                    + utQName.getPrefix());
+        }
         final TypeDefinitionBuilder resolvedType = findTypeDefinitionBuilder(union, dependentModuleBuilder,
                 utQName.getLocalName(), module.getName(), union.getLine());
         union.setTypedef(resolvedType);