BUG-981: untangle interfaces
[yangtools.git] / code-generator / binding-generator-impl / src / main / java / org / opendaylight / yangtools / sal / binding / generator / impl / LazyGeneratedCodecRegistry.java
index d3100c9bc0079b28b94d587f1685924201cb5feb..edc186e0ebe0dbe5aabd5305502508f0a2abae6d 100644 (file)
@@ -81,7 +81,7 @@ import com.google.common.collect.HashMultimap;
 import com.google.common.collect.Multimap;
 import com.google.common.collect.Multimaps;
 
-public class LazyGeneratedCodecRegistry implements //
+class LazyGeneratedCodecRegistry implements //
         CodecRegistry, //
         SchemaContextListener, //
         GeneratorListener {
@@ -89,11 +89,6 @@ public class LazyGeneratedCodecRegistry implements //
     private static final Logger LOG = LoggerFactory.getLogger(LazyGeneratedCodecRegistry.class);
     private static final LateMixinCodec NOT_READY_CODEC = new LateMixinCodec();
 
-    private final InstanceIdentifierCodec instanceIdentifierCodec = new InstanceIdentifierCodecImpl(this);
-    private final IdentityCompositeCodec identityRefCodec = new IdentityCompositeCodec();
-
-    private TransformerGenerator generator;
-
     // Concrete class to codecs
     private static final Map<Class<?>, DataContainerCodec<?>> containerCodecs = Collections
             .synchronizedMap(new WeakHashMap<Class<?>, DataContainerCodec<?>>());
@@ -117,9 +112,6 @@ public class LazyGeneratedCodecRegistry implements //
     @SuppressWarnings("rawtypes")
     private static final ConcurrentMap<Type, ChoiceCaseCodecImpl> typeToCaseCodecs = new ConcurrentHashMap<>();
 
-    private final CaseClassMapFacade classToCaseRawCodec = new CaseClassMapFacade();
-
-    private static final Map<SchemaPath, InstanceIdentifier<?>> pathToBindingIdentifier = new ConcurrentHashMap<>();
     private static final Map<SchemaPath, GeneratedTypeBuilder> pathToType = new ConcurrentHashMap<>();
     private static final Map<List<QName>, Type> pathToInstantiatedType = new ConcurrentHashMap<>();
     private static final Map<Type, QName> typeToQname = new ConcurrentHashMap<>();
@@ -131,29 +123,27 @@ public class LazyGeneratedCodecRegistry implements //
     private static final Multimap<Type, Type> choiceToCases = Multimaps.synchronizedMultimap(HashMultimap
             .<Type, Type> create());
 
+    private final InstanceIdentifierCodec instanceIdentifierCodec = new InstanceIdentifierCodecImpl(this);
+    private final CaseClassMapFacade classToCaseRawCodec = new CaseClassMapFacade();
+    private final IdentityCompositeCodec identityRefCodec = new IdentityCompositeCodec();
+    private final ClassLoadingStrategy classLoadingStrategy;
+    private final AbstractTransformerGenerator generator;
     private final SchemaLock lock;
 
+    // FIXME: how is this protected?
     private SchemaContext currentSchema;
 
-    private final ClassLoadingStrategy classLoadingStrategy;
-
-    LazyGeneratedCodecRegistry(final SchemaLock lock, final ClassLoadingStrategy identityClassLoadingStrategy) {
+    LazyGeneratedCodecRegistry(final SchemaLock lock, final AbstractTransformerGenerator generator,
+            final ClassLoadingStrategy classLoadingStrategy) {
         this.lock = Preconditions.checkNotNull(lock);
-        this.classLoadingStrategy = identityClassLoadingStrategy;
+        this.classLoadingStrategy = Preconditions.checkNotNull(classLoadingStrategy);
+        this.generator = Preconditions.checkNotNull(generator);
     }
 
     public SchemaLock getLock() {
         return lock;
     }
 
-    public TransformerGenerator getGenerator() {
-        return generator;
-    }
-
-    public void setGenerator(final TransformerGenerator generator) {
-        this.generator = generator;
-    }
-
     @Override
     public InstanceIdentifierCodec getInstanceIdentifierCodec() {
         return instanceIdentifierCodec;
@@ -240,22 +230,6 @@ public class LazyGeneratedCodecRegistry implements //
         bindingClassEncountered(cls);
     }
 
-    public InstanceIdentifier<?> getBindingIdentifierByPath(final SchemaPath path) {
-        return pathToBindingIdentifier.get(path);
-    }
-
-    public void putPathToBindingIdentifier(final SchemaPath path, final InstanceIdentifier<?> bindingIdentifier) {
-        pathToBindingIdentifier.put(path, bindingIdentifier);
-    }
-
-    public InstanceIdentifier<?> putPathToBindingIdentifier(final SchemaPath path,
-            final InstanceIdentifier<?> bindingIdentifier, final Class<?> childClass) {
-        @SuppressWarnings({ "unchecked", "rawtypes" })
-        InstanceIdentifier<?> newId = bindingIdentifier.builder().child((Class) childClass).build();
-        pathToBindingIdentifier.put(path, newId);
-        return newId;
-    }
-
     @Override
     public IdentifierCodec<?> getKeyCodecForPath(final List<QName> names) {
         @SuppressWarnings("unchecked")
@@ -428,7 +402,6 @@ public class LazyGeneratedCodecRegistry implements //
                 caseClass.getName());
         Preconditions.checkState(caseCodec.getSchema() != null, "Case schema is not available for %s",
                 caseClass.getName());
-        @SuppressWarnings("unchecked")
         Class<? extends BindingCodec> newCodec = generator.caseCodecFor(caseClass, caseCodec.getSchema());
         BindingCodec newInstance = newInstanceOf(newCodec);
         caseCodec.setDelegate(newInstance);
@@ -713,10 +686,35 @@ public class LazyGeneratedCodecRegistry implements //
             if (adaptedForPaths.contains(path)) {
                 return;
             }
+            /**
+             * We search in schema context if the use of this location aware codec (augmentable codec, case codec)
+             * makes sense on provided location (path)
+             *
+             */
             Optional<DataNodeContainer> contextNode = BindingSchemaContextUtils.findDataNodeContainer(currentSchema, path);
+            /**
+             * If context node is present, this codec makes sense on provided location.
+             *
+             */
             if (contextNode.isPresent()) {
                 synchronized (this) {
+                    /**
+                     *
+                     * We adapt (turn on / off) possible implementations of child codecs (augmentations, cases)
+                     * based on this location.
+                     *
+                     *
+                     */
                     adaptForPathImpl(path, contextNode.get());
+                    try  {
+                        /**
+                         * We trigger serialization of instance identifier, to make sure instance identifier
+                         * codec is aware of combination of this path / augmentation / case
+                         */
+                        instanceIdentifierCodec.serialize(path);
+                    } catch (Exception e) {
+                        LOG.warn("Exception during preparation of instance identifier codec for  path {}.",path,e);
+                    }
                     adaptedForPaths.add(path);
                 }
             } else {
@@ -1381,7 +1379,10 @@ public class LazyGeneratedCodecRegistry implements //
 
     }
 
-    public boolean isCodecAvailable(final Class<? extends DataContainer> cls) {
+    public boolean isCodecAvailable(final Class<?> cls) {
+        // FIXME: enforce type?
+        // Preconditions.checkArgument(DataContainer.class.isAssignableFrom(cls));
+
         if (containerCodecs.containsKey(cls)) {
             return true;
         }