Merge "BUG-944: optimize SchemaContextUtil"
authorTony Tkacik <ttkacik@cisco.com>
Thu, 26 Jun 2014 13:36:28 +0000 (13:36 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Thu, 26 Jun 2014 13:36:28 +0000 (13:36 +0000)
code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/LazyGeneratedCodecRegistry.java
code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/RuntimeGeneratedMappingServiceImpl.java
restconf/restconf-util/src/test/java/org/opendaylight/yangtools/restconf/utils/LazyGeneratedCodecRestartTest.java [new file with mode: 0644]
restconf/restconf-util/src/test/java/org/opendaylight/yangtools/restconf/utils/RestconfUtilsTest.java

index 22817ae3e1d70180a015fb448e0dfbeaa6d99414..ce6d284fe6e3e9f5f66674970e6493797d63b76c 100644 (file)
@@ -255,8 +255,7 @@ class LazyGeneratedCodecRegistry implements //
         if (Augmentation.class.isAssignableFrom(cls)) {
 
         } else if (DataObject.class.isAssignableFrom(cls)) {
-            @SuppressWarnings({ "unchecked", "unused" })
-            Object cdc = getCodecForDataObject((Class<? extends DataObject>) cls);
+            getCodecForDataObject((Class<? extends DataObject>) cls);
         }
     }
 
@@ -453,7 +452,7 @@ class LazyGeneratedCodecRegistry implements //
     private void resetDispatchCodecsAdaptation() {
         synchronized (dispatchCodecs) {
             for (LocationAwareDispatchCodec<?> codec : dispatchCodecs.values()) {
-                codec.resetAdaptation();
+                codec.resetCodec(this);
             }
         }
     }
@@ -466,7 +465,7 @@ class LazyGeneratedCodecRegistry implements //
         Preconditions.checkState(oldCodec == null);
         BindingCodec<Map<QName, Object>, Object> delegate = newInstanceOf(choiceCodec);
         PublicChoiceCodecImpl<?> newCodec = new PublicChoiceCodecImpl(delegate);
