Merge "Remove raw references to Map in XSQL"
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / sal / dom / broker / impl / SchemaAwareDataStoreAdapter.java
index b32d906d1e70cc8dc5997fea3c57c3379943b6b5..6a456ba0e8d2af5393cd09d3186d3dfbd4fb14d3 100644 (file)
@@ -8,12 +8,17 @@
 package org.opendaylight.controller.sal.dom.broker.impl;
 
 import static com.google.common.base.Preconditions.checkState;
-
+import com.google.common.base.Predicate;
+import com.google.common.collect.FluentIterable;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Iterables;
 import java.util.ArrayList;
 import java.util.Comparator;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 import java.util.Map.Entry;
 import java.util.concurrent.Future;
-
 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
 import org.opendaylight.controller.md.sal.common.api.data.DataModification;
 import org.opendaylight.controller.md.sal.common.api.data.DataReader;
@@ -25,32 +30,29 @@ import org.opendaylight.controller.sal.dom.broker.util.YangSchemaUtils;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.Node;
+import org.opendaylight.yangtools.yang.data.api.SimpleNode;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.impl.CompositeNodeTOImpl;
+import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.model.api.SchemaServiceListener;
+import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.base.Predicate;
-import com.google.common.collect.FluentIterable;
-import com.google.common.collect.ImmutableSet;
-
-public class SchemaAwareDataStoreAdapter extends AbstractLockableDelegator<DataStore> implements //
-        DataStore, //
-        SchemaServiceListener, //
-        AutoCloseable {
+@Deprecated
+public class SchemaAwareDataStoreAdapter extends AbstractLockableDelegator<DataStore> implements DataStore, SchemaContextListener, AutoCloseable {
 
     private final static Logger LOG = LoggerFactory.getLogger(SchemaAwareDataStoreAdapter.class);
 
     private SchemaContext schema = null;
     private boolean validationEnabled = false;
-    private final DataReader<InstanceIdentifier, CompositeNode> reader = new MergeFirstLevelReader();
+    private final DataReader<YangInstanceIdentifier, CompositeNode> reader = new MergeFirstLevelReader();
 
     @Override
-    public boolean containsConfigurationPath(InstanceIdentifier path) {
+    public boolean containsConfigurationPath(final YangInstanceIdentifier path) {
         try {
             getDelegateReadLock().lock();
             return getDelegate().containsConfigurationPath(path);
@@ -61,7 +63,7 @@ public class SchemaAwareDataStoreAdapter extends AbstractLockableDelegator<DataS
     }
 
     @Override
-    public boolean containsOperationalPath(InstanceIdentifier path) {
+    public boolean containsOperationalPath(final YangInstanceIdentifier path) {
         try {
             getDelegateReadLock().lock();
             return getDelegate().containsOperationalPath(path);
@@ -72,7 +74,7 @@ public class SchemaAwareDataStoreAdapter extends AbstractLockableDelegator<DataS
     }
 
     @Override
-    public Iterable<InstanceIdentifier> getStoredConfigurationPaths() {
+    public Iterable<YangInstanceIdentifier> getStoredConfigurationPaths() {
         try {
             getDelegateReadLock().lock();
             return getDelegate().getStoredConfigurationPaths();
@@ -83,7 +85,7 @@ public class SchemaAwareDataStoreAdapter extends AbstractLockableDelegator<DataS
     }
 
     @Override
-    public Iterable<InstanceIdentifier> getStoredOperationalPaths() {
+    public Iterable<YangInstanceIdentifier> getStoredOperationalPaths() {
         try {
             getDelegateReadLock().lock();
             return getDelegate().getStoredOperationalPaths();
@@ -94,18 +96,18 @@ public class SchemaAwareDataStoreAdapter extends AbstractLockableDelegator<DataS
     }
 
     @Override
-    public CompositeNode readConfigurationData(InstanceIdentifier path) {
+    public CompositeNode readConfigurationData(final YangInstanceIdentifier path) {
         return reader.readConfigurationData(path);
     }
 
     @Override
-    public CompositeNode readOperationalData(InstanceIdentifier path) {
+    public CompositeNode readOperationalData(final YangInstanceIdentifier path) {
         return reader.readOperationalData(path);
     }
 
     @Override
-    public org.opendaylight.controller.md.sal.common.api.data.DataCommitHandler.DataCommitTransaction<InstanceIdentifier, CompositeNode> requestCommit(
-            DataModification<InstanceIdentifier, CompositeNode> modification) {
+    public org.opendaylight.controller.md.sal.common.api.data.DataCommitHandler.DataCommitTransaction<YangInstanceIdentifier, CompositeNode> requestCommit(
+            final DataModification<YangInstanceIdentifier, CompositeNode> modification) {
         validateAgainstSchema(modification);
         NormalizedDataModification cleanedUp = prepareMergedTransaction(modification);
         cleanedUp.status = TransactionStatus.SUBMITED;
@@ -116,11 +118,11 @@ public class SchemaAwareDataStoreAdapter extends AbstractLockableDelegator<DataS
         return validationEnabled;
     }
 
-    public void setValidationEnabled(boolean validationEnabled) {
+    public void setValidationEnabled(final boolean validationEnabled) {
         this.validationEnabled = validationEnabled;
     }
 
-    private void validateAgainstSchema(DataModification<InstanceIdentifier, CompositeNode> modification) {
+    private void validateAgainstSchema(final DataModification<YangInstanceIdentifier, CompositeNode> modification) {
         if (!validationEnabled) {
             return;
         }
@@ -132,12 +134,12 @@ public class SchemaAwareDataStoreAdapter extends AbstractLockableDelegator<DataS
     }
 
     @Override
-    protected void onDelegateChanged(DataStore oldDelegate, DataStore newDelegate) {
+    protected void onDelegateChanged(final DataStore oldDelegate, final DataStore newDelegate) {
         // NOOP
     }
 
     @Override
-    public void onGlobalContextUpdated(SchemaContext context) {
+    public void onGlobalContextUpdated(final SchemaContext context) {
         this.schema = context;
     }
 
@@ -146,9 +148,9 @@ public class SchemaAwareDataStoreAdapter extends AbstractLockableDelegator<DataS
         this.schema = null;
     }
 
-    protected CompositeNode mergeData(InstanceIdentifier path, CompositeNode stored, CompositeNode modified,
-            boolean config) {
-        long startTime = System.nanoTime();
+    protected CompositeNode mergeData(final YangInstanceIdentifier path, final CompositeNode stored, final CompositeNode modified,
+            final boolean config) {
+        // long startTime = System.nanoTime();
         try {
             DataSchemaNode node = schemaNodeFor(path);
             return YangDataOperations.merge(node, stored, modified, config);
@@ -158,48 +160,55 @@ public class SchemaAwareDataStoreAdapter extends AbstractLockableDelegator<DataS
         }
     }
 
-    private DataSchemaNode schemaNodeFor(InstanceIdentifier path) {
+    private DataSchemaNode schemaNodeFor(final YangInstanceIdentifier path) {
         checkState(schema != null, "YANG Schema is not available");
         return YangSchemaUtils.getSchemaNode(schema, path);
     }
 
     private NormalizedDataModification prepareMergedTransaction(
-            DataModification<InstanceIdentifier, CompositeNode> original) {
+            final DataModification<YangInstanceIdentifier, CompositeNode> original) {
         NormalizedDataModification normalized = new NormalizedDataModification(original);
-        for (Entry<InstanceIdentifier, CompositeNode> entry : original.getUpdatedConfigurationData().entrySet()) {
-            normalized.putConfigurationData(entry.getKey(), entry.getValue());
-        }
-        for (Entry<InstanceIdentifier, CompositeNode> entry : original.getUpdatedOperationalData().entrySet()) {
-            normalized.putOperationalData(entry.getKey(), entry.getValue());
-        }
-        for (InstanceIdentifier entry : original.getRemovedConfigurationData()) {
+        LOG.trace("Transaction: {} Removed Configuration {}, Removed Operational {}", original.getIdentifier(),
+                original.getRemovedConfigurationData(), original.getRemovedConfigurationData());
+        LOG.trace("Transaction: {} Created Configuration {}, Created Operational {}", original.getIdentifier(),
+                original.getCreatedConfigurationData().entrySet(), original.getCreatedOperationalData().entrySet());
+        LOG.trace("Transaction: {} Updated Configuration {}, Updated Operational {}", original.getIdentifier(),
+                original.getUpdatedConfigurationData().entrySet(), original.getUpdatedOperationalData().entrySet());
+
+        for (YangInstanceIdentifier entry : original.getRemovedConfigurationData()) {
             normalized.deepRemoveConfigurationData(entry);
         }
-        for (InstanceIdentifier entry : original.getRemovedOperationalData()) {
+        for (YangInstanceIdentifier entry : original.getRemovedOperationalData()) {
             normalized.deepRemoveOperationalData(entry);
         }
+        for (Entry<YangInstanceIdentifier, CompositeNode> entry : original.getUpdatedConfigurationData().entrySet()) {
+            normalized.putDeepConfigurationData(entry.getKey(), entry.getValue());
+        }
+        for (Entry<YangInstanceIdentifier, CompositeNode> entry : original.getUpdatedOperationalData().entrySet()) {
+            normalized.putDeepOperationalData(entry.getKey(), entry.getValue());
+        }
         return normalized;
     }
 
-    private Iterable<InstanceIdentifier> getConfigurationSubpaths(InstanceIdentifier entry) {
+    private Iterable<YangInstanceIdentifier> getConfigurationSubpaths(final YangInstanceIdentifier entry) {
         // FIXME: This should be replaced by index
-        Iterable<InstanceIdentifier> paths = getStoredConfigurationPaths();
+        Iterable<YangInstanceIdentifier> paths = getStoredConfigurationPaths();
 
         return getChildrenPaths(entry, paths);
 
     }
 
-    public Iterable<InstanceIdentifier> getOperationalSubpaths(InstanceIdentifier entry) {
+    public Iterable<YangInstanceIdentifier> getOperationalSubpaths(final YangInstanceIdentifier entry) {
         // FIXME: This should be indexed
-        Iterable<InstanceIdentifier> paths = getStoredOperationalPaths();
+        Iterable<YangInstanceIdentifier> paths = getStoredOperationalPaths();
 
         return getChildrenPaths(entry, paths);
     }
 
-    private static final Iterable<InstanceIdentifier> getChildrenPaths(InstanceIdentifier entry,
-            Iterable<InstanceIdentifier> paths) {
-        ImmutableSet.Builder<InstanceIdentifier> children = ImmutableSet.builder();
-        for (InstanceIdentifier potential : paths) {
+    private static final Iterable<YangInstanceIdentifier> getChildrenPaths(final YangInstanceIdentifier entry,
+            final Iterable<YangInstanceIdentifier> paths) {
+        ImmutableSet.Builder<YangInstanceIdentifier> children = ImmutableSet.builder();
+        for (YangInstanceIdentifier potential : paths) {
             if (entry.contains(potential)) {
                 children.add(entry);
             }
@@ -207,40 +216,40 @@ public class SchemaAwareDataStoreAdapter extends AbstractLockableDelegator<DataS
         return children.build();
     }
 
-    private final Comparator<Entry<InstanceIdentifier, CompositeNode>> preparationComparator = new Comparator<Entry<InstanceIdentifier, CompositeNode>>() {
+    private final Comparator<Entry<YangInstanceIdentifier, CompositeNode>> preparationComparator = new Comparator<Entry<YangInstanceIdentifier, CompositeNode>>() {
         @Override
-        public int compare(Entry<InstanceIdentifier, CompositeNode> o1, Entry<InstanceIdentifier, CompositeNode> o2) {
-            InstanceIdentifier o1Key = o1.getKey();
-            InstanceIdentifier o2Key = o2.getKey();
-            return Integer.compare(o1Key.getPath().size(), o2Key.getPath().size());
+        public int compare(final Entry<YangInstanceIdentifier, CompositeNode> o1, final Entry<YangInstanceIdentifier, CompositeNode> o2) {
+            YangInstanceIdentifier o1Key = o1.getKey();
+            YangInstanceIdentifier o2Key = o2.getKey();
+            return Integer.compare(Iterables.size(o1Key.getPathArguments()), Iterables.size(o2Key.getPathArguments()));
         }
     };
 
-    private class MergeFirstLevelReader implements DataReader<InstanceIdentifier, CompositeNode> {
+    private class MergeFirstLevelReader implements DataReader<YangInstanceIdentifier, CompositeNode> {
 
         @Override
-        public CompositeNode readConfigurationData(final InstanceIdentifier path) {
+        public CompositeNode readConfigurationData(final YangInstanceIdentifier path) {
             getDelegateReadLock().lock();
             try {
-                if (path.getPath().isEmpty()) {
+                if (Iterables.isEmpty(path.getPathArguments())) {
                     return null;
                 }
                 QName qname = null;
                 CompositeNode original = getDelegate().readConfigurationData(path);
                 ArrayList<Node<?>> childNodes = new ArrayList<Node<?>>();
                 if (original != null) {
-                    childNodes.addAll(original.getChildren());
+                    childNodes.addAll(original.getValue());
                     qname = original.getNodeType();
                 } else {
-                    qname = path.getPath().get(path.getPath().size() - 1).getNodeType();
+                    qname = path.getLastPathArgument().getNodeType();
                 }
 
-                FluentIterable<InstanceIdentifier> directChildren = FluentIterable.from(getStoredConfigurationPaths())
-                        .filter(new Predicate<InstanceIdentifier>() {
+                FluentIterable<YangInstanceIdentifier> directChildren = FluentIterable.from(getStoredConfigurationPaths())
+                        .filter(new Predicate<YangInstanceIdentifier>() {
                             @Override
-                            public boolean apply(InstanceIdentifier input) {
+                            public boolean apply(final YangInstanceIdentifier input) {
                                 if (path.contains(input)) {
-                                    int nesting = input.getPath().size() - path.getPath().size();
+                                    int nesting = Iterables.size(input.getPathArguments()) - Iterables.size(path.getPathArguments());
                                     if (nesting == 1) {
                                         return true;
                                     }
@@ -248,7 +257,7 @@ public class SchemaAwareDataStoreAdapter extends AbstractLockableDelegator<DataS
                                 return false;
                             }
                         });
-                for (InstanceIdentifier instanceIdentifier : directChildren) {
+                for (YangInstanceIdentifier instanceIdentifier : directChildren) {
                     childNodes.add(getDelegate().readConfigurationData(instanceIdentifier));
                 }
                 if (original == null && childNodes.isEmpty()) {
@@ -262,28 +271,28 @@ public class SchemaAwareDataStoreAdapter extends AbstractLockableDelegator<DataS
         }
 
         @Override
-        public CompositeNode readOperationalData(final InstanceIdentifier path) {
+        public CompositeNode readOperationalData(final YangInstanceIdentifier path) {
             getDelegateReadLock().lock();
             try {
-                if (path.getPath().isEmpty()) {
+                if (Iterables.isEmpty(path.getPathArguments())) {
                     return null;
                 }
                 QName qname = null;
                 CompositeNode original = getDelegate().readOperationalData(path);
                 ArrayList<Node<?>> childNodes = new ArrayList<Node<?>>();
                 if (original != null) {
-                    childNodes.addAll(original.getChildren());
+                    childNodes.addAll(original.getValue());
                     qname = original.getNodeType();
                 } else {
-                    qname = path.getPath().get(path.getPath().size() - 1).getNodeType();
+                    qname = path.getLastPathArgument().getNodeType();
                 }
 
-                FluentIterable<InstanceIdentifier> directChildren = FluentIterable.from(getStoredOperationalPaths())
-                        .filter(new Predicate<InstanceIdentifier>() {
+                FluentIterable<YangInstanceIdentifier> directChildren = FluentIterable.from(getStoredOperationalPaths())
+                        .filter(new Predicate<YangInstanceIdentifier>() {
                             @Override
-                            public boolean apply(InstanceIdentifier input) {
+                            public boolean apply(final YangInstanceIdentifier input) {
                                 if (path.contains(input)) {
-                                    int nesting = input.getPath().size() - path.getPath().size();
+                                    int nesting = Iterables.size(input.getPathArguments()) - Iterables.size(path.getPathArguments());
                                     if (nesting == 1) {
                                         return true;
                                     }
@@ -292,7 +301,7 @@ public class SchemaAwareDataStoreAdapter extends AbstractLockableDelegator<DataS
                             }
                         });
 
-                for (InstanceIdentifier instanceIdentifier : directChildren) {
+                for (YangInstanceIdentifier instanceIdentifier : directChildren) {
                     childNodes.add(getDelegate().readOperationalData(instanceIdentifier));
                 }
                 if (original == null && childNodes.isEmpty()) {
@@ -306,12 +315,14 @@ public class SchemaAwareDataStoreAdapter extends AbstractLockableDelegator<DataS
         }
     }
 
-    private class NormalizedDataModification extends AbstractDataModification<InstanceIdentifier, CompositeNode> {
+    private class NormalizedDataModification extends AbstractDataModification<YangInstanceIdentifier, CompositeNode> {
 
+        private final String CONFIGURATIONAL_DATA_STORE_MARKER = "configurational";
+        private final String OPERATIONAL_DATA_STORE_MARKER = "operational";
         private final Object identifier;
         private TransactionStatus status;
 
-        public NormalizedDataModification(DataModification<InstanceIdentifier, CompositeNode> original) {
+        public NormalizedDataModification(final DataModification<YangInstanceIdentifier, CompositeNode> original) {
             super(getDelegate());
             identifier = original;
             status = TransactionStatus.NEW;
@@ -324,22 +335,30 @@ public class SchemaAwareDataStoreAdapter extends AbstractLockableDelegator<DataS
          *
          * @param entry
          */
-        public void deepRemoveOperationalData(InstanceIdentifier entry) {
-            Iterable<InstanceIdentifier> paths = getOperationalSubpaths(entry);
+        public void deepRemoveOperationalData(final YangInstanceIdentifier entry) {
+            Iterable<YangInstanceIdentifier> paths = getOperationalSubpaths(entry);
             removeOperationalData(entry);
-            for (InstanceIdentifier potential : paths) {
+            for (YangInstanceIdentifier potential : paths) {
                 removeOperationalData(potential);
             }
         }
 
-        public void deepRemoveConfigurationData(InstanceIdentifier entry) {
-            Iterable<InstanceIdentifier> paths = getConfigurationSubpaths(entry);
+        public void deepRemoveConfigurationData(final YangInstanceIdentifier entry) {
+            Iterable<YangInstanceIdentifier> paths = getConfigurationSubpaths(entry);
             removeConfigurationData(entry);
-            for (InstanceIdentifier potential : paths) {
+            for (YangInstanceIdentifier potential : paths) {
                 removeConfigurationData(potential);
             }
         }
 
+        public void putDeepConfigurationData(final YangInstanceIdentifier entryKey, final CompositeNode entryData) {
+            this.putCompositeNodeData(entryKey, entryData, CONFIGURATIONAL_DATA_STORE_MARKER);
+        }
+
+        public void putDeepOperationalData(final YangInstanceIdentifier entryKey, final CompositeNode entryData) {
+            this.putCompositeNodeData(entryKey, entryData, OPERATIONAL_DATA_STORE_MARKER);
+        }
+
         @Override
         public Object getIdentifier() {
             return this.identifier;
@@ -356,16 +375,82 @@ public class SchemaAwareDataStoreAdapter extends AbstractLockableDelegator<DataS
         }
 
         @Override
-        protected CompositeNode mergeConfigurationData(InstanceIdentifier path, CompositeNode stored,
-                CompositeNode modified) {
+        protected CompositeNode mergeConfigurationData(final YangInstanceIdentifier path, final CompositeNode stored,
+                final CompositeNode modified) {
             return mergeData(path, stored, modified, true);
         }
 
         @Override
-        protected CompositeNode mergeOperationalData(InstanceIdentifier path, CompositeNode stored,
-                CompositeNode modified) {
+        protected CompositeNode mergeOperationalData(final YangInstanceIdentifier path, final CompositeNode stored,
+                final CompositeNode modified) {
             return mergeData(path, stored, modified, false);
         }
-    }
 
+        private void putData(final YangInstanceIdentifier entryKey, final CompositeNode entryData, final String dataStoreIdentifier) {
+            if (dataStoreIdentifier != null && entryKey != null && entryData != null) {
+                switch (dataStoreIdentifier) {
+                case (CONFIGURATIONAL_DATA_STORE_MARKER):
+                    this.putConfigurationData(entryKey, entryData);
+                break;
+                case (OPERATIONAL_DATA_STORE_MARKER):
+                    this.putOperationalData(entryKey, entryData);
+                break;
+
+                default:
+                    LOG.error(dataStoreIdentifier + " is NOT valid DataStore switch marker");
+                    throw new RuntimeException(dataStoreIdentifier + " is NOT valid DataStore switch marker");
+                }
+            }
+        }
+
+        private void putCompositeNodeData(final YangInstanceIdentifier entryKey, final CompositeNode entryData,
+                final String dataStoreIdentifier) {
+            this.putData(entryKey, entryData, dataStoreIdentifier);
+
+            for (Node<?> child : entryData.getValue()) {
+                YangInstanceIdentifier subEntryId = YangInstanceIdentifier.builder(entryKey).node(child.getNodeType())
+                        .toInstance();
+                if (child instanceof CompositeNode) {
+                    DataSchemaNode subSchema = schemaNodeFor(subEntryId);
+                    CompositeNode compNode = (CompositeNode) child;
+                    YangInstanceIdentifier instanceId = null;
+
+                    if (subSchema instanceof ListSchemaNode) {
+                        ListSchemaNode listSubSchema = (ListSchemaNode) subSchema;
+                        Map<QName, Object> mapOfSubValues = this.getValuesFromListSchema(listSubSchema,
+                                (CompositeNode) child);
+                        if (mapOfSubValues != null) {
+                            instanceId = YangInstanceIdentifier.builder(entryKey)
+                                    .nodeWithKey(listSubSchema.getQName(), mapOfSubValues).toInstance();
+                        }
+                    } else if (subSchema instanceof ContainerSchemaNode) {
+                        ContainerSchemaNode containerSchema = (ContainerSchemaNode) subSchema;
+                        instanceId = YangInstanceIdentifier.builder(entryKey).node(subSchema.getQName()).toInstance();
+                    }
+                    if (instanceId != null) {
+                        this.putCompositeNodeData(instanceId, compNode, dataStoreIdentifier);
+                    }
+                }
+            }
+        }
+
+        private Map<QName, Object> getValuesFromListSchema(final ListSchemaNode listSchema, final CompositeNode entryData) {
+            List<QName> keyDef = listSchema.getKeyDefinition();
+            if (keyDef != null && !keyDef.isEmpty()) {
+                Map<QName, Object> map = new HashMap<QName, Object>();
+                for (QName key : keyDef) {
+                    List<Node<?>> data = entryData.get(key);
+                    if (data != null && !data.isEmpty()) {
+                        for (Node<?> nodeData : data) {
+                            if (nodeData instanceof SimpleNode<?>) {
+                                map.put(key, data.get(0).getValue());
+                            }
+                        }
+                    }
+                }
+                return map;
+            }
+            return null;
+        }
+    }
 }