2 * Copyright (c) 2016 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
9 package org.opendaylight.mdsal.binding2.generator.util;
11 import com.google.common.annotations.Beta;
12 import com.google.common.base.CharMatcher;
13 import com.google.common.collect.ImmutableList;
14 import com.google.common.collect.Iterables;
15 import java.util.Comparator;
16 import java.util.Iterator;
17 import java.util.List;
18 import org.opendaylight.mdsal.binding2.model.api.Restrictions;
19 import org.opendaylight.mdsal.binding2.model.api.Type;
20 import org.opendaylight.mdsal.binding2.model.api.type.builder.TypeMemberBuilder;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
23 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
24 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
25 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
28 * Standard Util class that contains various method for converting
29 * input strings to valid JAVA language strings e.g. package names,
30 * class names, attribute names and/or valid JavaDoc comments.
33 public final class Binding2GeneratorUtil {
35 private Binding2GeneratorUtil() {
36 throw new UnsupportedOperationException("Utility class");
39 private static final CharMatcher GT_MATCHER = CharMatcher.is('>');
40 private static final CharMatcher LT_MATCHER = CharMatcher.is('<');
42 private static final Restrictions EMPTY_RESTRICTIONS = new Restrictions() {
44 public List<LengthConstraint> getLengthConstraints() {
45 return ImmutableList.of();
49 public List<PatternConstraint> getPatternConstraints() {
50 return ImmutableList.of();
54 public List<RangeConstraint> getRangeConstraints() {
55 return ImmutableList.of();
59 public boolean isEmpty() {
64 private static final Comparator<TypeMemberBuilder<?>> SUID_MEMBER_COMPARATOR =
65 (o1, o2) -> o1.getName().compareTo(o2.getName());
67 private static final Comparator<Type> SUID_NAME_COMPARATOR =
68 (o1, o2) -> o1.getFullyQualifiedName().compareTo(o2.getFullyQualifiedName());
71 * Creates package name from specified <code>basePackageName</code> (package
72 * name for module) and <code>schemaPath</code>.
74 * Resulting package name is concatenation of <code>basePackageName</code>
75 * and all local names of YANG nodes which are parents of some node for
76 * which <code>schemaPath</code> is specified.
78 * Based on type of node, there is also possible suffix added in order
79 * to prevent package name conflicts.
81 * @param basePackageName
82 * string with package name of the module, MUST be normalized,
83 * otherwise this method may return an invalid string.
85 * list of names of YANG nodes which are parents of some node +
87 * @return string with valid JAVA package name
88 * @throws NullPointerException if any of the arguments are null
90 public static String packageNameForGeneratedType(final String basePackageName, final SchemaPath schemaPath) {
91 final Iterable<QName> pathTowardsRoot = schemaPath.getPathTowardsRoot();
92 final Iterable<QName> pathFromRoot = schemaPath.getPathFromRoot();
93 final int size = Iterables.size(pathTowardsRoot) - 1;
95 return basePackageName;
98 return generateNormalizedPackageName(basePackageName, pathFromRoot, size);
102 * Creates package name from specified <code>basePackageName</code> (package
103 * name for module) and <code>schemaPath</code> which crosses an augmentation.
105 * Resulting package name is concatenation of <code>basePackageName</code>
106 * and all local names of YANG nodes which are parents of some node for
107 * which <code>schemaPath</code> is specified.
109 * Based on type of node, there is also possible suffix added in order
110 * to prevent package name conflicts.
112 * @param basePackageName
113 * string with package name of the module, MUST be normalized,
114 * otherwise this method may return an invalid string.
116 * list of names of YANG nodes which are parents of some node +
118 * @return string with valid JAVA package name
119 * @throws NullPointerException if any of the arguments are null
121 public static String packageNameForAugmentedGeneratedType(final String basePackageName, final SchemaPath schemaPath) {
122 final Iterable<QName> pathTowardsRoot = schemaPath.getPathTowardsRoot();
123 final Iterable<QName> pathFromRoot = schemaPath.getPathFromRoot();
124 final int size = Iterables.size(pathTowardsRoot);
126 return basePackageName;
129 return generateNormalizedPackageName(basePackageName, pathFromRoot, size);
133 private static String generateNormalizedPackageName(final String base, final Iterable<QName> path, final int size) {
134 final StringBuilder builder = new StringBuilder(base);
135 final Iterator<QName> iterator = path.iterator();
136 for (int i = 0; i < size; ++i) {
138 String nodeLocalName = iterator.next().getLocalName();
139 //FIXME: colon or dash in identifier?
140 builder.append(nodeLocalName);
142 return Binding2Mapping.normalizePackageName(builder.toString());
146 * Encodes angle brackets in yang statement description
147 * @param description description of a yang statement which is used to generate javadoc comments
148 * @return string with encoded angle brackets
150 public static String encodeAngleBrackets(String description) {
151 if (description != null) {
152 description = LT_MATCHER.replaceFrom(description, "<");
153 description = GT_MATCHER.replaceFrom(description, ">");
158 //TODO: further implementation of static util methods...