Binding2 - Add yang and tests
[mdsal.git] / binding2 / mdsal-binding2-dom-codec / src / main / java / org / opendaylight / mdsal / binding / javav2 / dom / codec / impl / BindingToNormalizedNodeCodec.java
index b051f871d21b1bb32ae0e1d5b2946e7bddecf65b..9268cc9d0b1599e3e79a131e672936b95f05da7b 100644 (file)
@@ -7,24 +7,24 @@
  */
 package org.opendaylight.mdsal.binding.javav2.dom.codec.impl;
 
+import static com.google.common.base.Preconditions.checkArgument;
+
 import com.google.common.annotations.Beta;
-import com.google.common.base.Function;
-import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.cache.CacheBuilder;
 import com.google.common.cache.CacheLoader;
 import com.google.common.cache.LoadingCache;
 import com.google.common.collect.ImmutableBiMap;
 import java.lang.reflect.Method;
-import java.net.URI;
 import java.util.AbstractMap.SimpleEntry;
 import java.util.Collection;
-import java.util.Date;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.Optional;
 import java.util.Set;
 import java.util.concurrent.TimeUnit;
+import java.util.function.Function;
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 import org.opendaylight.mdsal.binding.javav2.api.DataTreeIdentifier;
@@ -35,8 +35,10 @@ import org.opendaylight.mdsal.binding.javav2.dom.codec.api.serializer.BindingNor
 import org.opendaylight.mdsal.binding.javav2.generator.impl.GeneratedClassLoadingStrategy;
 import org.opendaylight.mdsal.binding.javav2.runtime.context.BindingRuntimeContext;
 import org.opendaylight.mdsal.binding.javav2.runtime.reflection.BindingReflections;
+import org.opendaylight.mdsal.binding.javav2.spec.base.Action;
 import org.opendaylight.mdsal.binding.javav2.spec.base.InstanceIdentifier;
 import org.opendaylight.mdsal.binding.javav2.spec.base.Notification;
+import org.opendaylight.mdsal.binding.javav2.spec.base.Rpc;
 import org.opendaylight.mdsal.binding.javav2.spec.base.TreeArgument;
 import org.opendaylight.mdsal.binding.javav2.spec.base.TreeNode;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
@@ -237,15 +239,15 @@ public final class BindingToNormalizedNodeCodec
      * create representation.
      *
      * <p>
-     * Returns Optional.absent for cases where target is mixin node except augmentation.
+     * Returns Optional.empty for cases where target is mixin node except augmentation.
      *
      */
     public Optional<InstanceIdentifier<? extends TreeNode>> toBinding(final YangInstanceIdentifier normalized)
             throws DeserializationException {
         try {
-            return Optional.fromNullable(codecRegistry.fromYangInstanceIdentifier(normalized));
+            return Optional.ofNullable(codecRegistry.fromYangInstanceIdentifier(normalized));
         } catch (final IllegalArgumentException e) {
-            return Optional.absent();
+            return Optional.empty();
         }
     }
 
@@ -265,9 +267,9 @@ public final class BindingToNormalizedNodeCodec
         try {
             final Entry<InstanceIdentifier<? extends TreeNode>, TreeNode> binding =
                     Entry.class.cast(codecRegistry.fromNormalizedNode(normalized.getKey(), normalized.getValue()));
-            return Optional.fromNullable(binding);
+            return Optional.ofNullable(binding);
         } catch (final IllegalArgumentException e) {
-            return Optional.absent();
+            return Optional.empty();
         }
     }
 
@@ -322,6 +324,7 @@ public final class BindingToNormalizedNodeCodec
      *            - RPC as binding object
      * @return map of method with path of specific RPC
      */
+    @Deprecated
     public ImmutableBiMap<Method, SchemaPath> getRPCMethodToSchemaPath(final Class<?> key) {
         final Module module = getModuleBlocking(key);
         final ImmutableBiMap.Builder<Method, SchemaPath> ret = ImmutableBiMap.builder();
@@ -337,31 +340,36 @@ public final class BindingToNormalizedNodeCodec
     }
 
     /**
-     * Resolve method with path of specific Action as binding object.
+     * Get Action schema path.
      *
-     * @param key
-     *            - action as binding object
-     * @return map of method with path of specific action
+     * @param type
+     *            - Action implementation class type
+     * @return schema path of Action
      */
