+ if (builder instanceof ChildNodeBuilder) {
+ ChildNodeBuilder cnb = (ChildNodeBuilder) builder;
+ correctAugmentChildPath(cnb, builder.getPath());
+ }
+
+ // if child can contains type, correct path for this type too
+ if(builder instanceof TypeAwareBuilder) {
+ TypeAwareBuilder nodeBuilder = (TypeAwareBuilder)builder;
+ QName nodeBuilderQName = nodeBuilder.getQName();
+ TypeDefinition<?> nodeBuilderType = nodeBuilder.getType();
+ if(nodeBuilderType != null) {
+ TypeDefinition<?> newType = createCorrectTypeDefinition(parentSchemaPath, nodeBuilderQName, nodeBuilderType);
+ nodeBuilder.setType(newType);
+ } else {
+ TypeDefinitionBuilder nodeBuilderTypedef = nodeBuilder.getTypedef();
+ SchemaPath newSchemaPath = createNewSchemaPath(nodeBuilderTypedef.getPath(), nodeBuilderQName, nodeBuilderTypedef.getQName());
+ nodeBuilderTypedef.setPath(newSchemaPath);
+ }
+ }
+ }
+ }
+
+ private static TypeDefinition<?> createCorrectTypeDefinition(SchemaPath parentSchemaPath, QName nodeQName, TypeDefinition<?> nodeType) {
+ TypeDefinition<?> result = null;
+ SchemaPath newSchemaPath = null;
+ if(nodeType != null) {
+ if(nodeType instanceof BinaryTypeDefinition) {
+ BinaryTypeDefinition binType = (BinaryTypeDefinition)nodeType;
+ newSchemaPath = createNewSchemaPath(parentSchemaPath, nodeQName, binType.getQName());
+ List<Byte> bytes = (List<Byte>)binType.getDefaultValue();
+ result = new BinaryType(newSchemaPath, bytes, binType.getLengthConstraints(), binType.getUnits());
+ } else if(nodeType instanceof BitsTypeDefinition) {
+ BitsTypeDefinition bitsType = (BitsTypeDefinition)nodeType;
+ newSchemaPath = createNewSchemaPath(parentSchemaPath, nodeQName, nodeType.getQName());
+ result = new BitsType(newSchemaPath, bitsType.getBits(), bitsType.getUnits());
+ } else if(nodeType instanceof BooleanTypeDefinition) {
+ BooleanTypeDefinition booleanType = (BooleanTypeDefinition)nodeType;
+ newSchemaPath = createNewSchemaPath(parentSchemaPath, nodeQName, booleanType.getQName());
+ result = new BooleanType(newSchemaPath, (Boolean)booleanType.getDefaultValue(), booleanType.getUnits());
+ } else if(nodeType instanceof DecimalTypeDefinition) {
+ DecimalTypeDefinition decimalType = (DecimalTypeDefinition)nodeType;
+ newSchemaPath = createNewSchemaPath(parentSchemaPath, nodeQName, decimalType.getQName());
+ BigDecimal defaultValue = (BigDecimal)decimalType.getDefaultValue();
+ result = new Decimal64(newSchemaPath, decimalType.getUnits(), defaultValue, decimalType.getRangeStatements(), decimalType.getFractionDigits());
+ } else if(nodeType instanceof EmptyTypeDefinition) {
+ newSchemaPath = createNewSchemaPath(parentSchemaPath, nodeQName, nodeType.getQName());
+ result = new EmptyType(newSchemaPath);
+ } else if(nodeType instanceof EnumTypeDefinition) {
+ EnumTypeDefinition enumType = (EnumTypeDefinition)nodeType;
+ newSchemaPath = createNewSchemaPath(parentSchemaPath, nodeQName, enumType.getQName());
+ result = new EnumerationType(newSchemaPath, (EnumPair)enumType.getDefaultValue(), enumType.getValues(), enumType.getUnits());
+ } else if(nodeType instanceof IdentityrefTypeDefinition) {
+ IdentityrefTypeDefinition idrefType = (IdentityrefTypeDefinition)nodeType;
+ newSchemaPath = createNewSchemaPath(parentSchemaPath, nodeQName, idrefType.getQName());
+ result = new IdentityrefType(idrefType.getIdentity(), newSchemaPath);
+ } else if(nodeType instanceof InstanceIdentifierTypeDefinition) {
+ InstanceIdentifierTypeDefinition instIdType = (InstanceIdentifierTypeDefinition)nodeType;
+ newSchemaPath = createNewSchemaPath(parentSchemaPath, nodeQName, instIdType.getQName());
+ return new InstanceIdentifier(newSchemaPath, instIdType.getPathStatement(), instIdType.requireInstance());
+ } else if(nodeType instanceof StringTypeDefinition) {
+ result = copyStringType(parentSchemaPath, nodeQName, (StringTypeDefinition)nodeType);
+ } else if(nodeType instanceof IntegerTypeDefinition) {
+ result = copyIntType(parentSchemaPath, nodeQName, (IntegerTypeDefinition)nodeType);
+ } else if(nodeType instanceof UnsignedIntegerTypeDefinition) {
+ result = copyUIntType(parentSchemaPath, nodeQName, (UnsignedIntegerTypeDefinition)nodeType);
+ } else if(nodeType instanceof LeafrefTypeDefinition) {
+ newSchemaPath = createNewSchemaPath(parentSchemaPath, nodeQName, nodeType.getQName());
+ result = new Leafref(newSchemaPath, ((LeafrefTypeDefinition)nodeType).getPathStatement());
+ } else if(nodeType instanceof UnionTypeDefinition) {
+ UnionTypeDefinition unionType = (UnionTypeDefinition)nodeType;
+ newSchemaPath = createNewSchemaPath(parentSchemaPath, nodeQName, unionType.getQName());
+ return new UnionType(newSchemaPath, unionType.getTypes());
+ }
+ }
+ return result;
+ }
+
+ private static TypeDefinition<?> copyStringType(SchemaPath schemaPath, QName nodeQName, StringTypeDefinition nodeType) {
+ List<QName> path = schemaPath.getPath();
+ List<QName> newPath = new ArrayList<QName>(path);
+ newPath.add(nodeQName);
+ newPath.add(nodeType.getQName());
+ SchemaPath newSchemaPath = new SchemaPath(newPath, schemaPath.isAbsolute());
+
+ String newDefault = nodeType.getDefaultValue().toString();
+ String newUnits = nodeType.getUnits();
+ List<LengthConstraint> lengths = nodeType.getLengthStatements();
+ List<PatternConstraint> patterns = nodeType.getPatterns();
+
+ return new StringType(newSchemaPath, newDefault, lengths, patterns, newUnits);
+ }
+
+ private static TypeDefinition<?> copyIntType(SchemaPath schemaPath, QName nodeQName, IntegerTypeDefinition type) {
+ QName typeQName = type.getQName();
+ SchemaPath newSchemaPath = createNewSchemaPath(schemaPath, nodeQName, typeQName);
+
+ String localName = typeQName.getLocalName();
+ List<RangeConstraint> ranges = type.getRangeStatements();
+ String units = type.getUnits();
+
+ if("int8".equals(localName)) {
+ Byte defaultValue = (Byte)type.getDefaultValue();
+ return new Int8(newSchemaPath, ranges, units, defaultValue);
+ } else if("int16".equals(localName)) {
+ Short defaultValue = (Short)type.getDefaultValue();
+ return new Int16(newSchemaPath, ranges, units, defaultValue);
+ } else if("int32".equals(localName)) {
+ Integer defaultValue = (Integer)type.getDefaultValue();
+ return new Int32(newSchemaPath, ranges, units, defaultValue);
+ } else if("int64".equals(localName)) {
+ Long defaultValue = (Long)type.getDefaultValue();
+ return new Int64(newSchemaPath, ranges, units, defaultValue);
+ } else {
+ return null;
+ }
+ }
+
+ private static TypeDefinition<?> copyUIntType(SchemaPath schemaPath, QName nodeQName, UnsignedIntegerTypeDefinition type) {
+ QName typeQName = type.getQName();
+ SchemaPath newSchemaPath = createNewSchemaPath(schemaPath, nodeQName, typeQName);
+
+ String localName = typeQName.getLocalName();
+ List<RangeConstraint> ranges = type.getRangeStatements();
+ String units = type.getUnits();
+
+ if("uint8".equals(localName)) {
+ Byte defaultValue = (Byte)type.getDefaultValue();
+ return new Int8(newSchemaPath, ranges, units, defaultValue);
+ } else if("uint16".equals(localName)) {
+ Short defaultValue = (Short)type.getDefaultValue();
+ return new Int16(newSchemaPath, ranges, units, defaultValue);
+ } else if("uint32".equals(localName)) {
+ Integer defaultValue = (Integer)type.getDefaultValue();
+ return new Int32(newSchemaPath, ranges, units, defaultValue);
+ } else if("uint64".equals(localName)) {
+ Long defaultValue = (Long)type.getDefaultValue();
+ return new Int64(newSchemaPath, ranges, units, defaultValue);
+ } else {
+ return null;
+ }
+ }
+
+ private static SchemaPath createNewSchemaPath(SchemaPath schemaPath, QName currentQName, QName qname) {
+ List<QName> newPath = new ArrayList<QName>(schemaPath.getPath());
+ newPath.add(currentQName);
+ newPath.add(qname);
+ return new SchemaPath(newPath, schemaPath.isAbsolute());
+ }
+
+ public static void refineLeaf(LeafSchemaNodeBuilder leaf,
+ RefineHolder refine, int line) {
+ String defaultStr = refine.getDefaultStr();
+ Boolean mandatory = refine.isMandatory();
+ MustDefinition must = refine.getMust();
+ List<UnknownSchemaNodeBuilder> unknownNodes = refine.getUnknownNodes();
+
+ if (defaultStr != null && !("".equals(defaultStr))) {
+ leaf.setDefaultStr(defaultStr);
+ }
+ if (mandatory != null) {
+ leaf.getConstraints().setMandatory(mandatory);
+ }
+ if (must != null) {
+ leaf.getConstraints().addMustDefinition(must);
+ }
+ if (unknownNodes != null) {
+ for (UnknownSchemaNodeBuilder unknown : unknownNodes) {
+ leaf.addUnknownSchemaNode(unknown);
+ }
+ }
+ }
+
+ public static void refineContainer(ContainerSchemaNodeBuilder container,
+ RefineHolder refine, int line) {
+ Boolean presence = refine.isPresence();
+ MustDefinition must = refine.getMust();
+ List<UnknownSchemaNodeBuilder> unknownNodes = refine.getUnknownNodes();
+
+ if (presence != null) {
+ container.setPresence(presence);
+ }
+ if (must != null) {
+ container.getConstraints().addMustDefinition(must);
+ }
+ if (unknownNodes != null) {
+ for (UnknownSchemaNodeBuilder unknown : unknownNodes) {
+ container.addUnknownSchemaNode(unknown);
+ }
+ }
+ }
+
+ public static void refineList(ListSchemaNodeBuilder list,
+ RefineHolder refine, int line) {
+ MustDefinition must = refine.getMust();
+ Integer min = refine.getMinElements();
+ Integer max = refine.getMaxElements();
+ List<UnknownSchemaNodeBuilder> unknownNodes = refine.getUnknownNodes();
+
+ if (must != null) {
+ list.getConstraints().addMustDefinition(must);
+ }
+ if (min != null) {
+ list.getConstraints().setMinElements(min);
+ }
+ if (max != null) {
+ list.getConstraints().setMaxElements(max);
+ }
+ if (unknownNodes != null) {
+ for (UnknownSchemaNodeBuilder unknown : unknownNodes) {
+ list.addUnknownSchemaNode(unknown);
+ }
+ }
+ }
+
+ public static void refineLeafList(LeafListSchemaNodeBuilder leafList,
+ RefineHolder refine, int line) {
+ MustDefinition must = refine.getMust();
+ Integer min = refine.getMinElements();
+ Integer max = refine.getMaxElements();
+ List<UnknownSchemaNodeBuilder> unknownNodes = refine.getUnknownNodes();
+
+ if (must != null) {
+ leafList.getConstraints().addMustDefinition(must);
+ }
+ if (min != null) {
+ leafList.getConstraints().setMinElements(min);
+ }
+ if (max != null) {
+ leafList.getConstraints().setMaxElements(max);
+ }
+ if (unknownNodes != null) {
+ for (UnknownSchemaNodeBuilder unknown : unknownNodes) {
+ leafList.addUnknownSchemaNode(unknown);
+ }
+ }
+ }
+
+ public static void refineChoice(ChoiceBuilder choice, RefineHolder refine,
+ int line) {
+ String defaultStr = refine.getDefaultStr();
+ Boolean mandatory = refine.isMandatory();
+ List<UnknownSchemaNodeBuilder> unknownNodes = refine.getUnknownNodes();
+
+ if (defaultStr != null) {
+ choice.setDefaultCase(defaultStr);
+ }
+ if (mandatory != null) {
+ choice.getConstraints().setMandatory(mandatory);
+ }
+ if (unknownNodes != null) {
+ for (UnknownSchemaNodeBuilder unknown : unknownNodes) {
+ choice.addUnknownSchemaNode(unknown);
+ }
+ }
+ }
+
+ public static void refineAnyxml(AnyXmlBuilder anyXml, RefineHolder refine,
+ int line) {
+ Boolean mandatory = refine.isMandatory();
+ MustDefinition must = refine.getMust();
+ List<UnknownSchemaNodeBuilder> unknownNodes = refine.getUnknownNodes();
+
+ if (mandatory != null) {
+ anyXml.getConstraints().setMandatory(mandatory);
+ }
+ if (must != null) {
+ anyXml.getConstraints().addMustDefinition(must);
+ }
+ if (unknownNodes != null) {
+ for (UnknownSchemaNodeBuilder unknown : unknownNodes) {
+ anyXml.addUnknownSchemaNode(unknown);
+ }
+ }
+ }
+
+ /**
+ * Perform refine operation of following parameters:
+ * <ul>
+ * <li>description</li>
+ * <li>reference</li>
+ * <li>config</li>
+ * </ul>
+ *
+ * These parameters may be refined for any node.
+ *
+ * @param node
+ * node to refine
+ * @param refine
+ * refine holder containing values to refine
+ * @param line
+ * current line in yang model
+ */
+ public static void refineDefault(Builder node, RefineHolder refine, int line) {
+ Class<? extends Builder> cls = node.getClass();
+
+ String description = refine.getDescription();
+ if (description != null) {
+ try {
+ Method method = cls.getDeclaredMethod("setDescription",
+ String.class);
+ method.invoke(node, description);
+ } catch (Exception e) {
+ throw new YangParseException(line,
+ "Cannot refine description in " + cls.getName(), e);
+ }
+ }
+
+ String reference = refine.getReference();
+ if (reference != null) {
+ try {
+ Method method = cls.getDeclaredMethod("setReference",
+ String.class);
+ method.invoke(node, reference);
+ } catch (Exception e) {
+ throw new YangParseException(line,
+ "Cannot refine reference in " + cls.getName(), e);
+ }
+ }
+
+ Boolean config = refine.isConfig();
+ if (config != null) {
+ try {
+ Method method = cls.getDeclaredMethod("setConfiguration",
+ Boolean.TYPE);
+ method.invoke(node, config);
+ } catch (Exception e) {
+ throw new YangParseException(line, "Cannot refine config in "
+ + cls.getName(), e);