Improve BindingCodecContext.getCodecContextNode()
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / SchemaRootCodecContext.java
index 25745c3ed49f795e0ad332442f09db1fc405b570..b93ec3f2ad581ed79c01f92d65cb004949b6942b 100644 (file)
@@ -35,7 +35,6 @@ import org.opendaylight.mdsal.binding.runtime.api.ContainerLikeRuntimeType;
 import org.opendaylight.mdsal.binding.runtime.api.DataRuntimeType;
 import org.opendaylight.mdsal.binding.runtime.api.NotificationRuntimeType;
 import org.opendaylight.mdsal.binding.runtime.api.RuntimeType;
-import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
 import org.opendaylight.yangtools.util.ClassLoaderUtils;
 import org.opendaylight.yangtools.yang.binding.Action;
@@ -47,6 +46,7 @@ import org.opendaylight.yangtools.yang.binding.KeyedListAction;
 import org.opendaylight.yangtools.yang.binding.Notification;
 import org.opendaylight.yangtools.yang.binding.RpcInput;
 import org.opendaylight.yangtools.yang.binding.RpcOutput;
+import org.opendaylight.yangtools.yang.binding.contract.Naming;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
@@ -121,7 +121,7 @@ final class SchemaRootCodecContext<D extends DataObject> extends DataContainerCo
                 final QNameModule qnameModule = qname.getModule();
                 final Module module = context.getEffectiveModelContext().findModule(qnameModule)
                     .orElseThrow(() -> new IllegalArgumentException("Failed to find module for " + qnameModule));
-                final String className = BindingMapping.getClassName(qname);
+                final String className = Naming.getClassName(qname);
 
                 for (final RpcDefinition potential : module.getRpcs()) {
                     final QName potentialQName = potential.getQName();
@@ -132,7 +132,7 @@ final class SchemaRootCodecContext<D extends DataObject> extends DataContainerCo
                      *
                      * FIXME: Rework this to have more precise logic regarding Binding Specification.
                      */
-                    if (key.getSimpleName().equals(BindingMapping.getClassName(potentialQName) + className)) {
+                    if (key.getSimpleName().equals(Naming.getClassName(potentialQName) + className)) {
                         final ContainerLike schema = getRpcDataSchema(potential, qname);
                         checkArgument(schema != null, "Schema for %s does not define input / output.", potentialQName);
 
@@ -198,19 +198,8 @@ final class SchemaRootCodecContext<D extends DataObject> extends DataContainerCo
             }
         });
 