-    public ImmutableBiMap<Method, SchemaPath> getActionMethodToSchemaPath(final Class<?> key) {
-        final Module module = getModuleBlocking(key);
+    public SchemaPath getActionPath(final Class<? extends Action<?, ?, ?, ?>> type) {
+        final ActionDefinition schema = runtimeContext.getActionDefinition(type);
+        checkArgument(schema != null, "Failed to find schema for %s", type);
+        return schema.getPath();
+    }
 
-        final ImmutableBiMap.Builder<Method, SchemaPath> ret = ImmutableBiMap.builder();
-        try {
-            for (final ActionDefinition actionDefinition : runtimeContext.getSchemaContext().getActions()) {
-                final QName qName = actionDefinition.getQName();
-                if (qName.getModule().equals(module.getQNameModule())) {
-                    final Method method = runtimeContext.findOperationMethod(key, actionDefinition);
-                    ret.put(method, actionDefinition.getPath());
-                }
-            }
-        } catch (final NoSuchMethodException e) {
-            throw new IllegalStateException("Action defined in model does not have representation in generated class.",
-                    e);
-        }
-        return ret.build();
+    /**
+     * Get RPC schema path.
+     *
+     * @param type
+     *            - RPC implementation class type
+     * @return schema path of RPC
+     */
+    public SchemaPath getRpcPath(final Class<? extends Rpc<?, ?>> type) {
+        final RpcDefinition schema = runtimeContext.getRpcDefinition(type);
+        checkArgument(schema != null, "Failed to find schema for %s", type);
+        return schema.getPath();
     }
 
+    public RpcDefinition getRpcDefinition(final Class<? extends Rpc<?, ?>> type) {
+        final RpcDefinition schema = runtimeContext.getRpcDefinition(type);
+        checkArgument(schema != null, "Failed to find schema for %s", type);
+        return schema;
+    }
 
     /**
      * Resolve method with definition of specific RPC as binding object.
@@ -411,15 +419,13 @@ public final class BindingToNormalizedNodeCodec
 
     private Module getModuleBlocking(final Class<?> modeledClass) {
         final QNameModule moduleName = BindingReflections.getQNameModule(modeledClass);
-        final URI namespace = moduleName.getNamespace();
-        final Date revision = moduleName.getRevision();
         BindingRuntimeContext localRuntimeContext = runtimeContext;
         Module module = localRuntimeContext == null ? null
-                : localRuntimeContext.getSchemaContext().findModuleByNamespaceAndRevision(namespace, revision);
-        if (module == null && futureSchema != null && futureSchema.waitForSchema(namespace, revision)) {
+                : localRuntimeContext.getSchemaContext().findModule(moduleName).get();
+        if (module == null && futureSchema != null && futureSchema.waitForSchema(moduleName)) {
             localRuntimeContext = runtimeContext;
             Preconditions.checkState(localRuntimeContext != null, "BindingRuntimeContext is not available.");
-            module = localRuntimeContext.getSchemaContext().findModuleByNamespaceAndRevision(namespace, revision);
+            module = localRuntimeContext.getSchemaContext().findModule(moduleName).get();
         }
         Preconditions.checkState(module != null, "Schema for %s is not available.", modeledClass);
         return module;
@@ -459,7 +465,7 @@ public final class BindingToNormalizedNodeCodec
 
         final BindingTreeCodec currentCodecTree = codecRegistry.getCodecContext();
         final InstanceIdentifier<?> bindingPath = codecRegistry.fromYangInstanceIdentifier(domIdentifier);
-        Preconditions.checkArgument(bindingPath != null);
+        checkArgument(bindingPath != null);
         /**
          * If we are able to deserialize YANG instance identifier, getSubtreeCodec must return non-null value.
          */
@@ -548,6 +554,17 @@ public final class BindingToNormalizedNodeCodec
         return ret;
     }
 
+    //FIXME: avoid the duplication of the function above.
+    public <P extends TreeNode> Set<DOMDataTreeIdentifier>
+            toDOMDataTreeIdentifiers(final Set<DataTreeIdentifier<P>> subtrees) {
+        final Set<DOMDataTreeIdentifier> ret = new HashSet<>(subtrees.size());
+
+        for (final DataTreeIdentifier<?> subtree : subtrees) {
+            ret.add(toDOMDataTreeIdentifier(subtree));
+        }
+        return ret;
+    }
+
     /**
      * Create new DOM data tree identifier from Binding data tree identifier.
      *