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.base.Preconditions;
18 import com.google.common.base.Strings;
19 import com.google.common.collect.ImmutableMap;
20 import java.math.BigDecimal;
21 import java.math.BigInteger;
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.model.api.AccessModifier;
38 import org.opendaylight.mdsal.binding.model.api.ConcreteType;
39 import org.opendaylight.mdsal.binding.model.api.Enumeration;
40 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty;
41 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
42 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
43 import org.opendaylight.mdsal.binding.model.api.Restrictions;
44 import org.opendaylight.mdsal.binding.model.api.Type;
45 import org.opendaylight.mdsal.binding.model.api.type.builder.EnumBuilder;
46 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedPropertyBuilder;
47 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTOBuilder;
48 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilder;
49 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilderBase;
50 import org.opendaylight.mdsal.binding.model.api.type.builder.MethodSignatureBuilder;
51 import org.opendaylight.mdsal.binding.model.util.BindingGeneratorUtil;
52 import org.opendaylight.mdsal.binding.model.util.TypeConstants;
53 import org.opendaylight.mdsal.binding.model.util.Types;
54 import org.opendaylight.mdsal.binding.model.util.generated.type.builder.AbstractEnumerationBuilder;
55 import org.opendaylight.mdsal.binding.model.util.generated.type.builder.GeneratedPropertyBuilderImpl;
56 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
57 import org.opendaylight.yangtools.yang.common.QName;
58 import org.opendaylight.yangtools.yang.common.Revision;
59 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
60 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
61 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
62 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
63 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
64 import org.opendaylight.yangtools.yang.model.api.Module;
65 import org.opendaylight.yangtools.yang.model.api.PathExpression;
66 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
67 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
68 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
69 import org.opendaylight.yangtools.yang.model.api.Status;
70 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
71 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
72 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
73 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
74 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
75 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
76 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
77 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
78 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
79 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
80 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
81 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
82 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
83 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
84 import org.opendaylight.yangtools.yang.model.util.ModuleDependencySort;
85 import org.opendaylight.yangtools.yang.model.util.PathExpressionImpl;
86 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
87 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
88 import org.opendaylight.yangtools.yang.model.util.type.CompatUtils;
89 import org.slf4j.Logger;
90 import org.slf4j.LoggerFactory;
93 public abstract class AbstractTypeProvider implements TypeProvider {
94 private static final Logger LOG = LoggerFactory.getLogger(AbstractTypeProvider.class);
95 private static final Pattern GROUPS_PATTERN = Pattern.compile("\\[(.*?)\\]");
97 // Backwards compatibility: Union types used to be instantiated in YANG namespace, which is no longer
98 // the case, as unions are emitted to their correct schema path.
99 private static final SchemaPath UNION_PATH = SchemaPath.create(true,
100 org.opendaylight.yangtools.yang.model.util.BaseTypes.UNION_QNAME);
103 * Contains the schema data red from YANG files.
105 private final SchemaContext schemaContext;
107 private final Map<String, Map<Optional<Revision>, Map<String, Type>>> genTypeDefsContextMap = new HashMap<>();
110 * The map which maps schema paths to JAVA <code>Type</code>.
112 private final Map<SchemaPath, Type> referencedTypes = new HashMap<>();
113 private final Map<Module, Set<Type>> additionalTypes = new HashMap<>();
114 private final Map<SchemaNode, JavaTypeName> renames;
117 * Creates new instance of class <code>TypeProviderImpl</code>.
119 * @param schemaContext contains the schema data red from YANG files
120 * @param renames renaming table
121 * @throws IllegalArgumentException if <code>schemaContext</code> equal null.
123 AbstractTypeProvider(final SchemaContext schemaContext, final Map<SchemaNode, JavaTypeName> renames) {
124 Preconditions.checkArgument(schemaContext != null, "Schema Context cannot be null!");
125 this.schemaContext = schemaContext;
126 this.renames = requireNonNull(renames);
127 resolveTypeDefsFromContext();
131 * Puts <code>refType</code> to map with key <code>refTypePath</code>.
133 * @param refTypePath schema path used as the map key
134 * @param refType type which represents the map value
135 * @throws IllegalArgumentException
137 * <li>if <code>refTypePath</code> equal null</li>
138 * <li>if <code>refType</code> equal null</li>
142 public void putReferencedType(final SchemaPath refTypePath, final Type refType) {
143 Preconditions.checkArgument(refTypePath != null,
144 "Path reference of Enumeration Type Definition cannot be NULL!");
145 Preconditions.checkArgument(refType != null, "Reference to Enumeration Type cannot be NULL!");
146 referencedTypes.put(refTypePath, refType);
149 public Map<Module, Set<Type>> getAdditionalTypes() {
150 return additionalTypes;
154 public Type javaTypeForSchemaDefinitionType(final TypeDefinition<?> typeDefinition, final SchemaNode parentNode,
155 final boolean lenientRelativeLeafrefs) {
156 return javaTypeForSchemaDefinitionType(typeDefinition, parentNode, null, lenientRelativeLeafrefs);
160 * Converts schema definition type <code>typeDefinition</code> to JAVA <code>Type</code>.
162 * @param typeDefinition type definition which is converted to JAVA type
163 * @throws IllegalArgumentException
165 * <li>if <code>typeDefinition</code> equal null</li>
166 * <li>if Qname of <code>typeDefinition</code> equal null</li>
167 * <li>if name of <code>typeDefinition</code> equal null</li>
171 public Type javaTypeForSchemaDefinitionType(final TypeDefinition<?> typeDefinition, final SchemaNode parentNode,
172 final Restrictions restrictions, final boolean lenientRelativeLeafrefs) {
173 Preconditions.checkArgument(typeDefinition != null, "Type Definition cannot be NULL!");
174 Preconditions.checkArgument(typeDefinition.getQName() != null,
175 "Type Definition cannot have non specified QName (QName cannot be NULL!)");
176 final String typedefName = typeDefinition.getQName().getLocalName();
177 Preconditions.checkArgument(typedefName != null, "Type Definitions Local Name cannot be NULL!");
179 // Deal with base types
180 if (typeDefinition.getBaseType() == null) {
181 // We have to deal with differing handling of decimal64. The old parser used a fixed Decimal64 type
182 // and generated an enclosing ExtendedType to hold any range constraints. The new parser instantiates
183 // a base type which holds these constraints.
184 if (typeDefinition instanceof DecimalTypeDefinition) {
185 final Type ret = BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForSchemaDefinitionType(typeDefinition,
186 parentNode, restrictions, lenientRelativeLeafrefs);
192 // Deal with leafrefs/identityrefs
193 Type ret = javaTypeForLeafrefOrIdentityRef(typeDefinition, parentNode, lenientRelativeLeafrefs);
198 // FIXME: it looks as though we could be using the same codepath as above...
199 ret = BaseYangTypes.javaTypeForYangType(typeDefinition.getQName().getLocalName());
201 LOG.debug("Failed to resolve Java type for {}", typeDefinition);
207 Type returnType = javaTypeForExtendedType(typeDefinition, lenientRelativeLeafrefs);
208 if (restrictions != null && returnType instanceof GeneratedTransferObject) {
209 final GeneratedTransferObject gto = (GeneratedTransferObject) returnType;
210 final Module module = findParentModule(schemaContext, parentNode);
211 final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
212 final String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName,
213 typeDefinition.getPath());
214 final String genTOName = BindingMapping.getClassName(typedefName);
215 final String name = packageName + "." + genTOName;
216 if (!returnType.getFullyQualifiedName().equals(name)) {
217 returnType = shadedTOWithRestrictions(gto, restrictions);
223 public SchemaNode getTargetForLeafref(final LeafrefTypeDefinition leafrefType, final SchemaNode parentNode) {
224 final PathExpression xpath = leafrefType.getPathStatement();
225 Preconditions.checkArgument(xpath != null, "The Path Statement for Leafref Type Definition cannot be NULL!");
227 final Module module = findParentModule(schemaContext, parentNode);
228 Preconditions.checkArgument(module != null, "Failed to find module for parent %s", parentNode);
230 return xpath.isAbsolute() ? findDataTreeSchemaNode(schemaContext, module.getQNameModule(), xpath)
231 : findDataSchemaNodeForRelativeXPath(schemaContext, module, parentNode, xpath);
234 private GeneratedTransferObject shadedTOWithRestrictions(final GeneratedTransferObject gto,
235 final Restrictions restrictions) {
236 final GeneratedTOBuilder gtob = newGeneratedTOBuilder(gto.getIdentifier());
237 final GeneratedTransferObject parent = gto.getSuperType();
238 if (parent != null) {
239 gtob.setExtendsType(parent);
241 gtob.setRestrictions(restrictions);
242 for (GeneratedProperty gp : gto.getProperties()) {
243 final GeneratedPropertyBuilder gpb = gtob.addProperty(gp.getName());
244 gpb.setValue(gp.getValue());
245 gpb.setReadOnly(gp.isReadOnly());
246 gpb.setAccessModifier(gp.getAccessModifier());
247 gpb.setReturnType(gp.getReturnType());
248 gpb.setFinal(gp.isFinal());
249 gpb.setStatic(gp.isStatic());
254 private boolean isLeafRefSelfReference(final LeafrefTypeDefinition leafref, final SchemaNode parentNode) {
256 * First check if the leafref is an augment. If that is the case, skip it as it will be checked once augments
259 DataNodeContainer current = null;
260 DataSchemaNode dataChildByName;
261 for (QName next : parentNode.getPath().getPathFromRoot()) {
262 if (current == null) {
263 dataChildByName = schemaContext.getDataChildByName(next);
265 dataChildByName = current.getDataChildByName(next);
267 if (dataChildByName == null) {
270 if (dataChildByName.isAugmenting()) {
273 if (dataChildByName instanceof DataNodeContainer) {
274 current = (DataNodeContainer) dataChildByName;
278 // Then try to look up the expression.
279 final PathExpression leafRefXPath = leafref.getPathStatement();
280 final Module parentModule = getParentModule(parentNode);
281 final SchemaNode leafRefValueNode;
282 if (leafRefXPath.isAbsolute()) {
283 leafRefValueNode = SchemaContextUtil.findDataTreeSchemaNode(schemaContext, parentModule.getQNameModule(),
286 leafRefValueNode = SchemaContextUtil.findDataSchemaNodeForRelativeXPath(schemaContext, parentModule,
287 parentNode, new PathExpressionImpl(
288 GROUPS_PATTERN.matcher(leafRefXPath.getOriginalString()).replaceAll(""), false));
291 return leafRefValueNode != null && leafRefValueNode.equals(parentNode);
295 * Returns JAVA <code>Type</code> for instances of the type <code>LeafrefTypeDefinition</code> or
296 * <code>IdentityrefTypeDefinition</code>.
298 * @param typeDefinition type definition which is converted to JAVA <code>Type</code>
299 * @return JAVA <code>Type</code> instance for <code>typeDefinition</code>
301 private Type javaTypeForLeafrefOrIdentityRef(final TypeDefinition<?> typeDefinition, final SchemaNode parentNode,
302 final boolean inGrouping) {
303 if (typeDefinition instanceof LeafrefTypeDefinition) {
304 final LeafrefTypeDefinition leafref = (LeafrefTypeDefinition) typeDefinition;
305 Preconditions.checkArgument(!isLeafRefSelfReference(leafref, parentNode),
306 "Leafref %s is referencing itself, incoming StackOverFlowError detected.", leafref);
307 return provideTypeForLeafref(leafref, parentNode, inGrouping);
308 } else if (typeDefinition instanceof IdentityrefTypeDefinition) {
309 return provideTypeForIdentityref((IdentityrefTypeDefinition) typeDefinition);
316 * Returns JAVA <code>Type</code> for instances of the type <code>ExtendedType</code>.
318 * @param typeDefinition type definition which is converted to JAVA <code>Type</code>
319 * @return JAVA <code>Type</code> instance for <code>typeDefinition</code>
321 private Type javaTypeForExtendedType(final TypeDefinition<?> typeDefinition, final boolean lenient) {
322 final String typedefName = typeDefinition.getQName().getLocalName();
323 final TypeDefinition<?> baseTypeDef = baseTypeDefForExtendedType(typeDefinition);
324 Type returnType = javaTypeForLeafrefOrIdentityRef(baseTypeDef, typeDefinition, lenient);
325 if (returnType == null) {
326 if (baseTypeDef instanceof EnumTypeDefinition) {
327 final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) baseTypeDef;
328 returnType = provideTypeForEnum(enumTypeDef, typedefName, typeDefinition);
330 final Module module = findParentModule(schemaContext, typeDefinition);
331 final Restrictions r = BindingGeneratorUtil.getRestrictions(typeDefinition);
332 if (module != null) {
333 final Map<Optional<Revision>, Map<String, Type>> modulesByDate = genTypeDefsContextMap.get(
335 final Map<String, Type> genTOs = modulesByDate.get(module.getRevision());
336 if (genTOs != null) {
337 returnType = genTOs.get(typedefName);
339 if (returnType == null) {
340 returnType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForSchemaDefinitionType(
341 baseTypeDef, typeDefinition, r, lenient);
350 * Seeks for identity reference <code>idref</code> the JAVA <code>type</code>.
354 * If identy which is referenced via <code>idref</code> has name <b>Idn</b>
355 * then returning type is <b>{@code Class<? extends Idn>}</b></i>
357 * @param idref identityref type definition for which JAVA <code>Type</code> is sought
358 * @return JAVA <code>Type</code> of the identity which is referenced through <code>idref</code>
360 private Type provideTypeForIdentityref(final IdentityrefTypeDefinition idref) {
361 final Collection<IdentitySchemaNode> identities = idref.getIdentities();
362 if (identities.size() > 1) {
363 LOG.warn("Identity reference {} has multiple identities, using only the first one", idref);
366 final QName baseIdQName = identities.iterator().next().getQName();
367 final Module module = schemaContext.findModule(baseIdQName.getModule()).orElse(null);
368 IdentitySchemaNode identity = null;
369 for (IdentitySchemaNode id : module.getIdentities()) {
370 if (id.getQName().equals(baseIdQName)) {
374 Preconditions.checkArgument(identity != null, "Target identity '" + baseIdQName + "' do not exists");
376 final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
377 final JavaTypeName identifier = JavaTypeName.create(BindingGeneratorUtil.packageNameForGeneratedType(
378 basePackageName, identity.getPath()), BindingMapping.getClassName(identity.getQName()));
379 return Types.classType(Types.wildcardTypeFor(identifier));
383 * Converts <code>typeDefinition</code> to concrete JAVA <code>Type</code>.
385 * @param typeDefinition
386 * type definition which should be converted to JAVA
388 * @return JAVA <code>Type</code> which represents
389 * <code>typeDefinition</code>
390 * @throws IllegalArgumentException
392 * <li>if <code>typeDefinition</code> equal null</li>
393 * <li>if Q name of <code>typeDefinition</code></li>
394 * <li>if name of <code>typeDefinition</code></li>
397 public Type generatedTypeForExtendedDefinitionType(final TypeDefinition<?> typeDefinition,
398 final SchemaNode parentNode) {
399 Preconditions.checkArgument(typeDefinition != null, "Type Definition cannot be NULL!");
400 if (typeDefinition.getQName() == null) {
401 throw new IllegalArgumentException("Type Definition cannot have unspecified QName (QName cannot be NULL!)");
403 Preconditions.checkArgument(typeDefinition.getQName().getLocalName() != null,
404 "Type Definitions Local Name cannot be NULL!");
406 final TypeDefinition<?> baseTypeDef = baseTypeDefForExtendedType(typeDefinition);
407 if (baseTypeDef instanceof LeafrefTypeDefinition || baseTypeDef instanceof IdentityrefTypeDefinition) {
409 * This is backwards compatibility baggage from way back when. The problem at hand is inconsistency between
410 * the fact that identity is mapped to a Class, which is also returned from leaves which specify it like
423 * This results in getFoo() returning Class<? extends Iden>, which looks fine on the surface, but gets more
424 * dicey when we throw in:
438 * Now we have competing requirements: typedef would like us to use encapsulation to capture the defined
439 * type, while getBar() wants us to retain shape with getFoo(), as it should not matter how the identityref
442 * In this particular case getFoo() won just after the Binding Spec was frozen, hence we do not generate
443 * an encapsulation for identityref typedefs.
445 * In case you are thinking we could get by having foo-ref map to a subclass of Iden, that is not a good
446 * option, as it would look as though it is the product of a different construct:
452 * Leading to a rather nice namespace clash and also slight incompatibility with unknown third-party
453 * sub-identities of iden.
455 * The story behind leafrefs is probably similar, but that needs to be ascertained.
460 final Module module = findParentModule(schemaContext, parentNode);
461 if (module != null) {
462 final Map<Optional<Revision>, Map<String, Type>> modulesByDate = genTypeDefsContextMap.get(
464 final Map<String, Type> genTOs = modulesByDate.get(module.getRevision());
465 if (genTOs != null) {
466 return genTOs.get(typeDefinition.getQName().getLocalName());
473 * Gets base type definition for <code>extendTypeDef</code>. The method is
474 * recursively called until non <code>ExtendedType</code> type is found.
476 * @param extendTypeDef
477 * type definition for which is the base type definition sought
478 * @return type definition which is base type for <code>extendTypeDef</code>
479 * @throws IllegalArgumentException
480 * if <code>extendTypeDef</code> equal null
482 private static TypeDefinition<?> baseTypeDefForExtendedType(final TypeDefinition<?> extendTypeDef) {
483 Preconditions.checkArgument(extendTypeDef != null, "Type Definition reference cannot be NULL!");
485 TypeDefinition<?> ret = extendTypeDef;
486 while (ret.getBaseType() != null) {
487 ret = ret.getBaseType();
494 * Converts <code>leafrefType</code> to JAVA <code>Type</code>. The path of <code>leafrefType</code> is followed
495 * to find referenced node and its <code>Type</code> is returned.
497 * @param leafrefType leafref type definition for which is the type sought
498 * @param parentNode parent node of the leaf being resolved
499 * @param inGrouping true if we are resolving the type within a grouping.
500 * @return JAVA <code>Type</code> of data schema node which is referenced in <code>leafrefType</code>
501 * @throws IllegalArgumentException
503 * <li>if <code>leafrefType</code> equal null</li>
504 * <li>if path statement of <code>leafrefType</code> equal null</li>
507 public Type provideTypeForLeafref(final LeafrefTypeDefinition leafrefType, final SchemaNode parentNode,
508 final boolean inGrouping) {
509 Preconditions.checkArgument(leafrefType != null, "Leafref Type Definition reference cannot be NULL!");
511 final PathExpression xpath = leafrefType.getPathStatement();
512 Preconditions.checkArgument(xpath != null, "The Path Statement for Leafref Type Definition cannot be NULL!");
514 final String strXPath = xpath.getOriginalString();
515 if (strXPath.indexOf('[') != -1) {
516 // XXX: why are we special-casing this?
517 return Types.objectType();
520 final Module module = findParentModule(schemaContext, parentNode);
521 Preconditions.checkArgument(module != null, "Failed to find module for parent %s", parentNode);
523 final SchemaNode dataNode;
524 if (xpath.isAbsolute()) {
525 dataNode = findDataTreeSchemaNode(schemaContext, module.getQNameModule(), xpath);
527 dataNode = findDataSchemaNodeForRelativeXPath(schemaContext, module, parentNode, xpath);
528 if (dataNode == null && inGrouping) {
529 // Relative path within a grouping may end up being unresolvable because it may refer outside
530 // the grouping, in which case it is polymorphic based on instantiation, for example:
553 LOG.debug("Leafref type {} not found in parent {}, assuming polymorphic object", leafrefType,
555 return Types.objectType();
558 Preconditions.checkArgument(dataNode != null, "Failed to find leafref target: %s in module %s (%s)",
559 strXPath, this.getParentModule(parentNode).getName(), parentNode.getQName().getModule());
561 // FIXME: this block seems to be some weird magic hack. Analyze and refactor it.
562 Type returnType = null;
563 if (leafContainsEnumDefinition(dataNode)) {
564 returnType = referencedTypes.get(dataNode.getPath());
565 } else if (leafListContainsEnumDefinition(dataNode)) {
566 returnType = Types.listTypeFor(referencedTypes.get(dataNode.getPath()));
568 if (returnType == null) {
569 returnType = resolveTypeFromDataSchemaNode(dataNode, inGrouping);
571 Preconditions.checkArgument(returnType != null, "Failed to find leafref target: %s in module %s (%s)",
572 strXPath, this.getParentModule(parentNode).getName(), parentNode.getQName().getModule(), this);
577 * Checks if <code>dataNode</code> is <code>LeafSchemaNode</code> and if it so then checks if it is of type
578 * <code>EnumTypeDefinition</code>.
580 * @param dataNode data schema node for which is checked if it is leaf and if it is of enum type
581 * @return boolean value
583 * <li>true - if <code>dataNode</code> is leaf of type enumeration</li>
584 * <li>false - other cases</li>
587 private static boolean leafContainsEnumDefinition(final SchemaNode dataNode) {
588 if (dataNode instanceof LeafSchemaNode) {
589 final LeafSchemaNode leaf = (LeafSchemaNode) dataNode;
590 return CompatUtils.compatType(leaf) instanceof EnumTypeDefinition;
596 * Checks if <code>dataNode</code> is <code>LeafListSchemaNode</code> and if it so then checks if it is of type
597 * <code>EnumTypeDefinition</code>.
599 * @param dataNode data schema node for which is checked if it is leaflist and if it is of enum type
600 * @return boolean value
602 * <li>true - if <code>dataNode</code> is leaflist of type
604 * <li>false - other cases</li>
607 private static boolean leafListContainsEnumDefinition(final SchemaNode dataNode) {
608 if (dataNode instanceof LeafListSchemaNode) {
609 final LeafListSchemaNode leafList = (LeafListSchemaNode) dataNode;
610 return leafList.getType() instanceof EnumTypeDefinition;
616 * Converts <code>enumTypeDef</code> to {@link Enumeration enumeration}.
618 * @param enumTypeDef enumeration type definition which is converted to enumeration
619 * @param enumName string with name which is used as the enumeration name
620 * @return enumeration type which is built with data (name, enum values) from <code>enumTypeDef</code>
621 * @throws IllegalArgumentException
623 * <li>if <code>enumTypeDef</code> equals null</li>
624 * <li>if enum values of <code>enumTypeDef</code> equal null</li>
625 * <li>if Q name of <code>enumTypeDef</code> equal null</li>
626 * <li>if name of <code>enumTypeDef</code> equal null</li>
629 private Enumeration provideTypeForEnum(final EnumTypeDefinition enumTypeDef, final String enumName,
630 final SchemaNode parentNode) {
631 Preconditions.checkArgument(enumTypeDef != null, "EnumTypeDefinition reference cannot be NULL!");
632 Preconditions.checkArgument(enumTypeDef.getValues() != null,
633 "EnumTypeDefinition MUST contain at least ONE value definition!");
634 Preconditions.checkArgument(enumTypeDef.getQName() != null, "EnumTypeDefinition MUST contain NON-NULL QName!");
635 Preconditions.checkArgument(enumTypeDef.getQName().getLocalName() != null,
636 "Local Name in EnumTypeDefinition QName cannot be NULL!");
638 final Module module = findParentModule(schemaContext, parentNode);
639 final AbstractEnumerationBuilder enumBuilder = newEnumerationBuilder(JavaTypeName.create(
640 BindingMapping.getRootPackageName(module.getQNameModule()), BindingMapping.getClassName(enumName)));
641 addEnumDescription(enumBuilder, enumTypeDef);
642 enumTypeDef.getReference().ifPresent(enumBuilder::setReference);
643 enumBuilder.setModuleName(module.getName());
644 enumBuilder.setSchemaPath(enumTypeDef.getPath());
645 enumBuilder.updateEnumPairsFromEnumTypeDef(enumTypeDef);
646 return enumBuilder.toInstance(null);
650 * Adds enumeration to <code>typeBuilder</code>. The enumeration data are taken from <code>enumTypeDef</code>.
652 * @param enumTypeDef enumeration type definition is source of enumeration data for <code>typeBuilder</code>
653 * @param enumName string with the name of enumeration
654 * @param typeBuilder generated type builder to which is enumeration added
655 * @return enumeration type which contains enumeration data form <code>enumTypeDef</code>
656 * @throws IllegalArgumentException
658 * <li>if <code>enumTypeDef</code> equals null</li>
659 * <li>if enum values of <code>enumTypeDef</code> equal null</li>
660 * <li>if Q name of <code>enumTypeDef</code> equal null</li>
661 * <li>if name of <code>enumTypeDef</code> equal null</li>
662 * <li>if name of <code>typeBuilder</code> equal null</li>
666 private Enumeration addInnerEnumerationToTypeBuilder(final EnumTypeDefinition enumTypeDef,
667 final String enumName, final GeneratedTypeBuilderBase<?> typeBuilder) {
668 Preconditions.checkArgument(enumTypeDef != null, "EnumTypeDefinition reference cannot be NULL!");
669 Preconditions.checkArgument(enumTypeDef.getValues() != null,
670 "EnumTypeDefinition MUST contain at least ONE value definition!");
671 Preconditions.checkArgument(enumTypeDef.getQName() != null, "EnumTypeDefinition MUST contain NON-NULL QName!");
672 Preconditions.checkArgument(enumTypeDef.getQName().getLocalName() != null,
673 "Local Name in EnumTypeDefinition QName cannot be NULL!");
674 Preconditions.checkArgument(typeBuilder != null, "Generated Type Builder reference cannot be NULL!");
676 final EnumBuilder enumBuilder = typeBuilder.addEnumeration(BindingMapping.getClassName(enumName));
678 addEnumDescription(enumBuilder, enumTypeDef);
679 enumBuilder.updateEnumPairsFromEnumTypeDef(enumTypeDef);
680 return enumBuilder.toInstance(enumBuilder);
683 public abstract void addEnumDescription(EnumBuilder enumBuilder, EnumTypeDefinition enumTypeDef);
685 public abstract AbstractEnumerationBuilder newEnumerationBuilder(JavaTypeName identifier);
687 public abstract GeneratedTOBuilder newGeneratedTOBuilder(JavaTypeName identifier);
689 public abstract GeneratedTypeBuilder newGeneratedTypeBuilder(JavaTypeName identifier);
692 * Converts the pattern constraints to the list of the strings which represents these constraints.
694 * @param patternConstraints list of pattern constraints
695 * @return list of strings which represents the constraint patterns
697 public abstract Map<String, String> resolveRegExpressions(List<PatternConstraint> patternConstraints);
699 abstract void addCodegenInformation(GeneratedTypeBuilderBase<?> genTOBuilder, TypeDefinition<?> typeDef);
702 * Converts the pattern constraints from <code>typedef</code> to the list of the strings which represents these
705 * @param typedef extended type in which are the pattern constraints sought
706 * @return list of strings which represents the constraint patterns
707 * @throws IllegalArgumentException if <code>typedef</code> equals null
710 private Map<String, String> resolveRegExpressionsFromTypedef(final TypeDefinition<?> typedef) {
711 if (!(typedef instanceof StringTypeDefinition)) {
712 return ImmutableMap.of();
715 // TODO: run diff against base ?
716 return resolveRegExpressions(((StringTypeDefinition) typedef).getPatternConstraints());
720 * Converts <code>dataNode</code> to JAVA <code>Type</code>.
722 * @param dataNode contains information about YANG type
723 * @return JAVA <code>Type</code> representation of <code>dataNode</code>
725 private Type resolveTypeFromDataSchemaNode(final SchemaNode dataNode, final boolean inGrouping) {
726 Type returnType = null;
727 if (dataNode != null) {
728 if (dataNode instanceof LeafSchemaNode) {
729 final LeafSchemaNode leaf = (LeafSchemaNode) dataNode;
730 final TypeDefinition<?> type = CompatUtils.compatType(leaf);
731 returnType = javaTypeForSchemaDefinitionType(type, leaf, inGrouping);
732 } else if (dataNode instanceof LeafListSchemaNode) {
733 final LeafListSchemaNode leafList = (LeafListSchemaNode) dataNode;
734 returnType = javaTypeForSchemaDefinitionType(leafList.getType(), leafList, inGrouping);
741 * Passes through all modules and through all its type definitions and convert it to generated types.
744 * The modules are first sorted by mutual dependencies. The modules are sequentially passed. All type definitions
745 * of a module are at the beginning sorted so that type definition with less amount of references to other type
746 * definition are processed first.<br>
747 * For each module is created mapping record in the map
748 * {@link AbstractTypeProvider#genTypeDefsContextMap genTypeDefsContextMap}
749 * which map current module name to the map which maps type names to returned types (generated types).
751 private void resolveTypeDefsFromContext() {
752 final Set<Module> modules = schemaContext.getModules();
753 Preconditions.checkArgument(modules != null, "Set of Modules cannot be NULL!");
754 final List<Module> modulesSortedByDependency = ModuleDependencySort.sort(modules);
756 for (Module module : modulesSortedByDependency) {
757 Map<Optional<Revision>, Map<String, Type>> dateTypeMap = genTypeDefsContextMap.computeIfAbsent(
758 module.getName(), key -> new HashMap<>());
759 dateTypeMap.put(module.getRevision(), Collections.emptyMap());
760 genTypeDefsContextMap.put(module.getName(), dateTypeMap);
763 for (Module module : modulesSortedByDependency) {
764 if (module != null) {
765 final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
766 if (basePackageName != null) {
767 final List<TypeDefinition<?>> typeDefinitions = TypedefResolver.getAllTypedefs(module);
768 for (TypeDefinition<?> typedef : sortTypeDefinitionAccordingDepth(typeDefinitions)) {
769 typedefToGeneratedType(basePackageName, module, typedef);
777 * Create Type for specified type definition.
779 * @param basePackageName string with name of package to which the module belongs
780 * @param module string with the name of the module for to which the <code>typedef</code> belongs
781 * @param typedef type definition of the node for which should be created JAVA <code>Type</code>
782 * (usually generated TO)
783 * @return JAVA <code>Type</code> representation of <code>typedef</code> or
784 * <code>null</code> value if <code>basePackageName</code> or
785 * <code>modulName</code> or <code>typedef</code> or Q name of
786 * <code>typedef</code> equals <code>null</code>
788 private Type typedefToGeneratedType(final String basePackageName, final Module module,
789 final TypeDefinition<?> typedef) {
790 final TypeDefinition<?> baseTypedef = typedef.getBaseType();
792 // See generatedTypeForExtendedDefinitionType() above for rationale behind this special case.
793 if (baseTypedef instanceof LeafrefTypeDefinition || baseTypedef instanceof IdentityrefTypeDefinition) {
797 final String typedefName = typedef.getQName().getLocalName();
799 final Type returnType;
800 if (baseTypedef.getBaseType() != null) {
801 returnType = provideGeneratedTOFromExtendedType(typedef, baseTypedef, basePackageName,
803 } else if (baseTypedef instanceof UnionTypeDefinition) {
804 final GeneratedTOBuilder genTOBuilder = provideGeneratedTOBuilderForUnionTypeDef(
805 JavaTypeName.create(basePackageName, BindingMapping.getClassName(typedef.getQName())),
806 (UnionTypeDefinition) baseTypedef, typedef);
807 genTOBuilder.setTypedef(true);
808 genTOBuilder.setIsUnion(true);
809 addUnitsToGenTO(genTOBuilder, typedef.getUnits().orElse(null));
810 makeSerializable(genTOBuilder);
811 returnType = genTOBuilder.build();
813 // Define a corresponding union builder. Typedefs are always anchored at a Java package root,
814 // so we are placing the builder alongside the union.
815 final GeneratedTOBuilder unionBuilder = newGeneratedTOBuilder(
816 JavaTypeName.create(genTOBuilder.getPackageName(), genTOBuilder.getName() + "Builder"));
817 unionBuilder.setIsUnionBuilder(true);
818 final MethodSignatureBuilder method = unionBuilder.addMethod("getDefaultInstance");
819 method.setReturnType(returnType);
820 method.addParameter(Types.STRING, "defaultValue");
821 method.setAccessModifier(AccessModifier.PUBLIC);
822 method.setStatic(true);
823 additionalTypes.computeIfAbsent(module, key -> new HashSet<>()).add(unionBuilder.build());
824 } else if (baseTypedef instanceof EnumTypeDefinition) {
825 // enums are automatically Serializable
826 final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) baseTypedef;
827 // TODO units for typedef enum
828 returnType = provideTypeForEnum(enumTypeDef, typedefName, typedef);
829 } else if (baseTypedef instanceof BitsTypeDefinition) {
830 final GeneratedTOBuilder genTOBuilder = provideGeneratedTOBuilderForBitsTypeDefinition(
831 JavaTypeName.create(basePackageName, BindingMapping.getClassName(typedef.getQName())),
832 (BitsTypeDefinition) baseTypedef, module.getName());
833 genTOBuilder.setTypedef(true);
834 addUnitsToGenTO(genTOBuilder, typedef.getUnits().orElse(null));
835 makeSerializable(genTOBuilder);
836 returnType = genTOBuilder.build();
838 final Type javaType = javaTypeForSchemaDefinitionType(baseTypedef, typedef);
839 returnType = wrapJavaTypeIntoTO(basePackageName, typedef, javaType, module.getName());
841 if (returnType != null) {
842 final Map<Optional<Revision>, Map<String, Type>> modulesByDate =
843 genTypeDefsContextMap.get(module.getName());
844 final Optional<Revision> moduleRevision = module.getRevision();
845 Map<String, Type> typeMap = modulesByDate.get(moduleRevision);
846 if (typeMap != null) {
847 if (typeMap.isEmpty()) {
848 typeMap = new HashMap<>(4);
849 modulesByDate.put(moduleRevision, typeMap);
851 typeMap.put(typedefName, returnType);
859 * Wraps base YANG type to generated TO.
861 * @param basePackageName string with name of package to which the module belongs
862 * @param typedef type definition which is converted to the TO
863 * @param javaType JAVA <code>Type</code> to which is <code>typedef</code> mapped
864 * @return generated transfer object which represent<code>javaType</code>
866 private GeneratedTransferObject wrapJavaTypeIntoTO(final String basePackageName, final TypeDefinition<?> typedef,
867 final Type javaType, final String moduleName) {
868 requireNonNull(javaType, "javaType cannot be null");
870 final GeneratedTOBuilder genTOBuilder = typedefToTransferObject(basePackageName, typedef, moduleName);
871 genTOBuilder.setRestrictions(BindingGeneratorUtil.getRestrictions(typedef));
872 final GeneratedPropertyBuilder genPropBuilder = genTOBuilder.addProperty("value");
873 genPropBuilder.setReturnType(javaType);
874 genTOBuilder.addEqualsIdentity(genPropBuilder);
875 genTOBuilder.addHashIdentity(genPropBuilder);
876 genTOBuilder.addToStringProperty(genPropBuilder);
877 genTOBuilder.addImplementsType(TYPE_OBJECT);
878 if (typedef.getStatus() == Status.DEPRECATED) {
879 genTOBuilder.addAnnotation("java.lang", "Deprecated");
881 if (javaType instanceof ConcreteType && "String".equals(javaType.getName()) && typedef.getBaseType() != null) {
882 addStringRegExAsConstant(genTOBuilder, resolveRegExpressionsFromTypedef(typedef));
884 addUnitsToGenTO(genTOBuilder, typedef.getUnits().orElse(null));
885 genTOBuilder.setTypedef(true);
886 makeSerializable(genTOBuilder);
887 return genTOBuilder.build();
891 * Converts output list of generated TO builders to one TO builder (first
892 * from list) which contains the remaining builders as its enclosing TO.
894 * @param typeName new type identifier
895 * @param typedef type definition which should be of type {@link UnionTypeDefinition}
896 * @return generated TO builder with the list of enclosed generated TO builders
898 public GeneratedTOBuilder provideGeneratedTOBuilderForUnionTypeDef(final JavaTypeName typeName,
899 final UnionTypeDefinition typedef, final TypeDefinition<?> parentNode) {
900 final List<GeneratedTOBuilder> builders = provideGeneratedTOBuildersForUnionTypeDef(typeName, typedef,
902 Preconditions.checkState(!builders.isEmpty(), "No GeneratedTOBuilder objects generated from union %s", typedef);
904 final GeneratedTOBuilder resultTOBuilder = builders.remove(0);
905 builders.forEach(resultTOBuilder::addEnclosingTransferObject);
906 return resultTOBuilder;
910 * Converts <code>typedef</code> to generated TO with <code>typeDefName</code>. Every union type from
911 * <code>typedef</code> is added to generated TO builder as property.
913 * @param typeName new type identifier
914 * @param typedef type definition which should be of type <code>UnionTypeDefinition</code>
915 * @return generated TO builder which represents <code>typedef</code>
916 * @throws NullPointerException
918 * <li>if <code>basePackageName</code> is null</li>
919 * <li>if <code>typedef</code> is null</li>
920 * <li>if Qname of <code>typedef</code> is null</li>
923 public List<GeneratedTOBuilder> provideGeneratedTOBuildersForUnionTypeDef(final JavaTypeName typeName,
924 final UnionTypeDefinition typedef, final SchemaNode parentNode) {
925 requireNonNull(typedef, "Type Definition cannot be NULL!");
926 requireNonNull(typedef.getQName(), "Type definition QName cannot be NULL!");
928 final List<GeneratedTOBuilder> generatedTOBuilders = new ArrayList<>();
929 final List<TypeDefinition<?>> unionTypes = typedef.getTypes();
930 final Module module = findParentModule(schemaContext, parentNode);
932 final GeneratedTOBuilder unionGenTOBuilder = newGeneratedTOBuilder(typeName);
933 unionGenTOBuilder.setIsUnion(true);
934 unionGenTOBuilder.setSchemaPath(typedef.getPath());
935 unionGenTOBuilder.setModuleName(module.getName());
936 unionGenTOBuilder.addImplementsType(TYPE_OBJECT);
937 addCodegenInformation(unionGenTOBuilder, typedef);
938 generatedTOBuilders.add(unionGenTOBuilder);
940 // Pattern string is the key, XSD regex is the value. The reason for this choice is that the pattern carries
941 // also negation information and hence guarantees uniqueness.
942 final Map<String, String> expressions = new HashMap<>();
943 for (TypeDefinition<?> unionType : unionTypes) {
944 final String unionTypeName = unionType.getQName().getLocalName();
946 // If we have a base type we should follow the type definition backwards, except for identityrefs, as those
947 // do not follow type encapsulation -- we use the general case for that.
948 if (unionType.getBaseType() != null && !(unionType instanceof IdentityrefTypeDefinition)) {
949 resolveExtendedSubtypeAsUnion(unionGenTOBuilder, unionType, expressions, parentNode);
950 } else if (unionType instanceof UnionTypeDefinition) {
951 generatedTOBuilders.addAll(resolveUnionSubtypeAsUnion(unionGenTOBuilder,
952 (UnionTypeDefinition) unionType, parentNode));
953 } else if (unionType instanceof EnumTypeDefinition) {
954 final Enumeration enumeration = addInnerEnumerationToTypeBuilder((EnumTypeDefinition) unionType,
955 unionTypeName, unionGenTOBuilder);
956 updateUnionTypeAsProperty(unionGenTOBuilder, enumeration, unionTypeName);
958 final Type javaType = javaTypeForSchemaDefinitionType(unionType, parentNode);
959 updateUnionTypeAsProperty(unionGenTOBuilder, javaType, unionTypeName);
962 addStringRegExAsConstant(unionGenTOBuilder, expressions);
964 storeGenTO(typedef, unionGenTOBuilder, parentNode);
966 return generatedTOBuilders;
970 * Wraps code which handles the case when union subtype is also of the type <code>UnionType</code>.
973 * In this case the new generated TO is created for union subtype (recursive call of method
974 * {@link #provideGeneratedTOBuildersForUnionTypeDef(String, UnionTypeDefinition, String, SchemaNode)}
975 * provideGeneratedTOBuilderForUnionTypeDef} and in parent TO builder <code>parentUnionGenTOBuilder</code> is
976 * created property which type is equal to new generated TO.
978 * @param parentUnionGenTOBuilder generated TO builder to which is the property with the child union subtype added
979 * @param basePackageName string with the name of the module package
980 * @param unionSubtype type definition which represents union subtype
981 * @return list of generated TO builders. The number of the builders can be bigger one due to recursive call of
982 * <code>provideGeneratedTOBuildersForUnionTypeDef</code> method.
984 private List<GeneratedTOBuilder> resolveUnionSubtypeAsUnion(final GeneratedTOBuilder parentUnionGenTOBuilder,
985 final UnionTypeDefinition unionSubtype, final SchemaNode parentNode) {
986 final JavaTypeName newTOBuilderName = parentUnionGenTOBuilder.getIdentifier().createSibling(
987 provideAvailableNameForGenTOBuilder(parentUnionGenTOBuilder.getName()));
988 final List<GeneratedTOBuilder> subUnionGenTOBUilders = provideGeneratedTOBuildersForUnionTypeDef(
989 newTOBuilderName, unionSubtype, parentNode);
991 final GeneratedPropertyBuilder propertyBuilder;
992 propertyBuilder = parentUnionGenTOBuilder.addProperty(BindingMapping.getPropertyName(
993 newTOBuilderName.simpleName()));
994 propertyBuilder.setReturnType(subUnionGenTOBUilders.get(0).build());
995 parentUnionGenTOBuilder.addEqualsIdentity(propertyBuilder);
996 parentUnionGenTOBuilder.addToStringProperty(propertyBuilder);
998 return subUnionGenTOBUilders;
1002 * Wraps code which handle case when union subtype is of the type <code>ExtendedType</code>. If TO for this type
1003 * already exists it is used for the creation of the property in <code>parentUnionGenTOBuilder</code>. Otherwise
1004 * the base type is used for the property creation.
1006 * @param parentUnionGenTOBuilder generated TO builder in which new property is created
1007 * @param unionSubtype type definition of the <code>ExtendedType</code> type which represents union subtype
1008 * @param expressions list of strings with the regular expressions
1009 * @param parentNode parent Schema Node for Extended Subtype
1011 private void resolveExtendedSubtypeAsUnion(final GeneratedTOBuilder parentUnionGenTOBuilder,
1012 final TypeDefinition<?> unionSubtype, final Map<String, String> expressions, final SchemaNode parentNode) {
1013 final String unionTypeName = unionSubtype.getQName().getLocalName();
1014 final Type genTO = findGenTO(unionTypeName, unionSubtype);
1015 if (genTO != null) {
1016 updateUnionTypeAsProperty(parentUnionGenTOBuilder, genTO, genTO.getName());
1020 final TypeDefinition<?> baseType = baseTypeDefForExtendedType(unionSubtype);
1021 if (unionTypeName.equals(baseType.getQName().getLocalName())) {
1022 final Type javaType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForSchemaDefinitionType(baseType,
1023 parentNode, BindingGeneratorUtil.getRestrictions(unionSubtype));
1024 if (javaType != null) {
1025 updateUnionTypeAsProperty(parentUnionGenTOBuilder, javaType, unionTypeName);
1027 } else if (baseType instanceof LeafrefTypeDefinition) {
1028 final Type javaType = javaTypeForSchemaDefinitionType(baseType, parentNode);
1029 boolean typeExist = false;
1030 for (GeneratedPropertyBuilder generatedPropertyBuilder : parentUnionGenTOBuilder.getProperties()) {
1031 final Type origType = ((GeneratedPropertyBuilderImpl) generatedPropertyBuilder).getReturnType();
1032 if (origType != null && javaType != null && javaType == origType) {
1037 if (!typeExist && javaType != null) {
1038 updateUnionTypeAsProperty(parentUnionGenTOBuilder, javaType,
1039 javaType.getName() + parentUnionGenTOBuilder.getName() + "Value");
1042 if (baseType instanceof StringTypeDefinition) {
1043 expressions.putAll(resolveRegExpressionsFromTypedef(unionSubtype));
1048 * Searches for generated TO for <code>searchedTypeDef</code> type definition
1049 * in {@link #genTypeDefsContextMap genTypeDefsContextMap}.
1051 * @param searchedTypeName string with name of <code>searchedTypeDef</code>
1052 * @return generated TO for <code>searchedTypeDef</code> or <code>null</code> it it doesn't exist
1054 private Type findGenTO(final String searchedTypeName, final SchemaNode parentNode) {
1055 final Module typeModule = findParentModule(schemaContext, parentNode);
1056 if (typeModule != null && typeModule.getName() != null) {
1057 final Map<Optional<Revision>, Map<String, Type>> modulesByDate = genTypeDefsContextMap.get(
1058 typeModule.getName());
1059 final Map<String, Type> genTOs = modulesByDate.get(typeModule.getRevision());
1060 if (genTOs != null) {
1061 return genTOs.get(searchedTypeName);
1068 * Stores generated TO created from <code>genTOBuilder</code> for <code>newTypeDef</code>
1069 * to {@link #genTypeDefsContextMap genTypeDefsContextMap} if the module for <code>newTypeDef</code> exists.
1071 * @param newTypeDef type definition for which is <code>genTOBuilder</code> created
1072 * @param genTOBuilder generated TO builder which is converted to generated TO and stored
1074 private void storeGenTO(final TypeDefinition<?> newTypeDef, final GeneratedTOBuilder genTOBuilder,
1075 final SchemaNode parentNode) {
1076 if (!(newTypeDef instanceof UnionTypeDefinition)) {
1077 final Module parentModule = findParentModule(schemaContext, parentNode);
1078 if (parentModule != null && parentModule.getName() != null) {
1079 final Map<Optional<Revision>, Map<String, Type>> modulesByDate = genTypeDefsContextMap.get(
1080 parentModule.getName());
1081 final Map<String, Type> genTOsMap = modulesByDate.get(parentModule.getRevision());
1082 genTOsMap.put(newTypeDef.getQName().getLocalName(), genTOBuilder.build());
1088 * Adds a new property with the name <code>propertyName</code> and with type <code>type</code>
1089 * to <code>unonGenTransObject</code>.
1091 * @param unionGenTransObject generated TO to which should be property added
1092 * @param type JAVA <code>type</code> of the property which should be added to <code>unionGentransObject</code>
1093 * @param propertyName string with name of property which should be added to <code>unionGentransObject</code>
1095 private static void updateUnionTypeAsProperty(final GeneratedTOBuilder unionGenTransObject, final Type type,
1096 final String propertyName) {
1097 if (unionGenTransObject != null && type != null && !unionGenTransObject.containsProperty(propertyName)) {
1098 final GeneratedPropertyBuilder propBuilder = unionGenTransObject
1099 .addProperty(BindingMapping.getPropertyName(propertyName));
1100 propBuilder.setReturnType(type);
1102 unionGenTransObject.addEqualsIdentity(propBuilder);
1103 unionGenTransObject.addHashIdentity(propBuilder);
1104 unionGenTransObject.addToStringProperty(propBuilder);
1109 * Converts <code>typedef</code> to the generated TO builder.
1111 * @param basePackageName string with name of package to which the module belongs
1112 * @param typedef type definition from which is the generated TO builder created
1113 * @return generated TO builder which contains data from <code>typedef</code> and <code>basePackageName</code>
1115 private GeneratedTOBuilder typedefToTransferObject(final String basePackageName,
1116 final TypeDefinition<?> typedef, final String moduleName) {
1117 JavaTypeName name = renames.get(typedef);
1119 name = JavaTypeName.create(
1120 BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, typedef.getPath()),
1121 BindingMapping.getClassName(typedef.getQName().getLocalName()));
1124 final GeneratedTOBuilder newType = newGeneratedTOBuilder(name);
1125 newType.setSchemaPath(typedef.getPath());
1126 newType.setModuleName(moduleName);
1127 addCodegenInformation(newType, typedef);
1132 * Converts <code>typeDef</code> which should be of the type <code>BitsTypeDefinition</code>
1133 * to <code>GeneratedTOBuilder</code>. All the bits of the typeDef are added to returning generated TO as
1136 * @param typeName new type identifier
1137 * @param typeDef type definition from which is the generated TO builder created
1138 * @return generated TO builder which represents <code>typeDef</code>
1139 * @throws IllegalArgumentException
1141 * <li>if <code>typeDef</code> equals null</li>
1142 * <li>if <code>basePackageName</code> equals null</li>
1145 public GeneratedTOBuilder provideGeneratedTOBuilderForBitsTypeDefinition(final JavaTypeName typeName,
1146 final BitsTypeDefinition typeDef, final String moduleName) {
1147 final GeneratedTOBuilder genTOBuilder = newGeneratedTOBuilder(typeName);
1148 genTOBuilder.setSchemaPath(typeDef.getPath());
1149 genTOBuilder.setModuleName(moduleName);
1150 genTOBuilder.setBaseType(typeDef);
1151 genTOBuilder.addImplementsType(TYPE_OBJECT);
1152 addCodegenInformation(genTOBuilder, typeDef);
1154 final List<Bit> bitList = typeDef.getBits();
1155 GeneratedPropertyBuilder genPropertyBuilder;
1156 for (Bit bit : bitList) {
1157 final String name = bit.getName();
1158 genPropertyBuilder = genTOBuilder.addProperty(BindingMapping.getPropertyName(name));
1159 genPropertyBuilder.setReadOnly(true);
1160 genPropertyBuilder.setReturnType(BaseYangTypes.BOOLEAN_TYPE);
1162 genTOBuilder.addEqualsIdentity(genPropertyBuilder);
1163 genTOBuilder.addHashIdentity(genPropertyBuilder);
1164 genTOBuilder.addToStringProperty(genPropertyBuilder);
1167 return genTOBuilder;
1171 * Adds to the <code>genTOBuilder</code> the constant which contains regular expressions from
1172 * the <code>regularExpressions</code>.
1174 * @param genTOBuilder generated TO builder to which are <code>regular expressions</code> added
1175 * @param expressions list of string which represent regular expressions
1177 private static void addStringRegExAsConstant(final GeneratedTOBuilder genTOBuilder,
1178 final Map<String, String> expressions) {
1179 if (!expressions.isEmpty()) {
1180 genTOBuilder.addConstant(Types.listTypeFor(BaseYangTypes.STRING_TYPE), TypeConstants.PATTERN_CONSTANT_NAME,
1181 ImmutableMap.copyOf(expressions));
1186 * Creates generated TO with data about inner extended type <code>innerExtendedType</code>, about the package name
1187 * <code>typedefName</code> and about the generated TO name <code>typedefName</code>.
1190 * It is assumed that <code>innerExtendedType</code> is already present in
1191 * {@link AbstractTypeProvider#genTypeDefsContextMap genTypeDefsContextMap} to be possible set it as extended type
1192 * for the returning generated TO.
1194 * @param typedef Type Definition
1195 * @param innerExtendedType extended type which is part of some other extended type
1196 * @param basePackageName string with the package name of the module
1197 * @param moduleName Module Name
1198 * @return generated TO which extends generated TO for <code>innerExtendedType</code>
1199 * @throws IllegalArgumentException
1201 * <li>if <code>extendedType</code> equals null</li>
1202 * <li>if <code>basePackageName</code> equals null</li>
1203 * <li>if <code>typedefName</code> equals null</li>
1206 private GeneratedTransferObject provideGeneratedTOFromExtendedType(final TypeDefinition<?> typedef,
1207 final TypeDefinition<?> innerExtendedType, final String basePackageName, final String moduleName) {
1208 Preconditions.checkArgument(innerExtendedType != null, "Extended type cannot be NULL!");
1209 Preconditions.checkArgument(basePackageName != null, "String with base package name cannot be NULL!");
1211 final GeneratedTOBuilder genTOBuilder = newGeneratedTOBuilder(JavaTypeName.create(basePackageName,
1212 BindingMapping.getClassName(typedef.getQName())));
1213 genTOBuilder.setSchemaPath(typedef.getPath());
1214 genTOBuilder.setModuleName(moduleName);
1215 genTOBuilder.setTypedef(true);
1216 addCodegenInformation(genTOBuilder, typedef);
1218 final Restrictions r = BindingGeneratorUtil.getRestrictions(typedef);
1219 genTOBuilder.setRestrictions(r);
1220 addStringRegExAsConstant(genTOBuilder, resolveRegExpressionsFromTypedef(typedef));
1222 if (typedef.getStatus() == Status.DEPRECATED) {
1223 genTOBuilder.addAnnotation("java.lang", "Deprecated");
1226 if (baseTypeDefForExtendedType(innerExtendedType) instanceof UnionTypeDefinition) {
1227 genTOBuilder.setIsUnion(true);
1230 Map<Optional<Revision>, Map<String, Type>> modulesByDate = null;
1231 Map<String, Type> typeMap = null;
1232 final Module parentModule = findParentModule(schemaContext, innerExtendedType);
1233 if (parentModule != null) {
1234 modulesByDate = genTypeDefsContextMap.get(parentModule.getName());
1235 typeMap = modulesByDate.get(parentModule.getRevision());
1238 if (typeMap != null) {
1239 final String innerTypeDef = innerExtendedType.getQName().getLocalName();
1240 final Type type = typeMap.get(innerTypeDef);
1241 if (type instanceof GeneratedTransferObject) {
1242 genTOBuilder.setExtendsType((GeneratedTransferObject) type);
1245 addUnitsToGenTO(genTOBuilder, typedef.getUnits().orElse(null));
1246 makeSerializable(genTOBuilder);
1248 return genTOBuilder.build();
1252 * Add {@link java.io.Serializable} to implemented interfaces of this TO. Also compute and add serialVersionUID
1255 * @param gto transfer object which needs to be made serializable
1257 private static void makeSerializable(final GeneratedTOBuilder gto) {
1258 gto.addImplementsType(Types.serializableType());
1259 final GeneratedPropertyBuilder prop = new GeneratedPropertyBuilderImpl("serialVersionUID");
1260 prop.setValue(Long.toString(BindingGeneratorUtil.computeDefaultSUID(gto)));
1265 * Finds out for each type definition how many immersion (depth) is necessary to get to the base type. Every type
1266 * definition is inserted to the map which key is depth and value is list of type definitions with equal depth.
1267 * In next step are lists from this map concatenated to one list in ascending order according to their depth. All
1268 * type definitions are in the list behind all type definitions on which depends.
1270 * @param unsortedTypeDefinitions list of type definitions which should be sorted by depth
1271 * @return list of type definitions sorted according their each other dependencies (type definitions which are
1272 * dependent on other type definitions are in list behind them).
1274 private static List<TypeDefinition<?>> sortTypeDefinitionAccordingDepth(
1275 final Collection<TypeDefinition<?>> unsortedTypeDefinitions) {
1276 final List<TypeDefinition<?>> sortedTypeDefinition = new ArrayList<>();
1278 final Map<Integer, List<TypeDefinition<?>>> typeDefinitionsDepths = new TreeMap<>();
1279 for (TypeDefinition<?> unsortedTypeDefinition : unsortedTypeDefinitions) {
1280 final Integer depth = getTypeDefinitionDepth(unsortedTypeDefinition);
1281 List<TypeDefinition<?>> typeDefinitionsConcreteDepth =
1282 typeDefinitionsDepths.computeIfAbsent(depth, k -> new ArrayList<>());
1283 typeDefinitionsConcreteDepth.add(unsortedTypeDefinition);
1286 // SortedMap guarantees order corresponding to keys in ascending order
1287 for (List<TypeDefinition<?>> v : typeDefinitionsDepths.values()) {
1288 sortedTypeDefinition.addAll(v);
1291 return sortedTypeDefinition;
1295 * Returns how many immersion is necessary to get from the type definition to the base type.
1297 * @param typeDefinition type definition for which is depth sought.
1298 * @return number of immersions which are necessary to get from the type definition to the base type
1300 private static int getTypeDefinitionDepth(final TypeDefinition<?> typeDefinition) {
1301 // FIXME: rewrite this in a non-recursive manner
1302 if (typeDefinition == null) {
1305 final TypeDefinition<?> baseType = typeDefinition.getBaseType();
1306 if (baseType == null) {
1311 if (baseType.getBaseType() != null) {
1312 depth = depth + getTypeDefinitionDepth(baseType);
1313 } else if (baseType instanceof UnionTypeDefinition) {
1314 final List<TypeDefinition<?>> childTypeDefinitions = ((UnionTypeDefinition) baseType).getTypes();
1315 int maxChildDepth = 0;
1317 for (TypeDefinition<?> childTypeDefinition : childTypeDefinitions) {
1318 childDepth = childDepth + getTypeDefinitionDepth(childTypeDefinition);
1319 if (childDepth > maxChildDepth) {
1320 maxChildDepth = childDepth;
1323 return maxChildDepth;
1329 * Returns string which contains the same value as <code>name</code> but integer suffix is incremented by one. If
1330 * <code>name</code> contains no number suffix, a new suffix initialized at 1 is added. A suffix is actually
1331 * composed of a '$' marker, which is safe, as no YANG identifier can contain '$', and a unsigned decimal integer.
1333 * @param name string with name of augmented node
1334 * @return string with the number suffix incremented by one (or 1 is added)
1336 private static String provideAvailableNameForGenTOBuilder(final String name) {
1337 final int dollar = name.indexOf('$');
1342 final int newSuffix = Integer.parseUnsignedInt(name.substring(dollar + 1)) + 1;
1343 Preconditions.checkState(newSuffix > 0, "Suffix counter overflow");
1344 return name.substring(0, dollar + 1) + newSuffix;
1347 public static void addUnitsToGenTO(final GeneratedTOBuilder to, final String units) {
1348 if (!Strings.isNullOrEmpty(units)) {
1349 to.addConstant(Types.STRING, "_UNITS", "\"" + units + "\"");
1350 final GeneratedPropertyBuilder prop = new GeneratedPropertyBuilderImpl("UNITS");
1351 prop.setReturnType(Types.STRING);
1352 to.addToStringProperty(prop);
1357 public String getTypeDefaultConstruction(final LeafSchemaNode node) {
1358 return getTypeDefaultConstruction(node, (String) node.getType().getDefaultValue().orElse(null));
1361 public String getTypeDefaultConstruction(final LeafSchemaNode node, final String defaultValue) {
1362 final TypeDefinition<?> type = CompatUtils.compatType(node);
1363 final QName typeQName = type.getQName();
1364 final TypeDefinition<?> base = baseTypeDefForExtendedType(type);
1365 requireNonNull(type, () -> "Cannot provide default construction for null type of " + node);
1366 requireNonNull(defaultValue, () -> "Cannot provide default construction for null default statement of "
1369 final StringBuilder sb = new StringBuilder();
1370 String result = null;
1371 if (base instanceof BinaryTypeDefinition) {
1372 result = binaryToDef(defaultValue);
1373 } else if (base instanceof BitsTypeDefinition) {
1376 final Module parent = getParentModule(node);
1377 final Iterator<QName> path = node.getPath().getPathFromRoot().iterator();
1379 if (!path.hasNext()) {
1380 parentName = BindingMapping.getClassName(parent.getName()) + "Data";
1381 final String basePackageName = BindingMapping.getRootPackageName(parent.getQNameModule());
1382 className = basePackageName + "." + parentName + "." + BindingMapping.getClassName(node.getQName());
1384 final String basePackageName = BindingMapping.getRootPackageName(parent.getQNameModule());
1385 final String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName,
1387 parentName = BindingMapping.getClassName(parent.getName());
1388 className = packageName + "." + parentName + "." + BindingMapping.getClassName(node.getQName());
1390 result = bitsToDef((BitsTypeDefinition) base, className, defaultValue, type.getBaseType() != null);
1391 } else if (base instanceof BooleanTypeDefinition) {
1392 result = typeToBooleanDef(defaultValue);
1393 } else if (base instanceof DecimalTypeDefinition) {
1394 result = typeToDef(BigDecimal.class, defaultValue);
1395 } else if (base instanceof EmptyTypeDefinition) {
1396 result = typeToBooleanDef(defaultValue);
1397 } else if (base instanceof EnumTypeDefinition) {
1398 final char[] defValArray = defaultValue.toCharArray();
1399 final char first = Character.toUpperCase(defaultValue.charAt(0));
1400 defValArray[0] = first;
1401 final String newDefVal = new String(defValArray);
1403 if (type.getBaseType() != null) {
1404 final Module m = getParentModule(type);
1405 final String basePackageName = BindingMapping.getRootPackageName(m.getQNameModule());
1406 final String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName,
1408 className = packageName + "." + BindingMapping.getClassName(typeQName);
1410 final Module parentModule = getParentModule(node);
1411 final String basePackageName = BindingMapping.getRootPackageName(parentModule.getQNameModule());
1412 final String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName,
1414 className = packageName + "." + BindingMapping.getClassName(node.getQName());
1416 result = className + "." + newDefVal;
1417 } else if (base instanceof IdentityrefTypeDefinition) {
1418 throw new UnsupportedOperationException("Cannot get default construction for identityref type");
1419 } else if (base instanceof InstanceIdentifierTypeDefinition) {
1420 throw new UnsupportedOperationException("Cannot get default construction for instance-identifier type");
1421 } else if (BaseTypes.isInt8(base)) {
1422 result = typeToValueOfDef(Byte.class, defaultValue);
1423 } else if (BaseTypes.isInt16(base)) {
1424 result = typeToValueOfDef(Short.class, defaultValue);
1425 } else if (BaseTypes.isInt32(base)) {
1426 result = typeToValueOfDef(Integer.class, defaultValue);
1427 } else if (BaseTypes.isInt64(base)) {
1428 result = typeToValueOfDef(Long.class, defaultValue);
1429 } else if (base instanceof LeafrefTypeDefinition) {
1430 result = leafrefToDef(node, (LeafrefTypeDefinition) base, defaultValue);
1431 } else if (base instanceof StringTypeDefinition) {
1432 result = "\"" + defaultValue + "\"";
1433 } else if (BaseTypes.isUint8(base)) {
1434 result = typeToValueOfDef(Short.class, defaultValue);
1435 } else if (BaseTypes.isUint16(base)) {
1436 result = typeToValueOfDef(Integer.class, defaultValue);
1437 } else if (BaseTypes.isUint32(base)) {
1438 result = typeToValueOfDef(Long.class, defaultValue);
1439 } else if (BaseTypes.isUint64(base)) {
1440 switch (defaultValue) {
1442 result = "java.math.BigInteger.ZERO";
1445 result = "java.math.BigInteger.ONE";
1448 result = "java.math.BigInteger.TEN";
1451 result = typeToDef(BigInteger.class, defaultValue);
1453 } else if (base instanceof UnionTypeDefinition) {
1454 result = unionToDef(node);
1460 if (type.getBaseType() != null && !(base instanceof LeafrefTypeDefinition)
1461 && !(base instanceof EnumTypeDefinition) && !(base instanceof UnionTypeDefinition)) {
1462 final Module m = getParentModule(type);
1463 final String basePackageName = BindingMapping.getRootPackageName(m.getQNameModule());
1464 final String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName,
1466 final String className = packageName + "." + BindingMapping.getClassName(typeQName);
1467 sb.insert(0, "new " + className + "(");
1468 sb.insert(sb.length(), ')');
1471 return sb.toString();
1474 private static String typeToDef(final Class<?> clazz, final String defaultValue) {
1475 return "new " + clazz.getName() + "(\"" + defaultValue + "\")";
1478 private static String typeToValueOfDef(final Class<?> clazz, final String defaultValue) {
1479 return clazz.getName() + ".valueOf(\"" + defaultValue + "\")";
1482 private static String typeToBooleanDef(final String defaultValue) {
1483 switch (defaultValue) {
1485 return "java.lang.Boolean.FALSE";
1487 return "java.lang.Boolean.TRUE";
1489 return typeToValueOfDef(Boolean.class, defaultValue);
1493 private static String binaryToDef(final String defaultValue) {
1494 final StringBuilder sb = new StringBuilder();
1495 final byte[] encoded = Base64.getDecoder().decode(defaultValue);
1496 sb.append("new byte[] {");
1497 for (int i = 0; i < encoded.length; i++) {
1498 sb.append(encoded[i]);
1499 if (i != encoded.length - 1) {
1504 return sb.toString();
1507 private static final Comparator<Bit> BIT_NAME_COMPARATOR = Comparator.comparing(Bit::getName);
1509 private static String bitsToDef(final BitsTypeDefinition type, final String className, final String defaultValue,
1510 final boolean isExt) {
1511 final List<Bit> bits = new ArrayList<>(type.getBits());
1512 bits.sort(BIT_NAME_COMPARATOR);
1513 final StringBuilder sb = new StringBuilder();
1516 sb.append(className);
1519 for (int i = 0; i < bits.size(); i++) {
1520 if (bits.get(i).getName().equals(defaultValue)) {
1525 if (i != bits.size() - 1) {
1532 return sb.toString();
1535 private Module getParentModule(final SchemaNode node) {
1536 final QName qname = node.getPath().getPathFromRoot().iterator().next();
1537 return schemaContext.findModule(qname.getModule()).orElse(null);
1540 private String leafrefToDef(final LeafSchemaNode parentNode, final LeafrefTypeDefinition leafrefType,
1541 final String defaultValue) {
1542 Preconditions.checkArgument(leafrefType != null, "Leafref Type Definition reference cannot be NULL!");
1543 Preconditions.checkArgument(leafrefType.getPathStatement() != null,
1544 "The Path Statement for Leafref Type Definition cannot be NULL!");
1546 final PathExpression xpath = leafrefType.getPathStatement();
1547 final String strXPath = xpath.getOriginalString();
1549 if (strXPath != null) {
1550 if (strXPath.indexOf('[') == -1) {
1551 final Module module = findParentModule(schemaContext, parentNode);
1552 if (module != null) {
1553 final SchemaNode dataNode;
1554 if (xpath.isAbsolute()) {
1555 dataNode = findDataTreeSchemaNode(schemaContext, module.getQNameModule(), xpath);
1557 dataNode = findDataSchemaNodeForRelativeXPath(schemaContext, module, parentNode, xpath);
1559 final String result = getTypeDefaultConstruction((LeafSchemaNode) dataNode, defaultValue);
1563 return "new java.lang.Object()";
1570 private String unionToDef(final LeafSchemaNode node) {
1571 final TypeDefinition<?> type = CompatUtils.compatType(node);
1575 if (type.getBaseType() != null) {
1576 final QName typeQName = type.getQName();
1577 Module module = null;
1578 final Set<Module> modules = schemaContext.findModules(typeQName.getNamespace());
1579 if (modules.size() > 1) {
1580 for (Module m : modules) {
1581 if (m.getRevision().equals(typeQName.getRevision())) {
1586 if (module == null) {
1587 final List<Module> modulesList = new ArrayList<>(modules);
1588 modulesList.sort((o1, o2) -> Revision.compare(o1.getRevision(), o2.getRevision()));
1589 module = modulesList.get(0);
1592 module = modules.iterator().next();
1595 final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
1596 className = basePackageName + "." + BindingMapping.getClassName(typeQName);
1598 final Iterator<QName> path = node.getPath().getPathFromRoot().iterator();
1599 final QName first = path.next();
1600 final Module parent = schemaContext.findModule(first.getModule()).orElse(null);
1601 final String basePackageName = BindingMapping.getRootPackageName(parent.getQNameModule());
1602 if (!path.hasNext()) {
1603 parentName = BindingMapping.getClassName(parent.getName()) + "Data";
1604 className = basePackageName + "." + parentName + "." + BindingMapping.getClassName(node.getQName());
1606 final String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName,
1608 className = packageName + "." + BindingMapping.getClassName(node.getQName());
1611 return union(className, (String) node.getType().getDefaultValue().orElse(null), node);
1614 private static String union(final String className, final String defaultValue, final LeafSchemaNode node) {
1615 final StringBuilder sb = new StringBuilder();
1617 sb.append(className);
1619 sb.append(defaultValue);
1620 sb.append("\".toCharArray())");
1621 return sb.toString();
1625 public String getConstructorPropertyName(final SchemaNode node) {
1626 return node instanceof TypeDefinition<?> ? "value" : "";
1630 public String getParamNameFromType(final TypeDefinition<?> type) {
1631 return BindingMapping.getPropertyName(type.getQName().getLocalName());