-        DispatchChoiceCodecImpl dispatchCodec = new DispatchChoiceCodecImpl(choiceClass);
+        DispatchChoiceCodecImpl dispatchCodec = new DispatchChoiceCodecImpl(choiceClass,this);
         choiceCodecs.put(choiceClass, newCodec);
         synchronized (dispatchCodecs) {
             dispatchCodecs.put(choiceClass, dispatchCodec);
@@ -497,7 +496,7 @@ class LazyGeneratedCodecRegistry implements //
         if (ret != null) {
             return ret;
         }
-        ret = new AugmentableDispatchCodec(dataClass);
+        ret = new AugmentableDispatchCodec(dataClass,this);
         augmentableCodecs.put(dataClass, ret);
         synchronized (dispatchCodecs) {
             dispatchCodecs.put(dataClass, ret);
@@ -603,10 +602,16 @@ class LazyGeneratedCodecRegistry implements //
     }
 
     @SuppressWarnings("rawtypes")
-    private abstract class LocationAwareDispatchCodec<T extends LocationAwareBindingCodec> implements BindingCodec {
+    private static abstract class LocationAwareDispatchCodec<T extends LocationAwareBindingCodec> implements BindingCodec {
 
         private final Map<Class, T> implementations = Collections.synchronizedMap(new WeakHashMap<Class, T>());
         private final Set<InstanceIdentifier<?>> adaptedForPaths = new HashSet<>();
+        private LazyGeneratedCodecRegistry registry;
+
+
+        protected LocationAwareDispatchCodec(final LazyGeneratedCodecRegistry registry) {
+            this.registry = registry;
+        }
 
         protected Map<Class, T> getImplementations() {
             return implementations;
@@ -621,7 +626,8 @@ class LazyGeneratedCodecRegistry implements //
          * of new codecs and/or removal of existing ones.
          *
          */
-        public synchronized void resetAdaptation() {
+        public synchronized void resetCodec(final LazyGeneratedCodecRegistry currentRegistry) {
+            registry = currentRegistry;
             adaptedForPaths.clear();
             resetAdaptationImpl();
         }
@@ -631,6 +637,9 @@ class LazyGeneratedCodecRegistry implements //
             // behaviour.
         }
 
+        protected final LazyGeneratedCodecRegistry getRegistry() {
+            return registry;
+        }
         protected void addImplementation(final T implementation) {
             implementations.put(implementation.getDataType(), implementation);
         }
@@ -704,7 +713,7 @@ class LazyGeneratedCodecRegistry implements //
              * location (path)
              *
              */
-            Optional<DataNodeContainer> contextNode = BindingSchemaContextUtils.findDataNodeContainer(currentSchema,
+            Optional<DataNodeContainer> contextNode = BindingSchemaContextUtils.findDataNodeContainer(getRegistry().currentSchema,
                     path);
             /**
              * If context node is present, this codec makes sense on provided
@@ -729,7 +738,7 @@ class LazyGeneratedCodecRegistry implements //
                          * make sure instance identifier codec is aware of
                          * combination of this path / augmentation / case
                          */
-                        instanceIdentifierCodec.serialize(path);
+                        getRegistry().getInstanceIdentifierCodec().serialize(path);
                     } catch (Exception e) {
                         LOG.warn("Exception during preparation of instance identifier codec for  path {}.", path, e);
                     }
@@ -911,7 +920,8 @@ class LazyGeneratedCodecRegistry implements //
         private final Class<?> choiceType;
         private final QName choiceName;
 
-        private DispatchChoiceCodecImpl(final Class<?> type) {
+        private DispatchChoiceCodecImpl(final Class<?> type, final LazyGeneratedCodecRegistry registry) {
+            super(registry);
             choiceType = type;
             choiceName = BindingReflections.findQName(type);
         }
@@ -1010,11 +1020,12 @@ class LazyGeneratedCodecRegistry implements //
     }
 
     @SuppressWarnings({ "rawtypes", "unchecked" })
-    class AugmentableDispatchCodec extends LocationAwareDispatchCodec<AugmentationCodecWrapper> {
+    static class AugmentableDispatchCodec extends LocationAwareDispatchCodec<AugmentationCodecWrapper> {
 
         private final Class augmentableType;
 
-        public AugmentableDispatchCodec(final Class type) {
+        public AugmentableDispatchCodec(final Class type, final LazyGeneratedCodecRegistry registry) {
+            super(registry);
             Preconditions.checkArgument(Augmentable.class.isAssignableFrom(type));
             augmentableType = type;
         }
@@ -1046,7 +1057,7 @@ class LazyGeneratedCodecRegistry implements //
         private List serializeImpl(final Map<Class, Augmentation> input) {
             List ret = new ArrayList<>();
             for (Entry<Class, Augmentation> entry : input.entrySet()) {
-                AugmentationCodec codec = getCodecForAugmentation(entry.getKey());
+                AugmentationCodec codec = getRegistry().getCodecForAugmentation(entry.getKey());
                 CompositeNode node = codec.serialize(new ValueWithQName(null, entry.getValue()));
                 ret.addAll(node.getChildren());
             }
@@ -1073,7 +1084,7 @@ class LazyGeneratedCodecRegistry implements //
 
         protected Optional<AugmentationCodecWrapper> tryToLoadImplementation(final Type potential) {
             try {
-                Class<? extends Augmentation<?>> clazz = (Class<? extends Augmentation<?>>) classLoadingStrategy
+                Class<? extends Augmentation<?>> clazz = (Class<? extends Augmentation<?>>) getRegistry().classLoadingStrategy
                         .loadClass(potential);
                 return Optional.of(tryToLoadImplementation(clazz));
             } catch (ClassNotFoundException e) {
@@ -1084,7 +1095,7 @@ class LazyGeneratedCodecRegistry implements //
 
         @Override
         protected AugmentationCodecWrapper tryToLoadImplementation(final Class inputType) {
-            AugmentationCodecWrapper<? extends Augmentation<?>> potentialImpl = getCodecForAugmentation(inputType);
+            AugmentationCodecWrapper<? extends Augmentation<?>> potentialImpl = getRegistry().getCodecForAugmentation(inputType);
             addImplementation(potentialImpl);
             return potentialImpl;
         }
@@ -1147,7 +1158,7 @@ class LazyGeneratedCodecRegistry implements //
                         InstanceIdentifier augPath = augTarget.augmentation(augType);
                         try {
 
-                            org.opendaylight.yangtools.yang.data.api.InstanceIdentifier domPath = getInstanceIdentifierCodec()
+                            org.opendaylight.yangtools.yang.data.api.InstanceIdentifier domPath = getRegistry().getInstanceIdentifierCodec()
                                     .serialize(augPath);
                             if (domPath == null) {
                                 LOG.error("Unable to serialize instance identifier for {}", augPath);
index 08308163ed392a15fac36ce80f0601495133fab0..af2bfb350cb4dba3cd2a4f5bb5139c96ee1822bd 100644 (file)
@@ -112,7 +112,7 @@ public class RuntimeGeneratedMappingServiceImpl implements BindingIndependentMap
     public RuntimeGeneratedMappingServiceImpl(final ClassPool pool, final ClassLoadingStrategy strat) {
         this.pool = Preconditions.checkNotNull(pool);
         this.classLoadingStrategy = Preconditions.checkNotNull(strat);
-
+        // FIXME: this escapes constructor
         binding = new TransformerGenerator(this, pool);
         registry = new LazyGeneratedCodecRegistry(this, binding, classLoadingStrategy);
         binding.setListener(registry);
diff --git a/restconf/restconf-util/src/test/java/org/opendaylight/yangtools/restconf/utils/LazyGeneratedCodecRestartTest.java b/restconf/restconf-util/src/test/java/org/opendaylight/yangtools/restconf/utils/LazyGeneratedCodecRestartTest.java
new file mode 100644 (file)
index 0000000..80c5d6e
--- /dev/null
@@ -0,0 +1,30 @@
+package org.opendaylight.yangtools.restconf.utils;
+
+import org.junit.Before;
+import org.junit.Test;
+
+public class LazyGeneratedCodecRestartTest {
+
+    Bug527Test bug527;
+    Bug1196Test bug1196;
+    RestconfUtilsTest reUtils;
+
+
+    @Before
+    public void setup() {
+        bug527 = new Bug527Test();
+        bug1196 = new Bug1196Test();
+        reUtils = new RestconfUtilsTest();
+
+    }
+
+    @Test
+    public void test() {
+       bug1196.setup();
+       bug1196.testXmlDataToDataObjectUnixCase();
+
+       reUtils.setup();
+       reUtils.testToDataObjectMappingWithNestedAugmentations();
+    }
+
+}
index fef8a20547d5546562f177a2852f407e294ee5d9..e9131fbd43959f5b000acb538f4fa4c75efabeb5 100644 (file)
@@ -18,7 +18,9 @@ import org.junit.Before;
 import org.junit.Test;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Node1;
 import org.opendaylight.yangtools.sal.binding.generator.impl.ModuleInfoBackedContext;
@@ -40,6 +42,15 @@ public class RestconfUtilsTest {
         this.mappingService.onGlobalContextUpdated(moduleInfo.tryToCreateSchemaContext().get());
     }
 
+    @Test
+    public void firstTest() { // test static state collisions with the other test
+        final InstanceIdentifier<Topology> instanceIdentifier = InstanceIdentifier.builder(NetworkTopology.class)
+                .child(Topology.class, new TopologyKey(new TopologyId("example-pcep-topology"))).toInstance();
+        final InputStream is = this.getClass().getClassLoader().getResourceAsStream("topology-bug1196-linux.xml");
+        RestconfUtils.toRestconfIdentifier(instanceIdentifier, this.mappingService,
+                this.mappingService.getSchemaContext()).getValue();
+    }
+
     @Test
     public void testToDataObjectMappingWithNestedAugmentations() {
         final InstanceIdentifier<Topology> topologyIdentifier = InstanceIdentifier.builder(NetworkTopology.class)