Binding generator implementation & decomposition
[mdsal.git] / binding2 / mdsal-binding2-generator-impl / src / main / java / org / opendaylight / mdsal / binding / javav2 / generator / impl / RpcActionGenHelper.java
1 /*
2  * Copyright (c) 2017 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.binding.javav2.generator.impl;
10
11 import static com.google.common.base.Preconditions.checkState;
12 import static org.opendaylight.mdsal.binding.javav2.generator.impl.AuxiliaryGenUtils.annotateDeprecatedIfNecessary;
13 import static org.opendaylight.mdsal.binding.javav2.generator.impl.AuxiliaryGenUtils.checkModuleAndModuleName;
14 import static org.opendaylight.mdsal.binding.javav2.generator.impl.AuxiliaryGenUtils.createDescription;
15 import static org.opendaylight.mdsal.binding.javav2.generator.impl.AuxiliaryGenUtils.resolveListKeyTOBuilder;
16 import static org.opendaylight.mdsal.binding.javav2.generator.impl.GenHelperUtil.addImplementedInterfaceFromUses;
17 import static org.opendaylight.mdsal.binding.javav2.generator.impl.GenHelperUtil.addRawInterfaceDefinition;
18 import static org.opendaylight.mdsal.binding.javav2.generator.impl.GenHelperUtil.moduleTypeBuilder;
19 import static org.opendaylight.mdsal.binding.javav2.generator.util.BindingGeneratorUtil.encodeAngleBrackets;
20 import static org.opendaylight.mdsal.binding.javav2.generator.util.BindingTypes.ACTION;
21 import static org.opendaylight.mdsal.binding.javav2.generator.util.BindingTypes.INPUT;
22 import static org.opendaylight.mdsal.binding.javav2.generator.util.BindingTypes.INSTANCE_IDENTIFIER;
23 import static org.opendaylight.mdsal.binding.javav2.generator.util.BindingTypes.INSTANTIABLE;
24 import static org.opendaylight.mdsal.binding.javav2.generator.util.BindingTypes.KEYED_INSTANCE_IDENTIFIER;
25 import static org.opendaylight.mdsal.binding.javav2.generator.util.BindingTypes.LIST_ACTION;
26 import static org.opendaylight.mdsal.binding.javav2.generator.util.BindingTypes.OUTPUT;
27 import static org.opendaylight.mdsal.binding.javav2.generator.util.BindingTypes.RPC;
28 import static org.opendaylight.mdsal.binding.javav2.generator.util.BindingTypes.RPC_CALLBACK;
29 import static org.opendaylight.mdsal.binding.javav2.generator.util.BindingTypes.TREE_NODE;
30 import static org.opendaylight.mdsal.binding.javav2.generator.util.BindingTypes.augmentable;
31 import static org.opendaylight.mdsal.binding.javav2.generator.util.Types.CLASS;
32 import static org.opendaylight.mdsal.binding.javav2.generator.util.Types.VOID;
33 import static org.opendaylight.mdsal.binding.javav2.generator.util.Types.parameterizedTypeFor;
34
35 import com.google.common.annotations.Beta;
36 import com.google.common.base.Optional;
37 import java.util.Collection;
38 import java.util.Map;
39 import java.util.Set;
40 import org.opendaylight.mdsal.binding.javav2.generator.spi.TypeProvider;
41 import org.opendaylight.mdsal.binding.javav2.generator.util.BindingGeneratorUtil;
42 import org.opendaylight.mdsal.binding.javav2.generator.util.BindingTypes;
43 import org.opendaylight.mdsal.binding.javav2.model.api.GeneratedType;
44 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTOBuilder;
45 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTypeBuilder;
46 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.MethodSignatureBuilder;
47 import org.opendaylight.mdsal.binding.javav2.spec.runtime.BindingNamespaceType;
48 import org.opendaylight.yangtools.yang.common.QName;
49 import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
50 import org.opendaylight.yangtools.yang.model.api.ActionNodeContainer;
51 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
52 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
53 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
54 import org.opendaylight.yangtools.yang.model.api.Module;
55 import org.opendaylight.yangtools.yang.model.api.OperationDefinition;
56 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
57 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
58 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
59
60 /**
61  *
62  * Util class used for generation of types for RPCs, routedRPCs and Actions (YANG 1.1 only)
63  * in Binding spec. v2. In case of routed RPC detected in input YANG, RPC is turned to Action.
64  *
65  */
66 @Beta
67 public final class RpcActionGenHelper {
68
69     private static final QName CONTEXT_REFERENCE =
70             QName.create("urn:opendaylight:yang:extension:yang-ext", "2013-07-09", "context-reference").intern();
71
72     private RpcActionGenHelper() {
73         throw new UnsupportedOperationException("Util class");
74     }
75
76     /**
77      * Let's find out what context we are talking about
78      * 1. routed RPC
79      * 2. global RPC
80      *
81      * In 1st case, we need Binding Generator behave like YANG 1.1 Action
82      *
83      * @param schemaNode RPC input node
84      * @return presence optional
85      */
86     private static Optional<QName> getRoutingContext(final DataSchemaNode schemaNode) {
87         for (UnknownSchemaNode extension : schemaNode.getUnknownSchemaNodes()) {
88             if (CONTEXT_REFERENCE.equals(extension.getNodeType())) {
89                 return Optional.fromNullable(extension.getQName());
90             }
91         }
92         return Optional.absent();
93     }
94
95     /**
96      * Converts Yang 1.1 <b>Actions</b> to list of <code>Type</code> objects.
97      * @param module  module from which is obtained set of all Action objects to
98      *            iterate over them
99      * @param genCtx input, generated context
100      * @param verboseClassComments verbosity switch
101      * @return generated context
102      */
103     static Map<Module, ModuleContext> actionMethodsToGenType(final Module module, Map<Module, ModuleContext> genCtx,
104             final SchemaContext schemaContext, final boolean verboseClassComments, Map<String, Map<String,
105             GeneratedTypeBuilder>> genTypeBuilders, TypeProvider typeProvider) {
106
107         checkModuleAndModuleName(module);
108         final Collection<DataSchemaNode> potentials = module.getChildNodes();
109         for (DataSchemaNode potential : potentials) {
110             if (potential instanceof ActionNodeContainer) {
111                 final Set<ActionDefinition> actions = ((ActionNodeContainer) potential).getActions();
112                 for (ActionDefinition action: actions) {
113                     genCtx.get(module).addChildNodeType(potential, resolveOperation(potential, action, module,
114                             schemaContext, verboseClassComments, genTypeBuilders, genCtx, typeProvider, true));
115                 }
116             }
117         }
118         return genCtx;
119     }
120
121     /**
122      * Converts global <b>RPCs</b> inputs and outputs sub-statements of the module
123      * to the list of <code>Type</code> objects. In addition, containers
124      * and lists which belong to input or output are also part of returning list.
125      * Detected routed RPCs are turned to Yang 1.1 Actions
126      *
127      * @param module
128      *            module from which is obtained set of all RPC objects to
129      *            iterate over them
130      * @param genCtx input, generated context
131      * @param verboseClassComments verbosity switch
132      *
133      * @throws IllegalArgumentException
134      *             <ul>
135      *             <li>if the module is null</li>
136      *             <li>if the name of module is null</li>
137      *             </ul>
138      * @throws IllegalStateException
139      *             if set of RPCs from module is null
140      *
141      * @return generated context
142      */
143      static Map<Module, ModuleContext> rpcMethodsToGenType(final Module module, Map<Module, ModuleContext> genCtx,
144             final SchemaContext schemaContext, final boolean verboseClassComments, Map<String, Map<String,
145              GeneratedTypeBuilder>> genTypeBuilders, TypeProvider typeProvider) {
146
147         checkModuleAndModuleName(module);
148         final Set<RpcDefinition> rpcDefinitions = module.getRpcs();
149         checkState(rpcDefinitions != null, "Set of RPCs from module " + module.getName() + " cannot be NULL.");
150         if (rpcDefinitions.isEmpty()) {
151             return genCtx;
152         }
153
154         for (final RpcDefinition rpc : rpcDefinitions) {
155             //FIXME: get correct parent for routed RPCs only
156             DataSchemaNode parent = null;
157
158             ContainerSchemaNode input = rpc.getInput();
159             boolean isAction = false;
160             if (input != null) {
161                 for (DataSchemaNode schemaNode : input.getChildNodes()) {
162                     if (getRoutingContext(schemaNode).isPresent()) {
163                         isAction = true;
164                         break;
165                     }
166                 }
167             }
168
169             //routedRPC?
170             if (isAction) {
171                 genCtx.get(module).addChildNodeType(parent, resolveOperation(parent, rpc, module, schemaContext,
172                         verboseClassComments, genTypeBuilders, genCtx, typeProvider, true));
173             } else {
174                 //global RPC only
175                 genCtx.get(module).addTopLevelNodeType(resolveOperation(parent, rpc, module, schemaContext,
176                         verboseClassComments, genTypeBuilders, genCtx, typeProvider, false));
177
178             }
179         }
180         return genCtx;
181     }
182
183     /**
184      * Converts RPC, Action or routed RPC into generated type
185      * @return generated type
186      */
187     private static GeneratedTypeBuilder resolveOperation(final DataSchemaNode parent, final OperationDefinition operation,
188             final Module module, final SchemaContext schemaContext, final boolean verboseClassComments,
189             Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final Map<Module, ModuleContext> genCtx,
190             TypeProvider typeProvider, final boolean isAction) {
191
192         //operation name
193         final String operationName = operation.getQName().getLocalName();
194         //concrete operation name
195         final StringBuilder sb = new StringBuilder(operationName).append('_');
196         if (isAction) {
197             sb.append("Action");
198         } else {
199             sb.append("Rpc");
200         }
201         final GeneratedTypeBuilder interfaceBuilder = moduleTypeBuilder(module, sb.toString(),
202                 verboseClassComments);
203
204         final String basePackageName = interfaceBuilder.getPackageName();
205
206         interfaceBuilder.setDescription(createDescription(operation, interfaceBuilder.getFullyQualifiedName(),
207                 schemaContext, verboseClassComments));
208         final String operationComment = encodeAngleBrackets(operation.getDescription());
209         final MethodSignatureBuilder operationMethod = interfaceBuilder.addMethod("invoke");
210
211         //input
212         final ContainerSchemaNode input = operation.getInput();
213         final GeneratedTypeBuilder inType = resolveOperationNode(interfaceBuilder, module, operation.getInput(),
214                 basePackageName, schemaContext, operationName, verboseClassComments, typeProvider, genTypeBuilders, genCtx, true);
215         annotateDeprecatedIfNecessary(operation.getStatus(), inType);
216         inType.setParentTypeForBuilder(interfaceBuilder);
217         genCtx.get(module).addChildNodeType(input, inType);
218
219         //output
220         final ContainerSchemaNode output = operation.getOutput();
221         final GeneratedTypeBuilder outType = resolveOperationNode(interfaceBuilder, module, operation.getOutput(),
222                 basePackageName, schemaContext, operationName, verboseClassComments, typeProvider, genTypeBuilders, genCtx, false);
223         annotateDeprecatedIfNecessary(operation.getStatus(), outType);
224         outType.setParentTypeForBuilder(interfaceBuilder);
225         genCtx.get(module).addChildNodeType(output, outType);
226
227         final GeneratedType inTypeInstance = inType.toInstance();
228         operationMethod.addParameter(inTypeInstance, "input");
229
230         if (isAction) {
231             //action, routed RPC
232             String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, parent.getPath(),
233                     BindingNamespaceType.Data);
234             GeneratedTypeBuilder parentType = addRawInterfaceDefinition(packageName, parent, schemaContext,
235                     parent.getQName().getLocalName(), verboseClassComments, genTypeBuilders);
236             parentType.addImplementsType(TREE_NODE);
237             parentType.addImplementsType(augmentable(parentType));
238             annotateDeprecatedIfNecessary(parent.getStatus(), parentType);
239
240             operationMethod.addParameter(parameterizedTypeFor(INSTANCE_IDENTIFIER, parentType), "ii");
241
242             if (parent instanceof ListSchemaNode) {
243                 //ListAction
244                 final GeneratedTOBuilder keyType = resolveListKeyTOBuilder(basePackageName, (ListSchemaNode) parent);
245                 operationMethod.addParameter(
246                         parameterizedTypeFor(KEYED_INSTANCE_IDENTIFIER, parentType, keyType), "kii");
247                 operationMethod.setReturnType(keyType);
248                 interfaceBuilder.addImplementsType(parameterizedTypeFor(LIST_ACTION, parentType, inType, outType));
249             } else {
250                 //Action
251                 GenHelperUtil.resolveDataSchemaNodes(module, basePackageName, parentType, parentType,
252                         ((ContainerSchemaNode) parent).getChildNodes(), genCtx, schemaContext, verboseClassComments,
253                         genTypeBuilders, typeProvider);
254                 operationMethod.addParameter(parameterizedTypeFor(INSTANCE_IDENTIFIER, parentType), "ii");
255                 interfaceBuilder.addImplementsType(parameterizedTypeFor(ACTION, parentType, inType, outType));
256             }
257         } else {
258             //RPC
259             interfaceBuilder.addImplementsType(parameterizedTypeFor(RPC, inType, outType));
260             interfaceBuilder.addImplementsType(TREE_NODE);
261         }
262
263         operationMethod.addParameter(parameterizedTypeFor(RPC_CALLBACK, outType), "callback");
264
265         operationMethod.setComment(operationComment);
266         operationMethod.setReturnType(VOID);
267
268         return interfaceBuilder;
269     }
270
271     private static GeneratedTypeBuilder resolveOperationNode(GeneratedTypeBuilder parent, final Module module, final
272             ContainerSchemaNode operationNode, final String basePackageName, final SchemaContext schemaContext, final String
273             operationName, final boolean verboseClassComments, TypeProvider typeProvider, Map<String, Map<String,
274             GeneratedTypeBuilder>> genTypeBuilders, final Map<Module, ModuleContext> genCtx, final boolean isInput) {
275
276         final GeneratedTypeBuilder nodeType = addRawInterfaceDefinition(basePackageName, operationNode, schemaContext,
277                 operationName, verboseClassComments, genTypeBuilders);
278         addImplementedInterfaceFromUses(operationNode, nodeType, genCtx);
279         nodeType.addImplementsType(parameterizedTypeFor(BindingTypes.TREE_CHILD_NODE, parent, parameterizedTypeFor
280                 (BindingTypes.ITEM, parent)));
281         if (isInput) {
282             nodeType.addImplementsType(parameterizedTypeFor(INPUT, nodeType));
283         } else {
284             nodeType.addImplementsType(parameterizedTypeFor(OUTPUT, nodeType));
285         }
286         nodeType.addImplementsType(parameterizedTypeFor(INSTANTIABLE, nodeType));
287         nodeType.addImplementsType(augmentable(nodeType));
288         GenHelperUtil.resolveDataSchemaNodes(module, basePackageName, nodeType, nodeType, operationNode.getChildNodes(), genCtx,
289                 schemaContext, verboseClassComments, genTypeBuilders, typeProvider);
290
291         final MethodSignatureBuilder nodeMethod = nodeType.addMethod("implementedInterface");
292         nodeMethod.setReturnType(parameterizedTypeFor(CLASS, nodeType));
293         nodeMethod.addAnnotation("", "Override");
294
295         return nodeType;
296     }
297 }