2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.mdsal.binding.yang.types;
10 import static java.util.Objects.requireNonNull;
11 import static org.opendaylight.mdsal.binding.model.util.BindingTypes.TYPE_OBJECT;
12 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findDataSchemaNodeForRelativeXPath;
13 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findDataTreeSchemaNode;
14 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findParentModule;
16 import com.google.common.annotations.Beta;
17 import com.google.common.annotations.VisibleForTesting;
18 import com.google.common.base.Preconditions;
19 import com.google.common.base.Strings;
20 import com.google.common.collect.ImmutableMap;
21 import java.math.BigDecimal;
22 import java.util.ArrayList;
23 import java.util.Base64;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.Comparator;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.Iterator;
30 import java.util.List;
32 import java.util.Optional;
34 import java.util.TreeMap;
35 import java.util.regex.Pattern;
36 import org.opendaylight.mdsal.binding.generator.spi.TypeProvider;
37 import org.opendaylight.mdsal.binding.generator.util.BaseYangTypes;
38 import org.opendaylight.mdsal.binding.model.api.AccessModifier;
39 import org.opendaylight.mdsal.binding.model.api.ConcreteType;
40 import org.opendaylight.mdsal.binding.model.api.Enumeration;
41 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty;
42 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
43 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
44 import org.opendaylight.mdsal.binding.model.api.Restrictions;
45 import org.opendaylight.mdsal.binding.model.api.Type;
46 import org.opendaylight.mdsal.binding.model.api.type.builder.EnumBuilder;
47 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedPropertyBuilder;
48 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTOBuilder;
49 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilder;
50 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilderBase;
51 import org.opendaylight.mdsal.binding.model.api.type.builder.MethodSignatureBuilder;
52 import org.opendaylight.mdsal.binding.model.util.BindingGeneratorUtil;
53 import org.opendaylight.mdsal.binding.model.util.TypeConstants;
54 import org.opendaylight.mdsal.binding.model.util.Types;
55 import org.opendaylight.mdsal.binding.model.util.generated.type.builder.AbstractEnumerationBuilder;
56 import org.opendaylight.mdsal.binding.model.util.generated.type.builder.GeneratedPropertyBuilderImpl;
57 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
58 import org.opendaylight.yangtools.yang.common.QName;
59 import org.opendaylight.yangtools.yang.common.Revision;
60 import org.opendaylight.yangtools.yang.common.Uint16;
61 import org.opendaylight.yangtools.yang.common.Uint32;
62 import org.opendaylight.yangtools.yang.common.Uint64;
63 import org.opendaylight.yangtools.yang.common.Uint8;
64 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
65 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
66 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
67 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
68 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
69 import org.opendaylight.yangtools.yang.model.api.Module;
70 import org.opendaylight.yangtools.yang.model.api.PathExpression;
71 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
72 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
73 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
74 import org.opendaylight.yangtools.yang.model.api.Status;
75 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
76 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
77 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
78 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
79 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
80 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
81 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
82 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
83 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
84 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
85 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
86 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
87 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
88 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
89 import org.opendaylight.yangtools.yang.model.util.ModuleDependencySort;
90 import org.opendaylight.yangtools.yang.model.util.PathExpressionImpl;
91 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
92 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
93 import org.opendaylight.yangtools.yang.model.util.type.CompatUtils;
94 import org.slf4j.Logger;
95 import org.slf4j.LoggerFactory;
98 public abstract class AbstractTypeProvider implements TypeProvider {
99 private static final Logger LOG = LoggerFactory.getLogger(AbstractTypeProvider.class);
100 private static final Pattern GROUPS_PATTERN = Pattern.compile("\\[(.*?)\\]");
102 // Backwards compatibility: Union types used to be instantiated in YANG namespace, which is no longer
103 // the case, as unions are emitted to their correct schema path.
104 private static final SchemaPath UNION_PATH = SchemaPath.create(true,
105 org.opendaylight.yangtools.yang.model.util.BaseTypes.UNION_QNAME);
108 * Contains the schema data red from YANG files.
110 private final SchemaContext schemaContext;
112 private final Map<String, Map<Optional<Revision>, Map<String, Type>>> genTypeDefsContextMap = new HashMap<>();
115 * The map which maps schema paths to JAVA <code>Type</code>.
117 private final Map<SchemaPath, Type> referencedTypes = new HashMap<>();
118 private final Map<Module, Set<Type>> additionalTypes = new HashMap<>();
119 private final Map<SchemaNode, JavaTypeName> renames;
122 * Creates new instance of class <code>TypeProviderImpl</code>.
124 * @param schemaContext contains the schema data red from YANG files
125 * @param renames renaming table
126 * @throws IllegalArgumentException if <code>schemaContext</code> equal null.
128 AbstractTypeProvider(final SchemaContext schemaContext, final Map<SchemaNode, JavaTypeName> renames) {
129 Preconditions.checkArgument(schemaContext != null, "Schema Context cannot be null!");
130 this.schemaContext = schemaContext;
131 this.renames = requireNonNull(renames);
132 resolveTypeDefsFromContext();
136 * Puts <code>refType</code> to map with key <code>refTypePath</code>.
138 * @param refTypePath schema path used as the map key
139 * @param refType type which represents the map value
140 * @throws IllegalArgumentException
142 * <li>if <code>refTypePath</code> equal null</li>
143 * <li>if <code>refType</code> equal null</li>
147 public void putReferencedType(final SchemaPath refTypePath, final Type refType) {
148 Preconditions.checkArgument(refTypePath != null,
149 "Path reference of Enumeration Type Definition cannot be NULL!");
150 Preconditions.checkArgument(refType != null, "Reference to Enumeration Type cannot be NULL!");
151 referencedTypes.put(refTypePath, refType);
154 public Map<Module, Set<Type>> getAdditionalTypes() {
155 return additionalTypes;
159 public Type javaTypeForSchemaDefinitionType(final TypeDefinition<?> typeDefinition, final SchemaNode parentNode,
160 final boolean lenientRelativeLeafrefs) {
161 return javaTypeForSchemaDefinitionType(typeDefinition, parentNode, null, lenientRelativeLeafrefs);
165 * Converts schema definition type <code>typeDefinition</code> to JAVA <code>Type</code>.
167 * @param typeDefinition type definition which is converted to JAVA type
168 * @throws IllegalArgumentException
170 * <li>if <code>typeDefinition</code> equal null</li>
171 * <li>if Qname of <code>typeDefinition</code> equal null</li>
172 * <li>if name of <code>typeDefinition</code> equal null</li>
176 public Type javaTypeForSchemaDefinitionType(final TypeDefinition<?> typeDefinition, final SchemaNode parentNode,
177 final Restrictions restrictions, final boolean lenientRelativeLeafrefs) {
178 Preconditions.checkArgument(typeDefinition != null, "Type Definition cannot be NULL!");
179 Preconditions.checkArgument(typeDefinition.getQName() != null,
180 "Type Definition cannot have non specified QName (QName cannot be NULL!)");
181 final String typedefName = typeDefinition.getQName().getLocalName();
182 Preconditions.checkArgument(typedefName != null, "Type Definitions Local Name cannot be NULL!");
184 // Deal with base types
185 if (typeDefinition.getBaseType() == null) {
186 // We have to deal with differing handling of decimal64. The old parser used a fixed Decimal64 type
187 // and generated an enclosing ExtendedType to hold any range constraints. The new parser instantiates
188 // a base type which holds these constraints.
189 if (typeDefinition instanceof DecimalTypeDefinition) {
190 final Type ret = BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForSchemaDefinitionType(typeDefinition,
191 parentNode, restrictions, lenientRelativeLeafrefs);
197 // Deal with leafrefs/identityrefs
198 Type ret = javaTypeForLeafrefOrIdentityRef(typeDefinition, parentNode, lenientRelativeLeafrefs);
203 // FIXME: it looks as though we could be using the same codepath as above...
204 ret = BaseYangTypes.javaTypeForYangType(typeDefinition.getQName().getLocalName());
206 LOG.debug("Failed to resolve Java type for {}", typeDefinition);
212 Type returnType = javaTypeForExtendedType(typeDefinition, lenientRelativeLeafrefs);
213 if (restrictions != null && returnType instanceof GeneratedTransferObject) {
214 final GeneratedTransferObject gto = (GeneratedTransferObject) returnType;
215 final Module module = findParentModule(schemaContext, parentNode);
216 final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
217 final String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName,
218 typeDefinition.getPath());
219 final String genTOName = BindingMapping.getClassName(typedefName);
220 final String name = packageName + "." + genTOName;
221 if (!returnType.getFullyQualifiedName().equals(name)) {
222 returnType = shadedTOWithRestrictions(gto, restrictions);
228 private GeneratedTransferObject shadedTOWithRestrictions(final GeneratedTransferObject gto,
229 final Restrictions restrictions) {
230 final GeneratedTOBuilder gtob = newGeneratedTOBuilder(gto.getIdentifier());
231 final GeneratedTransferObject parent = gto.getSuperType();
232 if (parent != null) {
233 gtob.setExtendsType(parent);
235 gtob.setRestrictions(restrictions);
236 for (GeneratedProperty gp : gto.getProperties()) {
237 final GeneratedPropertyBuilder gpb = gtob.addProperty(gp.getName());
238 gpb.setValue(gp.getValue());
239 gpb.setReadOnly(gp.isReadOnly());
240 gpb.setAccessModifier(gp.getAccessModifier());
241 gpb.setReturnType(gp.getReturnType());
242 gpb.setFinal(gp.isFinal());
243 gpb.setStatic(gp.isStatic());
248 private boolean isLeafRefSelfReference(final LeafrefTypeDefinition leafref, final SchemaNode parentNode) {
250 * First check if the leafref is an augment. If that is the case, skip it as it will be checked once augments
253 DataNodeContainer current = null;
254 DataSchemaNode dataChildByName;
255 for (QName next : parentNode.getPath().getPathFromRoot()) {
256 if (current == null) {
257 dataChildByName = schemaContext.getDataChildByName(next);
259 dataChildByName = current.getDataChildByName(next);
261 if (dataChildByName == null) {
264 if (dataChildByName.isAugmenting()) {
267 if (dataChildByName instanceof DataNodeContainer) {
268 current = (DataNodeContainer) dataChildByName;
272 // Then try to look up the expression.
273 final PathExpression leafRefXPath = leafref.getPathStatement();
274 final Module parentModule = getParentModule(parentNode);
275 final SchemaNode leafRefValueNode;
276 if (leafRefXPath.isAbsolute()) {
277 leafRefValueNode = SchemaContextUtil.findDataTreeSchemaNode(schemaContext, parentModule.getQNameModule(),
280 leafRefValueNode = SchemaContextUtil.findDataSchemaNodeForRelativeXPath(schemaContext, parentModule,
281 parentNode, new PathExpressionImpl(
282 GROUPS_PATTERN.matcher(leafRefXPath.getOriginalString()).replaceAll(""), false));
285 return leafRefValueNode != null && leafRefValueNode.equals(parentNode);
289 * Returns JAVA <code>Type</code> for instances of the type <code>LeafrefTypeDefinition</code> or
290 * <code>IdentityrefTypeDefinition</code>.
292 * @param typeDefinition type definition which is converted to JAVA <code>Type</code>
293 * @return JAVA <code>Type</code> instance for <code>typeDefinition</code>
295 private Type javaTypeForLeafrefOrIdentityRef(final TypeDefinition<?> typeDefinition, final SchemaNode parentNode,
296 final boolean inGrouping) {
297 if (typeDefinition instanceof LeafrefTypeDefinition) {
298 final LeafrefTypeDefinition leafref = (LeafrefTypeDefinition) typeDefinition;
299 Preconditions.checkArgument(!isLeafRefSelfReference(leafref, parentNode),
300 "Leafref %s is referencing itself, incoming StackOverFlowError detected.", leafref);
301 return provideTypeForLeafref(leafref, parentNode, inGrouping);
302 } else if (typeDefinition instanceof IdentityrefTypeDefinition) {
303 return provideTypeForIdentityref((IdentityrefTypeDefinition) typeDefinition);
310 * Returns JAVA <code>Type</code> for instances of the type <code>ExtendedType</code>.
312 * @param typeDefinition type definition which is converted to JAVA <code>Type</code>
313 * @return JAVA <code>Type</code> instance for <code>typeDefinition</code>
315 private Type javaTypeForExtendedType(final TypeDefinition<?> typeDefinition, final boolean lenient) {
316 final String typedefName = typeDefinition.getQName().getLocalName();
317 final TypeDefinition<?> baseTypeDef = baseTypeDefForExtendedType(typeDefinition);
318 Type returnType = javaTypeForLeafrefOrIdentityRef(baseTypeDef, typeDefinition, lenient);
319 if (returnType == null) {
320 if (baseTypeDef instanceof EnumTypeDefinition) {
321 final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) baseTypeDef;
322 returnType = provideTypeForEnum(enumTypeDef, typedefName, typeDefinition);
324 final Module module = findParentModule(schemaContext, typeDefinition);
325 final Restrictions r = BindingGeneratorUtil.getRestrictions(typeDefinition);
326 if (module != null) {
327 final Map<Optional<Revision>, Map<String, Type>> modulesByDate = genTypeDefsContextMap.get(
329 final Map<String, Type> genTOs = modulesByDate.get(module.getRevision());
330 if (genTOs != null) {
331 returnType = genTOs.get(typedefName);
333 if (returnType == null) {
334 returnType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForSchemaDefinitionType(
335 baseTypeDef, typeDefinition, r, lenient);
344 * Seeks for identity reference <code>idref</code> the JAVA <code>type</code>.
348 * If identy which is referenced via <code>idref</code> has name <b>Idn</b>
349 * then returning type is <b>{@code Class<? extends Idn>}</b></i>
351 * @param idref identityref type definition for which JAVA <code>Type</code> is sought
352 * @return JAVA <code>Type</code> of the identity which is referenced through <code>idref</code>
354 private Type provideTypeForIdentityref(final IdentityrefTypeDefinition idref) {
355 final Collection<? extends IdentitySchemaNode> identities = idref.getIdentities();
356 if (identities.size() > 1) {
357 LOG.warn("Identity reference {} has multiple identities, using only the first one", idref);
360 final QName baseIdQName = identities.iterator().next().getQName();
361 final Module module = schemaContext.findModule(baseIdQName.getModule()).orElse(null);
362 IdentitySchemaNode identity = null;
363 for (IdentitySchemaNode id : module.getIdentities()) {
364 if (id.getQName().equals(baseIdQName)) {
368 Preconditions.checkArgument(identity != null, "Target identity '" + baseIdQName + "' do not exist");
370 final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
371 final JavaTypeName identifier = JavaTypeName.create(BindingGeneratorUtil.packageNameForGeneratedType(
372 basePackageName, identity.getPath()), BindingMapping.getClassName(identity.getQName()));
373 return Types.classType(Types.wildcardTypeFor(identifier));
377 * Converts <code>typeDefinition</code> to concrete JAVA <code>Type</code>.
379 * @param typeDefinition
380 * type definition which should be converted to JAVA
382 * @return JAVA <code>Type</code> which represents
383 * <code>typeDefinition</code>
384 * @throws IllegalArgumentException
386 * <li>if <code>typeDefinition</code> equal null</li>
387 * <li>if Q name of <code>typeDefinition</code></li>
388 * <li>if name of <code>typeDefinition</code></li>
391 public Type generatedTypeForExtendedDefinitionType(final TypeDefinition<?> typeDefinition,
392 final SchemaNode parentNode) {
393 Preconditions.checkArgument(typeDefinition != null, "Type Definition cannot be NULL!");
394 if (typeDefinition.getQName() == null) {
395 throw new IllegalArgumentException("Type Definition cannot have unspecified QName (QName cannot be NULL!)");
397 Preconditions.checkArgument(typeDefinition.getQName().getLocalName() != null,
398 "Type Definitions Local Name cannot be NULL!");
400 final TypeDefinition<?> baseTypeDef = baseTypeDefForExtendedType(typeDefinition);
401 if (baseTypeDef instanceof LeafrefTypeDefinition || baseTypeDef instanceof IdentityrefTypeDefinition) {
403 * This is backwards compatibility baggage from way back when. The problem at hand is inconsistency between
404 * the fact that identity is mapped to a Class, which is also returned from leaves which specify it like
417 * This results in getFoo() returning Class<? extends Iden>, which looks fine on the surface, but gets more
418 * dicey when we throw in:
432 * Now we have competing requirements: typedef would like us to use encapsulation to capture the defined
433 * type, while getBar() wants us to retain shape with getFoo(), as it should not matter how the identityref
436 * In this particular case getFoo() won just after the Binding Spec was frozen, hence we do not generate
437 * an encapsulation for identityref typedefs.
439 * In case you are thinking we could get by having foo-ref map to a subclass of Iden, that is not a good
440 * option, as it would look as though it is the product of a different construct:
446 * Leading to a rather nice namespace clash and also slight incompatibility with unknown third-party
447 * sub-identities of iden.
449 * The story behind leafrefs is probably similar, but that needs to be ascertained.
454 final Module module = findParentModule(schemaContext, parentNode);
455 if (module != null) {
456 final Map<Optional<Revision>, Map<String, Type>> modulesByDate = genTypeDefsContextMap.get(
458 final Map<String, Type> genTOs = modulesByDate.get(module.getRevision());
459 if (genTOs != null) {
460 return genTOs.get(typeDefinition.getQName().getLocalName());
467 * Gets base type definition for <code>extendTypeDef</code>. The method is
468 * recursively called until non <code>ExtendedType</code> type is found.
470 * @param extendTypeDef
471 * type definition for which is the base type definition sought
472 * @return type definition which is base type for <code>extendTypeDef</code>
473 * @throws IllegalArgumentException
474 * if <code>extendTypeDef</code> equal null
476 private static TypeDefinition<?> baseTypeDefForExtendedType(final TypeDefinition<?> extendTypeDef) {
477 Preconditions.checkArgument(extendTypeDef != null, "Type Definition reference cannot be NULL!");
479 TypeDefinition<?> ret = extendTypeDef;
480 while (ret.getBaseType() != null) {
481 ret = ret.getBaseType();
488 * Converts <code>leafrefType</code> to JAVA <code>Type</code>. The path of <code>leafrefType</code> is followed
489 * to find referenced node and its <code>Type</code> is returned.
491 * @param leafrefType leafref type definition for which is the type sought
492 * @param parentNode parent node of the leaf being resolved
493 * @param inGrouping true if we are resolving the type within a grouping.
494 * @return JAVA <code>Type</code> of data schema node which is referenced in <code>leafrefType</code>
495 * @throws IllegalArgumentException
497 * <li>if <code>leafrefType</code> equal null</li>
498 * <li>if path statement of <code>leafrefType</code> equal null</li>
502 Type provideTypeForLeafref(final LeafrefTypeDefinition leafrefType, final SchemaNode parentNode,
503 final boolean inGrouping) {
504 Preconditions.checkArgument(leafrefType != null, "Leafref Type Definition reference cannot be NULL!");
506 final PathExpression xpath = leafrefType.getPathStatement();
507 Preconditions.checkArgument(xpath != null, "The Path Statement for Leafref Type Definition cannot be NULL!");
509 final String strXPath = xpath.getOriginalString();
510 if (strXPath.indexOf('[') != -1) {
511 // XXX: why are we special-casing this?
512 return Types.objectType();
515 final Module module = findParentModule(schemaContext, parentNode);
516 Preconditions.checkArgument(module != null, "Failed to find module for parent %s", parentNode);
518 final SchemaNode dataNode;
519 if (xpath.isAbsolute()) {
520 dataNode = findDataTreeSchemaNode(schemaContext, module.getQNameModule(), xpath);
522 dataNode = findDataSchemaNodeForRelativeXPath(schemaContext, module, parentNode, xpath);
523 if (dataNode == null && inGrouping) {
524 // Relative path within a grouping may end up being unresolvable because it may refer outside
525 // the grouping, in which case it is polymorphic based on instantiation, for example:
548 LOG.debug("Leafref type {} not found in parent {}, assuming polymorphic object", leafrefType,
550 return Types.objectType();
553 Preconditions.checkArgument(dataNode != null, "Failed to find leafref target: %s in module %s (%s)",
554 strXPath, this.getParentModule(parentNode).getName(), parentNode.getQName().getModule());
556 // FIXME: this block seems to be some weird magic hack. Analyze and refactor it.
557 Type returnType = null;
558 if (leafContainsEnumDefinition(dataNode)) {
559 returnType = referencedTypes.get(dataNode.getPath());
560 } else if (leafListContainsEnumDefinition(dataNode)) {
561 returnType = Types.listTypeFor(referencedTypes.get(dataNode.getPath()));
563 if (returnType == null) {
564 returnType = resolveTypeFromDataSchemaNode(dataNode, inGrouping);
566 Preconditions.checkArgument(returnType != null, "Failed to find leafref target: %s in module %s (%s)",
567 strXPath, this.getParentModule(parentNode).getName(), parentNode.getQName().getModule(), this);
572 * Checks if <code>dataNode</code> is <code>LeafSchemaNode</code> and if it so then checks if it is of type
573 * <code>EnumTypeDefinition</code>.
575 * @param dataNode data schema node for which is checked if it is leaf and if it is of enum type
576 * @return boolean value
578 * <li>true - if <code>dataNode</code> is leaf of type enumeration</li>
579 * <li>false - other cases</li>
582 private static boolean leafContainsEnumDefinition(final SchemaNode dataNode) {
583 if (dataNode instanceof LeafSchemaNode) {
584 final LeafSchemaNode leaf = (LeafSchemaNode) dataNode;
585 return CompatUtils.compatType(leaf) instanceof EnumTypeDefinition;
591 * Checks if <code>dataNode</code> is <code>LeafListSchemaNode</code> and if it so then checks if it is of type
592 * <code>EnumTypeDefinition</code>.
594 * @param dataNode data schema node for which is checked if it is leaflist and if it is of enum type
595 * @return boolean value
597 * <li>true - if <code>dataNode</code> is leaflist of type
599 * <li>false - other cases</li>
602 private static boolean leafListContainsEnumDefinition(final SchemaNode dataNode) {
603 if (dataNode instanceof LeafListSchemaNode) {
604 final LeafListSchemaNode leafList = (LeafListSchemaNode) dataNode;
605 return leafList.getType() instanceof EnumTypeDefinition;
611 * Converts <code>enumTypeDef</code> to {@link Enumeration enumeration}.
613 * @param enumTypeDef enumeration type definition which is converted to enumeration
614 * @param enumName string with name which is used as the enumeration name
615 * @return enumeration type which is built with data (name, enum values) from <code>enumTypeDef</code>
616 * @throws IllegalArgumentException
618 * <li>if <code>enumTypeDef</code> equals null</li>
619 * <li>if enum values of <code>enumTypeDef</code> equal null</li>
620 * <li>if Q name of <code>enumTypeDef</code> equal null</li>
621 * <li>if name of <code>enumTypeDef</code> equal null</li>
624 private Enumeration provideTypeForEnum(final EnumTypeDefinition enumTypeDef, final String enumName,
625 final SchemaNode parentNode) {
626 Preconditions.checkArgument(enumTypeDef != null, "EnumTypeDefinition reference cannot be NULL!");
627 Preconditions.checkArgument(enumTypeDef.getValues() != null,
628 "EnumTypeDefinition MUST contain at least ONE value definition!");
629 Preconditions.checkArgument(enumTypeDef.getQName() != null, "EnumTypeDefinition MUST contain NON-NULL QName!");
630 Preconditions.checkArgument(enumTypeDef.getQName().getLocalName() != null,
631 "Local Name in EnumTypeDefinition QName cannot be NULL!");
633 final Module module = findParentModule(schemaContext, parentNode);
634 final AbstractEnumerationBuilder enumBuilder = newEnumerationBuilder(JavaTypeName.create(
635 BindingMapping.getRootPackageName(module.getQNameModule()), BindingMapping.getClassName(enumName)));
636 addEnumDescription(enumBuilder, enumTypeDef);
637 enumTypeDef.getReference().ifPresent(enumBuilder::setReference);
638 enumBuilder.setModuleName(module.getName());
639 enumBuilder.setSchemaPath(enumTypeDef.getPath());
640 enumBuilder.updateEnumPairsFromEnumTypeDef(enumTypeDef);
641 return enumBuilder.toInstance(null);
645 * Adds enumeration to <code>typeBuilder</code>. The enumeration data are taken from <code>enumTypeDef</code>.
647 * @param enumTypeDef enumeration type definition is source of enumeration data for <code>typeBuilder</code>
648 * @param enumName string with the name of enumeration
649 * @param typeBuilder generated type builder to which is enumeration added
650 * @return enumeration type which contains enumeration data form <code>enumTypeDef</code>
651 * @throws IllegalArgumentException
653 * <li>if <code>enumTypeDef</code> equals null</li>
654 * <li>if enum values of <code>enumTypeDef</code> equal null</li>
655 * <li>if Q name of <code>enumTypeDef</code> equal null</li>
656 * <li>if name of <code>enumTypeDef</code> equal null</li>
657 * <li>if name of <code>typeBuilder</code> equal null</li>
661 private Enumeration addInnerEnumerationToTypeBuilder(final EnumTypeDefinition enumTypeDef,
662 final String enumName, final GeneratedTypeBuilderBase<?> typeBuilder) {
663 Preconditions.checkArgument(enumTypeDef != null, "EnumTypeDefinition reference cannot be NULL!");
664 Preconditions.checkArgument(enumTypeDef.getValues() != null,
665 "EnumTypeDefinition MUST contain at least ONE value definition!");
666 Preconditions.checkArgument(enumTypeDef.getQName() != null, "EnumTypeDefinition MUST contain NON-NULL QName!");
667 Preconditions.checkArgument(enumTypeDef.getQName().getLocalName() != null,
668 "Local Name in EnumTypeDefinition QName cannot be NULL!");
669 Preconditions.checkArgument(typeBuilder != null, "Generated Type Builder reference cannot be NULL!");
671 final EnumBuilder enumBuilder = typeBuilder.addEnumeration(BindingMapping.getClassName(enumName));
673 addEnumDescription(enumBuilder, enumTypeDef);
674 enumBuilder.updateEnumPairsFromEnumTypeDef(enumTypeDef);
675 return enumBuilder.toInstance(enumBuilder);
678 public abstract void addEnumDescription(EnumBuilder enumBuilder, EnumTypeDefinition enumTypeDef);
680 public abstract AbstractEnumerationBuilder newEnumerationBuilder(JavaTypeName identifier);
682 public abstract GeneratedTOBuilder newGeneratedTOBuilder(JavaTypeName identifier);
684 public abstract GeneratedTypeBuilder newGeneratedTypeBuilder(JavaTypeName identifier);
687 * Converts the pattern constraints to the list of the strings which represents these constraints.
689 * @param patternConstraints list of pattern constraints
690 * @return list of strings which represents the constraint patterns
692 public abstract Map<String, String> resolveRegExpressions(List<PatternConstraint> patternConstraints);
694 abstract void addCodegenInformation(GeneratedTypeBuilderBase<?> genTOBuilder, TypeDefinition<?> typeDef);
697 * Converts the pattern constraints from <code>typedef</code> to the list of the strings which represents these
700 * @param typedef extended type in which are the pattern constraints sought
701 * @return list of strings which represents the constraint patterns
702 * @throws IllegalArgumentException if <code>typedef</code> equals null
705 private Map<String, String> resolveRegExpressionsFromTypedef(final TypeDefinition<?> typedef) {
706 if (!(typedef instanceof StringTypeDefinition)) {
707 return ImmutableMap.of();
710 // TODO: run diff against base ?
711 return resolveRegExpressions(((StringTypeDefinition) typedef).getPatternConstraints());
715 * Converts <code>dataNode</code> to JAVA <code>Type</code>.
717 * @param dataNode contains information about YANG type
718 * @return JAVA <code>Type</code> representation of <code>dataNode</code>
720 private Type resolveTypeFromDataSchemaNode(final SchemaNode dataNode, final boolean inGrouping) {
721 Type returnType = null;
722 if (dataNode != null) {
723 if (dataNode instanceof LeafSchemaNode) {
724 final LeafSchemaNode leaf = (LeafSchemaNode) dataNode;
725 final TypeDefinition<?> type = CompatUtils.compatType(leaf);
726 returnType = javaTypeForSchemaDefinitionType(type, leaf, inGrouping);
727 } else if (dataNode instanceof LeafListSchemaNode) {
728 final LeafListSchemaNode leafList = (LeafListSchemaNode) dataNode;
729 returnType = javaTypeForSchemaDefinitionType(leafList.getType(), leafList, inGrouping);
736 * Passes through all modules and through all its type definitions and convert it to generated types.
739 * The modules are first sorted by mutual dependencies. The modules are sequentially passed. All type definitions
740 * of a module are at the beginning sorted so that type definition with less amount of references to other type
741 * definition are processed first.<br>
742 * For each module is created mapping record in the map
743 * {@link AbstractTypeProvider#genTypeDefsContextMap genTypeDefsContextMap}
744 * which map current module name to the map which maps type names to returned types (generated types).
746 private void resolveTypeDefsFromContext() {
747 final List<Module> modulesSortedByDependency = ModuleDependencySort.sort(schemaContext.getModules());
749 for (Module module : modulesSortedByDependency) {
750 Map<Optional<Revision>, Map<String, Type>> dateTypeMap = genTypeDefsContextMap.computeIfAbsent(
751 module.getName(), key -> new HashMap<>());
752 dateTypeMap.put(module.getRevision(), Collections.emptyMap());
753 genTypeDefsContextMap.put(module.getName(), dateTypeMap);
756 for (Module module : modulesSortedByDependency) {
757 if (module != null) {
758 final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
759 if (basePackageName != null) {
760 final List<TypeDefinition<?>> typeDefinitions = TypedefResolver.getAllTypedefs(module);
761 for (TypeDefinition<?> typedef : sortTypeDefinitionAccordingDepth(typeDefinitions)) {
762 typedefToGeneratedType(basePackageName, module, typedef);
770 * Create Type for specified type definition.
772 * @param basePackageName string with name of package to which the module belongs
773 * @param module string with the name of the module for to which the <code>typedef</code> belongs
774 * @param typedef type definition of the node for which should be created JAVA <code>Type</code>
775 * (usually generated TO)
776 * @return JAVA <code>Type</code> representation of <code>typedef</code> or
777 * <code>null</code> value if <code>basePackageName</code> or
778 * <code>modulName</code> or <code>typedef</code> or Q name of
779 * <code>typedef</code> equals <code>null</code>
781 private Type typedefToGeneratedType(final String basePackageName, final Module module,
782 final TypeDefinition<?> typedef) {
783 final TypeDefinition<?> baseTypedef = typedef.getBaseType();
785 // See generatedTypeForExtendedDefinitionType() above for rationale behind this special case.
786 if (baseTypedef instanceof LeafrefTypeDefinition || baseTypedef instanceof IdentityrefTypeDefinition) {
790 final String typedefName = typedef.getQName().getLocalName();
792 final Type returnType;
793 if (baseTypedef.getBaseType() != null) {
794 returnType = provideGeneratedTOFromExtendedType(typedef, baseTypedef, basePackageName,
796 } else if (baseTypedef instanceof UnionTypeDefinition) {
797 final GeneratedTOBuilder genTOBuilder = provideGeneratedTOBuilderForUnionTypeDef(
798 JavaTypeName.create(basePackageName, BindingMapping.getClassName(typedef.getQName())),
799 (UnionTypeDefinition) baseTypedef, typedef);
800 genTOBuilder.setTypedef(true);
801 genTOBuilder.setIsUnion(true);
802 addUnitsToGenTO(genTOBuilder, typedef.getUnits().orElse(null));
803 makeSerializable(genTOBuilder);
804 returnType = genTOBuilder.build();
806 // Define a corresponding union builder. Typedefs are always anchored at a Java package root,
807 // so we are placing the builder alongside the union.
808 final GeneratedTOBuilder unionBuilder = newGeneratedTOBuilder(
809 JavaTypeName.create(genTOBuilder.getPackageName(), genTOBuilder.getName() + "Builder"));
810 unionBuilder.setIsUnionBuilder(true);
811 final MethodSignatureBuilder method = unionBuilder.addMethod("getDefaultInstance");
812 method.setReturnType(returnType);
813 method.addParameter(Types.STRING, "defaultValue");
814 method.setAccessModifier(AccessModifier.PUBLIC);
815 method.setStatic(true);
816 additionalTypes.computeIfAbsent(module, key -> new HashSet<>()).add(unionBuilder.build());
817 } else if (baseTypedef instanceof EnumTypeDefinition) {
818 // enums are automatically Serializable
819 final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) baseTypedef;
820 // TODO units for typedef enum
821 returnType = provideTypeForEnum(enumTypeDef, typedefName, typedef);
822 } else if (baseTypedef instanceof BitsTypeDefinition) {
823 final GeneratedTOBuilder genTOBuilder = provideGeneratedTOBuilderForBitsTypeDefinition(
824 JavaTypeName.create(basePackageName, BindingMapping.getClassName(typedef.getQName())),
825 (BitsTypeDefinition) baseTypedef, module.getName());
826 genTOBuilder.setTypedef(true);
827 addUnitsToGenTO(genTOBuilder, typedef.getUnits().orElse(null));
828 makeSerializable(genTOBuilder);
829 returnType = genTOBuilder.build();
831 final Type javaType = javaTypeForSchemaDefinitionType(baseTypedef, typedef);
832 returnType = wrapJavaTypeIntoTO(basePackageName, typedef, javaType, module.getName());
834 if (returnType != null) {
835 final Map<Optional<Revision>, Map<String, Type>> modulesByDate =
836 genTypeDefsContextMap.get(module.getName());
837 final Optional<Revision> moduleRevision = module.getRevision();
838 Map<String, Type> typeMap = modulesByDate.get(moduleRevision);
839 if (typeMap != null) {
840 if (typeMap.isEmpty()) {
841 typeMap = new HashMap<>(4);
842 modulesByDate.put(moduleRevision, typeMap);
844 typeMap.put(typedefName, returnType);
852 * Wraps base YANG type to generated TO.
854 * @param basePackageName string with name of package to which the module belongs
855 * @param typedef type definition which is converted to the TO
856 * @param javaType JAVA <code>Type</code> to which is <code>typedef</code> mapped
857 * @return generated transfer object which represent<code>javaType</code>
859 private GeneratedTransferObject wrapJavaTypeIntoTO(final String basePackageName, final TypeDefinition<?> typedef,
860 final Type javaType, final String moduleName) {
861 requireNonNull(javaType, "javaType cannot be null");
863 final GeneratedTOBuilder genTOBuilder = typedefToTransferObject(basePackageName, typedef, moduleName);
864 genTOBuilder.setRestrictions(BindingGeneratorUtil.getRestrictions(typedef));
865 final GeneratedPropertyBuilder genPropBuilder = genTOBuilder.addProperty("value");
866 genPropBuilder.setReturnType(javaType);
867 genTOBuilder.addEqualsIdentity(genPropBuilder);
868 genTOBuilder.addHashIdentity(genPropBuilder);
869 genTOBuilder.addToStringProperty(genPropBuilder);
870 genTOBuilder.addImplementsType(TYPE_OBJECT);
871 if (typedef.getStatus() == Status.DEPRECATED) {
872 genTOBuilder.addAnnotation("java.lang", "Deprecated");
874 if (javaType instanceof ConcreteType && "String".equals(javaType.getName()) && typedef.getBaseType() != null) {
875 addStringRegExAsConstant(genTOBuilder, resolveRegExpressionsFromTypedef(typedef));
877 addUnitsToGenTO(genTOBuilder, typedef.getUnits().orElse(null));
878 genTOBuilder.setTypedef(true);
879 makeSerializable(genTOBuilder);
880 return genTOBuilder.build();
884 * Converts output list of generated TO builders to one TO builder (first
885 * from list) which contains the remaining builders as its enclosing TO.
887 * @param typeName new type identifier
888 * @param typedef type definition which should be of type {@link UnionTypeDefinition}
889 * @return generated TO builder with the list of enclosed generated TO builders
891 public GeneratedTOBuilder provideGeneratedTOBuilderForUnionTypeDef(final JavaTypeName typeName,
892 final UnionTypeDefinition typedef, final TypeDefinition<?> parentNode) {
893 final List<GeneratedTOBuilder> builders = provideGeneratedTOBuildersForUnionTypeDef(typeName, typedef,
895 Preconditions.checkState(!builders.isEmpty(), "No GeneratedTOBuilder objects generated from union %s", typedef);
897 final GeneratedTOBuilder resultTOBuilder = builders.remove(0);
898 builders.forEach(resultTOBuilder::addEnclosingTransferObject);
899 return resultTOBuilder;
903 * Converts <code>typedef</code> to generated TO with <code>typeDefName</code>. Every union type from
904 * <code>typedef</code> is added to generated TO builder as property.
906 * @param typeName new type identifier
907 * @param typedef type definition which should be of type <code>UnionTypeDefinition</code>
908 * @return generated TO builder which represents <code>typedef</code>
909 * @throws NullPointerException
911 * <li>if <code>basePackageName</code> is null</li>
912 * <li>if <code>typedef</code> is null</li>
913 * <li>if Qname of <code>typedef</code> is null</li>
916 public List<GeneratedTOBuilder> provideGeneratedTOBuildersForUnionTypeDef(final JavaTypeName typeName,
917 final UnionTypeDefinition typedef, final SchemaNode parentNode) {
918 requireNonNull(typedef, "Type Definition cannot be NULL!");
919 requireNonNull(typedef.getQName(), "Type definition QName cannot be NULL!");
921 final List<GeneratedTOBuilder> generatedTOBuilders = new ArrayList<>();
922 final List<TypeDefinition<?>> unionTypes = typedef.getTypes();
923 final Module module = findParentModule(schemaContext, parentNode);
925 final GeneratedTOBuilder unionGenTOBuilder = newGeneratedTOBuilder(typeName);
926 unionGenTOBuilder.setIsUnion(true);
927 unionGenTOBuilder.setSchemaPath(typedef.getPath());
928 unionGenTOBuilder.setModuleName(module.getName());
929 unionGenTOBuilder.addImplementsType(TYPE_OBJECT);
930 addCodegenInformation(unionGenTOBuilder, typedef);
931 generatedTOBuilders.add(unionGenTOBuilder);
933 // Pattern string is the key, XSD regex is the value. The reason for this choice is that the pattern carries
934 // also negation information and hence guarantees uniqueness.
935 final Map<String, String> expressions = new HashMap<>();
936 for (TypeDefinition<?> unionType : unionTypes) {
937 final String unionTypeName = unionType.getQName().getLocalName();
939 // If we have a base type we should follow the type definition backwards, except for identityrefs, as those
940 // do not follow type encapsulation -- we use the general case for that.
941 if (unionType.getBaseType() != null && !(unionType instanceof IdentityrefTypeDefinition)) {
942 resolveExtendedSubtypeAsUnion(unionGenTOBuilder, unionType, expressions, parentNode);
943 } else if (unionType instanceof UnionTypeDefinition) {
944 generatedTOBuilders.addAll(resolveUnionSubtypeAsUnion(unionGenTOBuilder,
945 (UnionTypeDefinition) unionType, parentNode));
946 } else if (unionType instanceof EnumTypeDefinition) {
947 final Enumeration enumeration = addInnerEnumerationToTypeBuilder((EnumTypeDefinition) unionType,
948 unionTypeName, unionGenTOBuilder);
949 updateUnionTypeAsProperty(unionGenTOBuilder, enumeration, unionTypeName);
951 final Type javaType = javaTypeForSchemaDefinitionType(unionType, parentNode);
952 updateUnionTypeAsProperty(unionGenTOBuilder, javaType, unionTypeName);
955 addStringRegExAsConstant(unionGenTOBuilder, expressions);
957 storeGenTO(typedef, unionGenTOBuilder, parentNode);
959 return generatedTOBuilders;
963 * Wraps code which handles the case when union subtype is also of the type <code>UnionType</code>.
966 * In this case the new generated TO is created for union subtype (recursive call of method
967 * {@link #provideGeneratedTOBuildersForUnionTypeDef(String, UnionTypeDefinition, String, SchemaNode)}
968 * provideGeneratedTOBuilderForUnionTypeDef} and in parent TO builder <code>parentUnionGenTOBuilder</code> is
969 * created property which type is equal to new generated TO.
971 * @param parentUnionGenTOBuilder generated TO builder to which is the property with the child union subtype added
972 * @param basePackageName string with the name of the module package
973 * @param unionSubtype type definition which represents union subtype
974 * @return list of generated TO builders. The number of the builders can be bigger one due to recursive call of
975 * <code>provideGeneratedTOBuildersForUnionTypeDef</code> method.
977 private List<GeneratedTOBuilder> resolveUnionSubtypeAsUnion(final GeneratedTOBuilder parentUnionGenTOBuilder,
978 final UnionTypeDefinition unionSubtype, final SchemaNode parentNode) {
979 final JavaTypeName newTOBuilderName = parentUnionGenTOBuilder.getIdentifier().createSibling(
980 provideAvailableNameForGenTOBuilder(parentUnionGenTOBuilder.getName()));
981 final List<GeneratedTOBuilder> subUnionGenTOBUilders = provideGeneratedTOBuildersForUnionTypeDef(
982 newTOBuilderName, unionSubtype, parentNode);
984 final GeneratedPropertyBuilder propertyBuilder;
985 propertyBuilder = parentUnionGenTOBuilder.addProperty(BindingMapping.getPropertyName(
986 newTOBuilderName.simpleName()));
987 propertyBuilder.setReturnType(subUnionGenTOBUilders.get(0).build());
988 parentUnionGenTOBuilder.addEqualsIdentity(propertyBuilder);
989 parentUnionGenTOBuilder.addToStringProperty(propertyBuilder);
991 return subUnionGenTOBUilders;
995 * Wraps code which handle case when union subtype is of the type <code>ExtendedType</code>. If TO for this type
996 * already exists it is used for the creation of the property in <code>parentUnionGenTOBuilder</code>. Otherwise
997 * the base type is used for the property creation.
999 * @param parentUnionGenTOBuilder generated TO builder in which new property is created
1000 * @param unionSubtype type definition of the <code>ExtendedType</code> type which represents union subtype
1001 * @param expressions list of strings with the regular expressions
1002 * @param parentNode parent Schema Node for Extended Subtype
1004 private void resolveExtendedSubtypeAsUnion(final GeneratedTOBuilder parentUnionGenTOBuilder,
1005 final TypeDefinition<?> unionSubtype, final Map<String, String> expressions, final SchemaNode parentNode) {
1006 final String unionTypeName = unionSubtype.getQName().getLocalName();
1007 final Type genTO = findGenTO(unionTypeName, unionSubtype);
1008 if (genTO != null) {
1009 updateUnionTypeAsProperty(parentUnionGenTOBuilder, genTO, genTO.getName());
1013 final TypeDefinition<?> baseType = baseTypeDefForExtendedType(unionSubtype);
1014 if (unionTypeName.equals(baseType.getQName().getLocalName())) {
1015 final Type javaType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForSchemaDefinitionType(baseType,
1016 parentNode, BindingGeneratorUtil.getRestrictions(unionSubtype));
1017 if (javaType != null) {
1018 updateUnionTypeAsProperty(parentUnionGenTOBuilder, javaType, unionTypeName);
1020 } else if (baseType instanceof LeafrefTypeDefinition) {
1021 final Type javaType = javaTypeForSchemaDefinitionType(baseType, parentNode);
1022 boolean typeExist = false;
1023 for (GeneratedPropertyBuilder generatedPropertyBuilder : parentUnionGenTOBuilder.getProperties()) {
1024 final Type origType = ((GeneratedPropertyBuilderImpl) generatedPropertyBuilder).getReturnType();
1025 if (origType != null && javaType != null && javaType == origType) {
1030 if (!typeExist && javaType != null) {
1031 updateUnionTypeAsProperty(parentUnionGenTOBuilder, javaType,
1032 javaType.getName() + parentUnionGenTOBuilder.getName() + "Value");
1035 if (baseType instanceof StringTypeDefinition) {
1036 expressions.putAll(resolveRegExpressionsFromTypedef(unionSubtype));
1041 * Searches for generated TO for <code>searchedTypeDef</code> type definition
1042 * in {@link #genTypeDefsContextMap genTypeDefsContextMap}.
1044 * @param searchedTypeName string with name of <code>searchedTypeDef</code>
1045 * @return generated TO for <code>searchedTypeDef</code> or <code>null</code> it it doesn't exist
1047 private Type findGenTO(final String searchedTypeName, final SchemaNode parentNode) {
1048 final Module typeModule = findParentModule(schemaContext, parentNode);
1049 if (typeModule != null && typeModule.getName() != null) {
1050 final Map<Optional<Revision>, Map<String, Type>> modulesByDate = genTypeDefsContextMap.get(
1051 typeModule.getName());
1052 final Map<String, Type> genTOs = modulesByDate.get(typeModule.getRevision());
1053 if (genTOs != null) {
1054 return genTOs.get(searchedTypeName);
1061 * Stores generated TO created from <code>genTOBuilder</code> for <code>newTypeDef</code>
1062 * to {@link #genTypeDefsContextMap genTypeDefsContextMap} if the module for <code>newTypeDef</code> exists.
1064 * @param newTypeDef type definition for which is <code>genTOBuilder</code> created
1065 * @param genTOBuilder generated TO builder which is converted to generated TO and stored
1067 private void storeGenTO(final TypeDefinition<?> newTypeDef, final GeneratedTOBuilder genTOBuilder,
1068 final SchemaNode parentNode) {
1069 if (!(newTypeDef instanceof UnionTypeDefinition)) {
1070 final Module parentModule = findParentModule(schemaContext, parentNode);
1071 if (parentModule != null && parentModule.getName() != null) {
1072 final Map<Optional<Revision>, Map<String, Type>> modulesByDate = genTypeDefsContextMap.get(
1073 parentModule.getName());
1074 final Map<String, Type> genTOsMap = modulesByDate.get(parentModule.getRevision());
1075 genTOsMap.put(newTypeDef.getQName().getLocalName(), genTOBuilder.build());
1081 * Adds a new property with the name <code>propertyName</code> and with type <code>type</code>
1082 * to <code>unonGenTransObject</code>.
1084 * @param unionGenTransObject generated TO to which should be property added
1085 * @param type JAVA <code>type</code> of the property which should be added to <code>unionGentransObject</code>
1086 * @param propertyName string with name of property which should be added to <code>unionGentransObject</code>
1088 private static void updateUnionTypeAsProperty(final GeneratedTOBuilder unionGenTransObject, final Type type,
1089 final String propertyName) {
1090 if (unionGenTransObject != null && type != null && !unionGenTransObject.containsProperty(propertyName)) {
1091 final GeneratedPropertyBuilder propBuilder = unionGenTransObject
1092 .addProperty(BindingMapping.getPropertyName(propertyName));
1093 propBuilder.setReturnType(type);
1095 unionGenTransObject.addEqualsIdentity(propBuilder);
1096 unionGenTransObject.addHashIdentity(propBuilder);
1097 unionGenTransObject.addToStringProperty(propBuilder);
1102 * Converts <code>typedef</code> to the generated TO builder.
1104 * @param basePackageName string with name of package to which the module belongs
1105 * @param typedef type definition from which is the generated TO builder created
1106 * @return generated TO builder which contains data from <code>typedef</code> and <code>basePackageName</code>
1108 private GeneratedTOBuilder typedefToTransferObject(final String basePackageName,
1109 final TypeDefinition<?> typedef, final String moduleName) {
1110 JavaTypeName name = renames.get(typedef);
1112 name = JavaTypeName.create(
1113 BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, typedef.getPath()),
1114 BindingMapping.getClassName(typedef.getQName().getLocalName()));
1117 final GeneratedTOBuilder newType = newGeneratedTOBuilder(name);
1118 newType.setSchemaPath(typedef.getPath());
1119 newType.setModuleName(moduleName);
1120 addCodegenInformation(newType, typedef);
1125 * Converts <code>typeDef</code> which should be of the type <code>BitsTypeDefinition</code>
1126 * to <code>GeneratedTOBuilder</code>. All the bits of the typeDef are added to returning generated TO as
1129 * @param typeName new type identifier
1130 * @param typeDef type definition from which is the generated TO builder created
1131 * @return generated TO builder which represents <code>typeDef</code>
1132 * @throws IllegalArgumentException
1134 * <li>if <code>typeDef</code> equals null</li>
1135 * <li>if <code>basePackageName</code> equals null</li>
1138 public GeneratedTOBuilder provideGeneratedTOBuilderForBitsTypeDefinition(final JavaTypeName typeName,
1139 final BitsTypeDefinition typeDef, final String moduleName) {
1140 final GeneratedTOBuilder genTOBuilder = newGeneratedTOBuilder(typeName);
1141 genTOBuilder.setSchemaPath(typeDef.getPath());
1142 genTOBuilder.setModuleName(moduleName);
1143 genTOBuilder.setBaseType(typeDef);
1144 genTOBuilder.addImplementsType(TYPE_OBJECT);
1145 addCodegenInformation(genTOBuilder, typeDef);
1147 for (Bit bit : typeDef.getBits()) {
1148 final String name = bit.getName();
1149 GeneratedPropertyBuilder genPropertyBuilder = genTOBuilder.addProperty(
1150 BindingMapping.getPropertyName(name));
1151 genPropertyBuilder.setReadOnly(true);
1152 genPropertyBuilder.setReturnType(BaseYangTypes.BOOLEAN_TYPE);
1154 genTOBuilder.addEqualsIdentity(genPropertyBuilder);
1155 genTOBuilder.addHashIdentity(genPropertyBuilder);
1156 genTOBuilder.addToStringProperty(genPropertyBuilder);
1159 return genTOBuilder;
1163 * Adds to the <code>genTOBuilder</code> the constant which contains regular expressions from
1164 * the <code>regularExpressions</code>.
1166 * @param genTOBuilder generated TO builder to which are <code>regular expressions</code> added
1167 * @param expressions list of string which represent regular expressions
1169 private static void addStringRegExAsConstant(final GeneratedTOBuilder genTOBuilder,
1170 final Map<String, String> expressions) {
1171 if (!expressions.isEmpty()) {
1172 genTOBuilder.addConstant(Types.listTypeFor(BaseYangTypes.STRING_TYPE), TypeConstants.PATTERN_CONSTANT_NAME,
1173 ImmutableMap.copyOf(expressions));
1178 * Creates generated TO with data about inner extended type <code>innerExtendedType</code>, about the package name
1179 * <code>typedefName</code> and about the generated TO name <code>typedefName</code>.
1182 * It is assumed that <code>innerExtendedType</code> is already present in
1183 * {@link AbstractTypeProvider#genTypeDefsContextMap genTypeDefsContextMap} to be possible set it as extended type
1184 * for the returning generated TO.
1186 * @param typedef Type Definition
1187 * @param innerExtendedType extended type which is part of some other extended type
1188 * @param basePackageName string with the package name of the module
1189 * @param moduleName Module Name
1190 * @return generated TO which extends generated TO for <code>innerExtendedType</code>
1191 * @throws IllegalArgumentException
1193 * <li>if <code>extendedType</code> equals null</li>
1194 * <li>if <code>basePackageName</code> equals null</li>
1195 * <li>if <code>typedefName</code> equals null</li>
1198 private GeneratedTransferObject provideGeneratedTOFromExtendedType(final TypeDefinition<?> typedef,
1199 final TypeDefinition<?> innerExtendedType, final String basePackageName, final String moduleName) {
1200 Preconditions.checkArgument(innerExtendedType != null, "Extended type cannot be NULL!");
1201 Preconditions.checkArgument(basePackageName != null, "String with base package name cannot be NULL!");
1203 final GeneratedTOBuilder genTOBuilder = newGeneratedTOBuilder(JavaTypeName.create(basePackageName,
1204 BindingMapping.getClassName(typedef.getQName())));
1205 genTOBuilder.setSchemaPath(typedef.getPath());
1206 genTOBuilder.setModuleName(moduleName);
1207 genTOBuilder.setTypedef(true);
1208 addCodegenInformation(genTOBuilder, typedef);
1210 final Restrictions r = BindingGeneratorUtil.getRestrictions(typedef);
1211 genTOBuilder.setRestrictions(r);
1212 addStringRegExAsConstant(genTOBuilder, resolveRegExpressionsFromTypedef(typedef));
1214 if (typedef.getStatus() == Status.DEPRECATED) {
1215 genTOBuilder.addAnnotation("java.lang", "Deprecated");
1218 if (baseTypeDefForExtendedType(innerExtendedType) instanceof UnionTypeDefinition) {
1219 genTOBuilder.setIsUnion(true);
1222 Map<Optional<Revision>, Map<String, Type>> modulesByDate = null;
1223 Map<String, Type> typeMap = null;
1224 final Module parentModule = findParentModule(schemaContext, innerExtendedType);
1225 if (parentModule != null) {
1226 modulesByDate = genTypeDefsContextMap.get(parentModule.getName());
1227 typeMap = modulesByDate.get(parentModule.getRevision());
1230 if (typeMap != null) {
1231 final String innerTypeDef = innerExtendedType.getQName().getLocalName();
1232 final Type type = typeMap.get(innerTypeDef);
1233 if (type instanceof GeneratedTransferObject) {
1234 genTOBuilder.setExtendsType((GeneratedTransferObject) type);
1237 addUnitsToGenTO(genTOBuilder, typedef.getUnits().orElse(null));
1238 makeSerializable(genTOBuilder);
1240 return genTOBuilder.build();
1244 * Add {@link java.io.Serializable} to implemented interfaces of this TO. Also compute and add serialVersionUID
1247 * @param gto transfer object which needs to be made serializable
1249 private static void makeSerializable(final GeneratedTOBuilder gto) {
1250 gto.addImplementsType(Types.serializableType());
1251 final GeneratedPropertyBuilder prop = new GeneratedPropertyBuilderImpl("serialVersionUID");
1252 prop.setValue(Long.toString(BindingGeneratorUtil.computeDefaultSUID(gto)));
1257 * Finds out for each type definition how many immersion (depth) is necessary to get to the base type. Every type
1258 * definition is inserted to the map which key is depth and value is list of type definitions with equal depth.
1259 * In next step are lists from this map concatenated to one list in ascending order according to their depth. All
1260 * type definitions are in the list behind all type definitions on which depends.
1262 * @param unsortedTypeDefinitions list of type definitions which should be sorted by depth
1263 * @return list of type definitions sorted according their each other dependencies (type definitions which are
1264 * dependent on other type definitions are in list behind them).
1266 private static List<TypeDefinition<?>> sortTypeDefinitionAccordingDepth(
1267 final Collection<TypeDefinition<?>> unsortedTypeDefinitions) {
1268 final List<TypeDefinition<?>> sortedTypeDefinition = new ArrayList<>();
1270 final Map<Integer, List<TypeDefinition<?>>> typeDefinitionsDepths = new TreeMap<>();
1271 for (TypeDefinition<?> unsortedTypeDefinition : unsortedTypeDefinitions) {
1272 final Integer depth = getTypeDefinitionDepth(unsortedTypeDefinition);
1273 List<TypeDefinition<?>> typeDefinitionsConcreteDepth =
1274 typeDefinitionsDepths.computeIfAbsent(depth, k -> new ArrayList<>());
1275 typeDefinitionsConcreteDepth.add(unsortedTypeDefinition);
1278 // SortedMap guarantees order corresponding to keys in ascending order
1279 for (List<TypeDefinition<?>> v : typeDefinitionsDepths.values()) {
1280 sortedTypeDefinition.addAll(v);
1283 return sortedTypeDefinition;
1287 * Returns how many immersion is necessary to get from the type definition to the base type.
1289 * @param typeDefinition type definition for which is depth sought.
1290 * @return number of immersions which are necessary to get from the type definition to the base type
1292 private static int getTypeDefinitionDepth(final TypeDefinition<?> typeDefinition) {
1293 // FIXME: rewrite this in a non-recursive manner
1294 if (typeDefinition == null) {
1297 final TypeDefinition<?> baseType = typeDefinition.getBaseType();
1298 if (baseType == null) {
1303 if (baseType.getBaseType() != null) {
1304 depth = depth + getTypeDefinitionDepth(baseType);
1305 } else if (baseType instanceof UnionTypeDefinition) {
1306 final List<TypeDefinition<?>> childTypeDefinitions = ((UnionTypeDefinition) baseType).getTypes();
1307 int maxChildDepth = 0;
1309 for (TypeDefinition<?> childTypeDefinition : childTypeDefinitions) {
1310 childDepth = childDepth + getTypeDefinitionDepth(childTypeDefinition);
1311 if (childDepth > maxChildDepth) {
1312 maxChildDepth = childDepth;
1315 return maxChildDepth;
1321 * Returns string which contains the same value as <code>name</code> but integer suffix is incremented by one. If
1322 * <code>name</code> contains no number suffix, a new suffix initialized at 1 is added. A suffix is actually
1323 * composed of a '$' marker, which is safe, as no YANG identifier can contain '$', and a unsigned decimal integer.
1325 * @param name string with name of augmented node
1326 * @return string with the number suffix incremented by one (or 1 is added)
1328 private static String provideAvailableNameForGenTOBuilder(final String name) {
1329 final int dollar = name.indexOf('$');
1334 final int newSuffix = Integer.parseUnsignedInt(name.substring(dollar + 1)) + 1;
1335 Preconditions.checkState(newSuffix > 0, "Suffix counter overflow");
1336 return name.substring(0, dollar + 1) + newSuffix;
1339 public static void addUnitsToGenTO(final GeneratedTOBuilder to, final String units) {
1340 if (!Strings.isNullOrEmpty(units)) {
1341 to.addConstant(Types.STRING, "_UNITS", "\"" + units + "\"");
1342 final GeneratedPropertyBuilder prop = new GeneratedPropertyBuilderImpl("UNITS");
1343 prop.setReturnType(Types.STRING);
1344 to.addToStringProperty(prop);
1349 public String getTypeDefaultConstruction(final LeafSchemaNode node) {
1350 return getTypeDefaultConstruction(node, (String) node.getType().getDefaultValue().orElse(null));
1353 public String getTypeDefaultConstruction(final LeafSchemaNode node, final String defaultValue) {
1354 final TypeDefinition<?> type = CompatUtils.compatType(node);
1355 final QName typeQName = type.getQName();
1356 final TypeDefinition<?> base = baseTypeDefForExtendedType(type);
1357 requireNonNull(type, () -> "Cannot provide default construction for null type of " + node);
1358 requireNonNull(defaultValue, () -> "Cannot provide default construction for null default statement of "
1361 final StringBuilder sb = new StringBuilder();
1362 String result = null;
1363 if (base instanceof BinaryTypeDefinition) {
1364 result = binaryToDef(defaultValue);
1365 } else if (base instanceof BitsTypeDefinition) {
1368 final Module parent = getParentModule(node);
1369 final Iterator<QName> path = node.getPath().getPathFromRoot().iterator();
1371 if (!path.hasNext()) {
1372 parentName = BindingMapping.getClassName(parent.getName()) + "Data";
1373 final String basePackageName = BindingMapping.getRootPackageName(parent.getQNameModule());
1374 className = basePackageName + "." + parentName + "." + BindingMapping.getClassName(node.getQName());
1376 final String basePackageName = BindingMapping.getRootPackageName(parent.getQNameModule());
1377 final String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName,
1379 parentName = BindingMapping.getClassName(parent.getName());
1380 className = packageName + "." + parentName + "." + BindingMapping.getClassName(node.getQName());
1382 result = bitsToDef((BitsTypeDefinition) base, className, defaultValue, type.getBaseType() != null);
1383 } else if (base instanceof BooleanTypeDefinition) {
1384 result = typeToBooleanDef(defaultValue);
1385 } else if (base instanceof DecimalTypeDefinition) {
1386 result = typeToDef(BigDecimal.class, defaultValue);
1387 } else if (base instanceof EmptyTypeDefinition) {
1388 result = typeToBooleanDef(defaultValue);
1389 } else if (base instanceof EnumTypeDefinition) {
1390 final char[] defValArray = defaultValue.toCharArray();
1391 final char first = Character.toUpperCase(defaultValue.charAt(0));
1392 defValArray[0] = first;
1393 final String newDefVal = new String(defValArray);
1395 if (type.getBaseType() != null) {
1396 final Module m = getParentModule(type);
1397 final String basePackageName = BindingMapping.getRootPackageName(m.getQNameModule());
1398 final String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName,
1400 className = packageName + "." + BindingMapping.getClassName(typeQName);
1402 final Module parentModule = getParentModule(node);
1403 final String basePackageName = BindingMapping.getRootPackageName(parentModule.getQNameModule());
1404 final String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName,
1406 className = packageName + "." + BindingMapping.getClassName(node.getQName());
1408 result = className + "." + newDefVal;
1409 } else if (base instanceof IdentityrefTypeDefinition) {
1410 throw new UnsupportedOperationException("Cannot get default construction for identityref type");
1411 } else if (base instanceof InstanceIdentifierTypeDefinition) {
1412 throw new UnsupportedOperationException("Cannot get default construction for instance-identifier type");
1413 } else if (BaseTypes.isInt8(base)) {
1414 result = typeToValueOfDef(Byte.class, defaultValue);
1415 } else if (BaseTypes.isInt16(base)) {
1416 result = typeToValueOfDef(Short.class, defaultValue);
1417 } else if (BaseTypes.isInt32(base)) {
1418 result = typeToValueOfDef(Integer.class, defaultValue);
1419 } else if (BaseTypes.isInt64(base)) {
1420 result = typeToValueOfDef(Long.class, defaultValue);
1421 } else if (base instanceof LeafrefTypeDefinition) {
1422 result = leafrefToDef(node, (LeafrefTypeDefinition) base, defaultValue);
1423 } else if (base instanceof StringTypeDefinition) {
1424 result = "\"" + defaultValue + "\"";
1425 } else if (BaseTypes.isUint8(base)) {
1426 result = typeToValueOfDef(Uint8.class, defaultValue);
1427 } else if (BaseTypes.isUint16(base)) {
1428 result = typeToValueOfDef(Uint16.class, defaultValue);
1429 } else if (BaseTypes.isUint32(base)) {
1430 result = typeToValueOfDef(Uint32.class, defaultValue);
1431 } else if (BaseTypes.isUint64(base)) {
1432 result = typeToValueOfDef(Uint64.class, defaultValue);
1433 } else if (base instanceof UnionTypeDefinition) {
1434 result = unionToDef(node);
1440 if (type.getBaseType() != null && !(base instanceof LeafrefTypeDefinition)
1441 && !(base instanceof EnumTypeDefinition) && !(base instanceof UnionTypeDefinition)) {
1442 final Module m = getParentModule(type);
1443 final String basePackageName = BindingMapping.getRootPackageName(m.getQNameModule());
1444 final String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName,
1446 final String className = packageName + "." + BindingMapping.getClassName(typeQName);
1447 sb.insert(0, "new " + className + "(");
1448 sb.insert(sb.length(), ')');
1451 return sb.toString();
1454 private static String typeToDef(final Class<?> clazz, final String defaultValue) {
1455 return "new " + clazz.getName() + "(\"" + defaultValue + "\")";
1458 private static String typeToValueOfDef(final Class<?> clazz, final String defaultValue) {
1459 return clazz.getName() + ".valueOf(\"" + defaultValue + "\")";
1462 private static String typeToBooleanDef(final String defaultValue) {
1463 switch (defaultValue) {
1465 return "java.lang.Boolean.FALSE";
1467 return "java.lang.Boolean.TRUE";
1469 return typeToValueOfDef(Boolean.class, defaultValue);
1473 private static String binaryToDef(final String defaultValue) {
1474 final StringBuilder sb = new StringBuilder();
1475 final byte[] encoded = Base64.getDecoder().decode(defaultValue);
1476 sb.append("new byte[] {");
1477 for (int i = 0; i < encoded.length; i++) {
1478 sb.append(encoded[i]);
1479 if (i != encoded.length - 1) {
1484 return sb.toString();
1487 private static final Comparator<Bit> BIT_NAME_COMPARATOR = Comparator.comparing(Bit::getName);
1489 private static String bitsToDef(final BitsTypeDefinition type, final String className, final String defaultValue,
1490 final boolean isExt) {
1491 final List<Bit> bits = new ArrayList<>(type.getBits());
1492 bits.sort(BIT_NAME_COMPARATOR);
1493 final StringBuilder sb = new StringBuilder();
1496 sb.append(className);
1499 for (int i = 0; i < bits.size(); i++) {
1500 if (bits.get(i).getName().equals(defaultValue)) {
1505 if (i != bits.size() - 1) {
1512 return sb.toString();
1515 private Module getParentModule(final SchemaNode node) {
1516 final QName qname = node.getPath().getPathFromRoot().iterator().next();
1517 return schemaContext.findModule(qname.getModule()).orElse(null);
1520 private String leafrefToDef(final LeafSchemaNode parentNode, final LeafrefTypeDefinition leafrefType,
1521 final String defaultValue) {
1522 Preconditions.checkArgument(leafrefType != null, "Leafref Type Definition reference cannot be NULL!");
1523 Preconditions.checkArgument(leafrefType.getPathStatement() != null,
1524 "The Path Statement for Leafref Type Definition cannot be NULL!");
1526 final PathExpression xpath = leafrefType.getPathStatement();
1527 final String strXPath = xpath.getOriginalString();
1529 if (strXPath != null) {
1530 if (strXPath.indexOf('[') == -1) {
1531 final Module module = findParentModule(schemaContext, parentNode);
1532 if (module != null) {
1533 final SchemaNode dataNode;
1534 if (xpath.isAbsolute()) {
1535 dataNode = findDataTreeSchemaNode(schemaContext, module.getQNameModule(), xpath);
1537 dataNode = findDataSchemaNodeForRelativeXPath(schemaContext, module, parentNode, xpath);
1539 final String result = getTypeDefaultConstruction((LeafSchemaNode) dataNode, defaultValue);
1543 return "new java.lang.Object()";
1550 private String unionToDef(final LeafSchemaNode node) {
1551 final TypeDefinition<?> type = CompatUtils.compatType(node);
1555 if (type.getBaseType() != null) {
1556 final QName typeQName = type.getQName();
1557 Module module = null;
1558 final Collection<? extends Module> modules = schemaContext.findModules(typeQName.getNamespace());
1559 if (modules.size() > 1) {
1560 for (Module m : modules) {
1561 if (m.getRevision().equals(typeQName.getRevision())) {
1566 if (module == null) {
1567 final List<Module> modulesList = new ArrayList<>(modules);
1568 modulesList.sort((o1, o2) -> Revision.compare(o1.getRevision(), o2.getRevision()));
1569 module = modulesList.get(0);
1572 module = modules.iterator().next();
1575 final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
1576 className = basePackageName + "." + BindingMapping.getClassName(typeQName);
1578 final Iterator<QName> path = node.getPath().getPathFromRoot().iterator();
1579 final QName first = path.next();
1580 final Module parent = schemaContext.findModule(first.getModule()).orElse(null);
1581 final String basePackageName = BindingMapping.getRootPackageName(parent.getQNameModule());
1582 if (!path.hasNext()) {
1583 parentName = BindingMapping.getClassName(parent.getName()) + "Data";
1584 className = basePackageName + "." + parentName + "." + BindingMapping.getClassName(node.getQName());
1586 final String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName,
1588 className = packageName + "." + BindingMapping.getClassName(node.getQName());
1591 return union(className, (String) node.getType().getDefaultValue().orElse(null), node);
1594 private static String union(final String className, final String defaultValue, final LeafSchemaNode node) {
1595 final StringBuilder sb = new StringBuilder();
1597 sb.append(className);
1599 sb.append(defaultValue);
1600 sb.append("\".toCharArray())");
1601 return sb.toString();
1605 public String getConstructorPropertyName(final SchemaNode node) {
1606 return node instanceof TypeDefinition<?> ? "value" : "";
1610 public String getParamNameFromType(final TypeDefinition<?> type) {
1611 return BindingMapping.getPropertyName(type.getQName().getLocalName());