/* * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.mdsal.binding2.generator.util; import com.google.common.annotations.Beta; import com.google.common.base.CharMatcher; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; import java.util.Comparator; import java.util.Iterator; import java.util.List; import org.opendaylight.mdsal.binding2.model.api.Restrictions; import org.opendaylight.mdsal.binding2.model.api.Type; import org.opendaylight.mdsal.binding2.model.api.type.builder.TypeMemberBuilder; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.model.api.SchemaPath; import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint; import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint; import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint; /** * Standard Util class that contains various method for converting * input strings to valid JAVA language strings e.g. package names, * class names, attribute names and/or valid JavaDoc comments. */ @Beta public final class Binding2GeneratorUtil { private Binding2GeneratorUtil() { throw new UnsupportedOperationException("Utility class"); } private static final CharMatcher GT_MATCHER = CharMatcher.is('>'); private static final CharMatcher LT_MATCHER = CharMatcher.is('<'); private static final Restrictions EMPTY_RESTRICTIONS = new Restrictions() { @Override public List getLengthConstraints() { return ImmutableList.of(); } @Override public List getPatternConstraints() { return ImmutableList.of(); } @Override public List getRangeConstraints() { return ImmutableList.of(); } @Override public boolean isEmpty() { return true; } }; private static final Comparator> SUID_MEMBER_COMPARATOR = (o1, o2) -> o1.getName().compareTo(o2.getName()); private static final Comparator SUID_NAME_COMPARATOR = (o1, o2) -> o1.getFullyQualifiedName().compareTo(o2.getFullyQualifiedName()); /** * Creates package name from specified basePackageName (package * name for module) and schemaPath. * * Resulting package name is concatenation of basePackageName * and all local names of YANG nodes which are parents of some node for * which schemaPath is specified. * * Based on type of node, there is also possible suffix added in order * to prevent package name conflicts. * * @param basePackageName * string with package name of the module, MUST be normalized, * otherwise this method may return an invalid string. * @param schemaPath * list of names of YANG nodes which are parents of some node + * name of this node * @return string with valid JAVA package name * @throws NullPointerException if any of the arguments are null */ public static String packageNameForGeneratedType(final String basePackageName, final SchemaPath schemaPath) { final Iterable pathTowardsRoot = schemaPath.getPathTowardsRoot(); final Iterable pathFromRoot = schemaPath.getPathFromRoot(); final int size = Iterables.size(pathTowardsRoot) - 1; if (size <= 0) { return basePackageName; } return generateNormalizedPackageName(basePackageName, pathFromRoot, size); } /** * Creates package name from specified basePackageName (package * name for module) and schemaPath which crosses an augmentation. * * Resulting package name is concatenation of basePackageName * and all local names of YANG nodes which are parents of some node for * which schemaPath is specified. * * Based on type of node, there is also possible suffix added in order * to prevent package name conflicts. * * @param basePackageName * string with package name of the module, MUST be normalized, * otherwise this method may return an invalid string. * @param schemaPath * list of names of YANG nodes which are parents of some node + * name of this node * @return string with valid JAVA package name * @throws NullPointerException if any of the arguments are null */ public static String packageNameForAugmentedGeneratedType(final String basePackageName, final SchemaPath schemaPath) { final Iterable pathTowardsRoot = schemaPath.getPathTowardsRoot(); final Iterable pathFromRoot = schemaPath.getPathFromRoot(); final int size = Iterables.size(pathTowardsRoot); if (size == 0) { return basePackageName; } return generateNormalizedPackageName(basePackageName, pathFromRoot, size); } private static String generateNormalizedPackageName(final String base, final Iterable path, final int size) { final StringBuilder builder = new StringBuilder(base); final Iterator iterator = path.iterator(); for (int i = 0; i < size; ++i) { builder.append('.'); String nodeLocalName = iterator.next().getLocalName(); //FIXME: colon or dash in identifier? builder.append(nodeLocalName); } return Binding2Mapping.normalizePackageName(builder.toString()); } /** * Encodes angle brackets in yang statement description * @param description description of a yang statement which is used to generate javadoc comments * @return string with encoded angle brackets */ public static String encodeAngleBrackets(String description) { if (description != null) { description = LT_MATCHER.replaceFrom(description, "<"); description = GT_MATCHER.replaceFrom(description, ">"); } return description; } //TODO: further implementation of static util methods... }