Method can be used in code generation to create construction for new type with default value. Added tests.
Signed-off-by: Martin Vitez <mvitez@cisco.com>
if (node.augmenting || node.addedByUses) {
return null
}
- val packageName = packageNameForGeneratedType(basePackageName, (node).path)
+ val packageName = packageNameForGeneratedType(basePackageName, node.path)
val genType = addDefaultInterfaceDefinition(packageName, node, childOf)
if (node instanceof DataNodeContainer) {
genCtx.get(module).addChildNodeType(node.path, genType)
--- /dev/null
+/*
+ * 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.binding.generator.impl;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.io.File;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+
+import org.junit.Test;
+import org.opendaylight.yangtools.sal.binding.yang.types.TypeProviderImpl;
+import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.Module;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
+import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
+
+public class TypeProviderIntegrationTest {
+ private final String PKG = "org.opendaylight.yang.gen.v1.urn.opendaylight.test.rev131008.";
+
+ private SchemaContext resolveSchemaContextFromFiles(final String... yangFiles) {
+ final YangModelParser parser = new YangParserImpl();
+
+ final List<File> inputFiles = new ArrayList<File>();
+ for (int i = 0; i < yangFiles.length; ++i) {
+ inputFiles.add(new File(yangFiles[i]));
+ }
+
+ final Set<Module> modules = parser.parseYangModels(inputFiles);
+ return parser.resolveSchemaContext(modules);
+ }
+
+ @Test
+ public void testGetTypeDefaultConstruction1() throws ParseException {
+ final String path = getClass().getResource("/type-provider/test.yang").getPath();
+ final SchemaContext context = resolveSchemaContextFromFiles(path);
+ assertNotNull(context);
+ TypeProviderImpl provider = new TypeProviderImpl(context);
+ Module m = context.findModuleByName("test", new SimpleDateFormat("yyyy-MM-dd").parse("2013-10-08"));
+
+ LeafSchemaNode leaf = (LeafSchemaNode)m.getDataChildByName("id-binary");
+ String actual = provider.getTypeDefaultConstruction(leaf);
+ assertEquals("new byte[] {77, 97, 110}", actual);
+
+ leaf = (LeafSchemaNode)m.getDataChildByName("id-bits");
+ actual = provider.getTypeDefaultConstruction(leaf);
+ assertEquals("new " + PKG + "TestData.IdBits(false, false, true)", actual);
+
+ leaf = (LeafSchemaNode)m.getDataChildByName("id-boolean");
+ actual = provider.getTypeDefaultConstruction(leaf);
+ assertEquals("new java.lang.Boolean(\"true\")", actual);
+
+ leaf = (LeafSchemaNode)m.getDataChildByName("id-decimal64");
+ actual = provider.getTypeDefaultConstruction(leaf);
+ assertEquals("new java.math.BigDecimal(\"3.14\")", actual);
+
+ leaf = (LeafSchemaNode)m.getDataChildByName("id-empty");
+ actual = provider.getTypeDefaultConstruction(leaf);
+ assertEquals("new java.lang.Boolean(\"false\")", actual);
+
+ leaf = (LeafSchemaNode)m.getDataChildByName("id-enumeration");
+ actual = provider.getTypeDefaultConstruction(leaf);
+ assertEquals("org.opendaylight.yang.gen.v1.urn.opendaylight.test.rev131008.IdEnumeration.Seven", actual);
+
+ leaf = (LeafSchemaNode)m.getDataChildByName("id-8");
+ actual = provider.getTypeDefaultConstruction(leaf);
+ assertEquals("new java.lang.Byte(\"11\")", actual);
+
+ leaf = (LeafSchemaNode)m.getDataChildByName("id-16");
+ actual = provider.getTypeDefaultConstruction(leaf);
+ assertEquals("new java.lang.Short(\"111\")", actual);
+
+ leaf = (LeafSchemaNode)m.getDataChildByName("id-32");
+ actual = provider.getTypeDefaultConstruction(leaf);
+ assertEquals("new java.lang.Integer(\"1111\")", actual);
+
+ leaf = (LeafSchemaNode)m.getDataChildByName("id-64");
+ actual = provider.getTypeDefaultConstruction(leaf);
+ assertEquals("new java.lang.Long(\"11111\")", actual);
+
+ leaf = (LeafSchemaNode)m.getDataChildByName("id-leafref");
+ actual = provider.getTypeDefaultConstruction(leaf);
+ assertEquals("new java.math.BigDecimal(\"1.234\")", actual);
+
+ leaf = (LeafSchemaNode)m.getDataChildByName("id-string");
+ actual = provider.getTypeDefaultConstruction(leaf);
+ assertEquals("\"name\"", actual);
+
+ leaf = (LeafSchemaNode)m.getDataChildByName("id-u8");
+ actual = provider.getTypeDefaultConstruction(leaf);
+ assertEquals("new java.lang.Short(\"11\")", actual);
+
+ leaf = (LeafSchemaNode)m.getDataChildByName("id-u16");
+ actual = provider.getTypeDefaultConstruction(leaf);
+ assertEquals("new java.lang.Integer(\"111\")", actual);
+
+ leaf = (LeafSchemaNode)m.getDataChildByName("id-u32");
+ actual = provider.getTypeDefaultConstruction(leaf);
+ assertEquals("new java.lang.Long(\"1111\")", actual);
+
+ leaf = (LeafSchemaNode)m.getDataChildByName("id-u64");
+ actual = provider.getTypeDefaultConstruction(leaf);
+ assertEquals("new java.math.BigInteger(\"11111\")", actual);
+ }
+
+ @Test
+ public void testGetTypeDefaultConstruction2() throws ParseException {
+ final String path = getClass().getResource("/type-provider/test.yang").getPath();
+ final SchemaContext context = resolveSchemaContextFromFiles(path);
+ assertNotNull(context);
+ TypeProviderImpl provider = new TypeProviderImpl(context);
+ Module m = context.findModuleByName("test", new SimpleDateFormat("yyyy-MM-dd").parse("2013-10-08"));
+
+ LeafSchemaNode leaf = (LeafSchemaNode)m.getDataChildByName("ext-binary");
+ String actual = provider.getTypeDefaultConstruction(leaf);
+ assertEquals("new " + PKG + "MyBinary(new byte[] {77, 97, 110})", actual);
+
+ leaf = (LeafSchemaNode)m.getDataChildByName("ext-bits");
+ actual = provider.getTypeDefaultConstruction(leaf);
+ assertEquals("new " + PKG + "MyBits(false, false, true)", actual);
+
+ leaf = (LeafSchemaNode)m.getDataChildByName("ext-boolean");
+ actual = provider.getTypeDefaultConstruction(leaf);
+ assertEquals("new " + PKG + "MyBoolean(new java.lang.Boolean(\"true\"))", actual);
+
+ leaf = (LeafSchemaNode)m.getDataChildByName("ext-decimal64");
+ actual = provider.getTypeDefaultConstruction(leaf);
+ assertEquals("new " + PKG + "MyDecimal64(new java.math.BigDecimal(\"3.14\"))", actual);
+
+ leaf = (LeafSchemaNode)m.getDataChildByName("ext-empty");
+ actual = provider.getTypeDefaultConstruction(leaf);
+ assertEquals("new " + PKG + "MyEmpty(new java.lang.Boolean(\"false\"))", actual);
+
+ leaf = (LeafSchemaNode)m.getDataChildByName("ext-enumeration");
+ actual = provider.getTypeDefaultConstruction(leaf);
+ assertEquals("new " + PKG + "MyEnumeration(" + PKG + "MyEnumeration.Seven)", actual);
+
+ leaf = (LeafSchemaNode)m.getDataChildByName("ext-8");
+ actual = provider.getTypeDefaultConstruction(leaf);
+ assertEquals("new " + PKG + "My8(new java.lang.Byte(\"11\"))", actual);
+
+ leaf = (LeafSchemaNode)m.getDataChildByName("ext-16");
+ actual = provider.getTypeDefaultConstruction(leaf);
+ assertEquals("new " + PKG + "My16(new java.lang.Short(\"111\"))", actual);
+
+ leaf = (LeafSchemaNode)m.getDataChildByName("ext-32");
+ actual = provider.getTypeDefaultConstruction(leaf);
+ assertEquals("new " + PKG + "My32(new java.lang.Integer(\"1111\"))", actual);
+
+ leaf = (LeafSchemaNode)m.getDataChildByName("ext-64");
+ actual = provider.getTypeDefaultConstruction(leaf);
+ assertEquals("new " + PKG + "My64(new java.lang.Long(\"11111\"))", actual);
+
+ leaf = (LeafSchemaNode)m.getDataChildByName("ext-leafref");
+ actual = provider.getTypeDefaultConstruction(leaf);
+ assertEquals("new java.math.BigDecimal(\"1.234\")", actual);
+
+ leaf = (LeafSchemaNode)m.getDataChildByName("ext-string");
+ actual = provider.getTypeDefaultConstruction(leaf);
+ assertEquals("new " + PKG + "MyString(\"name\")", actual);
+
+ leaf = (LeafSchemaNode)m.getDataChildByName("ext-u8");
+ actual = provider.getTypeDefaultConstruction(leaf);
+ assertEquals("new " + PKG + "MyU8(new java.lang.Short(\"11\"))", actual);
+
+ leaf = (LeafSchemaNode)m.getDataChildByName("ext-u16");
+ actual = provider.getTypeDefaultConstruction(leaf);
+ assertEquals("new " + PKG + "MyU16(new java.lang.Integer(\"111\"))", actual);
+
+ leaf = (LeafSchemaNode)m.getDataChildByName("ext-u32");
+ actual = provider.getTypeDefaultConstruction(leaf);
+ assertEquals("new " + PKG + "MyU32(new java.lang.Long(\"1111\"))", actual);
+
+ leaf = (LeafSchemaNode)m.getDataChildByName("ext-u64");
+ actual = provider.getTypeDefaultConstruction(leaf);
+ assertEquals("new " + PKG + "MyU64(new java.math.BigInteger(\"11111\"))", actual);
+ }
+
+}
--- /dev/null
+module test {
+ yang-version 1;
+ namespace "urn:opendaylight:test";
+ prefix "t";
+
+ revision "2013-10-08" {
+ }
+
+
+ typedef my-binary {
+ type binary;
+ }
+
+ typedef my-bits {
+ type bits {
+ bit ctrl;
+ bit alt {
+ position 5;
+ }
+ bit delete;
+ }
+ }
+
+ typedef my-boolean {
+ type boolean;
+ }
+
+
+ typedef my-decimal64 {
+ type decimal64 {
+ fraction-digits 4;
+ }
+ }
+
+ typedef my-empty {
+ type empty;
+ }
+
+ typedef my-enumeration {
+ type enumeration {
+ enum zero;
+ enum one;
+ enum seven {
+ value 7;
+ }
+ }
+ }
+
+ typedef my-identityref {
+ type identityref {
+ base alg;
+ }
+ }
+
+ typedef my-8 {
+ type int8;
+ }
+ typedef my-16 {
+ type int16;
+ }
+ typedef my-32 {
+ type int32;
+ }
+ typedef my-64 {
+ type int64;
+ }
+
+ typedef my-leafref {
+ type leafref {
+ path "/links/node/id-64";
+ }
+ }
+
+ typedef my-string {
+ type string;
+ }
+
+ typedef my-u8 {
+ type uint8;
+ }
+ typedef my-u16 {
+ type uint16;
+ }
+ typedef my-u32 {
+ type uint32;
+ }
+ typedef my-u64 {
+ type uint64;
+ }
+
+
+
+ leaf id-binary {
+ type binary;
+ default "TWFu";
+ }
+
+ leaf id-bits {
+ type bits {
+ bit ctrl;
+ bit alt {
+ position 5;
+ }
+ bit delete;
+ }
+ default "delete";
+ }
+
+ leaf id-boolean {
+ type boolean;
+ default true;
+ }
+
+ leaf id-decimal64 {
+ type decimal64 {
+ fraction-digits 4;
+ }
+ default "3.14";
+ }
+
+ leaf id-empty {
+ type empty;
+ default false;
+ }
+
+ leaf id-enumeration {
+ type enumeration {
+ enum zero;
+ enum one;
+ enum seven {
+ value 7;
+ }
+ }
+ default seven;
+ }
+
+ leaf id-identityref {
+ type identityref {
+ base alg;
+ }
+ }
+
+ leaf id-8 {
+ type int8;
+ default "11";
+ }
+ leaf id-16 {
+ type int16;
+ default "111";
+ }
+ leaf id-32 {
+ type int32;
+ default "1111";
+ }
+ leaf id-64 {
+ type int64;
+ default "11111";
+ }
+
+ leaf id-leafref {
+ type leafref {
+ path "/links/node/id-64";
+ }
+ default "1.234";
+ }
+
+ leaf id-string {
+ type string;
+ default "name";
+ }
+
+ leaf id-u8 {
+ type uint8;
+ default "11";
+ }
+ leaf id-u16 {
+ type uint16;
+ default "111";
+ }
+ leaf id-u32 {
+ type uint32;
+ default "1111";
+ }
+ leaf id-u64 {
+ type uint64;
+ default "11111";
+ }
+
+
+
+ leaf ext-binary {
+ type my-binary;
+ default "TWFu";
+ }
+
+ leaf ext-bits {
+ type my-bits {
+ bit ctrl;
+ bit alt {
+ position 5;
+ }
+ bit delete;
+ }
+ default "delete";
+ }
+
+ leaf ext-boolean {
+ type my-boolean;
+ default true;
+ }
+
+ leaf ext-decimal64 {
+ type my-decimal64;
+ default "3.14";
+ }
+
+ leaf ext-empty {
+ type my-empty;
+ default false;
+ }
+
+ leaf ext-enumeration {
+ type my-enumeration;
+ default seven;
+ }
+
+ leaf ext-identityref {
+ type my-identityref;
+ }
+
+ leaf ext-8 {
+ type my-8;
+ default "11";
+ }
+ leaf ext-16 {
+ type my-16;
+ default "111";
+ }
+ leaf ext-32 {
+ type my-32;
+ default "1111";
+ }
+ leaf ext-64 {
+ type my-64;
+ default "11111";
+ }
+
+ leaf ext-leafref {
+ type my-leafref;
+ default "1.234";
+ }
+
+ leaf ext-string {
+ type my-string;
+ default "name";
+ }
+
+ leaf ext-u8 {
+ type my-u8;
+ default "11";
+ }
+ leaf ext-u16 {
+ type my-u16;
+ default "111";
+ }
+ leaf ext-u32 {
+ type my-u32;
+ default "1111";
+ }
+ leaf ext-u64 {
+ type my-u64;
+ default "11111";
+ }
+
+
+
+ list links {
+ container node {
+ leaf id-64 {
+ type decimal64 {
+ fraction-digits "7";
+ }
+ }
+ }
+ }
+
+ identity alg {
+ }
+
+}
\r
import org.opendaylight.yangtools.sal.binding.model.api.Restrictions;\r
import org.opendaylight.yangtools.sal.binding.model.api.Type;\r
+import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;\r
import org.opendaylight.yangtools.yang.model.api.SchemaNode;\r
import org.opendaylight.yangtools.yang.model.api.TypeDefinition;\r
\r
Type javaTypeForSchemaDefinitionType(final TypeDefinition<?> type, final SchemaNode parentNode);\r
\r
Type javaTypeForSchemaDefinitionType(final TypeDefinition<?> type, final SchemaNode parentNode, final Restrictions restrictions);\r
+\r
+ /**\r
+ * Returns string containing code for creation of new type instance.\r
+ *\r
+ * @param node\r
+ * @return\r
+ */\r
+ String getTypeDefaultConstruction(LeafSchemaNode node);\r
}\r
import org.opendaylight.yangtools.sal.binding.model.api.Restrictions;\r
import org.opendaylight.yangtools.sal.binding.model.api.Type;\r
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;\r
+import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;\r
import org.opendaylight.yangtools.yang.model.api.SchemaNode;\r
import org.opendaylight.yangtools.yang.model.api.TypeDefinition;\r
\r
default: return javaTypeForSchemaDefinitionType(type, parentNode);\r
}\r
}\r
+\r
+ @Override\r
+ public String getTypeDefaultConstruction(LeafSchemaNode node) {\r
+ return null;\r
+ }\r
};\r
}\r
import static org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil.*;
import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.*;
+import java.math.BigDecimal;
+import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTOBuilder;
import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTypeBuilderBase;
import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
+import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
import org.opendaylight.yangtools.yang.model.api.SchemaNode;
import org.opendaylight.yangtools.yang.model.api.SchemaPath;
import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.YangNode;
+import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
+import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
+import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
import org.opendaylight.yangtools.yang.model.util.DataNodeIterator;
import org.opendaylight.yangtools.yang.model.util.ExtendedType;
+import org.opendaylight.yangtools.yang.model.util.Int16;
+import org.opendaylight.yangtools.yang.model.util.Int32;
+import org.opendaylight.yangtools.yang.model.util.Int64;
+import org.opendaylight.yangtools.yang.model.util.Int8;
import org.opendaylight.yangtools.yang.model.util.StringType;
+import org.opendaylight.yangtools.yang.model.util.Uint16;
+import org.opendaylight.yangtools.yang.model.util.Uint32;
+import org.opendaylight.yangtools.yang.model.util.Uint64;
+import org.opendaylight.yangtools.yang.model.util.Uint8;
import org.opendaylight.yangtools.yang.model.util.UnionType;
import org.opendaylight.yangtools.yang.parser.util.ModuleDependencySort;
import com.google.common.base.Preconditions;
+import com.google.common.io.BaseEncoding;
public final class TypeProviderImpl implements TypeProvider {
/**
}
}
+ @Override
+ public String getTypeDefaultConstruction(LeafSchemaNode node) {
+ return getTypeDefaultConstruction(node, node.getDefault());
+ }
+
+ public String getTypeDefaultConstruction(LeafSchemaNode node, String defaultValue) {
+ TypeDefinition<?> type = node.getType();
+ Preconditions.checkNotNull(type, "Cannot provide default construction for null type of " + node);
+ Preconditions.checkNotNull(defaultValue, "Cannot provide default construction for null default statement of "
+ + node);
+
+ Module module = getParentModule(node);
+ String basePackageName = BindingGeneratorUtil.moduleNamespaceToPackageName(module);
+ String packageName = packageNameForGeneratedType(basePackageName, node.getPath());
+ String className = packageName + "." + parseToClassName(node.getQName().getLocalName());
+
+ StringBuilder sb = new StringBuilder();
+ TypeDefinition<?> base = baseTypeDefForExtendedType(type);
+ final boolean isBaseNull = base == null;
+
+ if (isBaseNull) {
+ base = type;
+ }
+
+ String result = null;
+ if (base instanceof BinaryTypeDefinition) {
+ result = binaryToDef(defaultValue);
+ } else if (base instanceof BitsTypeDefinition) {
+ String parentName;
+ YangNode parent = node.getParent();
+ if (parent instanceof Module) {
+ parentName = parseToClassName(((Module)parent).getName()) + "Data";
+ className = basePackageName + "." + parentName + "." + parseToClassName(node.getQName().getLocalName());
+ } else {
+ parentName = parseToClassName(((SchemaNode)parent).getQName().getLocalName());
+ className = packageName + "." + parentName + "." + parseToClassName(node.getQName().getLocalName());
+ }
+ result = bitsToDef((BitsTypeDefinition) base, className, defaultValue, isBaseNull);
+ } else if (base instanceof BooleanTypeDefinition) {
+ result = typeToDef(Boolean.class, defaultValue);
+ } else if (base instanceof DecimalTypeDefinition) {
+ result = typeToDef(BigDecimal.class, defaultValue);
+ } else if (base instanceof EmptyTypeDefinition) {
+ result = typeToDef(Boolean.class, defaultValue);
+ } else if (base instanceof EnumTypeDefinition) {
+ char[] defValArray = defaultValue.toCharArray();
+ char first = Character.toUpperCase(defaultValue.charAt(0));
+ defValArray[0] = first;
+ String newDefVal = new String(defValArray);
+ if (type instanceof ExtendedType) {
+ className = packageName + "." + parseToClassName(node.getType().getQName().getLocalName());
+ }
+ result = className + "." + newDefVal;
+ } else if (base instanceof IdentityrefTypeDefinition) {
+ throw new UnsupportedOperationException("Cannot get default construction for identityref type");
+ } else if (base instanceof InstanceIdentifierTypeDefinition) {
+ throw new UnsupportedOperationException("Cannot get default construction for instance-identifier type");
+ } else if (base instanceof Int8) {
+ result = typeToDef(Byte.class, defaultValue);
+ } else if (base instanceof Int16) {
+ result = typeToDef(Short.class, defaultValue);
+ } else if (base instanceof Int32) {
+ result = typeToDef(Integer.class, defaultValue);
+ } else if (base instanceof Int64) {
+ result = typeToDef(Long.class, defaultValue);
+ } else if (base instanceof LeafrefTypeDefinition) {
+ result = leafrefToDef(node, (LeafrefTypeDefinition)base);
+ } else if (base instanceof StringTypeDefinition) {
+ result = "\"" + defaultValue + "\"";
+ } else if (base instanceof Uint8) {
+ result = typeToDef(Short.class, defaultValue);
+ } else if (base instanceof Uint16) {
+ result = typeToDef(Integer.class, defaultValue);
+ } else if (base instanceof Uint32) {
+ result = typeToDef(Long.class, defaultValue);
+ } else if (base instanceof Uint64) {
+ result = typeToDef(BigInteger.class, defaultValue);
+ } else if (base instanceof UnionTypeDefinition) {
+ throw new UnsupportedOperationException("Cannot get default construction for union type");
+ } else {
+ result = "";
+ }
+ sb.append(result);
+
+ if (result != null && !result.isEmpty() && type instanceof ExtendedType && !(base instanceof LeafrefTypeDefinition)) {
+ className = packageName + "." + parseToClassName(node.getType().getQName().getLocalName());
+ sb.insert(0, "new " + className + "(");
+ sb.insert(sb.length(), ")");
+ }
+
+ return sb.toString();
+ }
+
+
+ private String typeToDef(Class<?> clazz, String defaultValue) {
+ return "new " + clazz.getName() + "(\"" + defaultValue + "\")";
+ }
+
+ private String binaryToDef(String defaultValue) {
+ StringBuilder sb = new StringBuilder();
+ BaseEncoding en = BaseEncoding.base64();
+ byte[] encoded = en.decode(defaultValue);
+ sb.append("new byte[] {");
+ for (int i = 0; i < encoded.length; i++) {
+ sb.append(encoded[i]);
+ if (i != encoded.length - 1) {
+ sb.append(", ");
+ }
+ }
+ sb.append("}");
+ return sb.toString();
+ }
+
+ private String bitsToDef(BitsTypeDefinition type, String className, String defaultValue, boolean isBase) {
+ List<Bit> bits = new ArrayList<>(type.getBits());
+ Collections.sort(bits, new Comparator<Bit>() {
+ @Override
+ public int compare(Bit o1, Bit o2) {
+ return o1.getName().compareTo(o2.getName());
+ }
+ });
+ StringBuilder sb = new StringBuilder();
+ sb.append(isBase ? "new " + className + "(" : "");
+ for (int i = 0; i < bits.size(); i++) {
+ if (bits.get(i).getName().equals(defaultValue)) {
+ sb.append(true);
+ } else {
+ sb.append(false);
+ }
+ if (i != bits.size() - 1) {
+ sb.append(", ");
+ }
+ }
+ sb.append(isBase ? ")" : "");
+ return sb.toString();
+ }
+
+ private Module getParentModule(YangNode node) {
+ if (node instanceof Module) {
+ return (Module) node;
+ }
+
+ YangNode parent = null;
+ if (node instanceof DataSchemaNode) {
+ parent = ((DataSchemaNode) node).getParent();
+ } else if (node instanceof DataNodeContainer) {
+ parent = ((DataNodeContainer) node).getParent();
+ } else {
+ parent = null;
+ }
+
+ while (parent != null && !(parent instanceof Module)) {
+ if (parent instanceof DataSchemaNode) {
+ parent = ((DataSchemaNode) parent).getParent();
+ } else if (parent instanceof DataNodeContainer) {
+ parent = ((DataNodeContainer) parent).getParent();
+ } else {
+ parent = null;
+ }
+ }
+ return (Module) parent;
+ }
+
+ private String leafrefToDef(LeafSchemaNode parentNode, LeafrefTypeDefinition leafrefType) {
+ Preconditions.checkArgument(leafrefType != null, "Leafref Type Definition reference cannot be NULL!");
+ Preconditions.checkArgument(leafrefType.getPathStatement() != null, "The Path Statement for Leafref Type Definition cannot be NULL!");
+
+ final RevisionAwareXPath xpath = leafrefType.getPathStatement();
+ final String strXPath = xpath.toString();
+
+ if (strXPath != null) {
+ if (strXPath.contains("[")) {
+ return "new java.lang.Object()";
+ } else {
+ final Module module = findParentModule(schemaContext, parentNode);
+ if (module != null) {
+ final SchemaNode dataNode;
+ if (xpath.isAbsolute()) {
+ dataNode = findDataSchemaNode(schemaContext, module, xpath);
+ } else {
+ dataNode = findDataSchemaNodeForRelativeXPath(schemaContext, module, parentNode, xpath);
+ }
+ return getTypeDefaultConstruction((LeafSchemaNode)dataNode, parentNode.getDefault());
+ }
+ }
+ }
+
+ return null;
+ }
+
}
patterns = Collections.emptyList();
}
- public static StringType getIntance() {
+ public static StringType getInstance() {
return INSTANCE;
}
type = Uint64.getInstance();
}
} else if ("string".equals(typeName)) {
- type = StringType.getIntance();
+ type = StringType.getInstance();
} else if ("binary".equals(typeName)) {
type = BinaryType.getInstance();
} else if ("boolean".equals(typeName)) {
List<EnumTypeDefinition.EnumPair> enumConstants = getEnumConstants(typeBody, actualPath, moduleName);
return new EnumerationType(baseTypePath, enumConstants);
} else if ("string".equals(typeName)) {
- StringTypeDefinition stringType = StringType.getIntance();
+ StringTypeDefinition stringType = StringType.getInstance();
constraints.addLengths(stringType.getLengthConstraints());
baseType = stringType;
} else if ("bits".equals(typeName)) {