Merge "Bug 1743: Fixed resolving of Identityrefs in xml."
authorRobert Varga <rovarga@cisco.com>
Mon, 8 Sep 2014 15:27:53 +0000 (15:27 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Mon, 8 Sep 2014 15:27:53 +0000 (15:27 +0000)
code-generator/binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/BaseTemplate.xtend
code-generator/binding-java-api-generator/src/test/java/org/opendaylight/yangtools/sal/java/api/generator/test/EndodingInJavaDocTest.java [new file with mode: 0644]
code-generator/binding-java-api-generator/src/test/resources/compilation/encoding-javadoc/encoding-javadoc.yang [new file with mode: 0644]
common/features/pom.xml
common/parent/pom.xml
yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/repo/util/InMemorySchemaSourceCache.java

index c258e12092b2188c444203470e6131430e1b4824..26c95d2fa3236738d775797a41816f4c458adaf8 100644 (file)
@@ -157,9 +157,7 @@ abstract class BaseTemplate {
     def protected CharSequence asJavadoc(String comment) {
         if(comment == null) return ''
         var txt = comment
-        if (txt.contains("*/")) {
-            txt = txt.replace("*/", "&#42;&#47;")
-        }
+
         txt = comment.trim
         txt = formatToParagraph(txt)
 
@@ -189,7 +187,7 @@ abstract class BaseTemplate {
     }
 
     def protected String formatDataForJavaDoc(GeneratedType type) {
-        val typeDescription = type.getDescription();
+        val typeDescription = type.getDescription().encodeJavadocSymbols;
 
         return '''
             «IF !typeDescription.nullOrEmpty»
@@ -198,6 +196,13 @@ abstract class BaseTemplate {
         '''.toString
     }
 
+    def encodeJavadocSymbols(String description) {
+        if (!description.nullOrEmpty) {
+            return description.replace("*/", "&#42;&#47;")
+        }
+        return description;
+    }
+
     def asLink(String text) {
         val StringBuilder sb = new StringBuilder()
         var tempText = text
@@ -230,7 +235,7 @@ abstract class BaseTemplate {
         var StringBuilder lineBuilder = new StringBuilder();
         var boolean isFirstElementOnNewLineEmptyChar = false;
 
-        formattedText = formattedText.replace("*/", "&#42;&#47;")
+        formattedText = formattedText.encodeJavadocSymbols
         formattedText = formattedText.replace(NEW_LINE, "")
         formattedText = formattedText.replace("\t", "")
         formattedText = formattedText.replaceAll(" +", " ");
@@ -254,8 +259,9 @@ abstract class BaseTemplate {
                 lineBuilder.setLength(0)
                 sb.append(NEW_LINE)
 
-                if(nextElement.toString == ' ')
+                if(nextElement.toString == ' ') {
                     isFirstElementOnNewLineEmptyChar = !isFirstElementOnNewLineEmptyChar;
+                }
             }
 
             if(isFirstElementOnNewLineEmptyChar) {
diff --git a/code-generator/binding-java-api-generator/src/test/java/org/opendaylight/yangtools/sal/java/api/generator/test/EndodingInJavaDocTest.java b/code-generator/binding-java-api-generator/src/test/java/org/opendaylight/yangtools/sal/java/api/generator/test/EndodingInJavaDocTest.java
new file mode 100644 (file)
index 0000000..80b073f
--- /dev/null
@@ -0,0 +1,43 @@
+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.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 java.io.File;
+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;
+
+/**
+ * Test if generated classes from yang file is compilable, generated javadoc comments contains
+ * symbols as javadoc comment tag, which caused of compilation problem.
+ */
+public class EndodingInJavaDocTest extends BaseCompilationTest {
+
+    @Test
+    public void testAugmentToUsesInAugment() throws Exception {
+        final File sourcesOutputDir = new File(GENERATOR_OUTPUT_PATH + FS + "encoding-javadoc");
+        assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
+        final File compiledOutputDir = new File(COMPILER_OUTPUT_PATH + FS + "encoding-javadoc");
+        assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
+
+        final List<File> sourceFiles = getSourceFiles("/compilation/encoding-javadoc");
+        final SchemaContext context = parser.parseFiles(sourceFiles);
+        final List<Type> types = bindingGenerator.generateTypes(context);
+        final GeneratorJavaFile generator = new GeneratorJavaFile(new HashSet<>(types));
+        generator.generateToFile(sourcesOutputDir);
+
+        // Test if sources are compilable
+        testCompilation(sourcesOutputDir, compiledOutputDir);
+        cleanUp(sourcesOutputDir, compiledOutputDir);
+    }
+
+}
diff --git a/code-generator/binding-java-api-generator/src/test/resources/compilation/encoding-javadoc/encoding-javadoc.yang b/code-generator/binding-java-api-generator/src/test/resources/compilation/encoding-javadoc/encoding-javadoc.yang
new file mode 100644 (file)
index 0000000..c66cef9
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ * 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
+ */
+module encoding-javadoc {
+    yang-version 1;
+    namespace "urn:opendaylight:encoding-javadoc";
+    prefix "enc";
+
+    revision "2014-04-09" {
+    }
+
+    container cont1 {
+        description "/e.g. 1/*/*";
+        reference "RFC 6020 - http://tools.ietf.org/html/rfc6020";
+
+        list list1 {
+            description "/e.g. 1/*/*";
+            reference "RFC 6020 - http://tools.ietf.org/html/rfc6020";
+
+            key "topology-id";
+            leaf topology-id {
+                description "/e.g. 1/*/*";
+                reference "RFC 6020 - http://tools.ietf.org/html/rfc6020";
+                type int32;
+            }
+            uses link1;
+        }
+    }
+
+    grouping link1 {
+        list link1 {
+            description "/e.g. 1/*/*";
+            reference "RFC 6020 - http://tools.ietf.org/html/rfc6020";
+            key "link-id";
+            uses link-attributes;
+        }
+    }
+
+    grouping link-attributes {
+        leaf link-id {
+            description "/e.g. 1/*/*";
+            reference "RFC 6020 - http://tools.ietf.org/html/rfc6020";
+            type int8;
+        }
+    }
+
+    leaf inclusion-rulez {
+        description "/e.g. 1/*/*";
+        reference "RFC 6020 http://technet.com";
+
+        type string;
+    }
+
+    leaf inclusion-rule {
+        description "/e.g. 1/*/*";
+        default include;
+
+        type enumeration {
+            enum include {
+                description
+                    "/e.g. 1/*/*";
+            }
+            enum exclude {
+                description
+                    "/e.g. 1/*/*";
+            }
+        }
+    }
+}
index 46c91492d06b1553f59ec5af433aaeb9985da0c0..7e457727033cd1492ed3b3fbcac02c63824b0716 100644 (file)
             <groupId>com.google.code.gson</groupId>
             <artifactId>gson</artifactId>
         </dependency>
+        <dependency>
+            <groupId>com.google.guava</groupId>
+            <artifactId>guava</artifactId>
+        </dependency>
 
         <!-- test the features.xml -->
         <dependency>
index 950a6ba9cb0cb81f5e9539f867650389824d9481..f39968b8d2e7960ebeb109523540fa4ff9e05d10 100644 (file)
             <dependency>
                 <groupId>org.javassist</groupId>
                 <artifactId>javassist</artifactId>
-                <version>3.17.1-GA</version>
+                <version>${javassist.version}</version>
             </dependency>
             <dependency>
                 <groupId>xml-apis</groupId>
index ad594e422116463671bb760fd75945ff647f0660..e63d7a8abcadf2615fdf04601d3c5f035f973d67 100644 (file)
@@ -7,14 +7,15 @@
 package org.opendaylight.yangtools.yang.model.repo.util;
 
 import com.google.common.annotations.Beta;
-import com.google.common.base.Preconditions;
+import com.google.common.base.FinalizablePhantomReference;
+import com.google.common.base.FinalizableReferenceQueue;
 import com.google.common.cache.Cache;
 import com.google.common.cache.CacheBuilder;
-import com.google.common.cache.RemovalListener;
-import com.google.common.cache.RemovalNotification;
 import com.google.common.util.concurrent.CheckedFuture;
 import com.google.common.util.concurrent.Futures;
-
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
@@ -24,29 +25,14 @@ import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
 
 @Beta
-public class InMemorySchemaSourceCache<T extends SchemaSourceRepresentation> extends AbstractSchemaSourceCache<T> {
-    private static final class CacheEntry<T extends SchemaSourceRepresentation> {
-        private final SchemaSourceRegistration<T> reg;
-        private final T source;
-
-        public CacheEntry(final T source, final SchemaSourceRegistration<T> reg) {
-            this.source = Preconditions.checkNotNull(source);
-            this.reg = Preconditions.checkNotNull(reg);
-        }
-    }
-
-    private static final RemovalListener<SourceIdentifier, CacheEntry<?>> LISTENER = new RemovalListener<SourceIdentifier, CacheEntry<?>>() {
-        @Override
-        public void onRemoval(final RemovalNotification<SourceIdentifier, CacheEntry<?>> notification) {
-            notification.getValue().reg.close();
-        }
-    };
-
-    private final Cache<SourceIdentifier, CacheEntry<T>> cache;
+public class InMemorySchemaSourceCache<T extends SchemaSourceRepresentation> extends AbstractSchemaSourceCache<T> implements AutoCloseable {
+    private final List<FinalizablePhantomReference<T>> regs = Collections.synchronizedList(new ArrayList<FinalizablePhantomReference<T>>());
+    private final FinalizableReferenceQueue queue = new FinalizableReferenceQueue();
+    private final Cache<SourceIdentifier, T> cache;
 
     protected InMemorySchemaSourceCache(final SchemaSourceRegistry consumer, final Class<T> representation, final CacheBuilder<Object, Object> builder) {
         super(consumer, representation, Costs.IMMEDIATE);
-        cache = builder.removalListener(LISTENER).build();
+        cache = builder.build();
     }
 
     public static <R extends SchemaSourceRepresentation> InMemorySchemaSourceCache<R> createSoftCache(final SchemaSourceRegistry consumer, final Class<R> representation) {
@@ -55,9 +41,9 @@ public class InMemorySchemaSourceCache<T extends SchemaSourceRepresentation> ext
 
     @Override
     public CheckedFuture<? extends T, SchemaSourceException> getSource(final SourceIdentifier sourceIdentifier) {
-        final CacheEntry<T> present = cache.getIfPresent(sourceIdentifier);
+        final T present = cache.getIfPresent(sourceIdentifier);
         if (present != null) {
-            return Futures.immediateCheckedFuture(present.source);
+            return Futures.immediateCheckedFuture(present);
         }
 
         return Futures.<T, SchemaSourceException>immediateFailedCheckedFuture(new MissingSchemaSourceException("Source not found", sourceIdentifier));
@@ -65,10 +51,31 @@ public class InMemorySchemaSourceCache<T extends SchemaSourceRepresentation> ext
 
     @Override
     protected void offer(final T source) {
-        final CacheEntry<T> present = cache.getIfPresent(source.getIdentifier());
+        final T present = cache.getIfPresent(source.getIdentifier());
         if (present == null) {
+            cache.put(source.getIdentifier(), source);
+
             final SchemaSourceRegistration<T> reg = register(source.getIdentifier());
-            cache.put(source.getIdentifier(), new CacheEntry<T>(source, reg));
+            final FinalizablePhantomReference<T> ref = new FinalizablePhantomReference<T>(source, queue) {
+                @Override
+                public void finalizeReferent() {
+                    reg.close();
+                    regs.remove(this);
+                }
+            };
+
+            regs.add(ref);
         }
     }
+
+    @Override
+    public void close() {
+        while (!regs.isEmpty()) {
+            final FinalizablePhantomReference<?> ref = regs.get(0);
+            ref.finalizeReferent();
+        }
+
+        cache.invalidateAll();
+        queue.close();
+    }
 }