Merge codec contexts caches
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / SchemaRootCodecContext.java
index e25ac36dadf57bd2ac484c7331b538b44454c808..35b537b7d91d32a4900982de85e731d8c4d329ee 100644 (file)
@@ -54,88 +54,78 @@ import org.opendaylight.yangtools.yang.model.util.SchemaNodeUtils;
 
 final class SchemaRootCodecContext<D extends DataObject> extends DataContainerCodecContext<D,SchemaContext> {
 
-    private final LoadingCache<Class<?>, DataContainerCodecContext<?,?>> childrenByClass = CacheBuilder.newBuilder()
-            .build(new CacheLoader<Class<?>, DataContainerCodecContext<?,?>>() {
-                @Override
-                public DataContainerCodecContext<?,?> load(final Class<?> key) {
-                    return createDataTreeChildContext(key);
-                }
-            });
-
-    private final LoadingCache<Class<? extends Action<?, ?, ?>>, ActionCodecContext> actionsByClass = CacheBuilder
-            .newBuilder().build(new CacheLoader<Class<? extends Action<?, ?, ?>>, ActionCodecContext>() {
-                @Override
-                public ActionCodecContext load(final Class<? extends Action<?, ?, ?>> key) {
-                    return createActionContext(key);
+    private final LoadingCache<Class<? extends DataObject>, DataContainerCodecContext<?, ?>> childrenByClass =
+        CacheBuilder.newBuilder().build(new CacheLoader<>() {
+            @Override
+            public DataContainerCodecContext<?, ?> load(final Class<? extends DataObject> key) {
+                if (Notification.class.isAssignableFrom(key)) {
+                    return createNotificationDataContext(key);
                 }
-            });
-
-    private final LoadingCache<Class<?>, ContainerNodeCodecContext<?>> rpcDataByClass = CacheBuilder.newBuilder().build(
-            new CacheLoader<Class<?>, ContainerNodeCodecContext<?>>() {
-                @Override
-                public ContainerNodeCodecContext<?> load(final Class<?> key) {
+                if (RpcInput.class.isAssignableFrom(key) || RpcOutput.class.isAssignableFrom(key)) {
                     return createRpcDataContext(key);
                 }
-            });
+                return createDataTreeChildContext(key);
+            }
+        });
 
-    private final LoadingCache<Class<?>, NotificationCodecContext<?>> notificationsByClass = CacheBuilder.newBuilder()
-            .build(new CacheLoader<Class<?>, NotificationCodecContext<?>>() {
-                @Override
-                public NotificationCodecContext<?> load(final Class<?> key) {
-                    return createNotificationDataContext(key);
-                }
-            });
+    private final LoadingCache<Class<? extends Action<?, ?, ?>>, ActionCodecContext> actionsByClass =
+        CacheBuilder.newBuilder().build(new CacheLoader<>() {
+            @Override
+            public ActionCodecContext load(final Class<? extends Action<?, ?, ?>> key) {
+                return createActionContext(key);
+            }
+        });
 
     private final LoadingCache<Class<? extends DataObject>, ChoiceNodeCodecContext<?>> choicesByClass =
-            CacheBuilder.newBuilder().build(new CacheLoader<Class<? extends DataObject>, ChoiceNodeCodecContext<?>>() {
-                @Override
-                public ChoiceNodeCodecContext<?> load(final Class<? extends DataObject> key) {
-                    return createChoiceDataContext(key);
-                }
-            });
-
-    private final LoadingCache<QName, DataContainerCodecContext<?,?>> childrenByQName = CacheBuilder.newBuilder().build(
-            new CacheLoader<QName, DataContainerCodecContext<?,?>>() {
-                @Override
-                public DataContainerCodecContext<?,?> load(final QName qname) {
-                    final DataSchemaNode childSchema = getSchema().getDataChildByName(qname);
-                    childNonNull(childSchema, qname,"Argument %s is not valid child of %s", qname,getSchema());
-                    if (childSchema instanceof DataNodeContainer || childSchema instanceof ChoiceSchemaNode) {
-                        @SuppressWarnings("unchecked")
-                        final Class<? extends DataObject> childCls = (Class<? extends DataObject>)
-                                factory().getRuntimeContext().getClassForSchema(childSchema);
-                        return streamChild(childCls);
-                    }
-
-                    throw new UnsupportedOperationException("Unsupported child type " + childSchema.getClass());
+        CacheBuilder.newBuilder().build(new CacheLoader<>() {
+            @Override
+            public ChoiceNodeCodecContext<?> load(final Class<? extends DataObject> key) {
+                return createChoiceDataContext(key);
+            }
+        });
+
+    private final LoadingCache<QName, DataContainerCodecContext<?,?>> childrenByQName =
+        CacheBuilder.newBuilder().build(new CacheLoader<>() {
+            @Override
+            public DataContainerCodecContext<?, ?> load(final QName qname) {
+                final DataSchemaNode childSchema = getSchema().getDataChildByName(qname);
+                childNonNull(childSchema, qname, "Argument %s is not valid child of %s", qname, getSchema());
+                if (childSchema instanceof DataNodeContainer || childSchema instanceof ChoiceSchemaNode) {
+                    @SuppressWarnings("unchecked")
+                    final Class<? extends DataObject> childCls = (Class<? extends DataObject>)
+                        factory().getRuntimeContext().getClassForSchema(childSchema);
+                    return streamChild(childCls);
                 }
-            });
 
-    private final LoadingCache<Absolute, RpcInputCodec<?>> rpcDataByPath = CacheBuilder.newBuilder().build(
-        new CacheLoader<Absolute, RpcInputCodec<?>>() {
+                throw new UnsupportedOperationException("Unsupported child type " + childSchema.getClass());
+            }
+        });
+
+    private final LoadingCache<Absolute, RpcInputCodec<?>> rpcDataByPath =
+        CacheBuilder.newBuilder().build(new CacheLoader<>() {
             @Override
             public RpcInputCodec<?> load(final Absolute key) {
                 final ContainerLike schema = SchemaContextUtil.getRpcDataSchema(getSchema(), key.asSchemaPath());
                 @SuppressWarnings("unchecked")
                 final Class<? extends DataContainer> cls = (Class<? extends DataContainer>)
-                        factory().getRuntimeContext().getClassForSchema(schema);
+                    factory().getRuntimeContext().getClassForSchema(schema);
                 return getRpc(cls);
             }
         });
 
-    private final LoadingCache<Absolute, NotificationCodecContext<?>> notificationsByPath = CacheBuilder.newBuilder()
-            .build(new CacheLoader<Absolute, NotificationCodecContext<?>>() {
-                @Override
-                public NotificationCodecContext<?> load(final Absolute key) {
-                    final NotificationDefinition schema = SchemaContextUtil.getNotificationSchema(getSchema(),
-                        // FIXME: do not convert here!
-                        key.asSchemaPath());
-                    @SuppressWarnings("unchecked")
-                    final Class<? extends Notification> clz = (Class<? extends Notification>)
-                            factory().getRuntimeContext().getClassForSchema(schema);
-                    return getNotification(clz);
-                }
-            });
+    private final LoadingCache<Absolute, NotificationCodecContext<?>> notificationsByPath =
+        CacheBuilder.newBuilder().build(new CacheLoader<>() {
+            @Override
+            public NotificationCodecContext<?> load(final Absolute key) {
+                final NotificationDefinition schema = SchemaContextUtil.getNotificationSchema(getSchema(),
+                    // FIXME: do not convert here!
+                    key.asSchemaPath());
+                @SuppressWarnings("unchecked")
+                final Class<? extends Notification> clz = (Class<? extends Notification>)
+                    factory().getRuntimeContext().getClassForSchema(schema);
+                return getNotification(clz);
+            }
+        });
 
     private SchemaRootCodecContext(final DataContainerCodecPrototype<SchemaContext> dataPrototype) {
         super(dataPrototype);
@@ -157,15 +147,7 @@ final class SchemaRootCodecContext<D extends DataObject> extends DataContainerCo
     @SuppressWarnings("unchecked")
     @Override
     public <C extends DataObject> DataContainerCodecContext<C, ?> streamChild(final Class<C> childClass) {
-        /* FIXME: This is still not solved for RPCs
-         * TODO: Probably performance wise RPC, Data and Notification loading cache
-         *       should be merge for performance resons. Needs microbenchmark to
-         *       determine which is faster (keeping them separate or in same cache).
-         */
-        if (Notification.class.isAssignableFrom(childClass)) {
-            return (DataContainerCodecContext<C, ?>) getNotification((Class<? extends Notification>)childClass);
-        }
-        return (DataContainerCodecContext<C, ?>) getOrRethrow(childrenByClass,childClass);
+        return (DataContainerCodecContext<C, ?>) getOrRethrow(childrenByClass, childClass);
     }
 
     @Override
@@ -189,7 +171,7 @@ final class SchemaRootCodecContext<D extends DataObject> extends DataContainerCo
     }
 
     NotificationCodecContext<?> getNotification(final Class<? extends Notification> notification) {
-        return getOrRethrow(notificationsByClass, notification);
+        return (NotificationCodecContext<?>) streamChild((Class<? extends DataObject>)notification);
     }
 
     NotificationCodecContext<?> getNotification(final Absolute notification) {
@@ -197,7 +179,7 @@ final class SchemaRootCodecContext<D extends DataObject> extends DataContainerCo
     }
 
     ContainerNodeCodecContext<?> getRpc(final Class<? extends DataContainer> rpcInputOrOutput) {
-        return getOrRethrow(rpcDataByClass, rpcInputOrOutput);
+        return (ContainerNodeCodecContext<?>) streamChild((Class<? extends DataObject>)rpcInputOrOutput);
     }
 
     RpcInputCodec<?> getRpc(final Absolute containerPath) {