-    private SchemaRootCodecContext(final DataContainerCodecPrototype<BindingRuntimeTypes> dataPrototype) {
-        super(dataPrototype);
-    }
-
-    /**
-     * Creates RootNode from supplied CodecContextFactory.
-     *
-     * @param factory
-     *            CodecContextFactory
-     * @return A new root node
-     */
-    static SchemaRootCodecContext<?> create(final CodecContextFactory factory) {
-        return new SchemaRootCodecContext<>(DataContainerCodecPrototype.rootPrototype(factory));
+    SchemaRootCodecContext(final CodecContextFactory factory) {
+        super(DataContainerCodecPrototype.rootPrototype(factory));
     }
 
     @Override
@@ -221,8 +210,8 @@ final class SchemaRootCodecContext<D extends DataObject> extends DataContainerCo
     @Override
     @SuppressWarnings("unchecked")
     public <C extends DataObject> DataContainerCodecContext<C, ?> streamChild(final Class<C> childClass) {
-        final DataContainerCodecContext<?, ?> result = Notification.class.isAssignableFrom(childClass)
-            ? getNotificationImpl(childClass) : getOrRethrow(childrenByClass, childClass);
+        final var result = Notification.class.isAssignableFrom(childClass) ? getNotificationImpl(childClass)
+            : getOrRethrow(childrenByClass, childClass);
         return (DataContainerCodecContext<C, ?>) result;
     }
 
@@ -289,7 +278,7 @@ final class SchemaRootCodecContext<D extends DataObject> extends DataContainerCo
         final Optional<ParameterizedType> optParamType = ClassLoaderUtils.findParameterizedType(action, actionType);
         checkState(optParamType.isPresent(), "%s does not specialize %s", action, actionType);
 
-        final ParameterizedType paramType = optParamType.get();
+        final ParameterizedType paramType = optParamType.orElseThrow();
         final Type[] args = paramType.getActualTypeArguments();
         checkArgument(args.length == expectedArgsLength, "Unexpected (%s) Action generatic arguments", args.length);
         final ActionRuntimeType schema = factory().getRuntimeContext().getActionDefinition(action);
@@ -324,13 +313,17 @@ final class SchemaRootCodecContext<D extends DataObject> extends DataContainerCo
     }
 
     ChoiceNodeCodecContext<?> createChoiceDataContext(final Class<? extends DataObject> caseType) {
-        final Class<?> choiceClass = findCaseChoice(caseType);
-        checkArgument(choiceClass != null, "Class %s is not a valid case representation", caseType);
-        final CompositeRuntimeType schema = factory().getRuntimeContext().getSchemaDefinition(choiceClass);
-        checkArgument(schema instanceof ChoiceRuntimeType, "Class %s does not refer to a choice", caseType);
+        final var choiceClass = findCaseChoice(caseType);
+        if (choiceClass == null) {
+            throw new IllegalArgumentException(caseType + " is not a valid case representation");
+        }
+
+        final var runtimeType = factory().getRuntimeContext().getSchemaDefinition(choiceClass);
+        if (!(runtimeType instanceof ChoiceRuntimeType choiceType)) {
+            throw new IllegalArgumentException(caseType + " does not refer to a choice");
+        }
 
-        final DataContainerCodecContext<?, ChoiceRuntimeType> choice = DataContainerCodecPrototype.from(choiceClass,
-            (ChoiceRuntimeType)schema, factory()).get();
+        final var choice = DataContainerCodecPrototype.from(choiceClass, choiceType, factory()).get();
         verify(choice instanceof ChoiceNodeCodecContext);
         return (ChoiceNodeCodecContext<?>) choice;
     }
@@ -355,12 +348,12 @@ final class SchemaRootCodecContext<D extends DataObject> extends DataContainerCo
     @Override
     public DataContainerCodecContext<?, ?> bindingPathArgumentChild(final InstanceIdentifier.PathArgument arg,
             final List<PathArgument> builder) {
-        final Optional<? extends Class<? extends DataObject>> caseType = arg.getCaseType();
+        final var caseType = arg.getCaseType();
         if (caseType.isPresent()) {
             final @NonNull Class<? extends DataObject> type = caseType.orElseThrow();
-            final ChoiceNodeCodecContext<?> choice = choicesByClass.getUnchecked(type);
+            final var choice = choicesByClass.getUnchecked(type);
             choice.addYangPathArgument(arg, builder);
-            final DataContainerCodecContext<?, ?> caze = choice.streamChild(type);
+            final var caze = choice.streamChild(type);
             caze.addYangPathArgument(arg, builder);
             return caze.bindingPathArgumentChild(arg, builder);
         }
@@ -369,11 +362,9 @@ final class SchemaRootCodecContext<D extends DataObject> extends DataContainerCo
     }
 
     private static Class<?> findCaseChoice(final Class<? extends DataObject> caseClass) {
-        for (Type type : caseClass.getGenericInterfaces()) {
-            if (type instanceof Class<?> typeClass) {
-                if (ChoiceIn.class.isAssignableFrom(typeClass)) {
-                    return typeClass.asSubclass(ChoiceIn.class);
-                }
+        for (var type : caseClass.getGenericInterfaces()) {
+            if (type instanceof Class<?> typeClass && ChoiceIn.class.isAssignableFrom(typeClass)) {
+                return typeClass.asSubclass(ChoiceIn.class);
             }
         }
         return null;
@@ -382,8 +373,8 @@ final class SchemaRootCodecContext<D extends DataObject> extends DataContainerCo
     private static <K,V> V getOrRethrow(final LoadingCache<K, V> cache, final K key) {
         try {
             return cache.getUnchecked(key);
-        } catch (final UncheckedExecutionException e) {
-            final Throwable cause = e.getCause();
+        } catch (UncheckedExecutionException e) {
+            final var cause = e.getCause();
             if (cause != null) {
                 Throwables.throwIfUnchecked(cause);
             }