--- /dev/null
+/*
+ * Copyright (c) 2015 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.binding.data.codec.api;
+
+import javax.annotation.Nullable;
+import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+
+/**
+ *
+ * Navigable tree representing hierarchy of Binding to Normalized Node codecs
+ *
+ * This navigable tree is associated to conrete set of YANG models, represented
+ * by SchemaContext and provides access to subtree specific serialization
+ * context.
+ *
+ * TODO: Add more detailed documentation
+ **/
+public interface BindingCodecTree {
+
+ @Nullable <T extends DataObject> BindingCodecTreeNode<T> getSubtreeCodec(InstanceIdentifier<T> path);
+
+ @Nullable BindingCodecTreeNode<?> getSubtreeCodec(YangInstanceIdentifier path);
+
+ @Nullable BindingCodecTreeNode<?> getSubtreeCodec(SchemaPath path);
+
+}
\ No newline at end of file
--- /dev/null
+/*
+ * Copyright (c) 2015 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.binding.data.codec.api;
+
+import com.google.common.annotations.Beta;
+import org.opendaylight.yangtools.sal.binding.generator.util.BindingRuntimeContext;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+
+public interface BindingCodecTreeFactory {
+
+ /**
+ *
+ * Creates Binding Codec Tree for specified Binding runtime context.
+ *
+ * @param context
+ * Binding Runtime Context for which Binding codecs should be
+ * instantiated.
+ * @return Binding Codec Tree for specified Binding runtime context.
+ */
+ BindingCodecTree create(BindingRuntimeContext context);
+
+ /**
+ *
+ * Creates Binding Codec Tree for specified Binding runtime context.
+ *
+ * @param context
+ * Binding Runtime Context for which Binding codecs should be
+ * instantiated.
+ * @param bindingClasses
+ * @return Binding Codec Tree for specified Binding runtime context.
+ */
+ @Beta
+ BindingCodecTree create(SchemaContext context, Class<?>... bindingClasses);
+
+}
--- /dev/null
+/*
+ * Copyright (c) 2015 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.binding.data.codec.api;
+
+import com.google.common.annotations.Beta;
+import com.google.common.base.Optional;
+import com.google.common.collect.ImmutableCollection;
+import java.util.List;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+import org.opendaylight.yangtools.yang.binding.BindingStreamEventWriter;
+import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
+
+/**
+ * Subtree codec specific to model subtree between Java Binding and
+ * NormalizedNode.
+ *
+ */
+@Beta
+public interface BindingCodecTreeNode<T extends DataObject> extends BindingNormalizedNodeCodec<T> {
+
+ /**
+ *
+ * Returns binding class of interface which represents API of current
+ * schema node.
+ *
+ * The result is same as invoking {@link DataObject#getImplementedInterface()}
+ * on instance of data.
+ *
+ * @return interface which defines API of binding representation of data.
+ */
+ @Nonnull
+ Class<T> getBindingClass();
+
+ /**
+ *
+ * Returns child context as if it was walked by
+ * {@link BindingStreamEventWriter}. This means that to enter case, one must
+ * issue getChild(ChoiceClass).getChild(CaseClass).
+ *
+ * @param childClass
+ * @return Context of child
+ * @throws IllegalArgumentException
+ * If supplied child class is not valid in specified context.
+ */
+ @Nonnull
+ <E extends DataObject> BindingCodecTreeNode<E> streamChild(@Nonnull Class<E> childClass);
+
+ /**
+ *
+ * Returns child context as if it was walked by
+ * {@link BindingStreamEventWriter}. This means that to enter case, one must
+ * issue getChild(ChoiceClass).getChild(CaseClass).
+ *
+ * This method differs from {@link #streamChild(Class)}, that is less
+ * stricter for interfaces representing augmentation and cases, that
+ * may return {@link BindingCodecTreeNode} even if augmentation interface
+ * containing same data was supplied and does not represent augmentation
+ * of this node.
+ *
+ * @param childClass
+ * @return Context of child or Optional absent is supplied class is not
+ * applicable in context.
+ */
+ <E extends DataObject> Optional<? extends BindingCodecTreeNode<E>> possibleStreamChild(@Nonnull Class<E> childClass);
+
+ /**
+ * Returns nested node context using supplied YANG Instance Identifier
+ *
+ * @param child
+ * Yang Instance Identifier Argument
+ * @return Context of child
+ * @throws IllegalArgumentException
+ * If supplied argument does not represent valid child.
+ */
+ @Nonnull
+ BindingCodecTreeNode<?> yangPathArgumentChild(@Nonnull YangInstanceIdentifier.PathArgument child);
+
+ /**
+ * Returns nested node context using supplied Binding Instance Identifier
+ * and adds YANG instance identifiers to supplied list.
+ *
+ * @param arg
+ * Binding Instance Identifier Argument
+ * @param builder
+ * Mutable instance of list, which is appended by YangInstanceIdentifiers
+ * as tree is walked. Use null if such side-product is not needed.
+ * @return Context of child
+ * @throws IllegalArgumentException
+ * If supplied argument does not represent valid child.
+ */
+ @Nonnull
+ BindingCodecTreeNode<?> bindingPathArgumentChild(@Nonnull InstanceIdentifier.PathArgument arg,
+ @Nullable List<YangInstanceIdentifier.PathArgument> builder);
+
+ /**
+ *
+ * Returns codec which uses caches serialization / deserialization results
+ *
+ * Caching may introduce performance penalty to serialization / deserialization
+ * but may decrease use of heap for repetitive objects.
+ *
+ *
+ * @param cacheSpecifier Set of objects, for which cache may be in place
+ * @return Codec whihc uses cache for serialization / deserialization.
+ */
+ @Nonnull
+ BindingNormalizedNodeCachingCodec<T> createCachingCodec(@Nonnull
+ ImmutableCollection<Class<? extends DataObject>> cacheSpecifier);
+
+ @Beta
+ void writeAsNormalizedNode(T data, NormalizedNodeStreamWriter writer);
+}
--- /dev/null
+/*
+ * Copyright (c) 2015 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.binding.data.codec.api;
+
+import com.google.common.annotations.Beta;
+import org.opendaylight.yangtools.yang.binding.DataObject;
+
+/**
+ *
+ * Caching variant of Binding to Normalized Node codec.
+ *
+ * Caching may introduce performance penalty to serialization / deserialization
+ * but may decrease use of heap for repetitive objects.
+ *
+ * @param <T> Binding representtion of data
+ *
+ */
+@Beta
+public interface BindingNormalizedNodeCachingCodec<T extends DataObject> extends BindingNormalizedNodeCodec<T>, AutoCloseable {
+
+ /**
+ *
+ * Invoking close will invalidate this codec and any of its child
+ * codecs and will invalidate cache.
+ *
+ * Any subsequent calls to this codec will fail with {@link IllegalStateException}
+ * thrown.
+ *
+ */
+ @Override
+ public void close();
+}
--- /dev/null
+/*
+ * Copyright (c) 2015 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.binding.data.codec.api;
+
+import com.google.common.annotations.Beta;
+import javax.annotation.Nonnull;
+import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+
+/**
+ *
+ * Codec providing serialization and deserializiation between Binding
+ * and NormalizedNode representation of data.
+ *
+ *
+ * @param <T> Binding representation of data
+ */
+@Beta
+public interface BindingNormalizedNodeCodec<T extends DataObject> {
+
+ /**
+ * Converts from Normalized Node to Binding representation of data.
+ *
+ * @param data Normalized Node representation of data
+ * @return Binding representation of data
+ */
+ @Nonnull T deserialize(@Nonnull NormalizedNode<?,?> data);
+
+ /**
+ * Converts from Binding to Normalized Node representation of data.
+ *
+ * @param data Binding representation of data
+ * @return Normalized Node representation of data
+ */
+ @Nonnull NormalizedNode<?,?> serialize(@Nonnull T data);
+
+}
protected def imports() '''
«IF !importMap.empty»
«FOR entry : importMap.entrySet»
- «IF entry.value != fullyQualifiedName»
+ «IF !hasSamePackage(entry.value)»
import «entry.value».«entry.key»;
«ENDIF»
«ENDFOR»
'''
+ /**
+ * Checks if packages of generated type and imported type is the same
+ *
+ * @param importedTypePackageNam
+ * the package name of imported type
+ * @return true if the packages are the same false otherwise
+ */
+ final private def boolean hasSamePackage(String importedTypePackageName) {
+ return type.packageName.equals(importedTypePackageName);
+ }
+
protected abstract def CharSequence body();
// Helper patterns
--- /dev/null
+/*
+ * 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.sal.java.api.generator.test;
+
+import static org.junit.Assert.assertFalse;
+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.io.IOException;
+import java.net.URISyntaxException;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+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;
+
+public class UnionTypedefUnusedImportTest extends BaseCompilationTest {
+
+ @Test
+ public void testUnionTypedefUnusedImport() throws Exception {
+ final File sourcesOutputDir = new File(GENERATOR_OUTPUT_PATH + FS + "union-typedef");
+ assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
+ final File compiledOutputDir = new File(COMPILER_OUTPUT_PATH + FS + "union-typedef");
+ assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
+
+ final List<File> sourceFiles = getSourceFiles("/compilation/union-typedef");
+ final SchemaContext context = parser.parseFiles(sourceFiles);
+ final List<Type> types = bindingGenerator.generateTypes(context);
+ final GeneratorJavaFile generator = new GeneratorJavaFile(new HashSet<>(types));
+ generator.generateToFile(sourcesOutputDir);
+ final boolean isUsedImport = containsImport("org.opendaylight.yang.gen.v1.org.opendaylight.yangtools.union.typedef.rev130208.TypedefUnion");
+ assertFalse(String.format("Class shouldn't contain import for this type '%s'", types.get(1).getName()),
+ isUsedImport);
+
+ testCompilation(sourcesOutputDir, compiledOutputDir);
+ cleanUp(sourcesOutputDir, compiledOutputDir);
+ }
+
+ private String readFile(String path, Charset encoding) throws IOException {
+ byte[] encoded = Files.readAllBytes(Paths.get(path));
+ return new String(encoded, encoding);
+ }
+
+ private boolean containsImport(final String fullImport) throws URISyntaxException, IOException {
+ final String filePath = GENERATOR_OUTPUT_PATH + FS + "union-typedef" + FS + "org" + FS + "opendaylight" + FS + "yang" + FS + "gen" + FS + "v1" + FS + "org" + FS + "opendaylight" + FS + "yangtools" + FS + "union" + FS + "typedef" + FS + "rev141124" + FS + "TypedefUnionBuilder.java";
+ final String fileContent = readFile(filePath, StandardCharsets.UTF_8);
+
+ if (fileContent.contains(fullImport)) {
+ return true;
+ }
+ return false;
+ }
+}
--- /dev/null
+module union-typedef-test {
+ yang-version 1;
+ namespace "org:opendaylight:yangtools:union:typedef";
+ prefix "tp";
+
+ description
+ "Test unused import";
+
+ revision "2014-11-24" {
+ reference "WILL BE DEFINED LATER";
+ }
+
+ typedef typedef-union {
+ type union {
+ type int32;
+ type string;
+ }
+ }
+}
\ No newline at end of file