Bug 1459-1 - Re-organize mdsal-binding2-spec
[mdsal.git] / binding2 / mdsal-binding2-generator-util / src / main / java / org / opendaylight / mdsal / binding2 / generator / util / BindingGeneratorUtil.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.mdsal.binding2.generator.util;
10
11 import com.google.common.annotations.Beta;
12 import com.google.common.base.CharMatcher;
13 import com.google.common.collect.Iterables;
14 import java.util.Iterator;
15 import org.opendaylight.mdsal.binding2.util.BindingMapping;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
18
19 /**
20  * Standard Util class that contains various method for converting
21  * input strings to valid JAVA language strings e.g. package names,
22  * class names, attribute names and/or valid JavaDoc comments.
23  */
24 @Beta
25 public final class BindingGeneratorUtil {
26
27     private static final CharMatcher GT_MATCHER = CharMatcher.is('>');
28     private static final CharMatcher LT_MATCHER = CharMatcher.is('<');
29
30     private BindingGeneratorUtil() {
31         throw new UnsupportedOperationException("Utility class");
32     }
33
34     /**
35      * Creates package name from specified <code>basePackageName</code> (package
36      * name for module) and <code>schemaPath</code>.
37      *
38      * Resulting package name is concatenation of <code>basePackageName</code>
39      * and all local names of YANG nodes which are parents of some node for
40      * which <code>schemaPath</code> is specified.
41      *
42      * Based on type of node, there is also possible suffix added in order
43      * to prevent package name conflicts.
44      *
45      * @param basePackageName
46      *            string with package name of the module, MUST be normalized,
47      *            otherwise this method may return an invalid string.
48      * @param schemaPath
49      *            list of names of YANG nodes which are parents of some node +
50      *            name of this node
51      * @return string with valid JAVA package name
52      * @throws NullPointerException if any of the arguments are null
53      */
54     public static String packageNameForGeneratedType(final String basePackageName, final SchemaPath schemaPath) {
55         final Iterable<QName> pathTowardsRoot = schemaPath.getPathTowardsRoot();
56         final Iterable<QName> pathFromRoot = schemaPath.getPathFromRoot();
57         final int size = Iterables.size(pathTowardsRoot) - 1;
58         if (size <= 0) {
59             return basePackageName;
60         }
61
62         return generateNormalizedPackageName(basePackageName, pathFromRoot, size);
63     }
64
65     /**
66      * Creates package name from specified <code>basePackageName</code> (package
67      * name for module) and <code>schemaPath</code> which crosses an augmentation.
68      *
69      * Resulting package name is concatenation of <code>basePackageName</code>
70      * and all local names of YANG nodes which are parents of some node for
71      * which <code>schemaPath</code> is specified.
72      *
73      * Based on type of node, there is also possible suffix added in order
74      * to prevent package name conflicts.
75      *
76      * @param basePackageName
77      *            string with package name of the module, MUST be normalized,
78      *            otherwise this method may return an invalid string.
79      * @param schemaPath
80      *            list of names of YANG nodes which are parents of some node +
81      *            name of this node
82      * @return string with valid JAVA package name
83      * @throws NullPointerException if any of the arguments are null
84      */
85     public static String packageNameForAugmentedGeneratedType(final String basePackageName, final SchemaPath schemaPath) {
86         final Iterable<QName> pathTowardsRoot = schemaPath.getPathTowardsRoot();
87         final Iterable<QName> pathFromRoot = schemaPath.getPathFromRoot();
88         final int size = Iterables.size(pathTowardsRoot);
89         if (size == 0) {
90             return basePackageName;
91         }
92
93         return generateNormalizedPackageName(basePackageName, pathFromRoot, size);
94     }
95
96
97     private static String generateNormalizedPackageName(final String base, final Iterable<QName> path, final int size) {
98         final StringBuilder builder = new StringBuilder(base);
99         final Iterator<QName> iterator = path.iterator();
100         for (int i = 0; i < size; ++i) {
101             builder.append('.');
102             String nodeLocalName = iterator.next().getLocalName();
103             //FIXME: colon or dash in identifier?
104             builder.append(nodeLocalName);
105         }
106         return BindingMapping.normalizePackageName(builder.toString());
107     }
108
109     /**
110      * Encodes angle brackets in yang statement description
111      * @param description description of a yang statement which is used to generate javadoc comments
112      * @return string with encoded angle brackets
113      */
114     public static String encodeAngleBrackets(String description) {
115         String newDesc = description;
116         if (newDesc != null) {
117             newDesc = LT_MATCHER.replaceFrom(newDesc, "&lt;");
118             newDesc = GT_MATCHER.replaceFrom(newDesc, "&gt;");
119         }
120         return newDesc;
121     }
122
123     //TODO: further implementation of static util methods...
124 }