BUG-4638: Introduce LeafTypeBuilders 17/29717/5
authorRobert Varga <robert.varga@pantheon.sk>
Sun, 15 Nov 2015 01:18:34 +0000 (02:18 +0100)
committerGerrit Code Review <gerrit@opendaylight.org>
Mon, 16 Nov 2015 11:29:02 +0000 (11:29 +0000)
Leaves need to override units/default value of their type, but unlike
typedefs do not need retain their SchemaPath.

Change-Id: I4c0539983b0f5023bd57a98c57aca75ebc9975fb
Signed-off-by: Robert Varga <robert.varga@pantheon.sk>
yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/type/DerivedTypeBuilder.java
yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/type/LeafTypeBuilder.java [new file with mode: 0644]
yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/type/LeafTypes.java [new file with mode: 0644]

index d90d1d88c86d7747476139fc00ca146cf713f467..5110f530af7e1a141824d1f8a32fff70b13f4200 100644 (file)
@@ -13,6 +13,11 @@ import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.Status;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
 
+/**
+ * Builder of {@link TypeDefinitions} for use in typedef statements.
+ *
+ * @param <T> Resulting {@link TypeDefinition}
+ */
 public abstract class DerivedTypeBuilder<T extends TypeDefinition<T>> extends TypeBuilder<T> {
     private Object defaultValue;
     private String description;
diff --git a/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/type/LeafTypeBuilder.java b/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/type/LeafTypeBuilder.java
new file mode 100644 (file)
index 0000000..8d2010c
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2015 Pantheon Technologies s.r.o. 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.model.util.type;
+
+import com.google.common.annotations.Beta;
+import com.google.common.base.Verify;
+import java.util.Objects;
+import javax.annotation.Nonnull;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
+
+/**
+ * Builder of {@link TypeDefinitions} for use in leaf statements. While similar to {@link DerivedTypeBuilder}, this
+ * builder does not support adding of unknown nodes and will return the base type if the type is not modified, hence
+ * not preserving the schema path.
+ *
+ * @param <T> Resulting {@link TypeDefinition}
+ */
+@Beta
+public abstract class LeafTypeBuilder<T extends TypeDefinition<T>> extends DerivedTypeBuilder<T> {
+    LeafTypeBuilder(final T baseType, final SchemaPath path) {
+        super(baseType, path);
+
+        if (baseType.getDescription() != null) {
+            setDescription(baseType.getDescription());
+        }
+        if (baseType.getReference() != null) {
+            setReference(baseType.getReference());
+        }
+        if (baseType.getStatus() != null) {
+            setStatus(baseType.getStatus());
+        }
+    }
+
+    /**
+     * Build the resulting type.
+     *
+     * @return A new type instance
+     */
+    @Nonnull abstract T buildType();
+
+    @Override
+    public final T build() {
+        final T base = getBaseType();
+        if (Objects.equals(getDefaultValue(), base.getDefaultValue()) && Objects.equals(getUnits(), base.getUnits())) {
+            return base;
+        }
+
+        return Verify.verifyNotNull(buildType());
+    }
+}
diff --git a/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/type/LeafTypes.java b/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/type/LeafTypes.java
new file mode 100644 (file)
index 0000000..3b5ee59
--- /dev/null
@@ -0,0 +1,199 @@
+/*
+ * Copyright (c) 2015 Pantheon Technologies s.r.o. 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.model.util.type;
+
+import com.google.common.annotations.Beta;
+import javax.annotation.Nonnull;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
+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.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.IntegerTypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
+
+/**
+ * Support for creating {@link TypeDefinition} instances defined by leaf and leaf-list statements.
+ */
+@Beta
+public final class LeafTypes {
+    private LeafTypes() {
+        throw new UnsupportedOperationException();
+    }
+
+    public static LeafTypeBuilder<?> leafTypeBuilder(@Nonnull final TypeDefinition<?> baseType,
+            @Nonnull final SchemaPath path) {
+        if (baseType instanceof BinaryTypeDefinition) {
+            return leafBinaryBuilder((BinaryTypeDefinition) baseType, path);
+        } else if (baseType instanceof BitsTypeDefinition) {
+            return leafBitsBuilder((BitsTypeDefinition) baseType, path);
+        } else if (baseType instanceof BooleanTypeDefinition) {
+            return leafBooleanBuilder((BooleanTypeDefinition) baseType, path);
+        } else if (baseType instanceof DecimalTypeDefinition) {
+            return leafDecimalBuilder((DecimalTypeDefinition) baseType, path);
+        } else if (baseType instanceof EmptyTypeDefinition) {
+            return leafEmptyBuilder((EmptyTypeDefinition) baseType, path);
+        } else if (baseType instanceof EnumTypeDefinition) {
+            return leafEnumerationBuilder((EnumTypeDefinition) baseType, path);
+        } else if (baseType instanceof IdentityrefTypeDefinition) {
+            return leafIdentityrefBuilder((IdentityrefTypeDefinition) baseType, path);
+        } else if (baseType instanceof InstanceIdentifierTypeDefinition) {
+            return leafInstanceIdentifierBuilder((InstanceIdentifierTypeDefinition) baseType, path);
+        } else if (baseType instanceof IntegerTypeDefinition) {
+            return leafIntegerBuilder((IntegerTypeDefinition) baseType, path);
+        } else if (baseType instanceof LeafrefTypeDefinition) {
+            return leafLeafrefBuilder((LeafrefTypeDefinition) baseType, path);
+        } else if (baseType instanceof StringTypeDefinition) {
+            return leafStringBuilder((StringTypeDefinition) baseType, path);
+        } else if (baseType instanceof UnionTypeDefinition) {
+            return leafUnionBuilder((UnionTypeDefinition) baseType, path);
+        } else if (baseType instanceof UnsignedIntegerTypeDefinition) {
+            return leafUnsignedBuilder((UnsignedIntegerTypeDefinition) baseType, path);
+        } else {
+            throw new IllegalArgumentException("Unhandled type definition class " + baseType.getClass());
+        }
+    }
+
+    private static LeafTypeBuilder<BinaryTypeDefinition> leafBinaryBuilder(@Nonnull final BinaryTypeDefinition baseType, @Nonnull final SchemaPath path) {
+        return new LeafTypeBuilder<BinaryTypeDefinition>(baseType, path) {
+            @Override
+            public BinaryTypeDefinition buildType() {
+                return new DerivedBinaryType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
+                    getStatus(), getUnits(), getUnknownSchemaNodes());
+            }
+        };
+    }
+
+    private static LeafTypeBuilder<BitsTypeDefinition> leafBitsBuilder(final BitsTypeDefinition baseType, final SchemaPath path) {
+        return new LeafTypeBuilder<BitsTypeDefinition>(baseType, path) {
+            @Override
+            public BitsTypeDefinition buildType() {
+                return new DerivedBitsType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
+                    getStatus(), getUnits(), getUnknownSchemaNodes());
+            }
+        };
+    }
+
+    private static LeafTypeBuilder<BooleanTypeDefinition> leafBooleanBuilder(@Nonnull final BooleanTypeDefinition baseType, @Nonnull final SchemaPath path) {
+        return new LeafTypeBuilder<BooleanTypeDefinition>(baseType, path) {
+            @Override
+            public BooleanTypeDefinition buildType() {
+                return new DerivedBooleanType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
+                    getStatus(), getUnits(), getUnknownSchemaNodes());
+            }
+        };
+    }
+
+    private static LeafTypeBuilder<DecimalTypeDefinition> leafDecimalBuilder(final DecimalTypeDefinition baseType, final SchemaPath path) {
+        return new LeafTypeBuilder<DecimalTypeDefinition>(baseType, path) {
+            @Override
+            public DecimalTypeDefinition buildType() {
+                return new DerivedDecimalType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
+                    getStatus(), getUnits(), getUnknownSchemaNodes());
+            }
+        };
+    }
+
+    private static LeafTypeBuilder<EmptyTypeDefinition> leafEmptyBuilder(final EmptyTypeDefinition baseType, final SchemaPath path) {
+        return new LeafTypeBuilder<EmptyTypeDefinition>(baseType, path) {
+            @Override
+            public EmptyTypeDefinition buildType() {
+                return new DerivedEmptyType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
+                    getStatus(), getUnits(), getUnknownSchemaNodes());
+            }
+        };
+    }
+
+    private static LeafTypeBuilder<EnumTypeDefinition> leafEnumerationBuilder(final EnumTypeDefinition baseType, final SchemaPath path) {
+        return new LeafTypeBuilder<EnumTypeDefinition>(baseType, path) {
+            @Override
+            public EnumTypeDefinition buildType() {
+                return new DerivedEnumerationType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
+                    getStatus(), getUnits(), getUnknownSchemaNodes());
+            }
+        };
+    }
+
+    private static LeafTypeBuilder<IdentityrefTypeDefinition> leafIdentityrefBuilder(final IdentityrefTypeDefinition baseType, final SchemaPath path) {
+        return new LeafTypeBuilder<IdentityrefTypeDefinition>(baseType, path) {
+            @Override
+            public IdentityrefTypeDefinition buildType() {
+                return new DerivedIdentityrefType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
+                    getStatus(), getUnits(), getUnknownSchemaNodes());
+            }
+        };
+    }
+
+    private static LeafTypeBuilder<InstanceIdentifierTypeDefinition> leafInstanceIdentifierBuilder(final InstanceIdentifierTypeDefinition baseType, final SchemaPath path) {
+        return new LeafTypeBuilder<InstanceIdentifierTypeDefinition>(baseType, path) {
+            @Override
+            public InstanceIdentifierTypeDefinition buildType() {
+                return new DerivedInstanceIdentifierType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
+                    getStatus(), getUnits(), getUnknownSchemaNodes(), baseType.requireInstance());
+            }
+        };
+    }
+
+    private static LeafTypeBuilder<IntegerTypeDefinition> leafIntegerBuilder(final IntegerTypeDefinition baseType, final SchemaPath path) {
+        return new LeafTypeBuilder<IntegerTypeDefinition>(baseType, path) {
+            @Override
+            public IntegerTypeDefinition buildType() {
+                return new DerivedIntegerType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
+                    getStatus(), getUnits(), getUnknownSchemaNodes());
+            }
+        };
+    }
+
+    private static LeafTypeBuilder<LeafrefTypeDefinition> leafLeafrefBuilder(final LeafrefTypeDefinition baseType, final SchemaPath path) {
+        return new LeafTypeBuilder<LeafrefTypeDefinition>(baseType, path) {
+            @Override
+            public LeafrefTypeDefinition buildType() {
+                return new DerivedLeafrefType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
+                    getStatus(), getUnits(), getUnknownSchemaNodes());
+            }
+        };
+    }
+
+    private static LeafTypeBuilder<StringTypeDefinition> leafStringBuilder(final StringTypeDefinition baseType, final SchemaPath path) {
+        return new LeafTypeBuilder<StringTypeDefinition>(baseType, path) {
+            @Override
+            public StringTypeDefinition buildType() {
+                return new DerivedStringType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
+                    getStatus(), getUnits(), getUnknownSchemaNodes());
+            }
+        };
+    }
+
+    private static LeafTypeBuilder<UnionTypeDefinition> leafUnionBuilder(final UnionTypeDefinition baseType, final SchemaPath path) {
+        return new LeafTypeBuilder<UnionTypeDefinition>(baseType, path) {
+            @Override
+            public DerivedUnionType buildType() {
+                return new DerivedUnionType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
+                    getStatus(), getUnits(), getUnknownSchemaNodes());
+            }
+        };
+    }
+
+    private static LeafTypeBuilder<UnsignedIntegerTypeDefinition> leafUnsignedBuilder(final UnsignedIntegerTypeDefinition baseType, final SchemaPath path) {
+        return new LeafTypeBuilder<UnsignedIntegerTypeDefinition>(baseType, path) {
+            @Override
+            public UnsignedIntegerTypeDefinition buildType() {
+                return new DerivedUnsignedType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
+                    getStatus(), getUnits(), getUnknownSchemaNodes());
+            }
+        };
+    }
+}