2 * Copyright (c) 2017 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.binding.javav2.generator.impl;
11 import static org.opendaylight.mdsal.binding.javav2.generator.util.BindingGeneratorUtil.encodeAngleBrackets;
12 import static org.opendaylight.mdsal.binding.javav2.generator.util.Types.BOOLEAN;
14 import com.google.common.annotations.Beta;
15 import com.google.common.annotations.VisibleForTesting;
16 import com.google.common.base.Splitter;
17 import com.google.common.base.Strings;
18 import com.google.common.collect.Iterables;
19 import java.util.List;
21 import java.util.regex.Pattern;
22 import org.opendaylight.mdsal.binding.javav2.generator.impl.txt.yangTemplateForModule;
23 import org.opendaylight.mdsal.binding.javav2.generator.impl.txt.yangTemplateForNode;
24 import org.opendaylight.mdsal.binding.javav2.generator.impl.util.YangTextTemplate;
25 import org.opendaylight.mdsal.binding.javav2.generator.util.Types;
26 import org.opendaylight.mdsal.binding.javav2.model.api.Constant;
27 import org.opendaylight.mdsal.binding.javav2.model.api.Type;
28 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTypeBuilder;
29 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTypeBuilderBase;
30 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.MethodSignatureBuilder;
31 import org.opendaylight.mdsal.binding.javav2.util.BindingMapping;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.Module;
36 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
37 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
38 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.Status;
41 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
42 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
45 * Auxiliary util class for {@link GenHelperUtil} class
48 final class AuxiliaryGenUtils {
50 private static final Splitter BSDOT_SPLITTER = Splitter.on("\\.");
51 private static final char NEW_LINE = '\n';
52 private static final Pattern UNICODE_CHAR_PATTERN = Pattern.compile("\\\\+u");
55 * Constant with the concrete name of identifier.
57 private static final String AUGMENT_IDENTIFIER_NAME = "augment-identifier";
60 * Constant with the concrete name of namespace.
62 private static final String YANG_EXT_NAMESPACE = "urn:opendaylight:yang:extension:yang-ext";
64 private AuxiliaryGenUtils() {
65 throw new UnsupportedOperationException("Util class");
68 static void annotateDeprecatedIfNecessary(final Status status, final GeneratedTypeBuilder builder) {
69 if (status == Status.DEPRECATED) {
70 builder.addAnnotation("", "Deprecated");
74 private static boolean hasBuilderClass(final SchemaNode schemaNode) {
75 if (schemaNode instanceof ContainerSchemaNode || schemaNode instanceof ListSchemaNode ||
76 schemaNode instanceof RpcDefinition || schemaNode instanceof NotificationDefinition) {
82 static Constant qNameConstant(final GeneratedTypeBuilderBase<?> toBuilder, final String constantName,
84 return toBuilder.addConstant(Types.typeForClass(QName.class), constantName, name);
88 * Created a method signature builder as part of
89 * <code>interfaceBuilder</code>.
91 * The method signature builder is created for the getter method of
92 * <code>schemaNodeName</code>. Also <code>comment</code> and
93 * <code>returnType</code> information are added to the builder.
95 * @param interfaceBuilder
96 * generated type builder for which the getter method should be
98 * @param schemaNodeName
99 * string with schema node name. The name will be the part of the
100 * getter method name.
102 * string with comment for the getter method
104 * type which represents the return type of the getter method
106 * status from yang file, for deprecated annotation
107 * @return method signature builder which represents the getter method of
108 * <code>interfaceBuilder</code>
110 static MethodSignatureBuilder constructGetter(final GeneratedTypeBuilder interfaceBuilder,
111 final String schemaNodeName, final String comment, final Type returnType, final Status status) {
113 final MethodSignatureBuilder getMethod = interfaceBuilder
114 .addMethod(getterMethodName(schemaNodeName, returnType));
115 if (status == Status.DEPRECATED) {
116 getMethod.addAnnotation("", "Deprecated");
118 getMethod.setComment(encodeAngleBrackets(comment));
119 getMethod.setReturnType(returnType);
124 * Creates the name of the getter method name from <code>localName</code>.
127 * string with the name of the getter method
130 * @return string with the name of the getter method for
131 * <code>methodName</code> in JAVA method format
133 private static String getterMethodName(final String localName, final Type returnType) {
134 final StringBuilder method = new StringBuilder();
135 if (BOOLEAN.equals(returnType)) {
138 method.append("get");
140 final String name = BindingMapping.toFirstUpper(BindingMapping.getPropertyName(localName));
142 return method.toString();
145 static String createDescription(final SchemaNode schemaNode, final String fullyQualifiedName,
146 final SchemaContext schemaContext, final boolean verboseClassComments) {
147 final StringBuilder sb = new StringBuilder();
148 final String nodeDescription = encodeAngleBrackets(schemaNode.getDescription());
149 final String formattedDescription = YangTextTemplate.formatToParagraph(nodeDescription, 0);
151 if (!Strings.isNullOrEmpty(formattedDescription)) {
152 sb.append(formattedDescription);
156 if (verboseClassComments) {
157 final Module module = SchemaContextUtil.findParentModule(schemaContext, schemaNode);
158 final StringBuilder linkToBuilderClass = new StringBuilder();
159 final String[] namespace = Iterables.toArray(BSDOT_SPLITTER.split(fullyQualifiedName), String.class);
160 final String className = namespace[namespace.length - 1];
162 if (hasBuilderClass(schemaNode)) {
163 linkToBuilderClass.append(className);
164 linkToBuilderClass.append("Builder");
168 sb.append("This class represents the following YANG schema fragment defined in module <b>");
169 sb.append(module.getName());
174 sb.append(encodeAngleBrackets(yangTemplateForNode.render(schemaNode).body()));
177 sb.append("The schema path to identify an instance is");
180 sb.append(YangTextTemplate.formatSchemaPath(module.getName(), schemaNode.getPath().getPathFromRoot()));
184 if (hasBuilderClass(schemaNode)) {
186 sb.append("<p>To create instances of this class use " + "{@link " + linkToBuilderClass + "}.");
189 sb.append(linkToBuilderClass);
191 if (schemaNode instanceof ListSchemaNode) {
192 final List<QName> keyDef = ((ListSchemaNode)schemaNode).getKeyDefinition();
193 if (keyDef != null && !keyDef.isEmpty()) {
195 sb.append(className);
203 return replaceAllIllegalChars(sb);
206 static String createDescription(final Module module, final boolean verboseClassComments) {
207 final StringBuilder sb = new StringBuilder();
208 final String moduleDescription = encodeAngleBrackets(module.getDescription());
209 final String formattedDescription = YangTextTemplate.formatToParagraph(moduleDescription, 0);
211 if (!Strings.isNullOrEmpty(formattedDescription)) {
212 sb.append(formattedDescription);
216 if (verboseClassComments) {
218 sb.append("This class represents the following YANG schema fragment defined in module <b>");
219 sb.append(module.getName());
224 sb.append(encodeAngleBrackets(yangTemplateForModule.render(module).body()));
228 return replaceAllIllegalChars(sb);
232 * Returns first unique name for the augment generated type builder. The
233 * generated type builder name for augment consists from name of augmented
234 * node and serial number of its augmentation.
237 * map of builders which were created in the package to which the
238 * augmentation belongs
240 * string with name of augmented node
241 * @return string with unique name for augmentation builder
243 static String augGenTypeName(final Map<String, GeneratedTypeBuilder> builders, final String genTypeName) {
245 if (builders != null) {
246 while (builders.containsKey(genTypeName + index)) {
250 return genTypeName + index;
254 * @param unknownSchemaNodes unknows schema nodes
255 * @return nodeParameter of UnknownSchemaNode
257 static String getAugmentIdentifier(final List<UnknownSchemaNode> unknownSchemaNodes) {
258 for (final UnknownSchemaNode unknownSchemaNode : unknownSchemaNodes) {
259 final QName nodeType = unknownSchemaNode.getNodeType();
260 if (AUGMENT_IDENTIFIER_NAME.equals(nodeType.getLocalName())
261 && YANG_EXT_NAMESPACE.equals(nodeType.getNamespace().toString())) {
262 return unknownSchemaNode.getNodeParameter();
269 public static String replaceAllIllegalChars(final StringBuilder stringBuilder){
270 final String ret = UNICODE_CHAR_PATTERN.matcher(stringBuilder).replaceAll("\\\\\\\\u");
271 return ret.isEmpty() ? "" : ret;