*/
package org.opendaylight.controller.sal.binding.test.util;
+import static com.google.common.base.Preconditions.checkState;
+
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javassist.ClassPool;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.controller.md.sal.dom.broker.impl.DOMDataBrokerImpl;
+import org.opendaylight.controller.md.sal.dom.broker.impl.compat.BackwardsCompatibleDataBroker;
+import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
import org.opendaylight.controller.sal.binding.api.mount.MountProviderService;
import org.opendaylight.controller.sal.core.api.RpcRegistrationListener;
import org.opendaylight.controller.sal.core.api.data.DataStore;
import org.opendaylight.controller.sal.core.api.mount.MountProvisionService;
+import org.opendaylight.controller.sal.core.spi.data.DOMStore;
import org.opendaylight.controller.sal.dom.broker.BrokerImpl;
import org.opendaylight.controller.sal.dom.broker.MountPointManagerImpl;
import org.opendaylight.controller.sal.dom.broker.impl.DataStoreStatsWrapper;
import org.opendaylight.yangtools.yang.data.impl.codec.BindingIndependentMappingService;
import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
import org.reflections.Reflections;
import org.reflections.scanners.ResourcesScanner;
import com.google.common.base.Predicate;
import com.google.common.collect.ClassToInstanceMap;
import com.google.common.collect.ImmutableClassToInstanceMap;
+import com.google.common.collect.ImmutableMap;
import com.google.common.util.concurrent.ListeningExecutorService;
-import static com.google.common.base.Preconditions.*;
-
public class BindingTestContext implements AutoCloseable, SchemaContextProvider {
public static final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier TREE_ROOT = org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
private BindingIndependentConnector baConnectImpl;
private org.opendaylight.controller.sal.dom.broker.DataBrokerImpl biDataImpl;
+ private org.opendaylight.controller.sal.core.api.data.DataProviderService biDataLegacyBroker;
private BrokerImpl biBrokerImpl;
private HashMapDataStore rawDataStore;
private SchemaAwareDataStoreAdapter schemaAwareDataStore;
private DataStoreStatsWrapper dataStoreStats;
private DataStore dataStore;
- private boolean dataStoreStatisticsEnabled = false;
+ private final boolean dataStoreStatisticsEnabled = false;
private final ListeningExecutorService executor;
private final ClassPool classPool;
private SchemaContext schemaContext;
+ private ImmutableMap<LogicalDatastoreType, DOMStore> newDatastores;
+
+ private BackwardsCompatibleDataBroker biCompatibleBroker;
+
+ private final List<SchemaContextListener> schemaListeners = new ArrayList<>();
+
+ @Override
public SchemaContext getSchemaContext() {
return schemaContext;
}
- protected BindingTestContext(ListeningExecutorService executor, ClassPool classPool, boolean startWithSchema) {
+ protected BindingTestContext(final ListeningExecutorService executor, final ClassPool classPool, final boolean startWithSchema) {
this.executor = executor;
this.classPool = classPool;
this.startWithSchema = startWithSchema;
checkState(executor != null, "Executor needs to be set");
biDataImpl = new org.opendaylight.controller.sal.dom.broker.DataBrokerImpl();
biDataImpl.setExecutor(executor);
+ biDataLegacyBroker = biDataImpl;
+ }
+
+ public void startNewDomDataBroker() {
+ checkState(executor != null, "Executor needs to be set");
+ InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("OPER", executor);
+ InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG", executor);
+ newDatastores = ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
+ .put(LogicalDatastoreType.OPERATIONAL, operStore)
+ .put(LogicalDatastoreType.CONFIGURATION, configStore)
+ .build();
+
+ DOMDataBrokerImpl newBiDataImpl = new DOMDataBrokerImpl(newDatastores, executor);
+
+ biCompatibleBroker = new BackwardsCompatibleDataBroker(newBiDataImpl);
+
+ schemaListeners.add(configStore);
+ schemaListeners.add(operStore);
+ schemaListeners.add(biCompatibleBroker);
+ biDataLegacyBroker = biCompatibleBroker;
}
public void startBindingDataBroker() {
public void startForwarding() {
checkState(baDataImpl != null, "Binding Data Broker needs to be started");
- checkState(biDataImpl != null, "DOM Data Broker needs to be started.");
+ checkState(biDataLegacyBroker != null, "DOM Data Broker needs to be started.");
checkState(mappingServiceImpl != null, "DOM Mapping Service needs to be started.");
baConnectImpl = BindingDomConnectorDeployer.createConnector(getBindingToDomMappingService());
}
private ProviderSession createMockContext() {
- // TODO Auto-generated method stub
+
final ClassToInstanceMap<BrokerService> domBrokerServices = ImmutableClassToInstanceMap
.<BrokerService> builder()
//
- .put(org.opendaylight.controller.sal.core.api.data.DataProviderService.class, biDataImpl) //
+ .put(org.opendaylight.controller.sal.core.api.data.DataProviderService.class, biDataLegacyBroker) //
.put(RpcProvisionRegistry.class, biBrokerImpl.getRouter()) //
.put(MountProvisionService.class, biMountImpl) //
.build();
return new ProviderSession() {
@Override
- public Future<RpcResult<CompositeNode>> rpc(QName rpc, CompositeNode input) {
+ public Future<RpcResult<CompositeNode>> rpc(final QName rpc, final CompositeNode input) {
throw new UnsupportedOperationException();
}
@Override
- public <T extends BrokerService> T getService(Class<T> service) {
+ public <T extends BrokerService> T getService(final Class<T> service) {
return domBrokerServices.getInstance(service);
}
@Override
public ListenerRegistration<RpcRegistrationListener> addRpcRegistrationListener(
- RpcRegistrationListener listener) {
+ final RpcRegistrationListener listener) {
return null;
}
@Override
- public RpcRegistration addRpcImplementation(QName rpcType, RpcImplementation implementation)
+ public RpcRegistration addRpcImplementation(final QName rpcType, final RpcImplementation implementation)
throws IllegalArgumentException {
return null;
}
@Override
- public RoutedRpcRegistration addRoutedRpcImplementation(QName rpcType, RpcImplementation implementation) {
+ public RoutedRpcRegistration addRoutedRpcImplementation(final QName rpcType, final RpcImplementation implementation) {
return null;
}
@Override
- public RoutedRpcRegistration addMountedRpcImplementation(QName rpcType, RpcImplementation implementation) {
+ public RoutedRpcRegistration addMountedRpcImplementation(final QName rpcType, final RpcImplementation implementation) {
return null;
}
};
mappingServiceImpl.init();
}
- public void updateYangSchema(String[] files) {
+ public void updateYangSchema(final String[] files) {
schemaContext = getContext(files);
+
if (schemaAwareDataStore != null) {
schemaAwareDataStore.onGlobalContextUpdated(schemaContext);
}
if (mappingServiceImpl != null) {
mappingServiceImpl.onGlobalContextUpdated(schemaContext);
}
+ for(SchemaContextListener listener : schemaListeners) {
+ listener.onGlobalContextUpdated(schemaContext);
+ }
}
public static String[] getAllYangFilesOnClasspath() {
Predicate<String> predicate = new Predicate<String>() {
@Override
- public boolean apply(String input) {
+ public boolean apply(final String input) {
return input.endsWith(".yang");
}
};
Reflections reflection = new Reflections("META-INF.yang", new ResourcesScanner());
Set<String> result = reflection.getResources(predicate);
- return (String[]) result.toArray(new String[result.size()]);
+ return result.toArray(new String[result.size()]);
}
- private static SchemaContext getContext(String[] yangFiles) {
+ private static SchemaContext getContext(final String[] yangFiles) {
ClassLoader loader = BindingTestContext.class.getClassLoader();
List<InputStream> streams = new ArrayList<>();
for (String string : yangFiles) {
return parser.resolveSchemaContext(modules);
}
- public void start() {
+ public void startLegacy() {
startBindingDataBroker();
startBindingNotificationBroker();
startBindingBroker();
}
}
+ public void start() {
+ startBindingDataBroker();
+ startBindingNotificationBroker();
+ startBindingBroker();
+ startNewDomDataBroker();
+ startDomBroker();
+ startDomMountPoint();
+ startBindingToDomMappingService();
+ startForwarding();
+ if (startWithSchema) {
+ loadYangSchemaFromClasspath();
+ }
+ }
+
private void startDomMountPoint() {
biMountImpl = new MountPointManagerImpl();
biMountImpl.setDataBroker(getDomDataBroker());
biBrokerImpl = new BrokerImpl();
biBrokerImpl.setExecutor(executor);
biBrokerImpl.setRouter(new SchemaAwareRpcBroker("/", this));
+
}
public void startBindingNotificationBroker() {
}
public org.opendaylight.controller.sal.core.api.data.DataProviderService getDomDataBroker() {
- return biDataImpl;
+ return biDataLegacyBroker;
}
public DataStore getDomDataStore() {
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
+import org.junit.Ignore;
import org.junit.Test;
import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
*
* Reported by Depthi V V
*
+ * @deprecated This test tests indirect generation, which should be tested
+ * different way. the test creates conflicting transactions
+ * and assumes correct commit - to test codec generation
+ *
*/
@Test
+ @Ignore
+ @Deprecated
public void testIndirectGeneration() throws Exception {
ExecutorService basePool = Executors.newFixedThreadPool(2);
private class CreateFlowTask implements Callable<Void> {
- public CreateFlowTask(Object startSync) {
+ public CreateFlowTask(final Object startSync) {
}
@Override
*/
package org.opendaylight.controller.sal.binding.test.bugfix;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.Callable;
import javassist.ClassPool;
+import org.junit.Ignore;
import org.junit.Test;
import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
-import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
import org.opendaylight.controller.sal.binding.test.AbstractDataServiceTest;
import org.opendaylight.controller.sal.binding.test.util.BindingBrokerTestFactory;
+import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.config.rev130819.flows.Flow;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodesBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.common.RpcResult;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
+import static org.junit.Assert.*;
+
public class DOMCodecBug02Test extends AbstractDataServiceTest {
private static final QName NODE_ID_QNAME = QName.create(Node.QNAME, "id");
/**
* This test is ignored, till found out better way to test generation
* of classes without leaking of instances from previous run
- *
+ *
* @throws Exception
*/
-
- @Override
+
public void setUp() {
ListeningExecutorService executor = MoreExecutors.sameThreadExecutor();
BindingBrokerTestFactory factory = new BindingBrokerTestFactory();
factory.setStartWithParsedSchema(getStartWithSchema());
testContext = factory.getTestContext();
testContext.start();
-
+
baDataService = testContext.getBindingDataBroker();
biDataService = testContext.getDomDataBroker();
dataStore = testContext.getDomDataStore();
mappingService = testContext.getBindingToDomMappingService();
};
-
+
@Test
public void testSchemaContextNotAvailable() throws Exception {
return transaction.commit();
}
});
-
-
+
+
RpcResult<TransactionStatus> result = future.get().get();
assertEquals(TransactionStatus.COMMITED, result.getResult());
-
+
Nodes nodes = checkForNodes();
assertNotNull(nodes);
return (Nodes) baDataService.readOperationalData(NODES_INSTANCE_ID_BA);
}
-
+
@Override
protected boolean getStartWithSchema() {
return false;
*/
package org.opendaylight.controller.sal.binding.test.connect.dom;
-import com.google.common.collect.ImmutableMap;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+import org.junit.Ignore;
import org.junit.Test;
import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
import org.opendaylight.controller.md.sal.common.api.data.DataCommitHandler;
import org.opendaylight.controller.md.sal.common.api.data.DataModification;
-import org.opendaylight.controller.sal.common.util.CommitHandlerTransactions;
import org.opendaylight.controller.sal.binding.test.AbstractDataServiceTest;
+import org.opendaylight.controller.sal.common.util.CommitHandlerTransactions;
import org.opendaylight.controller.sal.core.api.data.DataModificationTransaction;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpVersion;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
import org.opendaylight.yangtools.yang.common.RpcResult;
import org.opendaylight.yangtools.yang.data.api.CompositeNode;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-import java.util.Map;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
+import com.google.common.collect.ImmutableMap;
public class ChangeOriginatedInDomBrokerTest extends AbstractDataServiceTest {
.toInstance();
@Test
+ @Ignore
public void simpleModifyOperation() throws Exception {
assertNull(biDataService.readConfigurationData(FLOW_INSTANCE_ID_BI));
@Override
public org.opendaylight.controller.md.sal.common.api.data.DataCommitHandler.DataCommitTransaction<InstanceIdentifier<? extends DataObject>, DataObject> requestCommit(
- DataModification<InstanceIdentifier<? extends DataObject>, DataObject> modification) {
+ final DataModification<InstanceIdentifier<? extends DataObject>, DataObject> modification) {
modificationCapture = modification;
return CommitHandlerTransactions.allwaysSuccessfulTransaction(modification);
}
*/
package org.opendaylight.controller.md.sal.common.api.data;
-import java.util.concurrent.Future;
-
import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
import org.opendaylight.yangtools.concepts.Path;
import org.opendaylight.yangtools.yang.common.RpcResult;
+import com.google.common.util.concurrent.ListenableFuture;
+
public interface AsyncWriteTransaction<P extends Path<P>, D> extends AsyncTransaction<P, D> {
/**
* Cancels transaction.
* {@link TransactionStatus#FAILED} is reached.
* @throws IllegalStateException if the transaction is not {@link TransactionStatus#NEW}
*/
- public Future<RpcResult<TransactionStatus>> commit();
+ public ListenableFuture<RpcResult<TransactionStatus>> commit();
}
<groupId>org.eclipse.xtend</groupId>
<artifactId>org.eclipse.xtend.lib</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.opendaylight.yangtools</groupId>
+ <artifactId>yang-data-impl</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.opendaylight.yangtools</groupId>
+ <artifactId>yang-model-api</artifactId>
+ </dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
import static org.opendaylight.controller.md.sal.common.api.TransactionStatus.NEW;
import java.util.Collections;
+import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentMap;
import org.opendaylight.controller.md.sal.common.api.data.DataModification;
import org.opendaylight.controller.md.sal.common.api.data.DataReader;
public abstract class AbstractDataModification<P extends Path<P>, D> implements DataModification<P, D> {
- private final ConcurrentMap<P, D> operationalOriginal;
- private final ConcurrentMap<P, D> configurationOriginal;
+ private final Map<P, D> operationalOriginal;
+ private final Map<P, D> configurationOriginal;
- private final ConcurrentMap<P, D> operationalCreated;
- private final ConcurrentMap<P, D> configurationCreated;
+ private final Map<P, D> operationalCreated;
+ private final Map<P, D> configurationCreated;
- private final ConcurrentMap<P, D> configurationUpdate;
- private final ConcurrentMap<P, D> operationalUpdate;
+ private final Map<P, D> configurationUpdate;
+ private final Map<P, D> operationalUpdate;
- private final ConcurrentMap<P, P> configurationRemove;
- private final ConcurrentMap<P, P> operationalRemove;
+ private final Map<P, P> configurationRemove;
+ private final Map<P, P> operationalRemove;
private final Map<P, D> unmodifiable_configurationOriginal;
private final Map<P, D> unmodifiable_operationalOriginal;
private final Set<P> unmodifiable_OperationalRemove;
private final DataReader<P, D> reader;
- public AbstractDataModification(DataReader<P, D> reader) {
+ public AbstractDataModification(final DataReader<P, D> reader) {
this.reader = reader;
- this.configurationUpdate = new ConcurrentHashMap<>();
- this.operationalUpdate = new ConcurrentHashMap<>();
- this.configurationRemove = new ConcurrentHashMap<>();
- this.operationalRemove = new ConcurrentHashMap<>();
+ this.configurationUpdate = new LinkedHashMap<>();
+ this.operationalUpdate = new LinkedHashMap<>();
+ this.configurationRemove = new LinkedHashMap<>();
+ this.operationalRemove = new LinkedHashMap<>();
- this.configurationOriginal = new ConcurrentHashMap<>();
- this.operationalOriginal = new ConcurrentHashMap<>();
+ this.configurationOriginal = new LinkedHashMap<>();
+ this.operationalOriginal = new LinkedHashMap<>();
- this.configurationCreated = new ConcurrentHashMap<>();
- this.operationalCreated = new ConcurrentHashMap<>();
+ this.configurationCreated = new LinkedHashMap<>();
+ this.operationalCreated = new LinkedHashMap<>();
unmodifiable_configurationOriginal = Collections.unmodifiableMap(configurationOriginal);
unmodifiable_operationalOriginal = Collections.unmodifiableMap(operationalOriginal);
}
@Override
- public final void putConfigurationData(P path, D data) {
+ public final void putConfigurationData(final P path, final D data) {
checkMutable();
D original = null;
if ((original = getConfigurationOriginal(path)) == null) {
}
@Override
- public final void putOperationalData(P path, D data) {
+ public final void putOperationalData(final P path, final D data) {
checkMutable();
D original = null;
if ((original = getOperationalOriginal(path)) == null) {
}
@Override
- public final void removeOperationalData(P path) {
+ public final void removeOperationalData(final P path) {
checkMutable();
getOperationalOriginal(path);
operationalUpdate.remove(path);
}
@Override
- public final void removeConfigurationData(P path) {
+ public final void removeConfigurationData(final P path) {
checkMutable();
getConfigurationOriginal(path);
configurationUpdate.remove(path);
}
@Override
- public D readOperationalData(P path) {
+ public D readOperationalData(final P path) {
return reader.readOperationalData(path);
}
@Override
- public D readConfigurationData(P path) {
+ public D readConfigurationData(final P path) {
return reader.readConfigurationData(path);
}
- private D getConfigurationOriginal(P path) {
+ private D getConfigurationOriginal(final P path) {
D data = configurationOriginal.get(path);
if (data != null) {
return data;
}
data = reader.readConfigurationData(path);
if (data != null) {
- configurationOriginal.putIfAbsent(path, data);
+ configurationOriginal.put(path, data);
return data;
}
return null;
}
- private D getOperationalOriginal(P path) {
+ private D getOperationalOriginal(final P path) {
D data = operationalOriginal.get(path);
if (data != null) {
return data;
}
data = reader.readOperationalData(path);
if (data != null) {
- operationalOriginal.putIfAbsent(path, data);
+ operationalOriginal.put(path, data);
return data;
}
return null;
}
- protected D mergeOperationalData(P path,D stored, D modified) {
+ protected D mergeOperationalData(final P path,final D stored, final D modified) {
return modified;
}
- protected D mergeConfigurationData(P path,D stored, D modified) {
+ protected D mergeConfigurationData(final P path,final D stored, final D modified) {
return modified;
}
}
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
-final class ImmutableDataChangeEvent<P extends Path<P>, D> implements DataChangeEvent<P,D> {
+public final class ImmutableDataChangeEvent<P extends Path<P>, D> implements DataChangeEvent<P,D> {
private final D updatedOperationalSubtree;
private final Map<P, D> updatedOperational;
private final Map<P, D> createdConfiguration;
- public ImmutableDataChangeEvent(Builder<P, D> builder) {
+ private ImmutableDataChangeEvent(final Builder<P, D> builder) {
createdConfiguration = builder.getCreatedConfiguration().build();
createdOperational = builder.getCreatedOperational().build();
return updatedOperationalSubtree;
}
- static final <P extends Path<P>,D> Builder<P, D> builder() {
+ public static final <P extends Path<P>,D> Builder<P, D> builder() {
return new Builder<>();
}
- static final class Builder<P extends Path<P>,D> {
+ public static final class Builder<P extends Path<P>,D> {
private D updatedOperationalSubtree;
private D originalOperationalSubtree;
private final ImmutableMap.Builder<P, D> createdConfiguration = ImmutableMap.builder();
- protected Builder<P,D> addTransaction(DataModification<P, D> data, Predicate<P> keyFilter) {
+
+
+
+ protected Builder<P,D> addTransaction(final DataModification<P, D> data, final Predicate<P> keyFilter) {
updatedOperational.putAll(Maps.filterKeys(data.getUpdatedOperationalData(), keyFilter));
updatedConfiguration.putAll(Maps.filterKeys(data.getUpdatedConfigurationData(), keyFilter));
originalConfiguration.putAll(Maps.filterKeys(data.getOriginalConfigurationData(), keyFilter));
return this;
}
- protected Builder<P, D> addConfigurationChangeSet(RootedChangeSet<P, D> changeSet) {
+ protected Builder<P, D> addConfigurationChangeSet(final RootedChangeSet<P, D> changeSet) {
if(changeSet == null) {
return this;
}
return this;
}
- protected Builder<P, D> addOperationalChangeSet(RootedChangeSet<P, D> changeSet) {
+ protected Builder<P, D> addOperationalChangeSet(final RootedChangeSet<P, D> changeSet) {
if(changeSet == null) {
return this;
}
return this;
}
- protected ImmutableDataChangeEvent<P, D> build() {
+ public ImmutableDataChangeEvent<P, D> build() {
return new ImmutableDataChangeEvent<P,D>(this);
}
return updatedOperationalSubtree;
}
- protected Builder<P, D> setUpdatedOperationalSubtree(D updatedOperationalSubtree) {
+ public Builder<P, D> setUpdatedOperationalSubtree(final D updatedOperationalSubtree) {
this.updatedOperationalSubtree = updatedOperationalSubtree;
return this;
}
return originalOperationalSubtree;
}
- protected Builder<P,D> setOriginalOperationalSubtree(D originalOperationalSubtree) {
+ public Builder<P,D> setOriginalOperationalSubtree(final D originalOperationalSubtree) {
this.originalOperationalSubtree = originalOperationalSubtree;
return this;
}
return originalConfigurationSubtree;
}
- protected Builder<P, D> setOriginalConfigurationSubtree(D originalConfigurationSubtree) {
+ public Builder<P, D> setOriginalConfigurationSubtree(final D originalConfigurationSubtree) {
this.originalConfigurationSubtree = originalConfigurationSubtree;
return this;
}
return updatedConfigurationSubtree;
}
- protected Builder<P,D> setUpdatedConfigurationSubtree(D updatedConfigurationSubtree) {
+ public Builder<P,D> setUpdatedConfigurationSubtree(final D updatedConfigurationSubtree) {
this.updatedConfigurationSubtree = updatedConfigurationSubtree;
return this;
}
protected ImmutableMap.Builder<P, D> getCreatedConfiguration() {
return createdConfiguration;
}
+
+ public Builder<P,D> putOriginalOperational(final Map<? extends P, ? extends D> originalData) {
+ originalOperational.putAll(originalData);
+ return this;
+ }
+
+ public Builder<P,D> putCreatedOperational(final Map<? extends P, ? extends D> originalData) {
+ createdOperational.putAll(originalData);
+ return this;
+ }
+
+ public Builder<P,D> putUpdatedOperational(final Map<? extends P, ? extends D> originalData) {
+ updatedOperational.putAll(originalData);
+ return this;
+ }
+
+ public Builder<P,D> putRemovedOperational(final Set<? extends P> originalData) {
+ removedOperational.addAll(originalData);
+ return this;
+ }
}
}
--- /dev/null
+package org.opendaylight.controller.md.sal.common.impl.util.compat;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.opendaylight.yangtools.concepts.Identifiable;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.CompositeNode;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.AugmentationIdentifier;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeWithValue;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
+import org.opendaylight.yangtools.yang.data.api.Node;
+import org.opendaylight.yangtools.yang.data.api.SimpleNode;
+import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
+import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
+import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
+import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
+import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
+import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
+import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
+import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
+import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
+import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+
+import com.google.common.collect.FluentIterable;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
+
+public abstract class DataNormalizationOperation<T extends PathArgument> implements Identifiable<T> {
+
+ private final T identifier;
+
+ @Override
+ public T getIdentifier() {
+ return identifier;
+ };
+
+ protected DataNormalizationOperation(final T identifier) {
+ super();
+ this.identifier = identifier;
+ }
+
+ public boolean isMixin() {
+ return false;
+ }
+
+ protected Set<QName> getQNameIdentifiers() {
+ return Collections.singleton(identifier.getNodeType());
+ }
+
+ public abstract DataNormalizationOperation<?> getChild(final PathArgument child);
+
+ public abstract DataNormalizationOperation<?> getChild(QName child);
+
+ public abstract NormalizedNode<?, ?> normalize(Node<?> legacyData);
+
+ private static abstract class SimpleTypeNormalization<T extends PathArgument> extends DataNormalizationOperation<T> {
+
+ protected SimpleTypeNormalization(final T identifier) {
+ super(identifier);
+ }
+
+ @Override
+ public NormalizedNode<?, ?> normalize(final Node<?> legacyData) {
+ checkArgument(legacyData != null);
+ checkArgument(legacyData instanceof SimpleNode<?>);
+ return normalizeImpl((SimpleNode<?>) legacyData);
+ }
+
+ protected abstract NormalizedNode<?, ?> normalizeImpl(SimpleNode<?> node);
+
+ @Override
+ public DataNormalizationOperation<?> getChild(final PathArgument child) {
+ return null;
+ }
+
+ @Override
+ public DataNormalizationOperation<?> getChild(final QName child) {
+ return null;
+ }
+
+ @Override
+ public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ }
+
+ private static final class LeafNormalization extends SimpleTypeNormalization<NodeIdentifier> {
+
+ protected LeafNormalization(final NodeIdentifier identifier) {
+ super(identifier);
+ }
+
+ @Override
+ protected NormalizedNode<?, ?> normalizeImpl(final SimpleNode<?> node) {
+ return ImmutableNodes.leafNode(node.getNodeType(), node.getValue());
+ }
+
+ }
+
+ private static final class LeafListEntryNormalization extends SimpleTypeNormalization<NodeWithValue> {
+
+ public LeafListEntryNormalization(final LeafListSchemaNode potential) {
+ super(new NodeWithValue(potential.getQName(), null));
+ }
+
+ @Override
+ protected NormalizedNode<?, ?> normalizeImpl(final SimpleNode<?> node) {
+ NodeWithValue nodeId = new NodeWithValue(node.getNodeType(), node.getValue());
+ return Builders.leafSetEntryBuilder().withNodeIdentifier(nodeId).withValue(node.getValue()).build();
+ }
+
+ }
+
+ private static abstract class CompositeNodeNormalizationOpertation<T extends PathArgument> extends
+ DataNormalizationOperation<T> {
+
+ protected CompositeNodeNormalizationOpertation(final T identifier) {
+ super(identifier);
+ }
+
+ @SuppressWarnings({ "rawtypes", "unchecked" })
+ @Override
+ public final NormalizedNodeContainer<?, ?, ?> normalize(final Node<?> legacyData) {
+ checkArgument(legacyData != null);
+ if (!isMixin() && getIdentifier().getNodeType() != null) {
+ checkArgument(getIdentifier().getNodeType().equals(legacyData.getNodeType()),
+ "Node QName must be %s was %s", getIdentifier().getNodeType(), legacyData.getNodeType());
+ }
+ checkArgument(legacyData instanceof CompositeNode, "Node %s should be composite", legacyData);
+ CompositeNode compositeNode = (CompositeNode) legacyData;
+ NormalizedNodeContainerBuilder builder = createBuilder(compositeNode);
+
+ Set<DataNormalizationOperation<?>> usedMixins = new HashSet<>();
+ for (Node<?> childLegacy : compositeNode.getValue()) {
+ DataNormalizationOperation childOp = getChild(childLegacy.getNodeType());
+
+ // We skip unknown nodes if this node is mixin since
+ // it's nodes and parent nodes are interleaved
+ if (childOp == null && isMixin()) {
+ continue;
+ }
+
+ checkArgument(childOp != null, "Node %s is not allowed inside %s", childLegacy.getNodeType(),
+ getIdentifier());
+ if (childOp.isMixin()) {
+ if (usedMixins.contains(childOp)) {
+ // We already run / processed that mixin, so to avoid
+ // dupliciry we are
+ // skiping next nodes.
+ continue;
+ }
+ builder.addChild(childOp.normalize(compositeNode));
+ usedMixins.add(childOp);
+ } else {
+ builder.addChild(childOp.normalize(childLegacy));
+ }
+ }
+ return (NormalizedNodeContainer<?, ?, ?>) builder.build();
+ }
+
+ @SuppressWarnings("rawtypes")
+ protected abstract NormalizedNodeContainerBuilder createBuilder(final CompositeNode compositeNode);
+
+ }
+
+ private static abstract class DataContainerNormalizationOperation<T extends PathArgument> extends
+ CompositeNodeNormalizationOpertation<T> {
+
+ private final DataNodeContainer schema;
+ private final Map<QName, DataNormalizationOperation<?>> byQName;
+ private final Map<PathArgument, DataNormalizationOperation<?>> byArg;
+
+ protected DataContainerNormalizationOperation(final T identifier, final DataNodeContainer schema) {
+ super(identifier);
+ this.schema = schema;
+ this.byArg = new ConcurrentHashMap<>();
+ this.byQName = new ConcurrentHashMap<>();
+ }
+
+ @Override
+ public DataNormalizationOperation<?> getChild(final PathArgument child) {
+ DataNormalizationOperation<?> potential = byArg.get(child);
+ if (potential != null) {
+ return potential;
+ }
+ potential = fromSchema(schema, child);
+ return register(potential);
+ }
+
+ @Override
+ public DataNormalizationOperation<?> getChild(final QName child) {
+ DataNormalizationOperation<?> potential = byQName.get(child);
+ if (potential != null) {
+ return potential;
+ }
+ potential = fromSchemaAndPathArgument(schema, child);
+ return register(potential);
+ }
+
+ private DataNormalizationOperation<?> register(final DataNormalizationOperation<?> potential) {
+ if (potential != null) {
+ byArg.put(potential.getIdentifier(), potential);
+ for (QName qName : potential.getQNameIdentifiers()) {
+ byQName.put(qName, potential);
+ }
+ }
+ return potential;
+ }
+
+ }
+
+ private static final class ListItemNormalization extends
+ DataContainerNormalizationOperation<NodeIdentifierWithPredicates> {
+
+ private final List<QName> keyDefinition;
+
+ protected ListItemNormalization(final NodeIdentifierWithPredicates identifier, final ListSchemaNode schema) {
+ super(identifier, schema);
+ keyDefinition = schema.getKeyDefinition();
+ }
+
+ @Override
+ protected NormalizedNodeContainerBuilder createBuilder(final CompositeNode compositeNode) {
+ ImmutableMap.Builder<QName, Object> keys = ImmutableMap.builder();
+ for (QName key : keyDefinition) {
+ SimpleNode<?> valueNode = checkNotNull(compositeNode.getFirstSimpleByName(key),"List node %s MUST contain leaf %s with value.",getIdentifier().getNodeType(),key);
+ keys.put(key, valueNode.getValue());
+ }
+
+ return Builders.mapEntryBuilder().withNodeIdentifier(
+ new NodeIdentifierWithPredicates(getIdentifier().getNodeType(), keys.build()));
+ }
+
+ @Override
+ public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
+ DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> builder = Builders
+ .mapEntryBuilder().withNodeIdentifier((NodeIdentifierWithPredicates) currentArg);
+ for (Entry<QName, Object> keyValue : ((NodeIdentifierWithPredicates) currentArg).getKeyValues().entrySet()) {
+ builder.addChild(Builders.leafBuilder()
+ //
+ .withNodeIdentifier(new NodeIdentifier(keyValue.getKey())).withValue(keyValue.getValue())
+ .build());
+ }
+ return builder.build();
+ }
+ }
+
+ private static final class ContainerNormalization extends DataContainerNormalizationOperation<NodeIdentifier> {
+
+ protected ContainerNormalization(final ContainerSchemaNode schema) {
+ super(new NodeIdentifier(schema.getQName()), schema);
+ }
+
+ @Override
+ protected NormalizedNodeContainerBuilder createBuilder(final CompositeNode compositeNode) {
+ return Builders.containerBuilder().withNodeIdentifier(getIdentifier());
+ }
+
+ @Override
+ public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
+ return Builders.containerBuilder().withNodeIdentifier((NodeIdentifier) currentArg).build();
+ }
+
+ }
+
+ private static abstract class MixinNormalizationOp<T extends PathArgument> extends
+ CompositeNodeNormalizationOpertation<T> {
+
+ protected MixinNormalizationOp(final T identifier) {
+ super(identifier);
+ }
+
+ @Override
+ public final boolean isMixin() {
+ return true;
+ }
+
+ }
+
+ private static final class LeafListMixinNormalization extends MixinNormalizationOp<NodeIdentifier> {
+
+ private final DataNormalizationOperation<?> innerOp;
+
+ public LeafListMixinNormalization(final LeafListSchemaNode potential) {
+ super(new NodeIdentifier(potential.getQName()));
+ innerOp = new LeafListEntryNormalization(potential);
+ }
+
+ @Override
+ protected NormalizedNodeContainerBuilder createBuilder(final CompositeNode compositeNode) {
+ return Builders.leafSetBuilder().withNodeIdentifier(getIdentifier());
+ }
+
+ @Override
+ public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
+ return Builders.leafSetBuilder().withNodeIdentifier(getIdentifier()).build();
+ }
+
+ @Override
+ public DataNormalizationOperation<?> getChild(final PathArgument child) {
+ if (child instanceof NodeWithValue) {
+ return innerOp;
+ }
+ return null;
+ }
+
+ @Override
+ public DataNormalizationOperation<?> getChild(final QName child) {
+ if (getIdentifier().getNodeType().equals(child)) {
+ return innerOp;
+ }
+ return null;
+ }
+ }
+
+ private static final class AugmentationNormalization extends MixinNormalizationOp<AugmentationIdentifier> {
+
+ private final Map<QName, DataNormalizationOperation<?>> byQName;
+ private final Map<PathArgument, DataNormalizationOperation<?>> byArg;
+
+ public AugmentationNormalization(final AugmentationSchema augmentation, final DataNodeContainer schema) {
+ super(augmentationIdentifierFrom(augmentation));
+
+ ImmutableMap.Builder<QName, DataNormalizationOperation<?>> byQNameBuilder = ImmutableMap.builder();
+ ImmutableMap.Builder<PathArgument, DataNormalizationOperation<?>> byArgBuilder = ImmutableMap.builder();
+
+ for (DataSchemaNode augNode : augmentation.getChildNodes()) {
+ DataSchemaNode resolvedNode = schema.getDataChildByName(augNode.getQName());
+ DataNormalizationOperation<?> resolvedOp = fromDataSchemaNode(resolvedNode);
+ byArgBuilder.put(resolvedOp.getIdentifier(), resolvedOp);
+ for (QName resQName : resolvedOp.getQNameIdentifiers()) {
+ byQNameBuilder.put(resQName, resolvedOp);
+ }
+ }
+ byQName = byQNameBuilder.build();
+ byArg = byArgBuilder.build();
+
+ }
+
+ @Override
+ public DataNormalizationOperation<?> getChild(final PathArgument child) {
+ return byArg.get(child);
+ }
+
+ @Override
+ public DataNormalizationOperation<?> getChild(final QName child) {
+ return byQName.get(child);
+ }
+
+ @Override
+ protected Set<QName> getQNameIdentifiers() {
+ return getIdentifier().getPossibleChildNames();
+ }
+
+ @SuppressWarnings("rawtypes")
+ @Override
+ protected NormalizedNodeContainerBuilder createBuilder(final CompositeNode compositeNode) {
+ return Builders.augmentationBuilder().withNodeIdentifier(getIdentifier());
+ }
+
+ @Override
+ public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
+ return Builders.augmentationBuilder().withNodeIdentifier(getIdentifier()).build();
+ }
+
+ }
+
+ private static final class ListMixinNormalization extends MixinNormalizationOp<NodeIdentifier> {
+
+ private final ListItemNormalization innerNode;
+
+ public ListMixinNormalization(final ListSchemaNode list) {
+ super(new NodeIdentifier(list.getQName()));
+ this.innerNode = new ListItemNormalization(new NodeIdentifierWithPredicates(list.getQName(),
+ Collections.<QName, Object> emptyMap()), list);
+ }
+
+ @SuppressWarnings("rawtypes")
+ @Override
+ protected NormalizedNodeContainerBuilder createBuilder(final CompositeNode compositeNode) {
+ return Builders.mapBuilder().withNodeIdentifier(getIdentifier());
+ }
+
+ @Override
+ public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
+ return Builders.mapBuilder().withNodeIdentifier(getIdentifier()).build();
+ }
+
+ @Override
+ public DataNormalizationOperation<?> getChild(final PathArgument child) {
+ if (child.getNodeType().equals(getIdentifier().getNodeType())) {
+ return innerNode;
+ }
+ return null;
+ }
+
+ @Override
+ public DataNormalizationOperation<?> getChild(final QName child) {
+ if (getIdentifier().getNodeType().equals(child)) {
+ return innerNode;
+ }
+ return null;
+ }
+
+ }
+
+ private static class ChoiceNodeNormalization extends MixinNormalizationOp<NodeIdentifier> {
+
+ private final ImmutableMap<QName, DataNormalizationOperation<?>> byQName;
+ private final ImmutableMap<PathArgument, DataNormalizationOperation<?>> byArg;
+
+ protected ChoiceNodeNormalization(final org.opendaylight.yangtools.yang.model.api.ChoiceNode schema) {
+ super(new NodeIdentifier(schema.getQName()));
+ ImmutableMap.Builder<QName, DataNormalizationOperation<?>> byQNameBuilder = ImmutableMap.builder();
+ ImmutableMap.Builder<PathArgument, DataNormalizationOperation<?>> byArgBuilder = ImmutableMap.builder();
+
+ for (ChoiceCaseNode caze : schema.getCases()) {
+ for (DataSchemaNode cazeChild : caze.getChildNodes()) {
+ DataNormalizationOperation<?> childOp = fromDataSchemaNode(cazeChild);
+ byArgBuilder.put(childOp.getIdentifier(), childOp);
+ for (QName qname : childOp.getQNameIdentifiers()) {
+ byQNameBuilder.put(qname, childOp);
+ }
+ }
+ }
+ byQName = byQNameBuilder.build();
+ byArg = byArgBuilder.build();
+ }
+
+ @Override
+ public DataNormalizationOperation<?> getChild(final PathArgument child) {
+ return byArg.get(child);
+ }
+
+ @Override
+ public DataNormalizationOperation<?> getChild(final QName child) {
+ return byQName.get(child);
+ }
+
+ @Override
+ protected NormalizedNodeContainerBuilder createBuilder(final CompositeNode compositeNode) {
+ return Builders.choiceBuilder().withNodeIdentifier(getIdentifier());
+ }
+
+ @Override
+ public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
+ return Builders.choiceBuilder().withNodeIdentifier(getIdentifier()).build();
+ }
+ }
+
+ public static DataNormalizationOperation<?> fromSchemaAndPathArgument(final DataNodeContainer schema,
+ final QName child) {
+ DataSchemaNode potential = schema.getDataChildByName(child);
+ if (potential == null) {
+ Iterable<org.opendaylight.yangtools.yang.model.api.ChoiceNode> choices = FluentIterable.from(
+ schema.getChildNodes()).filter(org.opendaylight.yangtools.yang.model.api.ChoiceNode.class);
+ potential = findChoice(choices, child);
+ }
+ checkArgument(potential != null, "Supplied QName %s is not valid according to schema %s", child, schema);
+ if ((schema instanceof DataSchemaNode) && !((DataSchemaNode) schema).isAugmenting() && potential.isAugmenting()) {
+ return fromAugmentation(schema, (AugmentationTarget) schema, potential);
+ }
+ return fromDataSchemaNode(potential);
+ }
+
+ private static org.opendaylight.yangtools.yang.model.api.ChoiceNode findChoice(
+ final Iterable<org.opendaylight.yangtools.yang.model.api.ChoiceNode> choices, final QName child) {
+ org.opendaylight.yangtools.yang.model.api.ChoiceNode foundChoice = null;
+ choiceLoop: for (org.opendaylight.yangtools.yang.model.api.ChoiceNode choice : choices) {
+ for (ChoiceCaseNode caze : choice.getCases()) {
+ if (caze.getDataChildByName(child) != null) {
+ foundChoice = choice;
+ break choiceLoop;
+ }
+ }
+ }
+ return foundChoice;
+ }
+
+ public static AugmentationIdentifier augmentationIdentifierFrom(final AugmentationSchema augmentation) {
+ ImmutableSet.Builder<QName> potentialChildren = ImmutableSet.builder();
+ for (DataSchemaNode child : augmentation.getChildNodes()) {
+ potentialChildren.add(child.getQName());
+ }
+ return new AugmentationIdentifier(null, potentialChildren.build());
+ }
+
+ private static AugmentationNormalization fromAugmentation(final DataNodeContainer schema,
+ final AugmentationTarget augments, final DataSchemaNode potential) {
+ AugmentationSchema augmentation = null;
+ for (AugmentationSchema aug : augments.getAvailableAugmentations()) {
+ DataSchemaNode child = aug.getDataChildByName(potential.getQName());
+ if (child != null) {
+ augmentation = aug;
+ break;
+ }
+
+ }
+ if (augmentation != null) {
+ return new AugmentationNormalization(augmentation, schema);
+ } else {
+ return null;
+ }
+ }
+
+ private static DataNormalizationOperation<?> fromSchema(final DataNodeContainer schema, final PathArgument child) {
+ if (child instanceof AugmentationIdentifier) {
+ return fromSchemaAndPathArgument(schema, ((AugmentationIdentifier) child).getPossibleChildNames()
+ .iterator().next());
+ }
+ return fromSchemaAndPathArgument(schema, child.getNodeType());
+ }
+
+ public static DataNormalizationOperation<?> fromDataSchemaNode(final DataSchemaNode potential) {
+ if (potential instanceof ContainerSchemaNode) {
+ return new ContainerNormalization((ContainerSchemaNode) potential);
+ } else if (potential instanceof ListSchemaNode) {
+ return new ListMixinNormalization((ListSchemaNode) potential);
+ } else if (potential instanceof LeafSchemaNode) {
+ return new LeafNormalization(new NodeIdentifier(potential.getQName()));
+ } else if (potential instanceof org.opendaylight.yangtools.yang.model.api.ChoiceNode) {
+ return new ChoiceNodeNormalization((org.opendaylight.yangtools.yang.model.api.ChoiceNode) potential);
+ } else if (potential instanceof LeafListSchemaNode) {
+ return new LeafListMixinNormalization((LeafListSchemaNode) potential);
+ }
+ return null;
+ }
+
+ public static DataNormalizationOperation<?> from(final SchemaContext ctx) {
+ return new ContainerNormalization(ctx);
+ }
+
+ public abstract NormalizedNode<?, ?> createDefault(PathArgument currentArg);
+}
--- /dev/null
+package org.opendaylight.controller.md.sal.common.impl.util.compat;
+
+import static com.google.common.base.Preconditions.checkArgument;
+
+import java.util.AbstractMap;
+import java.util.ArrayList;
+import java.util.Map;
+
+import org.opendaylight.yangtools.yang.data.api.CompositeNode;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.AugmentationIdentifier;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
+import org.opendaylight.yangtools.yang.data.api.Node;
+import org.opendaylight.yangtools.yang.data.api.SimpleNode;
+import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
+import org.opendaylight.yangtools.yang.data.api.schema.MixinNode;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
+import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
+import org.opendaylight.yangtools.yang.data.impl.SimpleNodeTOImpl;
+import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+
+import com.google.common.base.Preconditions;
+import com.google.common.base.Predicate;
+import com.google.common.collect.FluentIterable;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Iterables;
+
+public class DataNormalizer {
+
+ private final SchemaContext schemaContext;
+
+ private final DataNormalizationOperation<?> operation;
+
+ public DataNormalizer(final SchemaContext ctx) {
+ schemaContext = ctx;
+ operation = DataNormalizationOperation.from(ctx);
+ }
+
+ public InstanceIdentifier toNormalized(final InstanceIdentifier legacy) {
+ ImmutableList.Builder<PathArgument> normalizedArgs = ImmutableList.builder();
+
+ DataNormalizationOperation<?> currentOp = operation;
+ for (PathArgument legacyArg : legacy.getPath()) {
+ currentOp = currentOp.getChild(legacyArg);
+ checkArgument(currentOp != null, "Legacy Instance Identifier %s is not correct. Normalized Instance Identifier so far %s",legacy,normalizedArgs.build());
+ while (currentOp.isMixin()) {
+ normalizedArgs.add(currentOp.getIdentifier());
+ currentOp = currentOp.getChild(legacyArg.getNodeType());
+ }
+ normalizedArgs.add(legacyArg);
+ }
+ return new InstanceIdentifier(normalizedArgs.build());
+ }
+
+ public Map.Entry<InstanceIdentifier,NormalizedNode<?, ?>> toNormalized(final Map.Entry<InstanceIdentifier,CompositeNode> legacy) {
+ return toNormalized(legacy.getKey(), legacy.getValue());
+ }
+
+ public Map.Entry<InstanceIdentifier,NormalizedNode<?, ?>> toNormalized(final InstanceIdentifier legacyPath, final CompositeNode legacyData) {
+
+ InstanceIdentifier normalizedPath = toNormalized(legacyPath);
+
+ DataNormalizationOperation<?> currentOp = operation;
+ for (PathArgument arg : normalizedPath.getPath()) {
+ currentOp = currentOp.getChild(arg);
+ }
+ // Write Augmentaiton data resolution
+ if (legacyData.getChildren().size() == 1) {
+ DataNormalizationOperation<?> potentialOp = currentOp.getChild(legacyData.getChildren().get(0)
+ .getNodeType());
+ if(potentialOp.getIdentifier() instanceof AugmentationIdentifier) {
+ currentOp = potentialOp;
+ ArrayList<PathArgument> reworkedArgs = new ArrayList<>(normalizedPath.getPath());
+ reworkedArgs.add(potentialOp.getIdentifier());
+ normalizedPath = new InstanceIdentifier(reworkedArgs);
+ }
+ }
+
+ Preconditions.checkArgument(currentOp != null,
+ "Instance Identifier %s does not reference correct schema Node.", normalizedPath);
+ return new AbstractMap.SimpleEntry<InstanceIdentifier,NormalizedNode<?, ?>>(normalizedPath,currentOp.normalize(legacyData));
+ }
+
+ public InstanceIdentifier toLegacy(final InstanceIdentifier normalized) {
+ ImmutableList.Builder<PathArgument> legacyArgs = ImmutableList.builder();
+ PathArgument previous = null;
+ for (PathArgument normalizedArg : normalized.getPath()) {
+ if (normalizedArg instanceof NodeIdentifier) {
+ if (previous != null) {
+ legacyArgs.add(previous);
+ }
+ previous = normalizedArg;
+ } else if (normalizedArg instanceof NodeIdentifierWithPredicates) {
+ // We skip previous node, which was mixin.
+ previous = normalizedArg;
+ } else if (normalizedArg instanceof AugmentationIdentifier) {
+ // We ignore argument
+ }
+ // FIXME : Add option for reading choice
+ }
+ if (previous != null) {
+ legacyArgs.add(previous);
+ }
+ return new InstanceIdentifier(legacyArgs.build());
+ }
+
+ public CompositeNode toLegacy(final InstanceIdentifier normalizedPath, final NormalizedNode<?, ?> normalizedData) {
+ // Preconditions.checkArgument(normalizedData instanceof
+ // DataContainerNode<?>,"Node object %s, %s should be of type DataContainerNode",normalizedPath,normalizedData);
+ if (normalizedData instanceof DataContainerNode<?>) {
+ return toLegacyFromDataContainer((DataContainerNode<?>) normalizedData);
+ }
+ return null;
+ }
+
+ public static Node<?> toLegacy(final NormalizedNode<?, ?> node) {
+ if (node instanceof MixinNode) {
+ /**
+ * Direct reading of MixinNodes is not supported,
+ * since it is not possible in legacy APIs create pointer
+ * to Mixin Nodes.
+ *
+ */
+ return null;
+ }
+
+ if (node instanceof DataContainerNode<?>) {
+ return toLegacyFromDataContainer((DataContainerNode<?>) node);
+ }
+ return toLegacySimple(node);
+
+ }
+
+ private static SimpleNode<?> toLegacySimple(final NormalizedNode<?, ?> node) {
+ return new SimpleNodeTOImpl<Object>(node.getNodeType(), null, node.getValue());
+ }
+
+ @SuppressWarnings({ "unchecked", "rawtypes" })
+ private static CompositeNode toLegacyFromDataContainer(final DataContainerNode<?> node) {
+ CompositeNodeBuilder<ImmutableCompositeNode> builder = ImmutableCompositeNode.builder();
+ builder.setQName(node.getNodeType());
+ for (NormalizedNode<?, ?> child : node.getValue()) {
+ if (child instanceof MixinNode && child instanceof NormalizedNodeContainer<?, ?, ?>) {
+ builder.addAll(toLegacyNodesFromMixin((NormalizedNodeContainer) child));
+ } else {
+ addToBuilder(builder, toLegacy(child));
+ }
+ }
+ return builder.toInstance();
+ }
+
+ private static void addToBuilder(final CompositeNodeBuilder<ImmutableCompositeNode> builder, final Node<?> legacy) {
+ if (legacy != null) {
+ builder.add(legacy);
+ }
+ }
+
+ @SuppressWarnings({ "rawtypes", "unchecked" })
+ private static Iterable<Node<?>> toLegacyNodesFromMixin(
+ final NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>> mixin) {
+ ArrayList<Node<?>> ret = new ArrayList<>();
+ for (NormalizedNode<?, ?> child : mixin.getValue()) {
+ if(child instanceof MixinNode && child instanceof NormalizedNodeContainer<?, ?, ?>) {
+ Iterables.addAll(ret,toLegacyNodesFromMixin((NormalizedNodeContainer) child));
+ } else {
+ ret.add(toLegacy(child));
+ }
+ }
+ return FluentIterable.from(ret).filter(new Predicate<Node<?>>() {
+
+ @Override
+ public boolean apply(final Node<?> input) {
+ return input != null;
+ }
+ });
+ }
+
+ public DataNormalizationOperation<?> getRootOperation() {
+ return operation;
+ }
+
+}
package org.opendaylight.controller.md.sal.dom.api;
import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
+import org.opendaylight.controller.sal.core.api.BrokerService;
import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
-public interface DOMDataBroker extends AsyncDataBroker<InstanceIdentifier, NormalizedNode<?, ?>, DOMDataChangeListener>{
+public interface DOMDataBroker extends AsyncDataBroker<InstanceIdentifier, NormalizedNode<?, ?>, DOMDataChangeListener>, BrokerService {
@Override
DOMDataReadTransaction newReadOnlyTransaction();
base "config:service-type";
config:java-class "org.opendaylight.controller.sal.core.api.data.DataProviderService";
}
+
+ identity dom-async-data-broker {
+ base "config:service-type";
+ config:java-class "org.opendaylight.controller.md.sal.dom.api.DOMDataBroker";
+ }
identity dom-data-store {
base "config:service-type";
</scm>
<dependencies>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ </dependency>
<dependency>
<groupId>org.opendaylight.controller</groupId>
<artifactId>sal-core-api</artifactId>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-simple</artifactId>
+ <version>${slf4j.version}</version>
+ <scope>test</scope>
+ </dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<Private-Package>
org.opendaylight.controller.sal.dom.broker,
org.opendaylight.controller.sal.dom.broker.impl,
+ org.opendaylight.controller.sal.dom.broker.impl.*,
org.opendaylight.controller.sal.dom.broker.osgi,
org.opendaylight.controller.sal.dom.broker.util,
org.opendaylight.controller.config.yang.md.sal.dom.impl,
org.opendaylight.controller.config.yang.md.sal.dom.statistics,
+ org.opendaylight.controller.md.sal.dom.broker.impl,
+ org.opendaylight.controller.md.sal.dom.broker.impl.*,
+ org.opendaylight.controller.md.sal.dom.store.impl,
+ org.opendaylight.controller.md.sal.dom.store.impl.*,
org.opendaylight.yangtools.yang.util,
org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.dom.impl.rev131028.*
</Private-Package>
*/
package org.opendaylight.controller.config.yang.md.sal.dom.impl;
-import org.opendaylight.controller.config.yang.md.sal.dom.statistics.DomBrokerRuntimeMXBeanImpl;
+import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
import org.opendaylight.controller.sal.core.api.data.DataStore;
import org.opendaylight.controller.sal.dom.broker.BrokerConfigActivator;
import org.opendaylight.controller.sal.dom.broker.BrokerImpl;
import org.osgi.framework.BundleContext;
-import static com.google.common.base.Preconditions.*;
-
/**
*
*/
private BundleContext bundleContext;
- public DomBrokerImplModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier, org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
+ public DomBrokerImplModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier, final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
super(identifier, dependencyResolver);
}
- public DomBrokerImplModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier, org.opendaylight.controller.config.api.DependencyResolver dependencyResolver, DomBrokerImplModule oldModule, java.lang.AutoCloseable oldInstance) {
+ public DomBrokerImplModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier, final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver, final DomBrokerImplModule oldModule, final java.lang.AutoCloseable oldInstance) {
super(identifier, dependencyResolver, oldModule, oldInstance);
}
@Override
public void validate(){
super.validate();
- checkArgument(getDataStore() != null, "Data Store needs to be provided for DomBroker");
}
-
+
@Override
public java.lang.AutoCloseable createInstance() {
final BrokerImpl broker = new BrokerImpl();
final BrokerConfigActivator activator = new BrokerConfigActivator();
final DataStore store = getDataStoreDependency();
- activator.start(broker, store, getBundleContext());
-
- final DomBrokerImplRuntimeMXBean domBrokerRuntimeMXBean = new DomBrokerRuntimeMXBeanImpl(activator.getDataService());
- getRootRuntimeBeanRegistratorWrapper().register(domBrokerRuntimeMXBean);
+ final DOMDataBroker asyncBroker= getAsyncDataBrokerDependency();
+
+ activator.start(broker, store, asyncBroker,getBundleContext());
+
+// final DomBrokerImplRuntimeMXBean domBrokerRuntimeMXBean = new DomBrokerRuntimeMXBeanImpl(activator.getDataService());
+// getRootRuntimeBeanRegistratorWrapper().register(domBrokerRuntimeMXBean);
return broker;
}
return this.bundleContext;
}
- public void setBundleContext(BundleContext bundleContext) {
+ public void setBundleContext(final BundleContext bundleContext) {
this.bundleContext = bundleContext;
}
}
--- /dev/null
+/**
+ * Generated file
+
+ * Generated from: yang module name: opendaylight-sal-dom-broker-impl yang module local name: dom-inmemory-data-broker
+ * Generated by: org.opendaylight.controller.config.yangjmxgenerator.plugin.JMXGenerator
+ * Generated at: Fri Mar 28 17:32:48 CET 2014
+ *
+ * Do not modify this file unless it is present under src/main directory
+ */
+package org.opendaylight.controller.config.yang.md.sal.dom.impl;
+
+import java.util.Hashtable;
+import java.util.concurrent.Executors;
+
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
+import org.opendaylight.controller.md.sal.dom.broker.impl.DOMDataBrokerImpl;
+import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
+import org.opendaylight.controller.sal.core.spi.data.DOMStore;
+import org.osgi.framework.BundleContext;
+
+import com.google.common.collect.ImmutableMap;
+import com.google.common.util.concurrent.ListeningExecutorService;
+import com.google.common.util.concurrent.MoreExecutors;
+
+/**
+*
+*/
+public final class DomInmemoryDataBrokerModule extends
+ org.opendaylight.controller.config.yang.md.sal.dom.impl.AbstractDomInmemoryDataBrokerModule {
+
+ private BundleContext bundleContext;
+
+ public DomInmemoryDataBrokerModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier,
+ final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
+ super(identifier, dependencyResolver);
+ }
+
+ public DomInmemoryDataBrokerModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier,
+ final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver,
+ final DomInmemoryDataBrokerModule oldModule, final java.lang.AutoCloseable oldInstance) {
+
+ super(identifier, dependencyResolver, oldModule, oldInstance);
+ }
+
+ @Override
+ protected void customValidation() {
+ // Add custom validation for module attributes here.
+ }
+
+ @Override
+ public java.lang.AutoCloseable createInstance() {
+ ListeningExecutorService storeExecutor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(2));
+ InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("DOM-OPER", storeExecutor);
+ InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("DOM-CFG", storeExecutor);
+ ImmutableMap<LogicalDatastoreType, DOMStore> datastores = ImmutableMap
+ .<LogicalDatastoreType, DOMStore> builder().put(LogicalDatastoreType.OPERATIONAL, operStore)
+ .put(LogicalDatastoreType.CONFIGURATION, configStore).build();
+
+ DOMDataBrokerImpl newDataBroker = new DOMDataBrokerImpl(datastores, MoreExecutors.sameThreadExecutor());
+
+ getBundleContext().registerService(DOMDataBroker.class, newDataBroker, new Hashtable<String, String>());
+
+ getSchemaServiceDependency().registerSchemaServiceListener(operStore);
+ getSchemaServiceDependency().registerSchemaServiceListener(configStore);
+
+ return newDataBroker;
+ }
+
+ private BundleContext getBundleContext() {
+ return bundleContext;
+ }
+
+ void setBundleContext(final BundleContext ctx) {
+ bundleContext = ctx;
+ }
+}
--- /dev/null
+/**
+* Generated file
+
+* Generated from: yang module name: opendaylight-sal-dom-broker-impl yang module local name: dom-inmemory-data-broker
+* Generated by: org.opendaylight.controller.config.yangjmxgenerator.plugin.JMXGenerator
+* Generated at: Fri Mar 28 17:32:48 CET 2014
+*
+* Do not modify this file unless it is present under src/main directory
+*/
+package org.opendaylight.controller.config.yang.md.sal.dom.impl;
+
+import org.opendaylight.controller.config.api.DependencyResolver;
+import org.opendaylight.controller.config.api.DynamicMBeanWithInstance;
+import org.opendaylight.controller.config.spi.Module;
+import org.osgi.framework.BundleContext;
+
+/**
+*
+*/
+public class DomInmemoryDataBrokerModuleFactory extends org.opendaylight.controller.config.yang.md.sal.dom.impl.AbstractDomInmemoryDataBrokerModuleFactory
+{
+
+
+
+ @Override
+ public Module createModule(final String instanceName, final DependencyResolver dependencyResolver, final BundleContext bundleContext) {
+ DomInmemoryDataBrokerModule module = (DomInmemoryDataBrokerModule) super.createModule(instanceName, dependencyResolver, bundleContext);
+ module.setBundleContext(bundleContext);
+ return module;
+ }
+
+ @Override
+ public Module createModule(final String instanceName, final DependencyResolver dependencyResolver,
+ final DynamicMBeanWithInstance old, final BundleContext bundleContext) throws Exception {
+ DomInmemoryDataBrokerModule module = (DomInmemoryDataBrokerModule) super.createModule(instanceName, dependencyResolver, old, bundleContext);
+ module.setBundleContext(bundleContext);
+ return module;
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.md.sal.dom.broker.impl;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static com.google.common.base.Preconditions.checkState;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.Map.Entry;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.atomic.AtomicLong;
+
+import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
+import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
+import org.opendaylight.controller.md.sal.dom.api.DOMDataChangeListener;
+import org.opendaylight.controller.md.sal.dom.api.DOMDataReadTransaction;
+import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
+import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
+import org.opendaylight.controller.sal.common.util.Rpcs;
+import org.opendaylight.controller.sal.core.spi.data.DOMStore;
+import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
+import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
+import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
+import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransaction;
+import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
+import org.opendaylight.yangtools.concepts.ListenerRegistration;
+import org.opendaylight.yangtools.yang.common.RpcError;
+import org.opendaylight.yangtools.yang.common.RpcResult;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.Function;
+import com.google.common.base.Optional;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableList.Builder;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.ListeningExecutorService;
+
+public class DOMDataBrokerImpl implements DOMDataBroker, AutoCloseable {
+
+ private static final Logger LOG = LoggerFactory.getLogger(DOMDataBrokerImpl.class);
+ private static final Logger COORDINATOR_LOG = LoggerFactory.getLogger(CommitCoordination.class);
+ private final ImmutableMap<LogicalDatastoreType, DOMStore> datastores;
+ private final ListeningExecutorService executor;
+ private final AtomicLong txNum = new AtomicLong();
+
+ public DOMDataBrokerImpl(final ImmutableMap<LogicalDatastoreType, DOMStore> datastores,
+ final ListeningExecutorService executor) {
+ super();
+ this.datastores = datastores;
+ this.executor = executor;
+ }
+
+ private static final Function<Iterable<Boolean>, Boolean> AND_FUNCTION = new Function<Iterable<Boolean>, Boolean>() {
+
+ @Override
+ public Boolean apply(final Iterable<Boolean> input) {
+
+ for (Boolean value : input) {
+ if (value == false) {
+ return Boolean.FALSE;
+ }
+ }
+ return Boolean.TRUE;
+ }
+ };
+
+ @Override
+ public DOMDataReadTransaction newReadOnlyTransaction() {
+ ImmutableMap.Builder<LogicalDatastoreType, DOMStoreReadTransaction> builder = ImmutableMap.builder();
+ for (Entry<LogicalDatastoreType, DOMStore> store : datastores.entrySet()) {
+ builder.put(store.getKey(), store.getValue().newReadOnlyTransaction());
+ }
+ return new ReadOnlyTransactionImpl(newTransactionIdentifier(), builder.build());
+ }
+
+ private Object newTransactionIdentifier() {
+ return "DOM-" + txNum.getAndIncrement();
+ }
+
+ @Override
+ public DOMDataReadWriteTransaction newReadWriteTransaction() {
+ ImmutableMap.Builder<LogicalDatastoreType, DOMStoreReadWriteTransaction> builder = ImmutableMap.builder();
+ for (Entry<LogicalDatastoreType, DOMStore> store : datastores.entrySet()) {
+ builder.put(store.getKey(), store.getValue().newReadWriteTransaction());
+ }
+ return new ReadWriteTransactionImpl(newTransactionIdentifier(), builder.build(), this);
+ }
+
+ @Override
+ public DOMDataWriteTransaction newWriteOnlyTransaction() {
+ ImmutableMap.Builder<LogicalDatastoreType, DOMStoreWriteTransaction> builder = ImmutableMap.builder();
+ for (Entry<LogicalDatastoreType, DOMStore> store : datastores.entrySet()) {
+ builder.put(store.getKey(), store.getValue().newWriteOnlyTransaction());
+ }
+ return new WriteTransactionImpl<DOMStoreWriteTransaction>(newTransactionIdentifier(), builder.build(), this);
+ }
+
+ @Override
+ public ListenerRegistration<DOMDataChangeListener> registerDataChangeListener(final LogicalDatastoreType store,
+ final InstanceIdentifier path, final DOMDataChangeListener listener, final DataChangeScope triggeringScope) {
+
+ DOMStore potentialStore = datastores.get(store);
+ checkState(potentialStore != null, "Requested logical data store is not available.");
+ return potentialStore.registerChangeListener(path, listener, triggeringScope);
+ }
+
+ private ListenableFuture<RpcResult<TransactionStatus>> submit(
+ final WriteTransactionImpl<? extends DOMStoreWriteTransaction> transaction) {
+ LOG.debug("Tx: {} is submitted for execution.",transaction.getIdentifier());
+ return executor.submit(new CommitCoordination(transaction));
+ }
+
+ private abstract static class AbstractCompositeTransaction<K, T extends DOMStoreTransaction> implements
+ AsyncTransaction<InstanceIdentifier, NormalizedNode<?, ?>> {
+
+ private final ImmutableMap<K, T> backingTxs;
+ private final Object identifier;
+
+ protected AbstractCompositeTransaction(final Object identifier, final ImmutableMap<K, T> backingTxs) {
+ this.identifier = checkNotNull(identifier, "Identifier should not be null");
+ this.backingTxs = checkNotNull(backingTxs, "Backing transactions should not be null");
+ }
+
+ protected T getSubtransaction(final K key) {
+ return backingTxs.get(key);
+ }
+
+ public Iterable<T> getSubtransactions() {
+ return backingTxs.values();
+ }
+
+ @Override
+ public Object getIdentifier() {
+ return identifier;
+ }
+
+ @Override
+ public void close() {
+ try {
+ for (T subtransaction : backingTxs.values()) {
+ subtransaction.close();
+ }
+ } catch (Exception e) {
+ throw new IllegalStateException("Uncaught exception occured during closing transaction.", e);
+ }
+ }
+
+ }
+
+ private static class ReadOnlyTransactionImpl extends
+ AbstractCompositeTransaction<LogicalDatastoreType, DOMStoreReadTransaction> implements
+ DOMDataReadTransaction {
+
+ protected ReadOnlyTransactionImpl(final Object identifier,
+ final ImmutableMap<LogicalDatastoreType, DOMStoreReadTransaction> backingTxs) {
+ super(identifier, backingTxs);
+ }
+
+ @Override
+ public ListenableFuture<Optional<NormalizedNode<?, ?>>> read(final LogicalDatastoreType store,
+ final InstanceIdentifier path) {
+ return getSubtransaction(store).read(path);
+ }
+
+ }
+
+ private static class WriteTransactionImpl<T extends DOMStoreWriteTransaction> extends
+ AbstractCompositeTransaction<LogicalDatastoreType, T> implements DOMDataWriteTransaction {
+
+ private final DOMDataBrokerImpl broker;
+ private ImmutableList<DOMStoreThreePhaseCommitCohort> cohorts;
+
+ protected WriteTransactionImpl(final Object identifier, final ImmutableMap<LogicalDatastoreType, T> backingTxs,
+ final DOMDataBrokerImpl broker) {
+ super(identifier, backingTxs);
+ this.broker = broker;
+ }
+
+ public Iterable<DOMStoreThreePhaseCommitCohort> ready() {
+ checkState(cohorts == null, "Transaction was already marked as ready.");
+ ImmutableList.Builder<DOMStoreThreePhaseCommitCohort> cohortsBuilder = ImmutableList.builder();
+ for (DOMStoreWriteTransaction subTx : getSubtransactions()) {
+ cohortsBuilder.add(subTx.ready());
+ }
+ cohorts = cohortsBuilder.build();
+ return cohorts;
+ }
+
+ protected ImmutableList<DOMStoreThreePhaseCommitCohort> getCohorts() {
+ return cohorts;
+ }
+
+ @Override
+ public void put(final LogicalDatastoreType store, final InstanceIdentifier path, final NormalizedNode<?, ?> data) {
+ getSubtransaction(store).write(path, data);
+ }
+
+ @Override
+ public void delete(final LogicalDatastoreType store, final InstanceIdentifier path) {
+ getSubtransaction(store).delete(path);
+ }
+
+ @Override
+ public void merge(final LogicalDatastoreType store, final InstanceIdentifier path,
+ final NormalizedNode<?, ?> data) {
+ // TODO Auto-generated method stub
+ throw new UnsupportedOperationException("Not implemented yet.");
+ }
+
+ @Override
+ public void cancel() {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public ListenableFuture<RpcResult<TransactionStatus>> commit() {
+
+ ready();
+ return broker.submit(this);
+ }
+
+ }
+
+ private static class ReadWriteTransactionImpl extends WriteTransactionImpl<DOMStoreReadWriteTransaction> implements
+ DOMDataReadWriteTransaction {
+
+ protected ReadWriteTransactionImpl(final Object identifier,
+ final ImmutableMap<LogicalDatastoreType, DOMStoreReadWriteTransaction> backingTxs,
+ final DOMDataBrokerImpl broker) {
+ // super(identifier, backingTxs);
+ super(identifier, backingTxs, broker);
+ }
+
+ @Override
+ public ListenableFuture<Optional<NormalizedNode<?, ?>>> read(final LogicalDatastoreType store,
+ final InstanceIdentifier path) {
+ return getSubtransaction(store).read(path);
+ }
+
+ @Override
+ public void merge(final LogicalDatastoreType store, final InstanceIdentifier path, final NormalizedNode<?, ?> data) {
+
+ }
+ }
+
+ private final class CommitCoordination implements Callable<RpcResult<TransactionStatus>> {
+
+ private final WriteTransactionImpl<? extends DOMStoreWriteTransaction> transaction;
+
+ public CommitCoordination(final WriteTransactionImpl<? extends DOMStoreWriteTransaction> transaction) {
+ this.transaction = transaction;
+ }
+
+ @Override
+ public RpcResult<TransactionStatus> call() throws Exception {
+
+ Boolean canCommit = canCommit().get();
+ try {
+ if (canCommit) {
+ try {
+ preCommit().get();
+ try {
+ commit().get();
+ COORDINATOR_LOG.debug("Tx: {} Is commited.",transaction.getIdentifier());
+ return Rpcs.getRpcResult(true, TransactionStatus.COMMITED, Collections.<RpcError>emptySet());
+ } catch (InterruptedException | ExecutionException e) {
+ COORDINATOR_LOG.error("Tx: {} Error during commit", transaction.getIdentifier(), e);
+ }
+
+ } catch (InterruptedException | ExecutionException e) {
+ COORDINATOR_LOG.warn("Tx: {} Error during preCommit, starting Abort",
+ transaction.getIdentifier(), e);
+ }
+ } else {
+ COORDINATOR_LOG.info("Tx: {} Did not pass canCommit phase.");
+ abort().get();
+ }
+ } catch (InterruptedException | ExecutionException e) {
+ COORDINATOR_LOG.warn("Tx: {} Error during canCommit, starting Abort", transaction.getIdentifier(), e);
+
+ }
+ try {
+ abort().get();
+ } catch (InterruptedException | ExecutionException e) {
+ COORDINATOR_LOG.error("Tx: {} Error during abort", transaction.getIdentifier(), e);
+ }
+ return Rpcs.getRpcResult(false, TransactionStatus.FAILED, Collections.<RpcError>emptySet());
+ }
+
+ public ListenableFuture<Void> preCommit() {
+ COORDINATOR_LOG.debug("Transaction {}: PreCommit Started ", transaction.getIdentifier());
+ Builder<ListenableFuture<Void>> ops = ImmutableList.builder();
+ for (DOMStoreThreePhaseCommitCohort cohort : transaction.getCohorts()) {
+ ops.add(cohort.preCommit());
+ }
+ return (ListenableFuture) Futures.allAsList(ops.build());
+ }
+
+ public ListenableFuture<Void> commit() {
+ COORDINATOR_LOG.debug("Transaction {}: Commit Started ", transaction.getIdentifier());
+ Builder<ListenableFuture<Void>> ops = ImmutableList.builder();
+ for (DOMStoreThreePhaseCommitCohort cohort : transaction.getCohorts()) {
+ ops.add(cohort.commit());
+ }
+ return (ListenableFuture) Futures.allAsList(ops.build());
+ }
+
+ public ListenableFuture<Boolean> canCommit() {
+ COORDINATOR_LOG.debug("Transaction {}: CanCommit Started ", transaction.getIdentifier());
+ Builder<ListenableFuture<Boolean>> canCommitOperations = ImmutableList.builder();
+ for (DOMStoreThreePhaseCommitCohort cohort : transaction.getCohorts()) {
+ canCommitOperations.add(cohort.canCommit());
+ }
+ ListenableFuture<List<Boolean>> allCanCommits = Futures.allAsList(canCommitOperations.build());
+ return Futures.transform(allCanCommits, AND_FUNCTION);
+ }
+
+ public ListenableFuture<Void> abort() {
+ COORDINATOR_LOG.debug("Transaction {}: Abort Started ", transaction.getIdentifier());
+ Builder<ListenableFuture<Void>> ops = ImmutableList.builder();
+ for (DOMStoreThreePhaseCommitCohort cohort : transaction.getCohorts()) {
+ ops.add(cohort.abort());
+ }
+ return (ListenableFuture) Futures.allAsList(ops.build());
+ };
+
+ }
+
+ @Override
+ public void close() throws Exception {
+
+ }
+
+}
--- /dev/null
+package org.opendaylight.controller.md.sal.dom.broker.impl.compat;
+
+import org.opendaylight.controller.md.sal.common.api.RegistrationListener;
+import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
+import org.opendaylight.controller.md.sal.common.api.data.DataChangeEvent;
+import org.opendaylight.controller.md.sal.common.api.data.DataCommitHandler;
+import org.opendaylight.controller.md.sal.common.api.data.DataCommitHandlerRegistration;
+import org.opendaylight.controller.md.sal.common.api.data.DataReader;
+import org.opendaylight.controller.md.sal.common.impl.util.compat.DataNormalizer;
+import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
+import org.opendaylight.controller.md.sal.dom.api.DOMDataChangeListener;
+import org.opendaylight.controller.sal.common.DataStoreIdentifier;
+import org.opendaylight.controller.sal.core.api.data.DataChangeListener;
+import org.opendaylight.controller.sal.core.api.data.DataModificationTransaction;
+import org.opendaylight.controller.sal.core.api.data.DataProviderService;
+import org.opendaylight.controller.sal.core.api.data.DataValidator;
+import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
+import org.opendaylight.yangtools.concepts.Delegator;
+import org.opendaylight.yangtools.concepts.ListenerRegistration;
+import org.opendaylight.yangtools.concepts.Registration;
+import org.opendaylight.yangtools.concepts.util.ListenerRegistry;
+import org.opendaylight.yangtools.yang.data.api.CompositeNode;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
+
+public class BackwardsCompatibleDataBroker implements DataProviderService, SchemaContextListener {
+
+ DOMDataBroker backingBroker;
+ DataNormalizer normalizer;
+ private final ListenerRegistry<DataChangeListener> fakeRegistry = ListenerRegistry.create();
+
+
+ public BackwardsCompatibleDataBroker(final DOMDataBroker newBiDataImpl) {
+ backingBroker = newBiDataImpl;
+ }
+
+ @Override
+ public void onGlobalContextUpdated(final SchemaContext ctx) {
+ normalizer = new DataNormalizer(ctx);
+ }
+
+ @Override
+ public CompositeNode readConfigurationData(final InstanceIdentifier legacyPath) {
+ BackwardsCompatibleTransaction<?> tx = BackwardsCompatibleTransaction.readOnlyTransaction(backingBroker.newReadOnlyTransaction(),normalizer);
+ try {
+ return tx.readConfigurationData(legacyPath);
+ } finally {
+ tx.commit();
+ }
+ }
+
+ @Override
+ public CompositeNode readOperationalData(final InstanceIdentifier legacyPath) {
+ BackwardsCompatibleTransaction<?> tx = BackwardsCompatibleTransaction.readOnlyTransaction(backingBroker.newReadOnlyTransaction(),normalizer);
+ try {
+ return tx.readOperationalData(legacyPath);
+ } finally {
+ tx.commit();
+ }
+ }
+
+ @Override
+ public DataModificationTransaction beginTransaction() {
+ return BackwardsCompatibleTransaction.readWriteTransaction(backingBroker.newReadWriteTransaction(), normalizer);
+ }
+
+ @Override
+ public ListenerRegistration<DataChangeListener> registerDataChangeListener(final InstanceIdentifier path,
+ final DataChangeListener listener) {
+ return fakeRegistry .register(listener);
+ }
+
+ @Override
+ public Registration<DataCommitHandler<InstanceIdentifier, CompositeNode>> registerCommitHandler(
+ final InstanceIdentifier path, final DataCommitHandler<InstanceIdentifier, CompositeNode> commitHandler) {
+ // FIXME Do real forwarding
+ return new AbstractObjectRegistration<DataCommitHandler<InstanceIdentifier,CompositeNode>>(commitHandler) {
+ @Override
+ protected void removeRegistration() {
+ // NOOP
+ }
+ };
+ }
+
+ @Override
+ public ListenerRegistration<RegistrationListener<DataCommitHandlerRegistration<InstanceIdentifier, CompositeNode>>> registerCommitHandlerListener(
+ final RegistrationListener<DataCommitHandlerRegistration<InstanceIdentifier, CompositeNode>> commitHandlerListener) {
+ return null;
+ }
+
+ // Obsolote functionality
+
+ @Override
+ public void addValidator(final DataStoreIdentifier store, final DataValidator validator) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void removeValidator(final DataStoreIdentifier store, final DataValidator validator) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void addRefresher(final DataStoreIdentifier store, final DataRefresher refresher) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void removeRefresher(final DataStoreIdentifier store, final DataRefresher refresher) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public Registration<DataReader<InstanceIdentifier, CompositeNode>> registerConfigurationReader(
+ final InstanceIdentifier path, final DataReader<InstanceIdentifier, CompositeNode> reader) {
+ throw new UnsupportedOperationException("Data Reader contract is not supported.");
+ }
+
+ @Override
+ public Registration<DataReader<InstanceIdentifier, CompositeNode>> registerOperationalReader(
+ final InstanceIdentifier path, final DataReader<InstanceIdentifier, CompositeNode> reader) {
+ throw new UnsupportedOperationException("Data Reader contract is not supported.");
+ }
+
+ private final class TranslatingListenerInvoker implements DOMDataChangeListener, Delegator<DataChangeListener> {
+
+
+ private DataChangeListener delegate;
+
+ @Override
+ public void onDataChanged(final AsyncDataChangeEvent<InstanceIdentifier, NormalizedNode<?, ?>> normalizedChange) {
+
+ DataChangeEvent<InstanceIdentifier, CompositeNode> legacyChange = null;
+ delegate.onDataChanged(legacyChange);
+ }
+
+ @Override
+ public DataChangeListener getDelegate() {
+
+ return delegate;
+ }
+
+
+ }
+
+}
--- /dev/null
+package org.opendaylight.controller.md.sal.dom.broker.impl.compat;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+
+import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.controller.md.sal.common.impl.util.compat.DataNormalizationOperation;
+import org.opendaylight.controller.md.sal.common.impl.util.compat.DataNormalizer;
+import org.opendaylight.controller.md.sal.dom.api.DOMDataReadTransaction;
+import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
+import org.opendaylight.controller.sal.core.api.data.DataModificationTransaction;
+import org.opendaylight.yangtools.concepts.Delegator;
+import org.opendaylight.yangtools.concepts.ListenerRegistration;
+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.InstanceIdentifier.AugmentationIdentifier;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
+import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
+import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
+import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
+import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Iterables;
+import com.google.common.util.concurrent.ListenableFuture;
+
+public abstract class BackwardsCompatibleTransaction<T extends DOMDataReadTransaction> implements
+ DataModificationTransaction, Delegator<T> {
+
+ private static final Logger LOG = LoggerFactory.getLogger(BackwardsCompatibleTransaction.class);
+
+ private final T asyncTx;
+ private final DataNormalizer normalizer;
+
+ protected BackwardsCompatibleTransaction(final T asyncTx, final DataNormalizer normalizer) {
+ super();
+ this.asyncTx = asyncTx;
+ this.normalizer = normalizer;
+ }
+
+ public static BackwardsCompatibleTransaction<?> readOnlyTransaction(final DOMDataReadTransaction readTx,
+ final DataNormalizer normalizer) {
+
+ return new BackwardsCompatibleTransaction<DOMDataReadTransaction>(readTx, normalizer) {
+
+ @Override
+ public TransactionStatus getStatus() {
+ return TransactionStatus.NEW;
+ }
+
+ @Override
+ public Future<RpcResult<TransactionStatus>> commit() {
+ getDelegate().close();
+ return null;
+ }
+ };
+ }
+
+ public static BackwardsCompatibleTransaction<?> readWriteTransaction(final DOMDataReadWriteTransaction rwTx,
+ final DataNormalizer normalizer) {
+ return new ReadWriteTransaction(rwTx, normalizer);
+ }
+
+ protected DataNormalizer getNormalizer() {
+ return normalizer;
+ }
+
+ @Override
+ public T getDelegate() {
+ return asyncTx;
+ };
+
+ @Override
+ public CompositeNode readConfigurationData(final InstanceIdentifier legacyPath) {
+
+ InstanceIdentifier normalizedPath = normalizer.toNormalized(legacyPath);
+
+ ListenableFuture<Optional<NormalizedNode<?, ?>>> normalizedData = asyncTx.read(
+ LogicalDatastoreType.CONFIGURATION, normalizedPath);
+
+ try {
+ return normalizer.toLegacy(normalizedPath, normalizedData.get().orNull());
+ } catch (InterruptedException | ExecutionException e) {
+ return null;
+ }
+ }
+
+ @Override
+ public CompositeNode readOperationalData(final InstanceIdentifier legacyPath) {
+ InstanceIdentifier normalizedPath = normalizer.toNormalized(legacyPath);
+
+ ListenableFuture<Optional<NormalizedNode<?, ?>>> normalizedData = asyncTx.read(
+ LogicalDatastoreType.OPERATIONAL, normalizedPath);
+
+ try {
+ return normalizer.toLegacy(normalizedPath, normalizedData.get().orNull());
+ } catch (InterruptedException | ExecutionException e) {
+ return null;
+ }
+ }
+
+ @Override
+ public ListenerRegistration<DataTransactionListener> registerListener(final DataTransactionListener listener) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public Map<InstanceIdentifier, CompositeNode> getCreatedConfigurationData() {
+ return Collections.emptyMap();
+ }
+
+ @Override
+ public Map<InstanceIdentifier, CompositeNode> getCreatedOperationalData() {
+ return Collections.emptyMap();
+ }
+
+ @Override
+ public Map<InstanceIdentifier, CompositeNode> getOriginalConfigurationData() {
+ return Collections.emptyMap();
+ }
+
+ @Override
+ public Map<InstanceIdentifier, CompositeNode> getOriginalOperationalData() {
+ return Collections.emptyMap();
+ }
+
+ @Override
+ public Set<InstanceIdentifier> getRemovedConfigurationData() {
+ return Collections.emptySet();
+ }
+
+ @Override
+ public Set<InstanceIdentifier> getRemovedOperationalData() {
+ return Collections.emptySet();
+ }
+
+ @Override
+ public Map<InstanceIdentifier, CompositeNode> getUpdatedConfigurationData() {
+ return Collections.emptyMap();
+ }
+
+ @Override
+ public Map<InstanceIdentifier, CompositeNode> getUpdatedOperationalData() {
+ return Collections.emptyMap();
+ }
+
+ @Override
+ public void putConfigurationData(final InstanceIdentifier path, final CompositeNode data) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void putOperationalData(final InstanceIdentifier path, final CompositeNode data) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void removeConfigurationData(final InstanceIdentifier path) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void removeOperationalData(final InstanceIdentifier path) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public Object getIdentifier() {
+ return asyncTx.getIdentifier();
+ }
+
+ private static final class ReadWriteTransaction extends BackwardsCompatibleTransaction<DOMDataReadWriteTransaction> {
+
+ private TransactionStatus status = TransactionStatus.NEW;
+
+ protected ReadWriteTransaction(final DOMDataReadWriteTransaction asyncTx, final DataNormalizer normalizer) {
+ super(asyncTx, normalizer);
+ }
+
+ @Override
+ public TransactionStatus getStatus() {
+ return status;
+ }
+
+ @Override
+ public Future<RpcResult<TransactionStatus>> commit() {
+ Preconditions.checkState(status == TransactionStatus.NEW);
+ status = TransactionStatus.SUBMITED;
+ return getDelegate().commit();
+ }
+
+ @Override
+ public void putConfigurationData(final InstanceIdentifier legacyPath, final CompositeNode legacyData) {
+ checkNotNull(legacyPath, "Path MUST NOT be null.");
+ checkNotNull(legacyData, "Data for path %s MUST NOT be null",legacyData);
+ Entry<InstanceIdentifier, NormalizedNode<?, ?>> normalizedData = getNormalizer().toNormalized(legacyPath, legacyData);
+ putWithEnsuredParents(LogicalDatastoreType.CONFIGURATION, normalizedData.getKey(), normalizedData.getValue());
+ }
+
+ @Override
+ public void putOperationalData(final InstanceIdentifier legacyPath, final CompositeNode legacyData) {
+ checkNotNull(legacyPath, "Path MUST NOT be null.");
+ checkNotNull(legacyData, "Data for path %s MUST NOT be null",legacyData);
+ Entry<InstanceIdentifier, NormalizedNode<?, ?>> normalizedData = getNormalizer().toNormalized(legacyPath, legacyData);
+ putWithEnsuredParents(LogicalDatastoreType.OPERATIONAL, normalizedData.getKey(), normalizedData.getValue());
+ }
+
+ private void putWithEnsuredParents(final LogicalDatastoreType store, final InstanceIdentifier normalizedPath,
+ final NormalizedNode<?, ?> normalizedData) {
+
+ LOG.trace("write {}:{} ",store,normalizedPath);
+ try {
+ List<PathArgument> currentArguments = new ArrayList<>();
+ DataNormalizationOperation<?> currentOp = getNormalizer().getRootOperation();
+ Iterator<PathArgument> iterator = normalizedPath.getPath().iterator();
+ while(iterator.hasNext()) {
+ PathArgument currentArg = iterator.next();
+ currentOp = currentOp.getChild(currentArg);
+ currentArguments.add(currentArg);
+ InstanceIdentifier currentPath = new InstanceIdentifier(currentArguments);
+ boolean isPresent = getDelegate().read(store, currentPath).get().isPresent();
+ if(isPresent == false && iterator.hasNext()) {
+ getDelegate().put(store, currentPath, currentOp.createDefault(currentArg));
+ }
+ }
+ } catch (InterruptedException | ExecutionException e) {
+ LOG.error("Exception durring read.",e);
+ }
+
+ getDelegate().put(store, normalizedPath, normalizedData);
+ }
+
+ private boolean isAugmentationChild(final InstanceIdentifier normalizedPath) {
+ List<PathArgument> parentArgs = parentPath(normalizedPath).getPath();
+ if(parentArgs.isEmpty()) {
+ return false;
+ }
+ return Iterables.getLast(parentArgs) instanceof AugmentationIdentifier;
+ }
+
+ private void ensureParentNode(final LogicalDatastoreType store, final InstanceIdentifier normalizedPath,
+ final NormalizedNode<?, ?> normalizedData) {
+ InstanceIdentifier parentPath = parentPath(normalizedPath);
+ PathArgument parentType = Iterables.getLast(parentPath.getPath());
+ if(parentType instanceof AugmentationIdentifier) {
+ AugmentationNode node = Builders.augmentationBuilder()
+ .withNodeIdentifier((AugmentationIdentifier) parentType)
+ .build();
+ getDelegate().put(store, parentPath, node);
+ }
+ if(normalizedData instanceof MapEntryNode) {
+ MapNode mapNode = Builders.mapBuilder().withNodeIdentifier(new NodeIdentifier(normalizedData.getNodeType())).build();
+ getDelegate().put(store, parentPath, mapNode);
+ } else if (normalizedData instanceof LeafSetNode<?>){
+ LeafSetNode<Object> leafNode = Builders.leafSetBuilder().withNodeIdentifier(new NodeIdentifier(normalizedData.getNodeType())).build();
+ getDelegate().put(store, parentPath, leafNode);
+ }
+
+
+ }
+
+ private InstanceIdentifier parentPath(final InstanceIdentifier normalizedPath) {
+ List<PathArgument> childArgs = normalizedPath.getPath();
+ return new InstanceIdentifier(childArgs.subList(0, childArgs.size() -1));
+ }
+
+ private boolean parentNodeDoesNotExists(final LogicalDatastoreType store, final InstanceIdentifier normalizedPath) {
+ try {
+ return !getDelegate().read(store, parentPath(normalizedPath)).get().isPresent();
+ } catch (InterruptedException | ExecutionException e) {
+ throw new IllegalStateException(e);
+ }
+ }
+
+ @Override
+ public void removeConfigurationData(final InstanceIdentifier legacyPath) {
+ checkNotNull(legacyPath, "Path MUST NOT be null.");
+ getDelegate().delete(LogicalDatastoreType.CONFIGURATION, getNormalizer().toNormalized(legacyPath));
+ }
+
+ @Override
+ public void removeOperationalData(final InstanceIdentifier legacyPath) {
+ checkNotNull(legacyPath, "Path MUST NOT be null.");
+ getDelegate().delete(LogicalDatastoreType.OPERATIONAL, getNormalizer().toNormalized(legacyPath));
+ }
+ }
+}
--- /dev/null
+package org.opendaylight.controller.md.sal.dom.store.impl;
+
+import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
+import org.opendaylight.controller.md.sal.dom.store.impl.tree.ListenerRegistrationNode.DataChangeListenerRegistration;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+class ChangeListenerNotifyTask implements Runnable {
+
+ private static final Logger LOG = LoggerFactory.getLogger(ChangeListenerNotifyTask.class);
+ private final Iterable<DataChangeListenerRegistration<?>> listeners;
+ private final AsyncDataChangeEvent<InstanceIdentifier, NormalizedNode<?, ?>> event;
+
+ public ChangeListenerNotifyTask(final Iterable<DataChangeListenerRegistration<?>> listeners,
+ final AsyncDataChangeEvent<InstanceIdentifier, NormalizedNode<?, ?>> event) {
+ this.listeners = listeners;
+ this.event = event;
+ }
+
+ @Override
+ public void run() {
+
+ for (DataChangeListenerRegistration<?> listener : listeners) {
+ try {
+ listener.getInstance().onDataChanged(event);
+ } catch (Exception e) {
+ LOG.error("Unhandled exception during invoking listener {} with event {}", listener, event, e);
+ }
+ }
+
+ }
+
+}
--- /dev/null
+package org.opendaylight.controller.md.sal.dom.store.impl;
+
+import java.util.Map;
+import java.util.Set;
+
+import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
+
+public final class DOMImmutableDataChangeEvent implements
+ AsyncDataChangeEvent<InstanceIdentifier, NormalizedNode<?, ?>> {
+
+ private final NormalizedNode<?, ?> original;
+ private final NormalizedNode<?, ?> updated;
+ private final Map<InstanceIdentifier, ? extends NormalizedNode<?, ?>> originalData;
+ private final Map<InstanceIdentifier, NormalizedNode<?, ?>> createdData;
+ private final Map<InstanceIdentifier, NormalizedNode<?, ?>> updatedData;
+ private final Set<InstanceIdentifier> removedPaths;
+
+ private DOMImmutableDataChangeEvent(final Builder change) {
+ original = change.before;
+ updated = change.after;
+ originalData = change.original.build();
+ createdData = change.created.build();
+ updatedData = change.updated.build();
+ removedPaths = change.removed.build();
+ }
+
+ public static final Builder builder() {
+ return new Builder();
+ }
+
+ @Override
+ public NormalizedNode<?, ?> getOriginalSubtree() {
+ return original;
+ }
+
+ @Override
+ public NormalizedNode<?, ?> getUpdatedSubtree() {
+ return updated;
+ }
+
+ @Override
+ public Map<InstanceIdentifier, ? extends NormalizedNode<?, ?>> getOriginalData() {
+ return originalData;
+ }
+
+ @Override
+ public Map<InstanceIdentifier, NormalizedNode<?, ?>> getCreatedData() {
+ return createdData;
+ }
+
+ @Override
+ public Map<InstanceIdentifier, NormalizedNode<?, ?>> getUpdatedData() {
+ return updatedData;
+ }
+
+ @Override
+ public Set<InstanceIdentifier> getRemovedPaths() {
+ return removedPaths;
+ }
+
+ public static class Builder {
+
+ private NormalizedNode<?, ?> after;
+ private NormalizedNode<?, ?> before;
+
+ private final ImmutableMap.Builder<InstanceIdentifier, NormalizedNode<?, ?>> original = ImmutableMap.builder();
+ private final ImmutableMap.Builder<InstanceIdentifier, NormalizedNode<?, ?>> created = ImmutableMap.builder();
+ private final ImmutableMap.Builder<InstanceIdentifier, NormalizedNode<?, ?>> updated = ImmutableMap.builder();
+ private final ImmutableSet.Builder<InstanceIdentifier> removed = ImmutableSet.builder();
+
+
+ private Builder() {
+
+ }
+
+ public Builder setAfter(final NormalizedNode<?, ?> node) {
+ after = node;
+ return this;
+ }
+
+ public DOMImmutableDataChangeEvent build() {
+
+ return new DOMImmutableDataChangeEvent(this);
+ }
+
+ public void merge(final DOMImmutableDataChangeEvent nestedChanges) {
+
+ original.putAll(nestedChanges.getOriginalData());
+ created.putAll(nestedChanges.getCreatedData());
+ updated.putAll(nestedChanges.getUpdatedData());
+ removed.addAll(nestedChanges.getRemovedPaths());
+
+ }
+
+ public Builder setBefore(final NormalizedNode<?, ?> node) {
+ this.before = node;
+ return this;
+ }
+
+ public Builder addCreated(final InstanceIdentifier path, final NormalizedNode<?, ?> node) {
+ created.put(path, node);
+ return this;
+ }
+
+ public Builder addRemoved(final InstanceIdentifier path, final NormalizedNode<?, ?> node) {
+ original.put(path, node);
+ removed.add(path);
+ return this;
+ }
+
+ public Builder addUpdated(final InstanceIdentifier path, final NormalizedNode<?, ?> before,
+ final NormalizedNode<?, ?> after) {
+ original.put(path, before);
+ updated.put(path, after);
+ return this;
+ }
+ }
+
+}
+
--- /dev/null
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.md.sal.dom.store.impl;
+
+import org.opendaylight.controller.md.sal.dom.store.impl.tree.StoreMetadataNode;
+import org.opendaylight.controller.md.sal.dom.store.impl.tree.TreeNodeUtils;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+
+import com.google.common.base.Optional;
+import com.google.common.primitives.UnsignedLong;
+
+class DataAndMetadataSnapshot {
+
+ private final StoreMetadataNode metadataTree;
+ private final Optional<SchemaContext> schemaContext;
+
+ private DataAndMetadataSnapshot(final StoreMetadataNode metadataTree, final Optional<SchemaContext> schemaCtx) {
+ this.metadataTree = metadataTree;
+ this.schemaContext = schemaCtx;
+ }
+
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ public static DataAndMetadataSnapshot createEmpty() {
+ return createEmpty(new NodeIdentifier(SchemaContext.NAME));
+ }
+
+
+ public static DataAndMetadataSnapshot createEmpty(final NodeIdentifier rootNode) {
+ NormalizedNode<?, ?> data = Builders.containerBuilder().withNodeIdentifier(rootNode).build();
+ StoreMetadataNode metadata = StoreMetadataNode.builder()
+ .setNodeVersion(UnsignedLong.ZERO)
+ .setSubtreeVersion(UnsignedLong.ZERO)
+ .setData(data)
+ .build();
+ return new DataAndMetadataSnapshot(metadata,Optional.<SchemaContext>absent());
+ }
+
+ public static DataAndMetadataSnapshot createEmpty(final SchemaContext ctx) {
+ NodeIdentifier rootNodeIdentifier = new NodeIdentifier(ctx.getQName());
+ NormalizedNode<?, ?> data = Builders.containerBuilder().withNodeIdentifier(rootNodeIdentifier).build();
+ StoreMetadataNode metadata = StoreMetadataNode.builder()
+ .setData(data)
+ .setNodeVersion(UnsignedLong.ZERO)
+ .setSubtreeVersion(UnsignedLong.ZERO)
+ .build();
+ return new DataAndMetadataSnapshot(metadata, Optional.of(ctx));
+ }
+
+ public Optional<SchemaContext> getSchemaContext() {
+ return schemaContext;
+ }
+
+ public NormalizedNode<?, ?> getDataTree() {
+ return metadataTree.getData();
+ }
+
+ public StoreMetadataNode getMetadataTree() {
+ return metadataTree;
+ }
+
+ public Optional<StoreMetadataNode> read(final InstanceIdentifier path) {
+ return TreeNodeUtils.findNode(metadataTree, path);
+ }
+
+ public static class Builder {
+ private NormalizedNode<?, ?> dataTree;
+ private StoreMetadataNode metadataTree;
+ private SchemaContext schemaContext;
+
+ public NormalizedNode<?, ?> getDataTree() {
+ return dataTree;
+ }
+
+ public Builder setMetadataTree(final StoreMetadataNode metadataTree) {
+ this.metadataTree = metadataTree;
+ return this;
+ }
+
+ public Builder setSchemaContext(final SchemaContext schemaContext) {
+ this.schemaContext = schemaContext;
+ return this;
+ }
+
+ public DataAndMetadataSnapshot build() {
+ return new DataAndMetadataSnapshot(metadataTree, Optional.fromNullable(schemaContext));
+ }
+
+ }
+}
--- /dev/null
+package org.opendaylight.controller.md.sal.dom.store.impl;
+
+import static org.opendaylight.controller.md.sal.dom.store.impl.DOMImmutableDataChangeEvent.builder;
+import static org.opendaylight.controller.md.sal.dom.store.impl.StoreUtils.append;
+import static org.opendaylight.controller.md.sal.dom.store.impl.tree.TreeNodeUtils.getChild;
+
+import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
+import org.opendaylight.controller.md.sal.dom.store.impl.DOMImmutableDataChangeEvent.Builder;
+import org.opendaylight.controller.md.sal.dom.store.impl.tree.ListenerRegistrationNode;
+import org.opendaylight.controller.md.sal.dom.store.impl.tree.NodeModification;
+import org.opendaylight.controller.md.sal.dom.store.impl.tree.StoreMetadataNode;
+import org.opendaylight.controller.md.sal.dom.store.impl.tree.ListenerRegistrationNode.DataChangeListenerRegistration;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+
+import com.google.common.base.Optional;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
+
+public class DataChangeEventResolver {
+
+ private static final DOMImmutableDataChangeEvent NO_CHANGE = builder().build();
+ private InstanceIdentifier rootPath;
+ private ListenerRegistrationNode listenerRoot;
+ private NodeModification modificationRoot;
+ private Optional<StoreMetadataNode> beforeRoot;
+ private Optional<StoreMetadataNode> afterRoot;
+ private final ImmutableList.Builder<ChangeListenerNotifyTask> tasks = ImmutableList.builder();
+
+ protected InstanceIdentifier getRootPath() {
+ return rootPath;
+ }
+
+ protected DataChangeEventResolver setRootPath(final InstanceIdentifier rootPath) {
+ this.rootPath = rootPath;
+ return this;
+ }
+
+ protected ListenerRegistrationNode getListenerRoot() {
+ return listenerRoot;
+ }
+
+ protected DataChangeEventResolver setListenerRoot(final ListenerRegistrationNode listenerRoot) {
+ this.listenerRoot = listenerRoot;
+ return this;
+ }
+
+ protected NodeModification getModificationRoot() {
+ return modificationRoot;
+ }
+
+ protected DataChangeEventResolver setModificationRoot(final NodeModification modificationRoot) {
+ this.modificationRoot = modificationRoot;
+ return this;
+ }
+
+ protected Optional<StoreMetadataNode> getBeforeRoot() {
+ return beforeRoot;
+ }
+
+ protected DataChangeEventResolver setBeforeRoot(final Optional<StoreMetadataNode> beforeRoot) {
+ this.beforeRoot = beforeRoot;
+ return this;
+ }
+
+ protected Optional<StoreMetadataNode> getAfterRoot() {
+ return afterRoot;
+ }
+
+ protected DataChangeEventResolver setAfterRoot(final Optional<StoreMetadataNode> afterRoot) {
+ this.afterRoot = afterRoot;
+ return this;
+ }
+
+ public Iterable<ChangeListenerNotifyTask> resolve() {
+ resolveAnyChangeEvent(rootPath, Optional.of(listenerRoot), modificationRoot, beforeRoot, afterRoot);
+ return tasks.build();
+ }
+
+ private DOMImmutableDataChangeEvent resolveAnyChangeEvent(final InstanceIdentifier path,
+ final Optional<ListenerRegistrationNode> listeners, final NodeModification modification,
+ final Optional<StoreMetadataNode> before, final Optional<StoreMetadataNode> after) {
+ // No listeners are present in listener registration subtree
+ // no before and after state is present
+ if (!before.isPresent() && !after.isPresent()) {
+ return NO_CHANGE;
+ }
+ switch (modification.getModificationType()) {
+ case SUBTREE_MODIFIED:
+ return resolveSubtreeChangeEvent(path, listeners, modification, before.get(), after.get());
+ case WRITE:
+ if (before.isPresent()) {
+ return resolveReplacedEvent(path, listeners, modification, before.get(), after.get());
+ } else {
+ return resolveCreateEvent(path, listeners, after.get());
+ }
+ case DELETE:
+ return resolveDeleteEvent(path, listeners, before.get());
+ default:
+ return NO_CHANGE;
+ }
+
+ }
+
+ /**
+ * Resolves create events deep down the interest listener tree.
+ *
+ *
+ * @param path
+ * @param listeners
+ * @param afterState
+ * @return
+ */
+ private DOMImmutableDataChangeEvent resolveCreateEvent(final InstanceIdentifier path,
+ final Optional<ListenerRegistrationNode> listeners, final StoreMetadataNode afterState) {
+ final NormalizedNode<?, ?> node = afterState.getData();
+ Builder builder = builder().setAfter(node).addCreated(path, node);
+
+ for (StoreMetadataNode child : afterState.getChildren()) {
+ PathArgument childId = child.getIdentifier();
+ Optional<ListenerRegistrationNode> childListeners = getChild(listeners, childId);
+
+ InstanceIdentifier childPath = StoreUtils.append(path, childId);
+ builder.merge(resolveCreateEvent(childPath, childListeners, child));
+ }
+ DOMImmutableDataChangeEvent event = builder.build();
+ if (listeners.isPresent()) {
+ addNotifyTask(listeners.get().getListeners(), event);
+ }
+ return event;
+ }
+
+ private DOMImmutableDataChangeEvent resolveDeleteEvent(final InstanceIdentifier path,
+ final Optional<ListenerRegistrationNode> listeners, final StoreMetadataNode beforeState) {
+ final NormalizedNode<?, ?> node = beforeState.getData();
+ Builder builder = builder().setBefore(node).addRemoved(path, node);
+
+ for (StoreMetadataNode child : beforeState.getChildren()) {
+ PathArgument childId = child.getIdentifier();
+ Optional<ListenerRegistrationNode> childListeners = getChild(listeners, childId);
+ InstanceIdentifier childPath = StoreUtils.append(path, childId);
+ builder.merge(resolveDeleteEvent(childPath, childListeners, child));
+ }
+ DOMImmutableDataChangeEvent event = builder.build();
+ if (listeners.isPresent()) {
+ addNotifyTask(listeners.get().getListeners(), event);
+ }
+ return event;
+
+ }
+
+ private DOMImmutableDataChangeEvent resolveSubtreeChangeEvent(final InstanceIdentifier path,
+ final Optional<ListenerRegistrationNode> listeners, final NodeModification modification,
+ final StoreMetadataNode before, final StoreMetadataNode after) {
+
+ Builder one = builder().setBefore(before.getData()).setAfter(after.getData());
+
+ Builder subtree = builder();
+
+ for (NodeModification childMod : modification.getModifications()) {
+ PathArgument childId = childMod.getIdentifier();
+ InstanceIdentifier childPath = append(path, childId);
+ Optional<ListenerRegistrationNode> childListen = getChild(listeners, childId);
+
+ Optional<StoreMetadataNode> childBefore = before.getChild(childId);
+ Optional<StoreMetadataNode> childAfter = after.getChild(childId);
+
+ switch (childMod.getModificationType()) {
+ case WRITE:
+ case DELETE:
+ one.merge(resolveAnyChangeEvent(childPath, childListen, childMod, childBefore, childBefore));
+ break;
+ case SUBTREE_MODIFIED:
+ subtree.merge(resolveSubtreeChangeEvent(childPath, childListen, childMod, childBefore.get(),
+ childAfter.get()));
+ break;
+ }
+ }
+ DOMImmutableDataChangeEvent oneChangeEvent = one.build();
+ subtree.merge(oneChangeEvent);
+ DOMImmutableDataChangeEvent subtreeEvent = subtree.build();
+ if (listeners.isPresent()) {
+ addNotifyTask(listeners.get(), DataChangeScope.ONE, oneChangeEvent);
+ addNotifyTask(listeners.get(), DataChangeScope.SUBTREE, subtreeEvent);
+ }
+ return subtreeEvent;
+ }
+
+ private DOMImmutableDataChangeEvent resolveReplacedEvent(final InstanceIdentifier path,
+ final Optional<ListenerRegistrationNode> listeners, final NodeModification modification,
+ final StoreMetadataNode before, final StoreMetadataNode after) {
+ // FIXME Add task
+ return builder().build();
+ }
+
+ private void addNotifyTask(final ListenerRegistrationNode listenerRegistrationNode, final DataChangeScope one,
+ final DOMImmutableDataChangeEvent event) {
+
+
+
+ }
+
+ private void addNotifyTask(final Iterable<DataChangeListenerRegistration<?>> listeners,
+ final DOMImmutableDataChangeEvent event) {
+ tasks .add(new ChangeListenerNotifyTask(ImmutableSet.copyOf(listeners),event));
+ }
+
+ public static DataChangeEventResolver create() {
+ return new DataChangeEventResolver();
+ }
+
+
+
+}
--- /dev/null
+package org.opendaylight.controller.md.sal.dom.store.impl;
+
+import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
+import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
+import org.opendaylight.yangtools.concepts.ListenerRegistration;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+
+public interface DataChangeListenerRegistration<L extends AsyncDataChangeListener<InstanceIdentifier, NormalizedNode<?, ?>>>
+extends ListenerRegistration<L> {
+
+
+ @Override
+ public L getInstance();
+
+ InstanceIdentifier getPath();
+
+ DataChangeScope getScope();
+
+
+
+}
--- /dev/null
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.md.sal.dom.store.impl;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static com.google.common.base.Preconditions.checkState;
+import static org.opendaylight.controller.md.sal.dom.store.impl.StoreUtils.increase;
+
+import java.util.concurrent.Callable;
+import java.util.concurrent.atomic.AtomicLong;
+
+import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
+import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
+import org.opendaylight.controller.md.sal.dom.store.impl.tree.ListenerRegistrationNode;
+import org.opendaylight.controller.md.sal.dom.store.impl.tree.ModificationType;
+import org.opendaylight.controller.md.sal.dom.store.impl.tree.NodeModification;
+import org.opendaylight.controller.md.sal.dom.store.impl.tree.StoreMetadataNode;
+import org.opendaylight.controller.md.sal.dom.store.impl.tree.TreeNodeUtils;
+import org.opendaylight.controller.sal.core.spi.data.DOMStore;
+import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
+import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
+import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
+import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
+import org.opendaylight.yangtools.concepts.Identifiable;
+import org.opendaylight.yangtools.concepts.ListenerRegistration;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeUtils;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.Optional;
+import com.google.common.primitives.UnsignedLong;
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.ListeningExecutorService;
+
+public class InMemoryDOMDataStore implements DOMStore, Identifiable<String>, SchemaContextListener {
+
+ private static final Logger LOG = LoggerFactory.getLogger(InMemoryDOMDataStore.class);
+ private static final InstanceIdentifier PUBLIC_ROOT_PATH = InstanceIdentifier.builder().build();
+
+
+ private final ListeningExecutorService executor;
+ private final String name;
+ private final AtomicLong txCounter = new AtomicLong(0);
+
+ private DataAndMetadataSnapshot snapshot;
+ private ModificationApplyOperation operationTree;
+ private final ListenerRegistrationNode listenerTree;
+
+
+
+ private SchemaContext schemaContext;
+
+ public InMemoryDOMDataStore(final String name, final ListeningExecutorService executor) {
+ this.executor = executor;
+ this.name = name;
+ this.operationTree = new AllwaysFailOperation();
+ this.snapshot = DataAndMetadataSnapshot.createEmpty();
+ this.listenerTree = ListenerRegistrationNode.createRoot();
+ }
+
+ @Override
+ public String getIdentifier() {
+ return name;
+ }
+
+ @Override
+ public DOMStoreReadTransaction newReadOnlyTransaction() {
+ return new SnapshotBackedReadTransaction(nextIdentifier(), snapshot);
+ }
+
+ @Override
+ public DOMStoreReadWriteTransaction newReadWriteTransaction() {
+ return new SnapshotBackedReadWriteTransaction(nextIdentifier(), snapshot, this, operationTree);
+ }
+
+ @Override
+ public DOMStoreWriteTransaction newWriteOnlyTransaction() {
+ return new SnaphostBackedWriteTransaction(nextIdentifier(), snapshot, this, operationTree);
+ }
+
+ @Override
+ public synchronized void onGlobalContextUpdated(final SchemaContext ctx) {
+ operationTree = SchemaAwareApplyOperationRoot.from(ctx);
+ schemaContext = ctx;
+ }
+
+ @Override
+ public <L extends AsyncDataChangeListener<InstanceIdentifier, NormalizedNode<?, ?>>> ListenerRegistration<L> registerChangeListener(
+ final InstanceIdentifier path, final L listener, final DataChangeScope scope) {
+
+ Optional<ListenerRegistrationNode> listenerNode = TreeNodeUtils.findNode(listenerTree, path);
+ checkState(listenerNode.isPresent());
+ synchronized (listener) {
+ notifyInitialState(path, listener);
+ }
+ return listenerNode.get().registerDataChangeListener(listener, scope);
+ }
+
+ private void notifyInitialState(final InstanceIdentifier path,
+ final AsyncDataChangeListener<InstanceIdentifier, NormalizedNode<?, ?>> listener) {
+ Optional<StoreMetadataNode> currentState = snapshot.read(path);
+ try {
+ if (currentState.isPresent()) {
+ NormalizedNode<?, ?> data = currentState.get().getData();
+ listener.onDataChanged(DOMImmutableDataChangeEvent.builder() //
+ .setAfter(data) //
+ .addCreated(path, data) //
+ .build() //
+ );
+ }
+ } catch (Exception e) {
+ LOG.error("Unhandled exception encountered when invoking listener {}", listener, e);
+ }
+
+ }
+
+ private synchronized DOMStoreThreePhaseCommitCohort submit(
+ final SnaphostBackedWriteTransaction writeTx) {
+ LOG.debug("Tx: {} is submitted. Modifications: {}",writeTx.getIdentifier(),writeTx.getMutatedView());
+ return new ThreePhaseCommitImpl(writeTx);
+ }
+
+ private Object nextIdentifier() {
+ return name + "-" + txCounter.getAndIncrement();
+ }
+
+ private synchronized void commit(final DataAndMetadataSnapshot currentSnapshot,
+ final StoreMetadataNode newDataTree, final Iterable<ChangeListenerNotifyTask> listenerTasks) {
+ LOG.debug("Updating Store snaphot version: {} with version:{}",currentSnapshot.getMetadataTree().getSubtreeVersion(),newDataTree.getSubtreeVersion());
+ if(LOG.isTraceEnabled()) {
+ LOG.trace("Data Tree is {}",StoreUtils.toStringTree(newDataTree));
+ }
+ checkState(snapshot == currentSnapshot, "Store snapshot and transaction snapshot differs");
+ snapshot = DataAndMetadataSnapshot.builder() //
+ .setMetadataTree(newDataTree) //
+ .setSchemaContext(schemaContext) //
+ .build();
+
+ for(ChangeListenerNotifyTask task : listenerTasks) {
+ executor.submit(task);
+ }
+
+ }
+
+ private static class SnapshotBackedReadTransaction implements DOMStoreReadTransaction {
+
+ private DataAndMetadataSnapshot stableSnapshot;
+ private final Object identifier;
+
+ public SnapshotBackedReadTransaction(final Object identifier, final DataAndMetadataSnapshot snapshot) {
+ this.identifier = identifier;
+ this.stableSnapshot = snapshot;
+ LOG.debug("ReadOnly Tx: {} allocated with snapshot {}",identifier,snapshot.getMetadataTree().getSubtreeVersion());
+
+ }
+
+ @Override
+ public Object getIdentifier() {
+ return identifier;
+ }
+
+ @Override
+ public void close() {
+ stableSnapshot = null;
+ }
+
+ @Override
+ public ListenableFuture<Optional<NormalizedNode<?, ?>>> read(final InstanceIdentifier path) {
+ checkNotNull(path, "Path must not be null.");
+ checkState(stableSnapshot != null, "Transaction is closed");
+ return Futures.immediateFuture(NormalizedNodeUtils.findNode(stableSnapshot.getDataTree(), path));
+ }
+
+ @Override
+ public String toString() {
+ return "SnapshotBackedReadTransaction [id =" + identifier + "]";
+ }
+
+ }
+
+ private static class SnaphostBackedWriteTransaction implements DOMStoreWriteTransaction {
+
+ private MutableDataTree mutableTree;
+ private final Object identifier;
+ private InMemoryDOMDataStore store;
+
+ private boolean ready = false;
+
+ public SnaphostBackedWriteTransaction(final Object identifier, final DataAndMetadataSnapshot snapshot,
+ final InMemoryDOMDataStore store, final ModificationApplyOperation applyOper) {
+ this.identifier = identifier;
+ mutableTree = MutableDataTree.from(snapshot, applyOper);
+ this.store = store;
+ LOG.debug("Write Tx: {} allocated with snapshot {}",identifier,snapshot.getMetadataTree().getSubtreeVersion());
+ }
+
+ @Override
+ public Object getIdentifier() {
+ return identifier;
+ }
+
+ @Override
+ public void close() {
+ this.mutableTree = null;
+ this.store = null;
+ }
+
+ @Override
+ public void write(final InstanceIdentifier path, final NormalizedNode<?, ?> data) {
+ checkNotReady();
+ mutableTree.write(path, data);
+ }
+
+ @Override
+ public void delete(final InstanceIdentifier path) {
+ checkNotReady();
+ mutableTree.delete(path);
+ }
+
+ protected boolean isReady() {
+ return ready;
+ }
+
+ protected void checkNotReady() {
+ checkState(!ready, "Transaction is ready. No further modifications allowed.");
+ }
+
+ @Override
+ public synchronized DOMStoreThreePhaseCommitCohort ready() {
+ ready = true;
+ LOG.debug("Store transaction: {} : Ready", getIdentifier());
+ mutableTree.seal();
+ return store.submit(this);
+ }
+
+ protected MutableDataTree getMutatedView() {
+ return mutableTree;
+ }
+
+ @Override
+ public String toString() {
+ return "SnaphostBackedWriteTransaction [id=" + getIdentifier() + ", ready=" + isReady() + "]";
+ }
+
+ }
+
+ private static class SnapshotBackedReadWriteTransaction extends SnaphostBackedWriteTransaction implements
+ DOMStoreReadWriteTransaction {
+
+ protected SnapshotBackedReadWriteTransaction(final Object identifier, final DataAndMetadataSnapshot snapshot,
+ final InMemoryDOMDataStore store, final ModificationApplyOperation applyOper) {
+ super(identifier, snapshot, store, applyOper);
+ }
+
+ @Override
+ public ListenableFuture<Optional<NormalizedNode<?, ?>>> read(final InstanceIdentifier path) {
+ return Futures.immediateFuture(getMutatedView().read(path));
+ }
+
+ @Override
+ public String toString() {
+ return "SnapshotBackedReadWriteTransaction [id=" + getIdentifier() + ", ready=" + isReady() + "]";
+ }
+
+ }
+
+ private class ThreePhaseCommitImpl implements DOMStoreThreePhaseCommitCohort {
+
+ private final SnaphostBackedWriteTransaction transaction;
+ private final NodeModification modification;
+
+ private DataAndMetadataSnapshot storeSnapshot;
+ private Optional<StoreMetadataNode> proposedSubtree;
+ private Iterable<ChangeListenerNotifyTask> listenerTasks;
+
+ public ThreePhaseCommitImpl(final SnaphostBackedWriteTransaction writeTransaction) {
+ this.transaction = writeTransaction;
+ this.modification = transaction.getMutatedView().getRootModification();
+ }
+
+ @Override
+ public ListenableFuture<Boolean> canCommit() {
+ final DataAndMetadataSnapshot snapshotCapture = snapshot;
+ final ModificationApplyOperation snapshotOperation = operationTree;
+
+ return executor.submit(new Callable<Boolean>() {
+
+ @Override
+ public Boolean call() throws Exception {
+ boolean applicable = snapshotOperation.isApplicable(modification,
+ Optional.of(snapshotCapture.getMetadataTree()));
+ LOG.debug("Store Transcation: {} : canCommit : {}", transaction.getIdentifier(), applicable);
+ return applicable;
+ }
+ });
+ }
+
+ @Override
+ public ListenableFuture<Void> preCommit() {
+ storeSnapshot = snapshot;
+ if(modification.getModificationType() == ModificationType.UNMODIFIED) {
+ return Futures.immediateFuture(null);
+ }
+ return executor.submit(new Callable<Void>() {
+
+
+
+ @Override
+ public Void call() throws Exception {
+ StoreMetadataNode metadataTree = storeSnapshot.getMetadataTree();
+
+ proposedSubtree = operationTree.apply(modification, Optional.of(metadataTree),
+ increase(metadataTree.getSubtreeVersion()));
+
+
+ listenerTasks = DataChangeEventResolver.create() //
+ .setRootPath(PUBLIC_ROOT_PATH) //
+ .setBeforeRoot(Optional.of(metadataTree)) //
+ .setAfterRoot(proposedSubtree) //
+ .setModificationRoot(modification) //
+ .setListenerRoot(listenerTree) //
+ .resolve();
+
+ return null;
+ }
+ });
+ }
+
+ @Override
+ public ListenableFuture<Void> abort() {
+ storeSnapshot = null;
+ proposedSubtree = null;
+ return Futures.<Void> immediateFuture(null);
+ }
+
+ @Override
+ public ListenableFuture<Void> commit() {
+ if(modification.getModificationType() == ModificationType.UNMODIFIED) {
+ return Futures.immediateFuture(null);
+ }
+
+ checkState(proposedSubtree != null,"Proposed subtree must be computed");
+ checkState(storeSnapshot != null,"Proposed subtree must be computed");
+ // return ImmediateFuture<>;
+ InMemoryDOMDataStore.this.commit(storeSnapshot, proposedSubtree.get(),listenerTasks);
+ return Futures.<Void> immediateFuture(null);
+ }
+
+ }
+
+ private class AllwaysFailOperation implements ModificationApplyOperation {
+
+ @Override
+ public Optional<StoreMetadataNode> apply(final NodeModification modification,
+ final Optional<StoreMetadataNode> storeMeta, final UnsignedLong subtreeVersion) {
+ throw new IllegalStateException("Schema Context is not available.");
+ }
+
+ @Override
+ public boolean isApplicable(final NodeModification modification, final Optional<StoreMetadataNode> storeMetadata) {
+ throw new IllegalStateException("Schema Context is not available.");
+ }
+
+ @Override
+ public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
+ throw new IllegalStateException("Schema Context is not available.");
+ }
+
+ @Override
+ public void verifyStructure(final NodeModification modification) throws IllegalArgumentException {
+ throw new IllegalStateException("Schema Context is not available.");
+ }
+
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.md.sal.dom.store.impl;
+
+import org.opendaylight.controller.md.sal.dom.store.impl.tree.NodeModification;
+import org.opendaylight.controller.md.sal.dom.store.impl.tree.StoreMetadataNode;
+import org.opendaylight.controller.md.sal.dom.store.impl.tree.StoreTreeNode;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
+
+import com.google.common.base.Optional;
+import com.google.common.primitives.UnsignedLong;
+
+/**
+ *
+ * Operation responsible for applying {@link NodeModification} on tree.
+ *
+ * Operation is composite - operation on top level node consists of
+ * suboperations on child nodes. This allows to walk operation hierarchy and
+ * invoke suboperations independently.
+ *
+ * <b>Implementation notes</b>
+ * <ul>
+ * <li>
+ * Implementations MUST expose all nested suboperations which operates on child
+ * nodes expose via {@link #getChild(PathArgument)} method.
+ * <li>Same suboperations SHOULD be used when invoked via
+ * {@link #apply(NodeModification, Optional)} if applicable.
+ *
+ *
+ * Hierarchical composite operation which is responsible for applying
+ * modification on particular subtree and creating updated subtree
+ *
+ *
+ */
+public interface ModificationApplyOperation extends StoreTreeNode<ModificationApplyOperation> {
+
+ /**
+ *
+ * Implementation of this operation must be stateless and must not change
+ * state of this object.
+ *
+ * @param modification
+ * NodeModification to be applied
+ * @param storeMeta
+ * Store Metadata Node on which NodeModification should be
+ * applied
+ * @param subtreeVersion New subtree version of parent node
+ * @throws IllegalArgumentException
+ * If it is not possible to apply Operation on provided Metadata
+ * node
+ * @return new {@link StoreMetadataNode} if operation resulted in updating
+ * node, {@link Optional#absent()} if {@link NodeModification}
+ * resulted in deletion of this node.
+ */
+ Optional<StoreMetadataNode> apply(NodeModification modification, Optional<StoreMetadataNode> storeMeta, UnsignedLong subtreeVersion);
+
+ /**
+ *
+ * Checks if provided node modification could be applied to current metadata node.
+ *
+ * @param modification Modification
+ * @param current Metadata Node to which modification should be applied
+ * @return true if modification is applicable
+ * false if modification is no applicable
+ */
+ boolean isApplicable(NodeModification modification, Optional<StoreMetadataNode> current);
+
+ /**
+ *
+ * Performs structural verification of NodeModification, such as writen values / types
+ * uses right structural elements.
+ *
+ * @param modification to be verified.
+ * @throws IllegalArgumentException If provided NodeModification does not adhere to the structure.
+ */
+ void verifyStructure(NodeModification modification) throws IllegalArgumentException;
+
+ /**
+ * Returns a suboperation for specified tree node
+ *
+ * @return Reference to suboperation for specified tree node, {@link Optional#absent()}
+ * if suboperation is not supported for specified tree node.
+ */
+ @Override
+ public Optional<ModificationApplyOperation> getChild(PathArgument child);
+
+
+
+}
--- /dev/null
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.md.sal.dom.store.impl;
+
+import static com.google.common.base.Preconditions.checkState;
+
+import java.util.Map.Entry;
+
+import org.opendaylight.controller.md.sal.dom.store.impl.tree.NodeModification;
+import org.opendaylight.controller.md.sal.dom.store.impl.tree.StoreMetadataNode;
+import org.opendaylight.controller.md.sal.dom.store.impl.tree.TreeNodeUtils;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.Optional;
+
+class MutableDataTree {
+
+ private static final Logger log = LoggerFactory.getLogger(MutableDataTree.class);
+
+ final DataAndMetadataSnapshot snapshot;
+ final NodeModification rootModification;
+ final ModificationApplyOperation strategyTree;
+
+ private boolean sealed = false;
+
+ private MutableDataTree(final DataAndMetadataSnapshot snapshot, final ModificationApplyOperation strategyTree) {
+ this.snapshot = snapshot;
+ this.strategyTree = strategyTree;
+ this.rootModification = NodeModification.createUnmodified(snapshot.getMetadataTree());
+ }
+
+ public void write(final InstanceIdentifier path, final NormalizedNode<?, ?> value) {
+ checkSealed();
+ resolveModificationFor(path).write(value);
+ }
+
+ public void delete(final InstanceIdentifier path) {
+ checkSealed();
+ resolveModificationFor(path).delete();
+ }
+
+ public Optional<NormalizedNode<?, ?>> read(final InstanceIdentifier path) {
+ Entry<InstanceIdentifier, NodeModification> modification = TreeNodeUtils.findClosestsOrFirstMatch(rootModification, path, NodeModification.IS_TERMINAL_PREDICATE);
+
+ Optional<StoreMetadataNode> result = resolveSnapshot(modification);
+ if (result.isPresent()) {
+ NormalizedNode<?, ?> data = result.get().getData();
+ return NormalizedNodeUtils.findNode(modification.getKey(), data, path);
+ }
+ return Optional.absent();
+
+ }
+
+ private Optional<StoreMetadataNode> resolveSnapshot(
+ final Entry<InstanceIdentifier, NodeModification> keyModification) {
+ InstanceIdentifier path = keyModification.getKey();
+ NodeModification modification = keyModification.getValue();
+ return resolveSnapshot(path, modification);
+ }
+
+ private Optional<StoreMetadataNode> resolveSnapshot(final InstanceIdentifier path,
+ final NodeModification modification) {
+ try {
+ Optional<Optional<StoreMetadataNode>> potentialSnapshot = modification.getSnapshotCache();
+ if(potentialSnapshot.isPresent()) {
+ return potentialSnapshot.get();
+ }
+ return resolveModificationStrategy(path).apply(modification, modification.getOriginal(),
+ StoreUtils.increase(snapshot.getMetadataTree().getSubtreeVersion()));
+ } catch (Exception e) {
+ log.error("Could not create snapshot for {}", path,e);
+ throw e;
+ }
+ }
+
+ private ModificationApplyOperation resolveModificationStrategy(final InstanceIdentifier path) {
+ log.trace("Resolving modification apply strategy for {}", path);
+ return TreeNodeUtils.findNodeChecked(strategyTree, path);
+ }
+
+ private OperationWithModification resolveModificationFor(final InstanceIdentifier path) {
+ NodeModification modification = rootModification;
+ // We ensure strategy is present.
+ ModificationApplyOperation operation = resolveModificationStrategy(path);
+ for (PathArgument pathArg : path.getPath()) {
+ modification = modification.modifyChild(pathArg);
+ }
+ return OperationWithModification.from(operation, modification);
+ }
+
+ public static MutableDataTree from(final DataAndMetadataSnapshot snapshot, final ModificationApplyOperation resolver) {
+ return new MutableDataTree(snapshot, resolver);
+ }
+
+ public void seal() {
+ sealed = true;
+ rootModification.seal();
+ }
+
+ private void checkSealed() {
+ checkState(!sealed, "Data Tree is sealed. No further modifications allowed.");
+ }
+
+ protected NodeModification getRootModification() {
+ return rootModification;
+ }
+
+ @Override
+ public String toString() {
+ return "MutableDataTree [modification=" + rootModification + "]";
+ }
+
+
+}
--- /dev/null
+package org.opendaylight.controller.md.sal.dom.store.impl;
+
+import org.opendaylight.controller.md.sal.dom.store.impl.tree.NodeModification;
+import org.opendaylight.controller.md.sal.dom.store.impl.tree.StoreMetadataNode;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+
+import com.google.common.base.Optional;
+import com.google.common.primitives.UnsignedLong;
+
+public class OperationWithModification {
+
+ private final NodeModification modification;
+ private final ModificationApplyOperation applyOperation;
+
+ private OperationWithModification(final ModificationApplyOperation op, final NodeModification mod) {
+ this.modification = mod;
+ this.applyOperation = op;
+ }
+
+ public OperationWithModification write(final NormalizedNode<?, ?> value) {
+ modification.write(value);
+ applyOperation.verifyStructure(modification);
+ return this;
+ }
+
+ public OperationWithModification delete() {
+ modification.delete();
+ return this;
+ }
+
+ public boolean isApplicable(final Optional<StoreMetadataNode> data) {
+ return applyOperation.isApplicable(modification, data);
+ }
+
+ public Optional<StoreMetadataNode> apply(final Optional<StoreMetadataNode> data, final UnsignedLong subtreeVersion) {
+ return applyOperation.apply(modification, data, subtreeVersion);
+ }
+
+ public static OperationWithModification from(final ModificationApplyOperation operation,
+ final NodeModification modification) {
+ return new OperationWithModification(operation, modification);
+
+ }
+}
\ No newline at end of file
--- /dev/null
+package org.opendaylight.controller.md.sal.dom.store.impl;
+
+import static com.google.common.base.Preconditions.checkArgument;
+
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ExecutionException;
+
+import org.opendaylight.controller.md.sal.dom.store.impl.tree.ModificationType;
+import org.opendaylight.controller.md.sal.dom.store.impl.tree.NodeModification;
+import org.opendaylight.controller.md.sal.dom.store.impl.tree.StoreMetadataNode;
+import org.opendaylight.controller.md.sal.dom.store.impl.tree.StoreNodeCompositeBuilder;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.AugmentationIdentifier;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeWithValue;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
+import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
+import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
+import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
+import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
+import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
+import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
+import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
+import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
+import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
+import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
+import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeBuilder;
+import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
+import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
+import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder;
+import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeBuilder;
+import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
+import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
+import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
+import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
+import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
+import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
+
+import com.google.common.base.Function;
+import com.google.common.base.Optional;
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.ImmutableSet.Builder;
+import com.google.common.primitives.UnsignedLong;
+
+public abstract class SchemaAwareApplyOperation implements ModificationApplyOperation {
+
+ public static SchemaAwareApplyOperation from(final DataSchemaNode schemaNode) {
+ if (schemaNode instanceof ContainerSchemaNode) {
+ return new ContainerModificationStrategy((ContainerSchemaNode) schemaNode);
+ } else if (schemaNode instanceof ListSchemaNode) {
+ return new ListMapModificationStrategy((ListSchemaNode) schemaNode);
+ } else if (schemaNode instanceof ChoiceNode) {
+ return new ChoiceModificationStrategy((ChoiceNode) schemaNode);
+ } else if (schemaNode instanceof LeafListSchemaNode) {
+ return new LeafSetEntryModificationStrategy((LeafListSchemaNode) schemaNode);
+ } else if (schemaNode instanceof LeafSchemaNode) {
+ return new LeafModificationStrategy((LeafSchemaNode) schemaNode);
+ }
+ throw new IllegalArgumentException("Not supported schema node type for " + schemaNode.getClass());
+ }
+
+ public static SchemaAwareApplyOperation from(final DataNodeContainer resolvedTree,
+ final AugmentationTarget augSchemas, final AugmentationIdentifier identifier) {
+ AugmentationSchema augSchema = null;
+ allAugments : for (AugmentationSchema potential : augSchemas.getAvailableAugmentations()) {
+ boolean containsAll = true;
+ for(DataSchemaNode child : potential.getChildNodes()) {
+ if(identifier.getPossibleChildNames().contains(child.getQName())) {
+ augSchema = potential;
+ break allAugments;
+ }
+ }
+ }
+ if(augSchema != null) {
+ return new AugmentationModificationStrategy(augSchema,resolvedTree);
+ }
+ return null;
+ }
+
+
+
+ protected final ModificationApplyOperation resolveChildOperation(final PathArgument child) {
+ Optional<ModificationApplyOperation> potential = getChild(child);
+ checkArgument(potential.isPresent(), "Operation for child %s is not defined.", child);
+ return potential.get();
+ }
+
+ @Override
+ public void verifyStructure(final NodeModification modification) throws IllegalArgumentException {
+ if (modification.getModificationType() == ModificationType.WRITE) {
+ verifyWritenStructure(modification.getWritenValue());
+ }
+ }
+
+ protected abstract void verifyWritenStructure(NormalizedNode<?, ?> writenValue);
+
+ @Override
+ public boolean isApplicable(final NodeModification modification, final Optional<StoreMetadataNode> current) {
+ switch (modification.getModificationType()) {
+ case DELETE:
+ return isDeleteApplicable(modification, current);
+ case SUBTREE_MODIFIED:
+ return isSubtreeModificationApplicable(modification, current);
+ case WRITE:
+ return isWriteApplicable(modification, current);
+ case UNMODIFIED:
+ return true;
+ default:
+ return false;
+ }
+ }
+
+ protected boolean isWriteApplicable(final NodeModification modification, final Optional<StoreMetadataNode> current) {
+ Optional<StoreMetadataNode> original = modification.getOriginal();
+ if (original.isPresent() && current.isPresent()) {
+ return isNotConflicting(original.get(), current.get());
+ } else if (current.isPresent()) {
+ return false;
+ }
+ return true;
+
+ }
+
+ protected final boolean isNotConflicting(final StoreMetadataNode original, final StoreMetadataNode current) {
+ return original.getNodeVersion().equals(current.getNodeVersion())
+ && original.getSubtreeVersion().equals(current.getSubtreeVersion());
+ }
+
+ protected abstract boolean isSubtreeModificationApplicable(final NodeModification modification,
+ final Optional<StoreMetadataNode> current);
+
+ private boolean isDeleteApplicable(final NodeModification modification, final Optional<StoreMetadataNode> current) {
+ // FiXME: Add delete conflict detection.
+ return true;
+ }
+
+ @Override
+ public final Optional<StoreMetadataNode> apply(final NodeModification modification,
+ final Optional<StoreMetadataNode> currentMeta, final UnsignedLong subtreeVersion) {
+
+ switch (modification.getModificationType()) {
+ case DELETE:
+ return modification.storeSnapshot(Optional.<StoreMetadataNode>absent());
+ case SUBTREE_MODIFIED:
+ return modification.storeSnapshot(Optional.of(applySubtreeChange(modification, currentMeta.get(), subtreeVersion)));
+ case WRITE:
+ return modification.storeSnapshot(Optional.of(applyWrite(modification, currentMeta, subtreeVersion)));
+ case UNMODIFIED:
+ return currentMeta;
+ default:
+ throw new IllegalArgumentException("Provided modification type is not supported.");
+ }
+ }
+
+ protected abstract StoreMetadataNode applyWrite(NodeModification modification,
+ Optional<StoreMetadataNode> currentMeta, UnsignedLong subtreeVersion);
+
+ protected abstract StoreMetadataNode applySubtreeChange(NodeModification modification,
+ StoreMetadataNode currentMeta, UnsignedLong subtreeVersion);
+
+ public static abstract class ValueNodeModificationStrategy<T extends DataSchemaNode> extends
+ SchemaAwareApplyOperation {
+
+ private final T schema;
+ private final Class<? extends NormalizedNode<?, ?>> nodeClass;
+
+ protected ValueNodeModificationStrategy(final T schema, final Class<? extends NormalizedNode<?, ?>> nodeClass) {
+ super();
+ this.schema = schema;
+ this.nodeClass = nodeClass;
+ }
+
+ @Override
+ protected void verifyWritenStructure(final NormalizedNode<?, ?> writenValue) {
+ checkArgument(nodeClass.isInstance(writenValue), "Node should must be of type %s", nodeClass);
+ }
+
+ @Override
+ public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
+ throw new UnsupportedOperationException("Node " + schema.getPath()
+ + "is leaf type node. Child nodes not allowed");
+ }
+
+ @Override
+ protected StoreMetadataNode applySubtreeChange(final NodeModification modification,
+ final StoreMetadataNode currentMeta, final UnsignedLong subtreeVersion) {
+ throw new UnsupportedOperationException("Node " + schema.getPath()
+ + "is leaf type node. Subtree change is not allowed.");
+ }
+
+ @Override
+ protected StoreMetadataNode applyWrite(final NodeModification modification,
+ final Optional<StoreMetadataNode> currentMeta, final UnsignedLong subtreeVersion) {
+ UnsignedLong nodeVersion = subtreeVersion;
+ if (currentMeta.isPresent()) {
+ nodeVersion = StoreUtils.increase(currentMeta.get().getNodeVersion());
+ }
+
+ return StoreMetadataNode.builder().setNodeVersion(nodeVersion).setSubtreeVersion(subtreeVersion)
+ .setData(modification.getWritenValue()).build();
+ }
+
+ @Override
+ protected boolean isSubtreeModificationApplicable(final NodeModification modification,
+ final Optional<StoreMetadataNode> current) {
+ return false;
+ }
+
+ }
+
+ public static class LeafSetEntryModificationStrategy extends ValueNodeModificationStrategy<LeafListSchemaNode> {
+
+ @SuppressWarnings({ "unchecked", "rawtypes" })
+ protected LeafSetEntryModificationStrategy(final LeafListSchemaNode schema) {
+ super(schema, (Class) LeafSetEntryNode.class);
+ }
+ }
+
+ public static class LeafModificationStrategy extends ValueNodeModificationStrategy<LeafSchemaNode> {
+
+ @SuppressWarnings({ "unchecked", "rawtypes" })
+ protected LeafModificationStrategy(final LeafSchemaNode schema) {
+ super(schema, (Class) LeafNode.class);
+ }
+ }
+
+ public static abstract class NormalizedNodeContainerModificationStrategy extends SchemaAwareApplyOperation {
+
+ private final Class<? extends NormalizedNode<?, ?>> nodeClass;
+
+ protected NormalizedNodeContainerModificationStrategy(final Class<? extends NormalizedNode<?, ?>> nodeClass) {
+ this.nodeClass = nodeClass;
+ }
+
+ @Override
+ public void verifyStructure(final NodeModification modification) throws IllegalArgumentException {
+ if (modification.getModificationType() == ModificationType.WRITE) {
+
+ }
+ for (NodeModification childModification : modification.getModifications()) {
+ resolveChildOperation(childModification.getIdentifier()).verifyStructure(childModification);
+ }
+ }
+
+ @SuppressWarnings("rawtypes")
+ @Override
+ protected void verifyWritenStructure(final NormalizedNode<?, ?> writenValue) {
+ checkArgument(nodeClass.isInstance(writenValue), "Node should must be of type %s", nodeClass);
+ checkArgument(writenValue instanceof NormalizedNodeContainer);
+ NormalizedNodeContainer writenCont = (NormalizedNodeContainer) writenValue;
+ for (Object child : writenCont.getValue()) {
+ checkArgument(child instanceof NormalizedNode);
+ NormalizedNode childNode = (NormalizedNode) child;
+ }
+ }
+
+ @Override
+ protected StoreMetadataNode applyWrite(final NodeModification modification,
+ final Optional<StoreMetadataNode> currentMeta, final UnsignedLong subtreeVersion) {
+ //
+ NormalizedNode<?, ?> newValue = modification.getWritenValue();
+
+ UnsignedLong nodeVersion = subtreeVersion;
+ if (currentMeta.isPresent()) {
+ nodeVersion = StoreUtils.increase(currentMeta.get().getNodeVersion());
+ }
+ StoreMetadataNode newValueMeta = StoreMetadataNode.createRecursivelly(newValue, nodeVersion, nodeVersion);
+
+ if (!modification.hasAdditionalModifications()) {
+ return newValueMeta;
+ }
+ @SuppressWarnings("rawtypes")
+ NormalizedNodeContainerBuilder dataBuilder = createBuilder(modification.getIdentifier());
+ StoreNodeCompositeBuilder builder = StoreNodeCompositeBuilder.from(dataBuilder) //
+ .setNodeVersion(nodeVersion) //
+ .setSubtreeVersion(subtreeVersion);
+
+ Set<PathArgument> processedPreexisting = applyPreexistingChildren(modification, newValueMeta.getChildren(),
+ builder, nodeVersion);
+ applyNewChildren(modification, processedPreexisting, builder, nodeVersion);
+
+ return builder.build();
+
+ }
+
+ @Override
+ public StoreMetadataNode applySubtreeChange(final NodeModification modification,
+ final StoreMetadataNode currentMeta, final UnsignedLong subtreeVersion) {
+
+ UnsignedLong updatedSubtreeVersion = StoreUtils.increase(currentMeta.getSubtreeVersion());
+ @SuppressWarnings("rawtypes")
+ NormalizedNodeContainerBuilder dataBuilder = createBuilder(modification.getIdentifier());
+ StoreNodeCompositeBuilder builder = StoreNodeCompositeBuilder.from(dataBuilder)
+ .setIdentifier(modification.getIdentifier()).setNodeVersion(currentMeta.getNodeVersion())
+ .setSubtreeVersion(updatedSubtreeVersion);
+ // We process preexisting nodes
+ Set<PathArgument> processedPreexisting = applyPreexistingChildren(modification, currentMeta.getChildren(),
+ builder, updatedSubtreeVersion);
+ applyNewChildren(modification, processedPreexisting, builder, updatedSubtreeVersion);
+ return builder.build();
+ }
+
+ private void applyNewChildren(final NodeModification modification, final Set<PathArgument> ignore,
+ final StoreNodeCompositeBuilder builder, final UnsignedLong subtreeVersion) {
+ for (NodeModification childModification : modification.getModifications()) {
+ PathArgument childIdentifier = childModification.getIdentifier();
+ // We skip allready processed modifications
+ if (ignore.contains(childIdentifier)) {
+ continue;
+ }
+
+ builder.addIfPresent(resolveChildOperation(childIdentifier) //
+ .apply(childModification, Optional.<StoreMetadataNode> absent(), subtreeVersion));
+ }
+ }
+
+ private Set<PathArgument> applyPreexistingChildren(final NodeModification modification,
+ final Iterable<StoreMetadataNode> children, final StoreNodeCompositeBuilder nodeBuilder,
+ final UnsignedLong subtreeVersion) {
+ Builder<PathArgument> processedModifications = ImmutableSet.<PathArgument> builder();
+ for (StoreMetadataNode childMeta : children) {
+ PathArgument childIdentifier = childMeta.getIdentifier();
+ // We retrieve Child modification metadata
+ Optional<NodeModification> childModification = modification.getChild(childIdentifier);
+ // Node is modified
+ if (childModification.isPresent()) {
+ processedModifications.add(childIdentifier);
+ Optional<StoreMetadataNode> result = resolveChildOperation(childIdentifier) //
+ .apply(childModification.get(), Optional.of(childMeta), subtreeVersion);
+ nodeBuilder.addIfPresent(result);
+ } else {
+ // Child is unmodified - reuse existing metadata and data
+ // snapshot
+ nodeBuilder.add(childMeta);
+ }
+ }
+ return processedModifications.build();
+ }
+
+ @Override
+ protected boolean isSubtreeModificationApplicable(final NodeModification modification,
+ final Optional<StoreMetadataNode> current) {
+ if (false == current.isPresent()) {
+ return false;
+ }
+ boolean result = true;
+ StoreMetadataNode currentMeta = current.get();
+ for (NodeModification childMod : modification.getModifications()) {
+ PathArgument childId = childMod.getIdentifier();
+ Optional<StoreMetadataNode> childMeta = currentMeta.getChild(childId);
+ result &= resolveChildOperation(childId).isApplicable(childMod, childMeta);
+ }
+ return result;
+ }
+
+ @SuppressWarnings("rawtypes")
+ protected abstract NormalizedNodeContainerBuilder createBuilder(PathArgument identifier);
+ }
+
+ public static abstract class DataNodeContainerModificationStrategy<T extends DataNodeContainer> extends
+ NormalizedNodeContainerModificationStrategy {
+
+ private final T schema;
+ private final LoadingCache<PathArgument, ModificationApplyOperation> childCache = CacheBuilder.newBuilder().build(
+ CacheLoader.from(new Function<PathArgument, ModificationApplyOperation>() {
+
+ @Override
+ public ModificationApplyOperation apply(final PathArgument identifier) {
+ if (identifier instanceof AugmentationIdentifier && schema instanceof AugmentationTarget) {
+ return from(schema, (AugmentationTarget) schema, (AugmentationIdentifier) identifier);
+ }
+
+ DataSchemaNode child = schema.getDataChildByName(identifier.getNodeType());
+ if (child == null) {
+ return null;
+ }
+ return from(child);
+ }
+ }));
+
+ protected DataNodeContainerModificationStrategy(final T schema,
+ final Class<? extends NormalizedNode<?, ?>> nodeClass) {
+ super(nodeClass);
+ this.schema = schema;
+ }
+
+ protected T getSchema() {
+ return schema;
+ }
+
+ @Override
+ public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
+ try {
+ return Optional.<ModificationApplyOperation> fromNullable(childCache.get(identifier));
+ } catch (ExecutionException e) {
+ return Optional.absent();
+ }
+ }
+
+ @Override
+ @SuppressWarnings("rawtypes")
+ protected abstract DataContainerNodeBuilder createBuilder(PathArgument identifier);
+
+ @Override
+ public String toString() {
+ return getClass().getSimpleName() + " [" + schema + "]";
+ }
+
+ }
+
+ public static class ContainerModificationStrategy extends
+ DataNodeContainerModificationStrategy<ContainerSchemaNode> {
+
+ public ContainerModificationStrategy(final ContainerSchemaNode schemaNode) {
+ super(schemaNode, ContainerNode.class);
+ }
+
+ @Override
+ @SuppressWarnings("rawtypes")
+ protected DataContainerNodeBuilder createBuilder(final PathArgument identifier) {
+ // TODO Auto-generated method stub
+ checkArgument(identifier instanceof NodeIdentifier);
+ return ImmutableContainerNodeBuilder.create().withNodeIdentifier((NodeIdentifier) identifier);
+ }
+
+ }
+
+ public static class AugmentationModificationStrategy extends
+ DataNodeContainerModificationStrategy<AugmentationSchema> {
+
+ protected AugmentationModificationStrategy(final AugmentationSchema schema, final DataNodeContainer resolved) {
+ super(schema, AugmentationNode.class);
+ // FIXME: Use resolved children instead of unresolved.
+
+ }
+
+
+ @Override
+ protected DataContainerNodeBuilder createBuilder(final PathArgument identifier) {
+ return Builders.augmentationBuilder().withNodeIdentifier((AugmentationIdentifier) identifier);
+ }
+
+ }
+
+ public static class ChoiceModificationStrategy extends NormalizedNodeContainerModificationStrategy {
+
+ private final ChoiceNode schema;
+ private final Map<PathArgument,ModificationApplyOperation> childNodes;
+
+ public ChoiceModificationStrategy(final ChoiceNode schemaNode) {
+ super(org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode.class);
+ this.schema = schemaNode;
+ ImmutableMap.Builder<PathArgument, ModificationApplyOperation> child = ImmutableMap.builder();
+
+ for(ChoiceCaseNode caze : schemaNode.getCases()) {
+ for(DataSchemaNode cazeChild : caze.getChildNodes()) {
+ SchemaAwareApplyOperation childNode = from(cazeChild);
+ child.put(new NodeIdentifier(cazeChild.getQName()),childNode);
+ }
+ }
+ childNodes = child.build();
+ }
+
+ @Override
+ public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
+ return Optional.fromNullable(childNodes.get(child));
+ }
+
+ @Override
+ @SuppressWarnings("rawtypes")
+ protected DataContainerNodeBuilder createBuilder(final PathArgument identifier) {
+ checkArgument(identifier instanceof NodeIdentifier);
+ return ImmutableChoiceNodeBuilder.create().withNodeIdentifier((NodeIdentifier) identifier);
+ }
+
+ }
+
+ public static class ListEntryModificationStrategy extends DataNodeContainerModificationStrategy<ListSchemaNode> {
+
+ protected ListEntryModificationStrategy(final ListSchemaNode schema) {
+ super(schema, MapEntryNode.class);
+ }
+
+ @Override
+ @SuppressWarnings("rawtypes")
+ protected final DataContainerNodeBuilder createBuilder(final PathArgument identifier) {
+ return ImmutableMapEntryNodeBuilder.create().withNodeIdentifier((NodeIdentifierWithPredicates) identifier);
+ }
+
+ }
+
+ public static class LeafSetModificationStrategy extends NormalizedNodeContainerModificationStrategy {
+
+ private final Optional<ModificationApplyOperation> entryStrategy;
+
+ @SuppressWarnings({ "unchecked", "rawtypes" })
+ protected LeafSetModificationStrategy(final LeafListSchemaNode schema) {
+ super((Class) LeafSetNode.class);
+ entryStrategy = Optional.<ModificationApplyOperation> of(new LeafSetEntryModificationStrategy(schema));
+ }
+
+ @SuppressWarnings("rawtypes")
+ @Override
+ protected NormalizedNodeContainerBuilder createBuilder(final PathArgument identifier) {
+ return ImmutableLeafSetNodeBuilder.create().withNodeIdentifier((NodeIdentifier) identifier);
+ }
+
+ @Override
+ public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
+ if (identifier instanceof NodeWithValue) {
+ return entryStrategy;
+ }
+ return Optional.absent();
+ }
+
+ }
+
+ public static class ListMapModificationStrategy extends NormalizedNodeContainerModificationStrategy {
+
+ private final Optional<ModificationApplyOperation> entryStrategy;
+
+ protected ListMapModificationStrategy(final ListSchemaNode schema) {
+ super(MapNode.class);
+ entryStrategy = Optional.<ModificationApplyOperation> of(new ListEntryModificationStrategy(schema));
+ }
+
+ @SuppressWarnings("rawtypes")
+ @Override
+ protected NormalizedNodeContainerBuilder createBuilder(final PathArgument identifier) {
+ return ImmutableMapNodeBuilder.create().withNodeIdentifier((NodeIdentifier) identifier);
+ }
+
+ @Override
+ public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
+ if (identifier instanceof NodeIdentifierWithPredicates) {
+ return entryStrategy;
+ }
+ return Optional.absent();
+ }
+
+ @Override
+ public String toString() {
+ return "ListMapModificationStrategy [entry=" + entryStrategy + "]";
+ }
+ }
+
+ public void verifyIdentifier(final PathArgument identifier) {
+
+ }
+
+}
--- /dev/null
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.md.sal.dom.store.impl;
+
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
+import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
+import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
+import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
+import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+
+public class SchemaAwareApplyOperationRoot extends SchemaAwareApplyOperation.DataNodeContainerModificationStrategy<ContainerSchemaNode> {
+
+ private final SchemaContext context;
+
+ public SchemaAwareApplyOperationRoot(final SchemaContext context) {
+ super(context,ContainerNode.class);
+ this.context = context;
+ }
+
+ @Override
+ protected DataContainerNodeBuilder createBuilder(final PathArgument identifier) {
+ return ImmutableContainerNodeBuilder.create().withNodeIdentifier((NodeIdentifier) identifier);
+ }
+
+ public SchemaContext getContext() {
+ return context;
+ }
+
+ @Override
+ public String toString() {
+ return "SchemaAwareApplyOperationRoot [context=" + context + "]";
+ }
+
+}
--- /dev/null
+package org.opendaylight.controller.md.sal.dom.store.impl;
+
+import java.util.Collections;
+import java.util.Map;
+import java.util.Set;
+
+import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
+import org.opendaylight.controller.md.sal.dom.store.impl.tree.StoreMetadataNode;
+import org.opendaylight.yangtools.concepts.Identifiable;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.AugmentationIdentifier;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
+
+import com.google.common.base.Function;
+import com.google.common.base.Strings;
+import com.google.common.collect.FluentIterable;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.primitives.UnsignedLong;
+
+public final class StoreUtils {
+
+ private final static Function<Identifiable<Object>, Object> EXTRACT_IDENTIFIER = new Function<Identifiable<Object>, Object>() {
+
+ @Override
+ public Object apply(final Identifiable<Object> input) {
+ return input.getIdentifier();
+ }
+ };
+
+ public static final UnsignedLong increase(final UnsignedLong original) {
+ return original.plus(UnsignedLong.ONE);
+ }
+
+ public static final InstanceIdentifier append(final InstanceIdentifier parent, final PathArgument arg) {
+
+ return new InstanceIdentifier(ImmutableList.<PathArgument> builder().addAll(parent.getPath()).add(arg).build());
+ }
+
+ public static AsyncDataChangeEvent<InstanceIdentifier, NormalizedNode<?, ?>> initialChangeEvent(
+ final InstanceIdentifier path, final StoreMetadataNode data) {
+ return new InitialDataChangeEvent(path, data.getData());
+ }
+
+ private static final class InitialDataChangeEvent implements
+ AsyncDataChangeEvent<InstanceIdentifier, NormalizedNode<?, ?>> {
+
+ private final ImmutableMap<InstanceIdentifier, NormalizedNode<?, ?>> payload;
+ private final NormalizedNode<?, ?> data;
+
+ public InitialDataChangeEvent(final InstanceIdentifier path, final NormalizedNode<?, ?> data) {
+ payload = ImmutableMap.<InstanceIdentifier, NormalizedNode<?, ?>> of(path, data);
+ this.data = data;
+ }
+
+ @Override
+ public Map<InstanceIdentifier, NormalizedNode<?, ?>> getCreatedData() {
+ return payload;
+ }
+
+ @Override
+ public Map<InstanceIdentifier, ? extends NormalizedNode<?, ?>> getOriginalData() {
+ return Collections.emptyMap();
+ }
+
+ @Override
+ public NormalizedNode<?, ?> getOriginalSubtree() {
+ return null;
+ }
+
+ @Override
+ public Set<InstanceIdentifier> getRemovedPaths() {
+ return Collections.emptySet();
+ }
+
+ @Override
+ public Map<InstanceIdentifier, NormalizedNode<?, ?>> getUpdatedData() {
+ return payload;
+ }
+
+ @Override
+ public NormalizedNode<?, ?> getUpdatedSubtree() {
+ return data;
+ }
+
+ }
+
+ @SuppressWarnings({ "unchecked", "rawtypes" })
+ public static <V> Function<Identifiable<V>,V> identifierExtractor() {
+ return (Function) EXTRACT_IDENTIFIER;
+ }
+
+ public static <V> Set<V> toIdentifierSet(final Iterable<? extends Identifiable<V>> children) {
+ return FluentIterable.from(children).transform(StoreUtils.<V>identifierExtractor()).toSet();
+ }
+
+ public static String toStringTree(final StoreMetadataNode metaNode) {
+ StringBuilder builder = new StringBuilder();
+ toStringTree(builder, metaNode,0);
+ return builder.toString();
+
+ }
+
+ private static void toStringTree(final StringBuilder builder, final StoreMetadataNode metaNode,final int offset) {
+ String prefix = Strings.repeat(" ", offset);
+ builder.append(prefix).append(toStringTree(metaNode.getIdentifier()));
+ NormalizedNode<?, ?> dataNode = metaNode.getData();
+ if(dataNode instanceof NormalizedNodeContainer<?,?,?>) {
+ builder.append(" {").append("\n");
+ for(StoreMetadataNode child : metaNode.getChildren()) {
+ toStringTree(builder, child, offset+4);
+ }
+ builder.append(prefix).append("}");
+ } else {
+ builder.append(" ").append(dataNode.getValue());
+ }
+ builder.append("\n");
+ }
+
+ private static String toStringTree(final PathArgument identifier) {
+ if( identifier instanceof NodeIdentifierWithPredicates) {
+ StringBuilder builder = new StringBuilder();
+ builder.append(identifier.getNodeType().getLocalName());
+ builder.append(((NodeIdentifierWithPredicates) identifier).getKeyValues().values());
+ return builder.toString();
+ } else if (identifier instanceof AugmentationIdentifier) {
+ return "augmentation";
+ }
+ return identifier.getNodeType().getLocalName();
+ }
+}
--- /dev/null
+package org.opendaylight.controller.md.sal.dom.store.impl.tree;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+
+import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
+import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
+import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
+import org.opendaylight.yangtools.concepts.Identifiable;
+import org.opendaylight.yangtools.concepts.ListenerRegistration;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+
+import com.google.common.base.Optional;
+
+public class ListenerRegistrationNode implements StoreTreeNode<ListenerRegistrationNode>,Identifiable<PathArgument> {
+
+ private final ListenerRegistrationNode parent;
+ private final Map<PathArgument, ListenerRegistrationNode> children;
+ private final PathArgument identifier;
+ private final HashSet<DataChangeListenerRegistration<?>> listeners;
+
+ private ListenerRegistrationNode(final PathArgument identifier) {
+ this(null,identifier);
+ }
+
+ private ListenerRegistrationNode(final ListenerRegistrationNode parent,final PathArgument identifier) {
+ this.parent = parent;
+ this.identifier = identifier;
+ children = new HashMap<>();
+ listeners = new HashSet<>();
+ }
+
+ public final static ListenerRegistrationNode createRoot() {
+ return new ListenerRegistrationNode(null);
+ }
+
+ @Override
+ public PathArgument getIdentifier() {
+ return identifier;
+ }
+
+ public Iterable<DataChangeListenerRegistration<?>> getListeners() {
+ return listeners;
+ }
+
+ @Override
+ public synchronized Optional<ListenerRegistrationNode> getChild(final PathArgument child) {
+ ListenerRegistrationNode potential = (children.get(child));
+ if(potential == null) {
+ potential = new ListenerRegistrationNode(this, child);
+ children.put(child, potential);
+ }
+ return Optional.of(potential);
+ }
+
+ public <L extends AsyncDataChangeListener<InstanceIdentifier, NormalizedNode<?, ?>>> ListenerRegistration<L> registerDataChangeListener(
+ final L listener, final DataChangeScope scope) {
+ DataChangeListenerRegistration<L> listenerReg = new DataChangeListenerRegistration<L>(listener, scope,this);
+ listeners.add(listenerReg);
+ return listenerReg;
+ }
+
+ private void removeListener(final DataChangeListenerRegistration<?> listener) {
+ listeners.remove(listener);
+ removeThisIfUnused();
+ }
+
+
+ private void removeThisIfUnused() {
+ if(parent != null && listeners.isEmpty() && children.isEmpty()) {
+ parent.removeChildIfUnused(this);
+ }
+ }
+
+ public boolean isUnused() {
+ return (listeners.isEmpty() && children.isEmpty()) || areChildrenUnused();
+ }
+
+ private boolean areChildrenUnused() {
+ for(ListenerRegistrationNode child : children.values()) {
+ if(!child.isUnused()) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ private void removeChildIfUnused(final ListenerRegistrationNode listenerRegistrationNode) {
+ // FIXME Remove unnecessary
+ }
+
+
+
+
+ public static class DataChangeListenerRegistration<T extends AsyncDataChangeListener<InstanceIdentifier, NormalizedNode<?, ?>>> extends AbstractObjectRegistration<T>
+ implements ListenerRegistration<T> {
+
+ private final DataChangeScope scope;
+ private ListenerRegistrationNode node;
+
+ public DataChangeListenerRegistration(final T listener, final DataChangeScope scope, final ListenerRegistrationNode node) {
+ super(listener);
+
+ this.scope = scope;
+ this.node = node;
+ }
+
+ protected DataChangeScope getScope() {
+ return scope;
+ }
+
+ @Override
+ protected void removeRegistration() {
+ node.removeListener(this);
+ node = null;
+ }
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.md.sal.dom.store.impl.tree;
+
+public enum ModificationType {
+
+ /**
+ *
+ * Node is unmodified
+ *
+ *
+ */
+ UNMODIFIED,
+ /**
+ *
+ * Child of tree node was modified
+ *
+ */
+ SUBTREE_MODIFIED,
+ /**
+ * Tree node was replaced with new value / subtree
+ *
+ */
+ WRITE,
+ /**
+ *
+ * Tree node is to be deleted.
+ *
+ */
+ DELETE
+}
--- /dev/null
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.md.sal.dom.store.impl.tree;
+
+import static com.google.common.base.Preconditions.checkState;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import org.opendaylight.yangtools.concepts.Identifiable;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+
+import com.google.common.base.Optional;
+import com.google.common.base.Predicate;
+import com.google.common.primitives.UnsignedLong;
+
+/**
+ * Node Modification Node and Tree
+ *
+ * Tree which structurally resembles data tree and captures client modifications
+ * to the data store tree.
+ *
+ * This tree is lazily created and populated via {@link #modifyChild(PathArgument)}
+ * and {@link StoreMetadataNode} which represents original state {@link #getOriginal()}.
+ *
+ */
+public class NodeModification implements StoreTreeNode<NodeModification>, Identifiable<PathArgument> {
+
+ public static final Predicate<NodeModification> IS_TERMINAL_PREDICATE = new Predicate<NodeModification>() {
+ @Override
+ public boolean apply(final NodeModification input) {
+ return input.getModificationType() == ModificationType.WRITE || input.getModificationType() == ModificationType.DELETE;
+ }
+ };
+ private final PathArgument identifier;
+ private ModificationType modificationType = ModificationType.UNMODIFIED;
+
+
+ private final Optional<StoreMetadataNode> original;
+
+ private NormalizedNode<?, ?> value;
+
+ private UnsignedLong subtreeVersion;
+ private Optional<StoreMetadataNode> snapshotCache;
+
+ private final Map<PathArgument, NodeModification> childModification;
+
+ private boolean sealed = false;
+
+ protected NodeModification(final PathArgument identifier, final Optional<StoreMetadataNode> original) {
+ this.identifier = identifier;
+ this.original = original;
+ childModification = new LinkedHashMap<>();
+ }
+
+ /**
+ *
+ *
+ * @return
+ */
+ public NormalizedNode<?, ?> getWritenValue() {
+ return value;
+ }
+
+ @Override
+ public PathArgument getIdentifier() {
+ return identifier;
+ }
+
+ /**
+ *
+ * Returns original store metadata
+ * @return original store metadata
+ */
+ public final Optional<StoreMetadataNode> getOriginal() {
+ return original;
+ }
+
+ /**
+ * Returns modification type
+ *
+ * @return modification type
+ */
+ public final ModificationType getModificationType() {
+ return modificationType;
+ }
+
+ /**
+ *
+ * Returns child modification if child was modified
+ *
+ * @return Child modification if direct child or it's subtree
+ * was modified.
+ *
+ */
+ @Override
+ public Optional<NodeModification> getChild(final PathArgument child) {
+ return Optional.<NodeModification> fromNullable(childModification.get(child));
+ }
+
+ /**
+ *
+ * Returns child modification if child was modified, creates {@link NodeModification}
+ * for child otherwise.
+ *
+ * If this node's {@link ModificationType} is {@link ModificationType#UNMODIFIED}
+ * changes modification type to {@link ModificationType#SUBTREE_MODIFIED}
+ *
+ * @param child
+ * @return {@link NodeModification} for specified child, with {@link #getOriginal()}
+ * containing child metadata if child was present in original data.
+ */
+ public synchronized NodeModification modifyChild(final PathArgument child) {
+ checkSealed();
+ clearSnapshot();
+ if(modificationType == ModificationType.UNMODIFIED) {
+ updateModificationType(ModificationType.SUBTREE_MODIFIED);
+ }
+ final NodeModification potential = childModification.get(child);
+ if (potential != null) {
+ return potential;
+ }
+ Optional<StoreMetadataNode> currentMetadata = Optional.absent();
+ if(original.isPresent()) {
+ currentMetadata = original.get().getChild(child);
+ }
+ NodeModification newlyCreated = new NodeModification(child,currentMetadata);
+ childModification.put(child, newlyCreated);
+ return newlyCreated;
+ }
+
+ /**
+ *
+ * Returns all recorded direct child modification
+ *
+ * @return all recorded direct child modifications
+ */
+ public Iterable<NodeModification> getModifications() {
+ return childModification.values();
+ }
+
+
+ /**
+ *
+ * Records a delete for associated node.
+ *
+ */
+ public synchronized void delete() {
+ checkSealed();
+ clearSnapshot();
+ updateModificationType(ModificationType.DELETE);
+ childModification.clear();
+ this.value = null;
+ }
+
+ /**
+ *
+ * Records a write for associated node.
+ *
+ * @param value
+ */
+ public synchronized void write(final NormalizedNode<?, ?> value) {
+ checkSealed();
+ clearSnapshot();
+ updateModificationType(ModificationType.WRITE);
+ childModification.clear();
+ this.value = value;
+ }
+
+ private void checkSealed() {
+ checkState(!sealed, "Node Modification is sealed. No further changes allowed.");
+ }
+
+ public synchronized void seal() {
+ sealed = true;
+ clearSnapshot();
+ for(NodeModification child : childModification.values()) {
+ child.seal();
+ }
+ }
+
+ private void clearSnapshot() {
+ snapshotCache = null;
+ }
+
+ public Optional<StoreMetadataNode> storeSnapshot(final Optional<StoreMetadataNode> snapshot) {
+ snapshotCache = snapshot;
+ return snapshot;
+ }
+
+ public Optional<Optional<StoreMetadataNode>> getSnapshotCache() {
+ return Optional.fromNullable(snapshotCache);
+ }
+
+ public boolean hasAdditionalModifications() {
+ return !childModification.isEmpty();
+ }
+
+ public void updateModificationType(final ModificationType type) {
+ modificationType = type;
+ clearSnapshot();
+ }
+
+ @Override
+ public String toString() {
+ return "NodeModification [identifier=" + identifier + ", modificationType="
+ + modificationType + ", childModification=" + childModification + "]";
+ }
+
+ public static NodeModification createUnmodified(final StoreMetadataNode metadataTree) {
+ return new NodeModification(metadataTree.getIdentifier(), Optional.of(metadataTree));
+ }
+
+}
--- /dev/null
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.md.sal.dom.store.impl.tree;
+
+import static com.google.common.base.Preconditions.checkState;
+
+import java.util.Map;
+
+import org.opendaylight.yangtools.concepts.Identifiable;
+import org.opendaylight.yangtools.concepts.Immutable;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
+
+import com.google.common.base.Optional;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.primitives.UnsignedLong;
+
+public class StoreMetadataNode implements Immutable, Identifiable<PathArgument>, StoreTreeNode<StoreMetadataNode> {
+
+ private final UnsignedLong nodeVersion;
+ private final UnsignedLong subtreeVersion;
+ private final NormalizedNode<?, ?> data;
+
+ private final Map<PathArgument, StoreMetadataNode> children;
+
+ protected StoreMetadataNode(final NormalizedNode<?, ?> data, final UnsignedLong nodeVersion,
+ final UnsignedLong subtreeVersion, final Map<PathArgument, StoreMetadataNode> children) {
+ this.nodeVersion = nodeVersion;
+ this.subtreeVersion = subtreeVersion;
+ this.data = data;
+ this.children = ImmutableMap.copyOf(children);
+
+ }
+
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ public UnsignedLong getNodeVersion() {
+ return this.nodeVersion;
+ }
+
+ @Override
+ public PathArgument getIdentifier() {
+ return data.getIdentifier();
+ }
+
+ public UnsignedLong getSubtreeVersion() {
+ return subtreeVersion;
+ }
+
+ public NormalizedNode<?, ?> getData() {
+ return this.data;
+ }
+
+ public Iterable<StoreMetadataNode> getChildren() {
+ return children.values();
+ }
+
+ @Override
+ public Optional<StoreMetadataNode> getChild(final PathArgument key) {
+ return Optional.fromNullable(children.get(key));
+ }
+
+ @Override
+ public String toString() {
+ return "StoreMetadataNode [identifier=" + getIdentifier() + ", nodeVersion=" + nodeVersion + "]";
+ }
+
+ public static Optional<UnsignedLong> getVersion(final Optional<StoreMetadataNode> currentMetadata) {
+ if (currentMetadata.isPresent()) {
+ return Optional.of(currentMetadata.get().getNodeVersion());
+ }
+ return Optional.absent();
+ }
+
+ public static Optional<StoreMetadataNode> getChild(final Optional<StoreMetadataNode> parent,
+ final PathArgument child) {
+ if (parent.isPresent()) {
+ return parent.get().getChild(child);
+ }
+ return Optional.absent();
+ }
+
+ public static final StoreMetadataNode createRecursivelly(final NormalizedNode<?, ?> node,
+ final UnsignedLong nodeVersion, final UnsignedLong subtreeVersion) {
+ Builder builder = builder() //
+ .setNodeVersion(nodeVersion) //
+ .setSubtreeVersion(subtreeVersion) //
+ .setData(node);
+ if (node instanceof NormalizedNodeContainer<?, ?, ?>) {
+
+ @SuppressWarnings("unchecked")
+ NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>> nodeContainer = (NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>>) node;
+ for (NormalizedNode<?, ?> subNode : nodeContainer.getValue()) {
+ builder.add(createRecursivelly(subNode, nodeVersion, subtreeVersion));
+ }
+ }
+ return builder.build();
+ }
+
+ public static class Builder {
+
+ private UnsignedLong nodeVersion;
+ private UnsignedLong subtreeVersion;
+ private NormalizedNode<?, ?> data;
+ private final ImmutableMap.Builder<PathArgument, StoreMetadataNode> children = ImmutableMap.builder();
+
+ private Builder() {}
+
+
+ public UnsignedLong getVersion() {
+ return nodeVersion;
+
+ }
+
+ public Builder setNodeVersion(final UnsignedLong version) {
+ this.nodeVersion = version;
+ return this;
+ }
+
+ public Builder setSubtreeVersion(final UnsignedLong version) {
+ this.subtreeVersion = version;
+ return this;
+ }
+
+ public Builder setData(final NormalizedNode<?, ?> data) {
+ this.data = data;
+ return this;
+ }
+
+ public Builder add(final StoreMetadataNode node) {
+ children.put(node.getIdentifier(), node);
+ return this;
+ }
+
+ public StoreMetadataNode build() {
+ checkState(data != null, "Data node should not be null.");
+ checkState(subtreeVersion.compareTo(nodeVersion) >= 0,
+ "Subtree version must be equals or greater than node version.");
+ return new StoreMetadataNode(data, nodeVersion, subtreeVersion, children.build());
+ }
+ }
+
+ public static StoreMetadataNode createRecursivelly(final NormalizedNode<?, ?> node, final UnsignedLong version) {
+ return createRecursivelly(node, version, version);
+ }
+
+}
--- /dev/null
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.md.sal.dom.store.impl.tree;
+
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
+import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
+
+import com.google.common.base.Optional;
+import com.google.common.primitives.UnsignedLong;
+
+/**
+ *
+ * Helper builder
+ *
+ *
+ */
+@SuppressWarnings("rawtypes")
+public class StoreNodeCompositeBuilder {
+
+ private final StoreMetadataNode.Builder metadata;
+
+ private final NormalizedNodeContainerBuilder data;
+
+ private StoreNodeCompositeBuilder(final NormalizedNodeContainerBuilder nodeBuilder) {
+ this.metadata = StoreMetadataNode.builder();
+ this.data = nodeBuilder;
+ }
+
+ @SuppressWarnings("unchecked")
+ public StoreNodeCompositeBuilder add(final StoreMetadataNode node) {
+ metadata.add(node);
+ data.addChild(node.getData());
+ return this;
+ }
+
+ @SuppressWarnings("unchecked")
+ public StoreNodeCompositeBuilder addIfPresent(final Optional<StoreMetadataNode> potential) {
+ if (potential.isPresent()) {
+ StoreMetadataNode node = potential.get();
+ metadata.add(node);
+ data.addChild(node.getData());
+ }
+ return this;
+ }
+
+ public StoreMetadataNode build() {
+ return metadata.setData(data.build()).build();
+ }
+
+ public static StoreNodeCompositeBuilder from(final NormalizedNodeContainerBuilder nodeBuilder) {
+ return new StoreNodeCompositeBuilder(nodeBuilder);
+ }
+
+ @SuppressWarnings("unchecked")
+ public StoreNodeCompositeBuilder setIdentifier(final PathArgument identifier) {
+ data.withNodeIdentifier(identifier);
+ return this;
+ }
+
+ public StoreNodeCompositeBuilder setNodeVersion(final UnsignedLong nodeVersion) {
+ metadata.setNodeVersion(nodeVersion);
+ return this;
+ }
+
+ public StoreNodeCompositeBuilder setSubtreeVersion(final UnsignedLong updatedSubtreeVersion) {
+ metadata.setSubtreeVersion(updatedSubtreeVersion);
+ return this;
+ }
+
+}
--- /dev/null
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.md.sal.dom.store.impl.tree;
+
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
+
+import com.google.common.base.Optional;
+/**
+ *
+ * Tree node which contains references to it's leafs
+ *
+ * @param <C> Final node type
+ */
+public interface StoreTreeNode<C extends StoreTreeNode<C>> {
+
+ /**
+ *
+ * Returns direct child of the node
+ *
+ * @param child Identifier of child
+ * @return Optional with node if the child is existing, {@link Optional#absent()} otherwise.
+ */
+ Optional<C> getChild(PathArgument child);
+}
--- /dev/null
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.md.sal.dom.store.impl.tree;
+
+import java.util.AbstractMap.SimpleEntry;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
+
+import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
+import com.google.common.base.Predicate;
+import com.google.common.base.Predicates;
+
+public class TreeNodeUtils {
+
+ /**
+ * Finds a node in tree
+ *
+ * @param tree Data Tree
+ * @param path Path to the node
+ * @return Optional with node if the node is present in tree, {@link Optional#absent()} otherwise.
+ *
+ */
+ public static <T extends StoreTreeNode<T>> Optional<T> findNode(final T tree, final InstanceIdentifier path) {
+ Optional<T> current = Optional.<T> of(tree);
+ Iterator<PathArgument> pathIter = path.getPath().iterator();
+ while (current.isPresent() && pathIter.hasNext()) {
+ current = current.get().getChild(pathIter.next());
+ }
+ return current;
+ }
+
+
+ public static <T extends StoreTreeNode<T>> T findNodeChecked(final T tree, final InstanceIdentifier path) {
+ T current = tree;
+ List<PathArgument> nested = new ArrayList<>(path.getPath());
+ for(PathArgument pathArg : path.getPath()) {
+ Optional<T> potential = current.getChild(pathArg);
+ nested.add(pathArg);
+ Preconditions.checkArgument(potential.isPresent(),"Child %s is not present in tree.",nested);
+ current = potential.get();
+ }
+ return current;
+ }
+
+ /**
+ * Finds a node or closest parent in the tree
+ *
+ * @param tree Data Tree
+ * @param path Path to the node
+ * @return Map.Entry Entry with key which is path to closest parent and value is parent node.
+ *
+ */
+ public static <T extends StoreTreeNode<T>> Map.Entry<InstanceIdentifier, T> findClosest(final T tree, final InstanceIdentifier path) {
+ return findClosestsOrFirstMatch(tree, path, Predicates.<T>alwaysFalse());
+ }
+
+ public static <T extends StoreTreeNode<T>> Map.Entry<InstanceIdentifier, T> findClosestsOrFirstMatch(final T tree, final InstanceIdentifier path, final Predicate<T> predicate) {
+ Optional<T> parent = Optional.<T>of(tree);
+ Optional<T> current = Optional.<T> of(tree);
+
+ int nesting = 0;
+ Iterator<PathArgument> pathIter = path.getPath().iterator();
+ while (current.isPresent() && pathIter.hasNext() && !predicate.apply(current.get())) {
+ parent = current;
+ current = current.get().getChild(pathIter.next());
+ nesting++;
+ }
+ if(current.isPresent()) {
+ final InstanceIdentifier currentPath = new InstanceIdentifier(path.getPath().subList(0, nesting));
+ return new SimpleEntry<InstanceIdentifier,T>(currentPath,current.get());
+ }
+ // Nesting minus one is safe, since current is allways present when nesting = 0
+ // so this prat of code is never triggered, in cases nesting == 0;
+ final InstanceIdentifier parentPath = new InstanceIdentifier(path.getPath().subList(0, nesting - 1));
+ return new SimpleEntry<InstanceIdentifier,T>(parentPath,parent.get());
+
+ }
+
+ public static <T extends StoreTreeNode<T>> Optional<T> getChild(final Optional<T> parent,final PathArgument child) {
+ if(parent.isPresent()) {
+ return parent.get().getChild(child);
+ }
+ return Optional.absent();
+ }
+
+}
import org.osgi.framework.ServiceRegistration
import org.opendaylight.controller.sal.dom.broker.impl.SchemaContextProviders
import org.opendaylight.controller.sal.core.api.RpcProvisionRegistry
+import org.opendaylight.controller.md.sal.dom.broker.impl.compat.BackwardsCompatibleDataBroker
+import org.opendaylight.controller.md.sal.dom.broker.impl.DOMDataBrokerImpl
+import com.google.common.util.concurrent.MoreExecutors
+import com.google.common.collect.ImmutableMap
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType
+import org.opendaylight.controller.sal.core.spi.data.DOMStore
+import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore
+import java.util.concurrent.Executors
+import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker
class BrokerConfigActivator implements AutoCloseable {
private static val ROOT = InstanceIdentifier.builder().toInstance();
@Property
- private var DataBrokerImpl dataService;
+ private var DataProviderService dataService;
private var ServiceRegistration<DataBrokerService> dataReg;
private var ServiceRegistration<DataProviderService> dataProviderReg;
SchemaAwareDataStoreAdapter wrappedStore
- public def void start(BrokerImpl broker, DataStore store, BundleContext context) {
+ public def void start(BrokerImpl broker, DataStore store, DOMDataBroker asyncBroker,BundleContext context) {
val emptyProperties = new Hashtable<String, String>();
broker.setBundleContext(context);
schemaService = context.getService(serviceRef);
broker.setRouter(new SchemaAwareRpcBroker("/", SchemaContextProviders.fromSchemaService(schemaService)));
-
- dataService = new DataBrokerImpl();
- //dataService.setExecutor(broker.getExecutor());
-
- dataReg = context.registerService(DataBrokerService, dataService, emptyProperties);
- dataProviderReg = context.registerService(DataProviderService, dataService, emptyProperties);
-
- wrappedStore = new SchemaAwareDataStoreAdapter();
- wrappedStore.changeDelegate(store);
- wrappedStore.setValidationEnabled(false);
-
- context.registerService(SchemaServiceListener, wrappedStore, emptyProperties)
-
- dataService.registerConfigurationReader(ROOT, wrappedStore);
- dataService.registerCommitHandler(ROOT, wrappedStore);
- dataService.registerOperationalReader(ROOT, wrappedStore);
+
+
+ if(asyncBroker == null) {
+ dataService = new DataBrokerImpl();
+ dataProviderReg = context.registerService(DataProviderService, dataService, emptyProperties);
+
+ wrappedStore = new SchemaAwareDataStoreAdapter();
+ wrappedStore.changeDelegate(store);
+ wrappedStore.setValidationEnabled(false);
+ context.registerService(SchemaServiceListener, wrappedStore, emptyProperties)
+
+ dataService.registerConfigurationReader(ROOT, wrappedStore);
+ dataService.registerCommitHandler(ROOT, wrappedStore);
+ dataService.registerOperationalReader(ROOT, wrappedStore);
+ } else {
+ val compatibleDataBroker = new BackwardsCompatibleDataBroker(asyncBroker);
+ context.registerService(SchemaServiceListener,compatibleDataBroker,emptyProperties);
+ dataService = compatibleDataBroker;
+ }
+
+
+//
mountService = new MountPointManagerImpl();
- mountService.setDataBroker(dataService);
-
- mountReg = context.registerService(MountService, mountService, emptyProperties);
+ dataReg = context.registerService(DataBrokerService, dataService, emptyProperties);
+ mountReg = context.registerService(MountService, mountService, emptyProperties);
mountProviderReg = context.registerService(MountProvisionService, mountService, emptyProperties);
rpcProvisionRegistryReg = context.registerService(RpcProvisionRegistry, broker.getRouter(), emptyProperties);
private ListenerRegistry<SchemaServiceListener> listeners;
private BundleContext context;
- private BundleScanner scanner = new BundleScanner();
+ private final BundleScanner scanner = new BundleScanner();
private BundleTracker<ImmutableSet<Registration<URL>>> bundleTracker;
return listeners;
}
- public void setListeners(ListenerRegistry<SchemaServiceListener> listeners) {
+ public void setListeners(final ListenerRegistry<SchemaServiceListener> listeners) {
this.listeners = listeners;
}
return context;
}
- public void setContext(BundleContext context) {
+ public void setContext(final BundleContext context) {
this.context = context;
}
return getGlobalContext();
}
+ @Override
public SchemaContext getGlobalContext() {
return contextResolver.getSchemaContext().orNull();
}
@Override
- public void addModule(Module module) {
+ public void addModule(final Module module) {
throw new UnsupportedOperationException();
}
}
@Override
- public void removeModule(Module module) {
+ public void removeModule(final Module module) {
throw new UnsupportedOperationException();
}
@Override
- public ListenerRegistration<SchemaServiceListener> registerSchemaServiceListener(SchemaServiceListener listener) {
+ public ListenerRegistration<SchemaServiceListener> registerSchemaServiceListener(final SchemaServiceListener listener) {
+ Optional<SchemaContext> potentialCtx = contextResolver.getSchemaContext();
+ if(potentialCtx.isPresent()) {
+ listener.onGlobalContextUpdated(potentialCtx.get());
+ }
return listeners.register(listener);
}
}
- private void updateContext(SchemaContext snapshot) {
+ private void updateContext(final SchemaContext snapshot) {
Object[] services = listenerTracker.getServices();
if (services != null) {
for (Object rawListener : services) {
private class BundleScanner implements BundleTrackerCustomizer<ImmutableSet<Registration<URL>>> {
@Override
- public ImmutableSet<Registration<URL>> addingBundle(Bundle bundle, BundleEvent event) {
+ public ImmutableSet<Registration<URL>> addingBundle(final Bundle bundle, final BundleEvent event) {
if (bundle.getBundleId() == 0) {
return ImmutableSet.of();
}
@Override
- public void modifiedBundle(Bundle bundle, BundleEvent event, ImmutableSet<Registration<URL>> object) {
+ public void modifiedBundle(final Bundle bundle, final BundleEvent event, final ImmutableSet<Registration<URL>> object) {
logger.debug("Modified bundle {} {} {}", bundle, event, object);
}
*/
@Override
- public synchronized void removedBundle(Bundle bundle, BundleEvent event, ImmutableSet<Registration<URL>> urls) {
+ public synchronized void removedBundle(final Bundle bundle, final BundleEvent event, final ImmutableSet<Registration<URL>> urls) {
for (Registration<URL> url : urls) {
try {
url.close();
}
@Override
- public SchemaServiceListener addingService(ServiceReference<SchemaServiceListener> reference) {
+ public SchemaServiceListener addingService(final ServiceReference<SchemaServiceListener> reference) {
SchemaServiceListener listener = context.getService(reference);
SchemaContext _ctxContext = getGlobalContext();
}
@Override
- public void modifiedService(ServiceReference<SchemaServiceListener> reference, SchemaServiceListener service) {
+ public void modifiedService(final ServiceReference<SchemaServiceListener> reference, final SchemaServiceListener service) {
// NOOP
}
@Override
- public void removedService(ServiceReference<SchemaServiceListener> reference, SchemaServiceListener service) {
+ public void removedService(final ServiceReference<SchemaServiceListener> reference, final SchemaServiceListener service) {
context.ungetService(reference);
}
}
--- /dev/null
+package org.opendaylight.controller.sal.dom.broker.osgi;
+
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
+import org.opendaylight.controller.md.sal.dom.api.DOMDataChangeListener;
+import org.opendaylight.controller.md.sal.dom.api.DOMDataReadTransaction;
+import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
+import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
+import org.opendaylight.yangtools.concepts.ListenerRegistration;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
+import org.osgi.framework.ServiceReference;
+
+public class DOMDataBrokerProxy extends AbstractBrokerServiceProxy<DOMDataBroker> implements DOMDataBroker {
+
+ public DOMDataBrokerProxy(final ServiceReference<DOMDataBroker> ref, final DOMDataBroker delegate) {
+ super(ref, delegate);
+ }
+
+ @Override
+ public DOMDataReadTransaction newReadOnlyTransaction() {
+ return getDelegate().newReadOnlyTransaction();
+ }
+
+ @Override
+ public DOMDataReadWriteTransaction newReadWriteTransaction() {
+ return getDelegate().newReadWriteTransaction();
+ }
+
+ @Override
+ public DOMDataWriteTransaction newWriteOnlyTransaction() {
+ return getDelegate().newWriteOnlyTransaction();
+ }
+
+ @Override
+ public ListenerRegistration<DOMDataChangeListener> registerDataChangeListener(final LogicalDatastoreType store,
+ final InstanceIdentifier path, final DOMDataChangeListener listener,
+ final org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope triggeringScope) {
+ return getDelegate().registerDataChangeListener(store, path, listener, triggeringScope);
+ }
+
+}
import org.opendaylight.controller.sal.core.api.model.SchemaService
import org.opendaylight.controller.sal.core.api.mount.MountProvisionService
import org.opendaylight.controller.sal.core.api.RpcProvisionRegistry
+import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker
class ProxyFactory {
return createProxyImpl(serviceRef, service) as T;
}
+
private static def dispatch createProxyImpl(ServiceReference<?> ref, DataBrokerService service) {
new DataBrokerServiceProxy(ref as ServiceReference<DataBrokerService>, service);
}
private static def dispatch createProxyImpl(ServiceReference<?> ref, RpcProvisionRegistry service) {
new RpcProvisionRegistryProxy(ref as ServiceReference<RpcProvisionRegistry>, service);
}
+
+ private static def dispatch createProxyImpl(ServiceReference<?> ref, DOMDataBroker service) {
+ new DOMDataBrokerProxy(ref as ServiceReference<DOMDataBroker>, service)
+ }
+
private static def dispatch createProxyImpl(ServiceReference<?> reference, BrokerService service) {
throw new IllegalArgumentException("Not supported class");
config:java-name-prefix DomBrokerImpl;
}
+
+ identity dom-inmemory-data-broker {
+ base config:module-type;
+ config:provided-service sal:dom-async-data-broker;
+ }
+
identity hash-map-data-store {
base config:module-type;
config:provided-service sal:dom-data-store;
container data-store {
uses config:service-ref {
refine type {
- mandatory true;
+ mandatory false;
config:required-identity sal:dom-data-store;
}
}
}
+
+ container async-data-broker {
+ uses config:service-ref {
+ refine type {
+ mandatory false;
+ config:required-identity sal:dom-async-data-broker;
+ }
+ }
+
+ }
+ }
+ }
+
+ augment "/config:modules/config:module/config:configuration" {
+ case dom-inmemory-data-broker {
+ when "/config:modules/config:module/config:type = 'dom-inmemory-data-broker'";
+
+ container schema-service {
+ uses config:service-ref {
+ refine type {
+ mandatory false;
+ config:required-identity sal:schema-service;
+ }
+ }
+
+ }
}
}
--- /dev/null
+package org.opendaylight.controller.md.sal.dom.broker.impl;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION;
+import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.OPERATIONAL;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.Callable;
+import java.util.concurrent.Executors;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.controller.md.sal.dom.api.DOMDataReadTransaction;
+import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
+import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
+import org.opendaylight.controller.md.sal.dom.store.impl.TestModel;
+import org.opendaylight.controller.sal.core.spi.data.DOMStore;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.Optional;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.ListeningExecutorService;
+import com.google.common.util.concurrent.MoreExecutors;
+
+public class DOMBrokerPerformanceTest {
+
+ private static final Logger log = LoggerFactory.getLogger(DOMBrokerPerformanceTest.class);
+
+ private static NormalizedNode<?, ?> outerList(final int i) {
+ return ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, i);
+ }
+
+ private static InstanceIdentifier outerListPath(final int i) {
+ return InstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)//
+ .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, i) //
+ .build();
+ }
+
+ private SchemaContext schemaContext;
+ private DOMDataBrokerImpl domBroker;
+
+ private static <V> V measure(final String name, final Callable<V> callable) throws Exception {
+ // TODO Auto-generated method stub
+ log.debug("Measurement:{} Start", name);
+ long startNano = System.nanoTime();
+ try {
+ return callable.call();
+ } finally {
+ long endNano = System.nanoTime();
+ log.info("Measurement:\"{}\" Time:{} ms", name, (endNano - startNano) / 1000000.0d);
+ }
+ }
+
+ @Before
+ public void setupStore() {
+ InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("OPER", MoreExecutors.sameThreadExecutor());
+ InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG", MoreExecutors.sameThreadExecutor());
+ schemaContext = TestModel.createTestContext();
+
+ operStore.onGlobalContextUpdated(schemaContext);
+ configStore.onGlobalContextUpdated(schemaContext);
+
+ ImmutableMap<LogicalDatastoreType, DOMStore> stores = ImmutableMap.<LogicalDatastoreType, DOMStore> builder() //
+ .put(CONFIGURATION, configStore) //
+ .put(OPERATIONAL, operStore) //
+ .build();
+ ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
+ domBroker = new DOMDataBrokerImpl(stores, executor);
+ }
+
+ @Test
+ public void testPerformance() throws Exception {
+ measure("Test Suite (all tests)", new Callable<Void>() {
+
+ @Override
+ public Void call() throws Exception {
+ smallTestSuite(10, 1000);
+ //smallTestSuite(10, 100);
+ smallTestSuite(100, 100);
+ //smallTestSuite(100, 100);
+ //smallTestSuite(1000, 10);
+ smallTestSuite(1000, 10);
+ //smallTestSuite(1000, 1000);
+ return null;
+ }
+ });
+ }
+
+ private void smallTestSuite(final int txNum, final int innerListWriteNum) throws Exception {
+ measure("TestSuite (Txs:" + txNum + " innerWrites:" + innerListWriteNum + ")", new Callable<Void>() {
+
+ @Override
+ public Void call() throws Exception {
+ measureOneTransactionTopContainer();
+ measureSeparateWritesOneLevel(txNum, innerListWriteNum);
+ return null;
+ }
+ });
+ }
+
+ private void measureSeparateWritesOneLevel(final int txNum, final int innerNum) throws Exception {
+ final List<DOMDataReadWriteTransaction> transactions = measure("Txs:"+ txNum + " Allocate",
+ new Callable<List<DOMDataReadWriteTransaction>>() {
+ @Override
+ public List<DOMDataReadWriteTransaction> call() throws Exception {
+ List<DOMDataReadWriteTransaction> builder = new ArrayList<>(txNum);
+ for (int i = 0; i < txNum; i++) {
+ DOMDataReadWriteTransaction writeTx = domBroker.newReadWriteTransaction();
+ builder.add(writeTx);
+ }
+ return builder;
+ }
+ });
+ assertEquals(txNum, transactions.size());
+ measure("Txs:"+ txNum + " Writes:1", new Callable<Void>() {
+ @Override
+ public Void call() throws Exception {
+ int i = 0;
+ for (DOMDataReadWriteTransaction writeTx :transactions) {
+ // Writes /test/outer-list/i in writeTx
+ writeTx.put(OPERATIONAL, outerListPath(i), outerList(i));
+ i++;
+ }
+ return null;
+ }
+ });
+
+ measure("Txs:"+ txNum + " Writes:" + innerNum, new Callable<Void>() {
+ @Override
+ public Void call() throws Exception {
+ int i = 0;
+ for (DOMDataReadWriteTransaction writeTx :transactions) {
+ // Writes /test/outer-list/i in writeTx
+ InstanceIdentifier path = InstanceIdentifier.builder(outerListPath(i))
+ .node(TestModel.INNER_LIST_QNAME).build();
+ writeTx.put(OPERATIONAL, path, ImmutableNodes.mapNodeBuilder(TestModel.INNER_LIST_QNAME).build());
+ for (int j = 0; j < innerNum; j++) {
+ InstanceIdentifier innerPath = InstanceIdentifier.builder(path)
+ .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, String.valueOf(j))
+ .build();
+ writeTx.put(
+ OPERATIONAL,
+ innerPath,
+ ImmutableNodes.mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME,
+ String.valueOf(j)));
+ }
+ i++;
+ }
+ return null;
+ }
+ });
+
+ measure("Txs:" + txNum + " Submit, Finish", new Callable<Void>() {
+ @Override
+ public Void call() throws Exception {
+ List<ListenableFuture<?>> allFutures = measure(txNum + " Submits",
+ new Callable<List<ListenableFuture<?>>>() {
+ @Override
+ public List<ListenableFuture<?>> call() throws Exception {
+ List<ListenableFuture<?>> builder = new ArrayList<>(txNum);
+ for (DOMDataReadWriteTransaction tx :transactions) {
+ builder.add(tx.commit());
+ }
+ return builder;
+ }
+ });
+ Futures.allAsList(allFutures).get();
+ return null;
+ }
+ });
+
+ final DOMDataReadTransaction readTx = measure("Txs:1 (ro), Allocate", new Callable<DOMDataReadTransaction>() {
+ @Override
+ public DOMDataReadTransaction call() throws Exception {
+ return domBroker.newReadOnlyTransaction();
+
+ }
+ });
+
+
+ measure("Txs:1 (ro) Reads:" + txNum + " (1-level)" , new Callable<Void>() {
+ @Override
+ public Void call() throws Exception {
+ for (int i = 0; i < txNum; i++) {
+ ListenableFuture<Optional<NormalizedNode<?, ?>>> potential = readTx.read(OPERATIONAL,
+ outerListPath(i));
+ assertTrue("outerList/" + i, potential.get().isPresent());
+ }
+ return null;
+ }
+ });
+
+ measure("Txs:1 (ro) Reads:" + txNum * innerNum + " (2-level)", new Callable<Void>() {
+ @Override
+ public Void call() throws Exception {
+ for (int i = 0; i < txNum; i++) {
+ for (int j = 0; j < innerNum; j++) {
+ InstanceIdentifier path = InstanceIdentifier
+ .builder(outerListPath(i))
+ //
+ .node(TestModel.INNER_LIST_QNAME)
+ .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, String.valueOf(j))
+ .build();
+ ListenableFuture<Optional<NormalizedNode<?, ?>>> potential = readTx.read(OPERATIONAL, path);
+ assertTrue("outer-list/" + i + "/inner-list/" + j, potential.get().isPresent());
+ }
+ }
+ return null;
+ }
+ });
+ }
+
+ private void measureOneTransactionTopContainer() throws Exception {
+
+ final DOMDataReadWriteTransaction writeTx = measure("Txs:1 Allocate", new Callable<DOMDataReadWriteTransaction>() {
+ @Override
+ public DOMDataReadWriteTransaction call() throws Exception {
+ return domBroker.newReadWriteTransaction();
+ }
+ });
+
+ measure("Txs:1 Write", new Callable<Void>() {
+ @Override
+ public Void call() throws Exception {
+ writeTx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
+ writeTx.put(OPERATIONAL, TestModel.OUTER_LIST_PATH,
+ ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
+ return null;
+ }
+ });
+
+ measure("Txs:1 Reads:1", new Callable<Void>() {
+ @Override
+ public Void call() throws Exception {
+ // Reads /test in writeTx
+ ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(OPERATIONAL,
+ TestModel.TEST_PATH);
+ assertTrue(writeTxContainer.get().isPresent());
+ return null;
+ }
+ });
+
+ measure("Txs:1 Reads:1", new Callable<Void>() {
+ @Override
+ public Void call() throws Exception {
+ // Reads /test in writeTx
+ ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(OPERATIONAL,
+ TestModel.TEST_PATH);
+ assertTrue(writeTxContainer.get().isPresent());
+ return null;
+ }
+ });
+
+ measure("Txs:1 Submit, Finish", new Callable<Void>() {
+ @Override
+ public Void call() throws Exception {
+ measure("Txs:1 Submit", new Callable<ListenableFuture<?>>() {
+ @Override
+ public ListenableFuture<?> call() throws Exception {
+ return writeTx.commit();
+ }
+ }).get();
+ return null;
+ }
+ });
+ }
+}
--- /dev/null
+package org.opendaylight.controller.md.sal.dom.broker.impl;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION;
+import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.OPERATIONAL;
+
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Executors;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.controller.md.sal.dom.api.DOMDataReadTransaction;
+import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
+import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
+import org.opendaylight.controller.md.sal.dom.store.impl.TestModel;
+import org.opendaylight.controller.sal.core.spi.data.DOMStore;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+
+import com.google.common.base.Optional;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.ListeningExecutorService;
+import com.google.common.util.concurrent.MoreExecutors;
+
+public class DOMBrokerTest {
+
+ private SchemaContext schemaContext;
+ private DOMDataBrokerImpl domBroker;
+
+ @Before
+ public void setupStore() {
+ InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("OPER", MoreExecutors.sameThreadExecutor());
+ InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG", MoreExecutors.sameThreadExecutor());
+ schemaContext = TestModel.createTestContext();
+
+ operStore.onGlobalContextUpdated(schemaContext);
+ configStore.onGlobalContextUpdated(schemaContext);
+
+ ImmutableMap<LogicalDatastoreType, DOMStore> stores = ImmutableMap.<LogicalDatastoreType, DOMStore> builder() //
+ .put(CONFIGURATION, configStore) //
+ .put(OPERATIONAL, operStore) //
+ .build();
+
+ ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
+ domBroker = new DOMDataBrokerImpl(stores, executor);
+ }
+
+ @Test
+ public void testTransactionIsolation() throws InterruptedException, ExecutionException {
+
+ assertNotNull(domBroker);
+
+ DOMDataReadTransaction readTx = domBroker.newReadOnlyTransaction();
+ assertNotNull(readTx);
+
+ DOMDataReadWriteTransaction writeTx = domBroker.newReadWriteTransaction();
+ assertNotNull(writeTx);
+ /**
+ *
+ * Writes /test in writeTx
+ *
+ */
+ writeTx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
+
+ /**
+ *
+ * Reads /test from writeTx Read should return container.
+ *
+ */
+ ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(OPERATIONAL,
+ TestModel.TEST_PATH);
+ assertTrue(writeTxContainer.get().isPresent());
+
+ /**
+ *
+ * Reads /test from readTx Read should return Absent.
+ *
+ */
+ ListenableFuture<Optional<NormalizedNode<?, ?>>> readTxContainer = readTx
+ .read(OPERATIONAL, TestModel.TEST_PATH);
+ assertFalse(readTxContainer.get().isPresent());
+ }
+
+ @Test
+ public void testTransactionCommit() throws InterruptedException, ExecutionException {
+
+ DOMDataReadWriteTransaction writeTx = domBroker.newReadWriteTransaction();
+ assertNotNull(writeTx);
+ /**
+ *
+ * Writes /test in writeTx
+ *
+ */
+ writeTx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
+
+ /**
+ *
+ * Reads /test from writeTx Read should return container.
+ *
+ */
+ ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(OPERATIONAL,
+ TestModel.TEST_PATH);
+ assertTrue(writeTxContainer.get().isPresent());
+
+ writeTx.commit().get();
+
+ Optional<NormalizedNode<?, ?>> afterCommitRead = domBroker.newReadOnlyTransaction()
+ .read(OPERATIONAL, TestModel.TEST_PATH).get();
+ assertTrue(afterCommitRead.isPresent());
+ }
+
+
+
+}
--- /dev/null
+package org.opendaylight.controller.md.sal.dom.broker.impl;
+
+import static org.junit.Assert.assertNotNull;
+
+import org.junit.Test;
+import org.opendaylight.controller.md.sal.common.impl.util.compat.DataNormalizer;
+import org.opendaylight.controller.md.sal.dom.store.impl.TestModel;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates;
+import org.opendaylight.yangtools.yang.data.api.Node;
+import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
+import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
+import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
+import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+
+public class DataNormalizerTest {
+
+ private static final Short OUTER_LIST_ID = (short)10;
+
+ private static final InstanceIdentifier OUTER_LIST_PATH_LEGACY = InstanceIdentifier.builder(TestModel.TEST_QNAME)
+ .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME,OUTER_LIST_ID).build();
+
+ private static final InstanceIdentifier LEAF_TWO_PATH_LEGACY = InstanceIdentifier.builder(OUTER_LIST_PATH_LEGACY)
+ .node(TestModel.TWO_QNAME).build();
+
+ private static final ChoiceNode OUTER_CHOICE_ITEM = Builders.choiceBuilder()
+ .withNodeIdentifier(new NodeIdentifier(TestModel.OUTER_CHOICE_QNAME))
+ .withChild(ImmutableNodes.leafNode(TestModel.TWO_QNAME, "two"))
+ .withChild(ImmutableNodes.leafNode(TestModel.THREE_QNAME, "three"))
+ .build();
+
+ private static final MapEntryNode OUTER_LIST_WITHOUT_CHOICE = Builders.mapEntryBuilder()
+ .withNodeIdentifier(new NodeIdentifierWithPredicates(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME,OUTER_LIST_ID))
+ .withChild(ImmutableNodes.leafNode(TestModel.ID_QNAME, OUTER_LIST_ID))
+ .build();
+
+ private static final MapEntryNode OUTER_LIST_WITH_CHOICE = Builders.mapEntryBuilder()
+ .withNodeIdentifier(new NodeIdentifierWithPredicates(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME,OUTER_LIST_ID))
+ .withChild(ImmutableNodes.leafNode(TestModel.ID_QNAME, OUTER_LIST_ID))
+ .withChild(OUTER_CHOICE_ITEM)
+ .build();
+
+ @Test
+ public void test() {
+ SchemaContext testCtx = TestModel.createTestContext();
+ DataNormalizer normalizer = new DataNormalizer(testCtx);
+
+ InstanceIdentifier normalizedPath = normalizer.toNormalized(LEAF_TWO_PATH_LEGACY);
+
+ Node<?> outerListLegacy = normalizer.toLegacy(OUTER_LIST_WITH_CHOICE);
+ assertNotNull(outerListLegacy);
+
+
+
+
+ }
+
+}
--- /dev/null
+package org.opendaylight.controller.md.sal.dom.store.impl;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.util.concurrent.ExecutionException;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
+import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
+import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+
+import com.google.common.base.Optional;
+import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.MoreExecutors;
+
+public class InMemoryDataStoreTest {
+
+ private SchemaContext schemaContext;
+ private InMemoryDOMDataStore domStore;
+
+
+ @Before
+ public void setupStore() {
+ domStore = new InMemoryDOMDataStore("TEST", MoreExecutors.sameThreadExecutor());
+ schemaContext = TestModel.createTestContext();
+ domStore.onGlobalContextUpdated(schemaContext);
+
+ }
+
+
+ @Test
+ public void testTransactionIsolation() throws InterruptedException, ExecutionException {
+
+ assertNotNull(domStore);
+
+
+ DOMStoreReadTransaction readTx = domStore.newReadOnlyTransaction();
+ assertNotNull(readTx);
+
+ DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
+ assertNotNull(writeTx);
+ /**
+ *
+ * Writes /test in writeTx
+ *
+ */
+ writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
+
+ /**
+ *
+ * Reads /test from writeTx
+ * Read should return container.
+ *
+ */
+ ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(TestModel.TEST_PATH);
+ assertTrue(writeTxContainer.get().isPresent());
+
+ /**
+ *
+ * Reads /test from readTx
+ * Read should return Absent.
+ *
+ */
+ ListenableFuture<Optional<NormalizedNode<?, ?>>> readTxContainer = readTx.read(TestModel.TEST_PATH);
+ assertFalse(readTxContainer.get().isPresent());
+ }
+
+ @Test
+ public void testTransactionCommit() throws InterruptedException, ExecutionException {
+
+ DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
+ assertNotNull(writeTx);
+ /**
+ *
+ * Writes /test in writeTx
+ *
+ */
+ writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
+
+ /**
+ *
+ * Reads /test from writeTx
+ * Read should return container.
+ *
+ */
+ ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(TestModel.TEST_PATH);
+ assertTrue(writeTxContainer.get().isPresent());
+
+ DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
+
+ assertThreePhaseCommit(cohort);
+
+ Optional<NormalizedNode<?, ?>> afterCommitRead = domStore.newReadOnlyTransaction().read(TestModel.TEST_PATH).get();
+ assertTrue(afterCommitRead.isPresent());
+ }
+
+ @Test
+ public void testTransactionAbort() throws InterruptedException, ExecutionException {
+
+ DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
+ assertNotNull(writeTx);
+
+ assertTestContainerWrite(writeTx);
+
+ DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
+
+ assertTrue(cohort.canCommit().get().booleanValue());
+ cohort.preCommit().get();
+ cohort.abort().get();
+
+ Optional<NormalizedNode<?, ?>> afterCommitRead = domStore.newReadOnlyTransaction().read(TestModel.TEST_PATH).get();
+ assertFalse(afterCommitRead.isPresent());
+ }
+
+ @Test
+ public void testTransactionConflict() throws InterruptedException, ExecutionException {
+ DOMStoreReadWriteTransaction txOne = domStore.newReadWriteTransaction();
+ DOMStoreReadWriteTransaction txTwo = domStore.newReadWriteTransaction();
+ assertTestContainerWrite(txOne);
+ assertTestContainerWrite(txTwo);
+
+ /**
+ * Commits transaction
+ */
+ assertThreePhaseCommit(txOne.ready());
+
+ /**
+ * Asserts that txTwo could not be commited
+ */
+ assertFalse(txTwo.ready().canCommit().get());
+ }
+
+
+
+ private static void assertThreePhaseCommit(final DOMStoreThreePhaseCommitCohort cohort) throws InterruptedException, ExecutionException {
+ assertTrue(cohort.canCommit().get().booleanValue());
+ cohort.preCommit().get();
+ cohort.commit().get();
+ }
+
+
+ private static Optional<NormalizedNode<?, ?>> assertTestContainerWrite(final DOMStoreReadWriteTransaction writeTx) throws InterruptedException, ExecutionException {
+ /**
+ *
+ * Writes /test in writeTx
+ *
+ */
+ writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
+
+ /**
+ *
+ * Reads /test from writeTx
+ * Read should return container.
+ *
+ */
+ ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(TestModel.TEST_PATH);
+ assertTrue(writeTxContainer.get().isPresent());
+ return writeTxContainer.get();
+ }
+
+}
--- /dev/null
+package org.opendaylight.controller.md.sal.dom.store.impl;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+import static org.opendaylight.controller.md.sal.dom.store.impl.TestModel.ID_QNAME;
+import static org.opendaylight.controller.md.sal.dom.store.impl.TestModel.INNER_LIST_QNAME;
+import static org.opendaylight.controller.md.sal.dom.store.impl.TestModel.NAME_QNAME;
+import static org.opendaylight.controller.md.sal.dom.store.impl.TestModel.OUTER_LIST_PATH;
+import static org.opendaylight.controller.md.sal.dom.store.impl.TestModel.OUTER_LIST_QNAME;
+import static org.opendaylight.controller.md.sal.dom.store.impl.TestModel.TEST_PATH;
+import static org.opendaylight.controller.md.sal.dom.store.impl.TestModel.TEST_QNAME;
+import static org.opendaylight.controller.md.sal.dom.store.impl.TestModel.VALUE_QNAME;
+import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntry;
+import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntryBuilder;
+import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapNodeBuilder;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.controller.md.sal.dom.store.impl.tree.StoreMetadataNode;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
+import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
+import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
+import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+
+import com.google.common.base.Optional;
+import com.google.common.primitives.UnsignedLong;
+
+/**
+ *
+ * Schema structure of document is
+ *
+ * <pre>
+ * container root {Â
+ * list list-a {
+ * key leaf-a;
+ * leaf leaf-a;
+ * choice choice-a {
+ * case one {
+ * leaf one;
+ * }
+ * case two-three {
+ * leaf two;
+ * leaf three;
+ * }
+ * }
+ * list list-b {
+ * key leaf-b;
+ * leaf leaf-b;
+ * }
+ * }
+ * }
+ * </pre>
+ *
+ */
+public class ModificationMetadataTreeTest {
+
+ private static final Short ONE_ID = 1;
+ private static final Short TWO_ID = 2;
+ private static final String TWO_ONE_NAME = "one";
+ private static final String TWO_TWO_NAME = "two";
+
+ private static final InstanceIdentifier OUTER_LIST_1_PATH = InstanceIdentifier.builder(OUTER_LIST_PATH)
+ .nodeWithKey(OUTER_LIST_QNAME, ID_QNAME, ONE_ID) //
+ .build();
+
+ private static final InstanceIdentifier OUTER_LIST_2_PATH = InstanceIdentifier.builder(OUTER_LIST_PATH)
+ .nodeWithKey(OUTER_LIST_QNAME, ID_QNAME, TWO_ID) //
+ .build();
+
+ private static final InstanceIdentifier TWO_TWO_PATH = InstanceIdentifier.builder(OUTER_LIST_2_PATH)
+ .node(INNER_LIST_QNAME) //
+ .nodeWithKey(INNER_LIST_QNAME, NAME_QNAME, TWO_TWO_NAME) //
+ .build();
+
+ private static final InstanceIdentifier TWO_TWO_VALUE_PATH = InstanceIdentifier.builder(TWO_TWO_PATH)
+ .node(VALUE_QNAME) //
+ .build();
+
+ private static final MapEntryNode BAR_NODE = mapEntryBuilder(OUTER_LIST_QNAME, ID_QNAME, TWO_ID) //
+ .withChild(mapNodeBuilder(INNER_LIST_QNAME) //
+ .withChild(mapEntry(INNER_LIST_QNAME, NAME_QNAME, TWO_ONE_NAME)) //
+ .withChild(mapEntry(INNER_LIST_QNAME, NAME_QNAME, TWO_TWO_NAME)) //
+ .build()) //
+ .build();
+
+ private SchemaContext schemaContext;
+
+ @Before
+ public void prepare() {
+ schemaContext = TestModel.createTestContext();
+ assertNotNull("Schema context must not be null.", schemaContext);
+ }
+
+ /**
+ * Returns a test document
+ *
+ * <pre>
+ * test
+ * outer-list
+ * id 1
+ * outer-list
+ * id 2
+ * inner-list
+ * name "one"
+ * inner-list
+ * name "two"
+ *
+ * </pre>
+ *
+ * @return
+ */
+ public NormalizedNode<?, ?> createDocumentOne() {
+ return ImmutableContainerNodeBuilder
+ .create()
+ .withNodeIdentifier(new NodeIdentifier(schemaContext.getQName()))
+ .withChild(createTestContainer()).build();
+
+ }
+
+ private ContainerNode createTestContainer() {
+ return ImmutableContainerNodeBuilder
+ .create()
+ .withNodeIdentifier(new NodeIdentifier(TEST_QNAME))
+ .withChild(
+ mapNodeBuilder(OUTER_LIST_QNAME)
+ .withChild(mapEntry(OUTER_LIST_QNAME, ID_QNAME, ONE_ID))
+ .withChild(BAR_NODE).build()).build();
+ }
+
+ @Test
+ public void basicReadWrites() {
+ MutableDataTree modificationTree = MutableDataTree.from(
+ DataAndMetadataSnapshot.builder() //
+ .setMetadataTree(StoreMetadataNode.createRecursivelly(createDocumentOne(), UnsignedLong.valueOf(5))) //
+ .setSchemaContext(schemaContext) //
+ .build(), new SchemaAwareApplyOperationRoot(schemaContext));
+ Optional<NormalizedNode<?, ?>> originalBarNode = modificationTree.read(OUTER_LIST_2_PATH);
+ assertTrue(originalBarNode.isPresent());
+ assertSame(BAR_NODE, originalBarNode.get());
+
+ // writes node to /outer-list/1/inner_list/two/value
+ modificationTree.write(TWO_TWO_VALUE_PATH, ImmutableNodes.leafNode(VALUE_QNAME, "test"));
+
+ // reads node to /outer-list/1/inner_list/two/value
+ // and checks if node is already present
+ Optional<NormalizedNode<?, ?>> barTwoCModified = modificationTree.read(TWO_TWO_VALUE_PATH);
+ assertTrue(barTwoCModified.isPresent());
+ assertEquals(ImmutableNodes.leafNode(VALUE_QNAME, "test"), barTwoCModified.get());
+
+ // delete node to /outer-list/1/inner_list/two/value
+ modificationTree.delete(TWO_TWO_VALUE_PATH);
+ Optional<NormalizedNode<?, ?>> barTwoCAfterDelete = modificationTree.read(TWO_TWO_VALUE_PATH);
+ assertFalse(barTwoCAfterDelete.isPresent());
+ }
+
+
+ public MutableDataTree createEmptyModificationTree() {
+ /**
+ * Creates empty Snapshot with associated schema context.
+ */
+ DataAndMetadataSnapshot emptySnapshot = DataAndMetadataSnapshot.createEmpty(schemaContext);
+
+ /**
+ *
+ * Creates Mutable Data Tree based on provided snapshot and schema
+ * context.
+ *
+ */
+ MutableDataTree modificationTree = MutableDataTree.from(emptySnapshot, new SchemaAwareApplyOperationRoot(
+ schemaContext));
+ return modificationTree;
+ }
+
+ @Test
+ public void createFromEmptyState() {
+
+ MutableDataTree modificationTree = createEmptyModificationTree();
+ /**
+ * Writes empty container node to /test
+ *
+ */
+ modificationTree.write(TEST_PATH, ImmutableNodes.containerNode(TEST_QNAME));
+
+ /**
+ * Writes empty list node to /test/outer-list
+ */
+ modificationTree.write(OUTER_LIST_PATH, ImmutableNodes.mapNodeBuilder(OUTER_LIST_QNAME).build());
+
+ /**
+ * Reads list node from /test/outer-list
+ */
+ Optional<NormalizedNode<?, ?>> potentialOuterList = modificationTree.read(OUTER_LIST_PATH);
+ assertTrue(potentialOuterList.isPresent());
+
+ /**
+ * Reads container node from /test and verifies that it contains test
+ * node
+ */
+ Optional<NormalizedNode<?, ?>> potentialTest = modificationTree.read(TEST_PATH);
+ ContainerNode containerTest = assertPresentAndType(potentialTest, ContainerNode.class);
+
+ /**
+ *
+ * Gets list from returned snapshot of /test and verifies it contains
+ * outer-list
+ *
+ */
+ assertPresentAndType(containerTest.getChild(new NodeIdentifier(OUTER_LIST_QNAME)), MapNode.class);
+
+ }
+
+ @Test
+ public void writeSubtreeReadChildren() {
+ MutableDataTree modificationTree = createEmptyModificationTree();
+ modificationTree.write(TEST_PATH, createTestContainer());
+ Optional<NormalizedNode<?, ?>> potential = modificationTree.read(TWO_TWO_PATH);
+ MapEntryNode node = assertPresentAndType(potential, MapEntryNode.class);
+ }
+
+ @Test
+ public void writeSubtreeDeleteChildren() {
+ MutableDataTree modificationTree = createEmptyModificationTree();
+ modificationTree.write(TEST_PATH, createTestContainer());
+
+ // We verify data are present
+ Optional<NormalizedNode<?, ?>> potentialBeforeDelete = modificationTree.read(TWO_TWO_PATH);
+ MapEntryNode node = assertPresentAndType(potentialBeforeDelete, MapEntryNode.class);
+
+ modificationTree.delete(TWO_TWO_PATH);
+ Optional<NormalizedNode<?, ?>> potentialAfterDelete = modificationTree.read(TWO_TWO_PATH);
+ assertFalse(potentialAfterDelete.isPresent());
+
+ }
+
+ private static <T> T assertPresentAndType(final Optional<?> potential, final Class<T> type) {
+ assertNotNull(potential);
+ assertTrue(potential.isPresent());
+ assertTrue(type.isInstance(potential.get()));
+ return type.cast(potential.get());
+ }
+
+}
--- /dev/null
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.md.sal.dom.store.impl;
+
+import java.io.InputStream;
+import java.util.Collections;
+import java.util.Set;
+
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.model.api.Module;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
+
+public class TestModel {
+
+ public static final QName TEST_QNAME = QName.create("urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test", "2014-03-13",
+ "test");
+ public static final QName OUTER_LIST_QNAME = QName.create(TEST_QNAME, "outer-list");
+ public static final QName INNER_LIST_QNAME = QName.create(TEST_QNAME, "inner-list");
+ public static final QName OUTER_CHOICE_QNAME = QName.create(TEST_QNAME, "outer-choice");
+ public static final QName ID_QNAME = QName.create(TEST_QNAME, "id");
+ public static final QName NAME_QNAME = QName.create(TEST_QNAME, "name");
+ public static final QName VALUE_QNAME = QName.create(TEST_QNAME, "value");
+ private static final String DATASTORE_TEST_YANG = "/odl-datastore-test.yang";
+
+ public static final InstanceIdentifier TEST_PATH = InstanceIdentifier.of(TEST_QNAME);
+ public static final InstanceIdentifier OUTER_LIST_PATH = InstanceIdentifier.builder(TEST_PATH).node(OUTER_LIST_QNAME).build();
+ public static final QName TWO_QNAME = QName.create(TEST_QNAME,"two");
+ public static final QName THREE_QNAME = QName.create(TEST_QNAME,"three");
+
+
+ public static final InputStream getDatastoreTestInputStream() {
+ return getInputStream(DATASTORE_TEST_YANG);
+ }
+
+ private static InputStream getInputStream(final String resourceName) {
+ return TestModel.class.getResourceAsStream(DATASTORE_TEST_YANG);
+ }
+
+ public static SchemaContext createTestContext() {
+ YangParserImpl parser = new YangParserImpl();
+ Set<Module> modules = parser.parseYangModelsFromStreams(Collections.singletonList(getDatastoreTestInputStream()));
+ return parser.resolveSchemaContext(modules);
+ }
+}
--- /dev/null
+module odl-datastore-test {
+ yang-version 1;
+ namespace "urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test";
+ prefix "store-test";
+
+ revision "2014-03-13" {
+ description "Initial revision.";
+ }
+
+ container test {
+ list outer-list {
+ key id;
+ leaf id {
+ type uint16;
+ }
+ choice outer-choice {
+ case one {
+ leaf one {
+ type string;
+ }
+ }
+ case two-three {
+ leaf two {
+ type string;
+ }
+ leaf three {
+ type string;
+ }
+ }
+ }
+ list inner-list {
+ key name;
+ leaf name {
+ type string;
+ }
+ leaf value {
+ type string;
+ }
+ }
+ }
+ }
+}
\ No newline at end of file
import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.Toaster;
import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToasterData;
import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToasterService;
-
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// Register to md-sal
opendaylightToaster.setNotificationProvider(getNotificationServiceDependency());
+ opendaylightToaster.setDataProvider(getDataBrokerDependency());
final BindingAwareBroker.RpcRegistration<ToasterService> rpcRegistration = getRpcRegistryDependency()
.addRpcImplementation(ToasterService.class, opendaylightToaster);
public void close() throws Exception {
rpcRegistration.close();
runtimeReg.close();
+ opendaylightToaster.close();
log.info("Toaster provider (instance {}) torn down.", this);
}
import java.util.Collections;
import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.opendaylight.controller.config.yang.config.toaster_provider.impl.ToasterProviderRuntimeMXBean;
import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
+import org.opendaylight.controller.sal.binding.api.data.DataBrokerService;
+import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
import org.opendaylight.controller.sal.common.util.Rpcs;
import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.DisplayString;
import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.MakeToastInput;
import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToasterBuilder;
import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToasterData;
import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToasterService;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.opendaylight.yangtools.yang.common.RpcError;
import org.opendaylight.yangtools.yang.common.RpcResult;
import org.slf4j.Logger;
import com.google.common.util.concurrent.Futures;
-public class OpendaylightToaster implements ToasterData, ToasterService, ToasterProviderRuntimeMXBean {
+public class OpendaylightToaster implements ToasterData, ToasterService, ToasterProviderRuntimeMXBean, AutoCloseable {
private static final Logger log = LoggerFactory.getLogger(OpendaylightToaster.class);
+ private static final InstanceIdentifier<Toaster> toasterIID = InstanceIdentifier.builder(Toaster.class).build();
private static final DisplayString toasterManufacturer = new DisplayString("Opendaylight");
private static final DisplayString toasterModelNumber = new DisplayString("Model 1 - Binding Aware");
- private final ToasterStatus toasterStatus;
private NotificationProviderService notificationProvider;
+ private DataBrokerService dataProvider;
private final ExecutorService executor;
private Future<RpcResult<Void>> currentTask;
public OpendaylightToaster() {
- toasterStatus = ToasterStatus.Down;
executor = Executors.newFixedThreadPool(1);
}
@Override
- public Toaster getToaster() {
+ public synchronized Toaster getToaster() {
ToasterBuilder tb = new ToasterBuilder();
tb //
.setToasterManufacturer(toasterManufacturer) //
- .setToasterModelNumber(toasterModelNumber) //
- .setToasterStatus(toasterStatus);
+ .setToasterModelNumber(toasterModelNumber) //
+ .setToasterStatus(currentTask == null ? ToasterStatus.Up : ToasterStatus.Down);
return tb.build();
}
@Override
- public Future<RpcResult<Void>> cancelToast() {
+ public synchronized Future<RpcResult<Void>> cancelToast() {
if (currentTask != null) {
cancelToastImpl();
}
}
@Override
- public Future<RpcResult<Void>> makeToast(MakeToastInput input) {
- // TODO Auto-generated method stub
- log.trace("makeToast - Received input for toast");
+ public synchronized Future<RpcResult<Void>> makeToast(MakeToastInput input) {
+ log.debug("makeToast - Received input for toast");
logToastInput(input);
if (currentTask != null) {
return inProgressError();
}
currentTask = executor.submit(new MakeToastTask(input));
+ updateStatus();
return currentTask;
}
this.notificationProvider = salService;
}
+ public void setDataProvider(DataBrokerService salDataProvider) {
+ this.dataProvider = salDataProvider;
+ updateStatus();
+ }
+
private void logToastInput(MakeToastInput input) {
String toastType = input.getToasterToastType().getName();
String toastDoneness = input.getToasterDoneness().toString();
return toastsMade.get();
}
+ private void updateStatus() {
+ if (dataProvider != null) {
+ final DataModificationTransaction t = dataProvider.beginTransaction();
+ t.removeOperationalData(toasterIID);
+ t.putOperationalData(toasterIID, getToaster());
+
+ try {
+ t.commit().get();
+ } catch (InterruptedException | ExecutionException e) {
+ log.warn("Failed to update toaster status, operational otherwise", e);
+ }
+ } else {
+ log.trace("No data provider configured, not updating status");
+ }
+ }
+
+ @Override
+ public void close() throws ExecutionException, InterruptedException {
+ if (dataProvider != null) {
+ final DataModificationTransaction t = dataProvider.beginTransaction();
+ t.removeOperationalData(toasterIID);
+ t.commit().get();
+ }
+ }
+
private class MakeToastTask implements Callable<RpcResult<Void>> {
final MakeToastInput toastRequest;
}
@Override
- public RpcResult<Void> call() throws Exception {
- Thread.sleep(1000);
+ public RpcResult<Void> call() throws InterruptedException {
+ Thread.sleep(1000 * toastRequest.getToasterDoneness());
ToastDoneBuilder notifyBuilder = new ToastDoneBuilder();
notifyBuilder.setToastStatus(ToastStatus.Done);
notificationProvider.publish(notifyBuilder.build());
- log.trace("Toast Done");
+ log.debug("Toast Done");
logToastInput(toastRequest);
- currentTask = null;
+ currentTask = null;
toastsMade.incrementAndGet();
+ updateStatus();
return Rpcs.<Void> getRpcResult(true, null, Collections.<RpcError> emptySet());
}
}
}
+ container data-broker {
+ uses config:service-ref {
+ refine type {
+ mandatory false;
+ config:required-identity mdsal:binding-data-broker;
+ }
+ }
+ }
}
}
}
}
-}
\ No newline at end of file
+}
--- /dev/null
+package org.opendaylight.controller.sal.match.extensible;
+
+import java.util.Arrays;
+
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+
+import org.opendaylight.controller.sal.utils.HexEncode;
+import org.opendaylight.controller.sal.utils.NetUtils;
+
+@XmlRootElement
+@XmlAccessorType(XmlAccessType.NONE)
+public class DlDst extends MatchField<byte[]> {
+ private static final long serialVersionUID = 1L;
+ public static final String TYPE = "DL_DST";
+ private byte[] address;
+
+ /**
+ * Creates a Match field for the destination data layer address
+ *
+ * @param address
+ * the data layer address. The constructor makes a copy of it
+ */
+ public DlDst(byte[] address) {
+ super(TYPE);
+ if (address != null) {
+ this.address = Arrays.copyOf(address, address.length);
+ }
+ }
+
+ // To satisfy JAXB
+ public DlDst() {
+ super(TYPE);
+ }
+
+ @Override
+ public byte[] getValue() {
+ return Arrays.copyOf(address, address.length);
+ }
+
+ @Override
+ @XmlElement(name = "value")
+ protected String getValueString() {
+ return HexEncode.bytesToHexStringFormat(address);
+ }
+
+ @Override
+ public byte[] getMask() {
+ return null;
+ }
+
+ @Override
+ protected String getMaskString() {
+ return null;
+ }
+
+ @Override
+ public boolean isValid() {
+ return address != null && address.length == NetUtils.MACAddrLengthInBytes;
+ }
+
+ @Override
+ public boolean hasReverse() {
+ return true;
+ }
+
+ @Override
+ public DlSrc getReverse() {
+ return new DlSrc(address);
+ }
+
+ @Override
+ public DlDst clone() {
+ return new DlDst(address);
+ }
+
+ @Override
+ public boolean isV6() {
+ return true;
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + Arrays.hashCode(address);
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null) {
+ return false;
+ }
+ if (!(obj instanceof DlDst)) {
+ return false;
+ }
+ DlDst other = (DlDst) obj;
+ return Arrays.equals(address, other.address);
+ }
+}
--- /dev/null
+package org.opendaylight.controller.sal.match.extensible;
+
+import java.util.Arrays;
+
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+
+import org.opendaylight.controller.sal.utils.HexEncode;
+import org.opendaylight.controller.sal.utils.NetUtils;
+
+@XmlRootElement
+@XmlAccessorType(XmlAccessType.NONE)
+public class DlSrc extends MatchField<byte[]> {
+ private static final long serialVersionUID = 1L;
+ public static final String TYPE = "DL_SRC";
+ private byte[] address;
+
+ /**
+ * Creates a Match field for the source datalayer address
+ *
+ * @param address
+ * the datalayer address. The constructor makes a copy of it
+ */
+ public DlSrc(byte[] address) {
+ super(TYPE);
+ if (address != null) {
+ this.address = Arrays.copyOf(address, address.length);
+ }
+ }
+
+ // To satisfy JAXB
+ private DlSrc() {
+ super(TYPE);
+ }
+
+ @Override
+ public byte[] getValue() {
+ return Arrays.copyOf(address, address.length);
+ }
+
+ @Override
+ @XmlElement(name = "value")
+ protected String getValueString() {
+ return HexEncode.bytesToHexStringFormat(address);
+ }
+
+ @Override
+ public byte[] getMask() {
+ return null;
+ }
+
+ @Override
+ protected String getMaskString() {
+ return null;
+ }
+
+ @Override
+ public boolean isValid() {
+ return address != null && address.length == NetUtils.MACAddrLengthInBytes;
+ }
+
+ @Override
+ public boolean hasReverse() {
+ return true;
+ }
+
+ @Override
+ public DlDst getReverse() {
+ return new DlDst(address);
+ }
+
+ @Override
+ public DlSrc clone() {
+ return new DlSrc(address);
+ }
+
+ @Override
+ public boolean isV6() {
+ return true;
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + Arrays.hashCode(address);
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null) {
+ return false;
+ }
+ if (!(obj instanceof DlSrc)) {
+ return false;
+ }
+ DlSrc other = (DlSrc) obj;
+ return Arrays.equals(address, other.address);
+ }
+}
\ No newline at end of file
--- /dev/null
+package org.opendaylight.controller.sal.match.extensible;
+
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+
+import org.opendaylight.controller.sal.utils.EtherTypes;
+import org.opendaylight.controller.sal.utils.NetUtils;
+
+@XmlRootElement
+@XmlAccessorType(XmlAccessType.NONE)
+public class DlType extends MatchField<Short> {
+ private static final long serialVersionUID = 1L;
+ public static final String TYPE = "DL_TYPE";
+ private short ethertype;
+
+ /**
+ * Creates a Match field for the data layer type
+ *
+ * @param address
+ * the data layer type
+ */
+ public DlType(short ethertype) {
+ super(TYPE);
+ this.ethertype = ethertype;
+ }
+
+ // To satisfy JAXB
+ private DlType() {
+ super(TYPE);
+ }
+
+ @Override
+ public Short getValue() {
+ return ethertype;
+ }
+
+ @Override
+ @XmlElement(name = "value")
+ protected String getValueString() {
+ return String.format("0X%s", Integer.toHexString(NetUtils.getUnsignedShort(ethertype)));
+ }
+
+ @Override
+ public Short getMask() {
+ return null;
+ }
+
+ @Override
+ protected String getMaskString() {
+ return null;
+ }
+
+ @Override
+ public boolean isValid() {
+ return true;
+ }
+
+ @Override
+ public boolean hasReverse() {
+ return false;
+ }
+
+ @Override
+ public DlType getReverse() {
+ return this.clone();
+ }
+
+ @Override
+ public DlType clone() {
+ return new DlType(ethertype);
+ }
+
+ @Override
+ public boolean isV6() {
+ return this.ethertype == EtherTypes.IPv6.shortValue();
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ethertype;
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null) {
+ return false;
+ }
+ if (!(obj instanceof DlType)) {
+ return false;
+ }
+ DlType other = (DlType) obj;
+ if (ethertype != other.ethertype) {
+ return false;
+ }
+ return true;
+ }
+}
\ No newline at end of file
--- /dev/null
+package org.opendaylight.controller.sal.match.extensible;
+
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+
+
+@XmlRootElement
+@XmlAccessorType(XmlAccessType.NONE)
+public class DlVlan extends MatchField<Short> {
+ private static final long serialVersionUID = 1L;
+ public static final String TYPE = "DL_VLAN";
+ private static final short MAX = 4095;
+ private short vlan;
+
+ /**
+ * Creates a Match field for the data layer type
+ *
+ * @param address
+ * the data layer type
+ */
+ public DlVlan(short vlan) {
+ super(TYPE);
+ this.vlan = vlan;
+ }
+
+ // To satisfy JAXB
+ private DlVlan() {
+ super(TYPE);
+ }
+
+ @Override
+ public Short getValue() {
+ return vlan;
+ }
+
+ @Override
+ @XmlElement(name = "value")
+ protected String getValueString() {
+ return String.valueOf(vlan);
+ }
+
+ @Override
+ public Short getMask() {
+ return null;
+ }
+
+ @Override
+ protected String getMaskString() {
+ return null;
+ }
+
+ @Override
+ public boolean isValid() {
+ return vlan >= 0 && vlan <= MAX;
+ }
+
+ @Override
+ public DlVlan getReverse() {
+ return this.clone();
+ }
+
+ @Override
+ public boolean hasReverse() {
+ return false;
+ }
+
+ @Override
+ public DlVlan clone() {
+ return new DlVlan(vlan);
+ }
+
+ @Override
+ public boolean isV6() {
+ return true;
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + vlan;
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null) {
+ return false;
+ }
+ if (!(obj instanceof DlVlan)) {
+ return false;
+ }
+ DlVlan other = (DlVlan) obj;
+ if (vlan != other.vlan) {
+ return false;
+ }
+ return true;
+ }
+}
\ No newline at end of file
--- /dev/null
+package org.opendaylight.controller.sal.match.extensible;
+
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+
+import org.opendaylight.controller.sal.utils.NetUtils;
+
+@XmlRootElement
+@XmlAccessorType(XmlAccessType.NONE)
+public class DlVlanPriority extends MatchField<Byte> {
+ private static final long serialVersionUID = 1L;
+ public static final String TYPE = "DL_VLAN_PR";
+ private static final byte MAX = 7;
+ private byte vlanPriority;
+
+ /**
+ * Creates a Match field for the data layer type
+ *
+ * @param address
+ * the data layer type
+ */
+ public DlVlanPriority(byte vlanPriority) {
+ super(TYPE);
+ this.vlanPriority = vlanPriority;
+ }
+
+ // To satisfy JAXB
+ private DlVlanPriority() {
+ super(TYPE);
+ }
+
+ @Override
+ public Byte getValue() {
+ return vlanPriority;
+ }
+
+ @Override
+ @XmlElement(name = "mask")
+ protected String getValueString() {
+ return String.format("0X%s", Integer.toHexString(NetUtils.getUnsignedByte(vlanPriority)));
+ }
+
+ @Override
+ public Byte getMask() {
+ return null;
+ }
+
+ @Override
+ protected String getMaskString() {
+ return null;
+ }
+
+ @Override
+ public boolean isValid() {
+ return vlanPriority >= 0 && vlanPriority <= MAX;
+ }
+
+ @Override
+ public boolean hasReverse() {
+ return false;
+ }
+
+ @Override
+ public DlVlanPriority getReverse() {
+ return this.clone();
+ }
+
+ @Override
+ public DlVlanPriority clone() {
+ return new DlVlanPriority(vlanPriority);
+ }
+
+ @Override
+ public boolean isV6() {
+ return true;
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + vlanPriority;
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null) {
+ return false;
+ }
+ if (!(obj instanceof DlVlanPriority)) {
+ return false;
+ }
+ DlVlanPriority other = (DlVlanPriority) obj;
+ if (vlanPriority != other.vlanPriority) {
+ return false;
+ }
+ return true;
+ }
+}
\ No newline at end of file
--- /dev/null
+package org.opendaylight.controller.sal.match.extensible;
+
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+
+import org.opendaylight.controller.sal.core.NodeConnector;
+
+@XmlRootElement
+@XmlAccessorType(XmlAccessType.NONE)
+public class InPort extends MatchField<NodeConnector> {
+ private static final long serialVersionUID = 1L;
+ public static final String TYPE = "IN_PORT";
+ private NodeConnector port;
+
+ /**
+ * Creates a Match field for the input port
+ *
+ * @param port
+ * the input port
+ */
+ public InPort(NodeConnector port) {
+ super(TYPE);
+ this.port = port;
+ }
+
+ // To satisfy JAXB
+ private InPort() {
+ super(TYPE);
+ }
+
+ @Override
+ public NodeConnector getValue() {
+ return port;
+ }
+
+ @Override
+ @XmlElement(name = "value")
+ protected String getValueString() {
+ return port.toString();
+ }
+
+ @Override
+ public NodeConnector getMask() {
+ return null;
+ }
+
+ @Override
+ protected String getMaskString() {
+ return null;
+ }
+
+ @Override
+ public boolean isValid() {
+ return true;
+ }
+
+ @Override
+ public boolean hasReverse() {
+ return false;
+ }
+
+ @Override
+ public InPort getReverse() {
+ return this.clone();
+ }
+
+ @Override
+ public InPort clone() {
+ return new InPort(port);
+ }
+
+ @Override
+ public boolean isV6() {
+ return true;
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((port == null) ? 0 : port.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null) {
+ return false;
+ }
+ if (!(obj instanceof InPort)) {
+ return false;
+ }
+ InPort other = (InPort) obj;
+ if (port == null) {
+ if (other.port != null) {
+ return false;
+ }
+ } else if (!port.equals(other.port)) {
+ return false;
+ }
+ return true;
+ }
+}
\ No newline at end of file
--- /dev/null
+
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.controller.sal.match.extensible;
+
+import java.io.Serializable;
+import java.net.Inet4Address;
+import java.net.Inet6Address;
+import java.net.InetAddress;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+
+
+import org.opendaylight.controller.sal.utils.NetUtils;
+
+/**
+ * Represents the generic match criteria for a network frame/packet/message
+ * It contains a collection of individual field match
+ *
+ */
+@XmlRootElement
+@XmlAccessorType(XmlAccessType.NONE)
+public class Match implements Cloneable, Serializable {
+ private static final long serialVersionUID = 1L;
+ private Map<String, MatchField<?>> fields;
+
+ public Match() {
+ fields = new HashMap<String, MatchField<?>>();
+ }
+
+ public Match(Match match) {
+ fields = new HashMap<String, MatchField<?>>(match.fields);
+ }
+
+ /**
+ * Generic setter for frame/packet/message's header field against which to match
+ *
+ * @param field the fields parameters as MAtchField object
+ */
+ public void setField(MatchField<?> field) {
+ if (field.isValid()) {
+ fields.put(field.getType(), field);
+ }
+ }
+
+ /**
+ * Generic method to clear a field from the match
+ */
+ public void clearField(String type) {
+ fields.remove(type);
+ }
+
+ /**
+ * Generic getter for fields against which the match is programmed
+ *
+ * @param type frame/packet/message's header field type
+ * @return
+ */
+ public MatchField<?> getField(String type) {
+ return fields.get(type);
+ }
+
+ /**
+ * Returns the list of MatchType fields the match is set for
+ *
+ * @return List of individual MatchType fields.
+ */
+ public List<String> getMatchesList() {
+ return new ArrayList<String>(fields.keySet());
+ }
+
+ /**
+ * Returns the list of MatchFields the match is set for
+ *
+ * @return List of individual MatchField values.
+ */
+ @XmlElement(name="matchField")
+ public List<MatchField<?>> getMatchFields() {
+ return new ArrayList<MatchField<?>>(fields.values());
+ }
+
+ /**
+ * Returns whether this match is for an IPv6 flow
+ */
+ public boolean isIPv6() {
+ if (isPresent(DlType.TYPE)) {
+ for (MatchField<?> field : fields.values()) {
+ if (!field.isV6()) {
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+
+ /**
+ * Returns whether this match is for an IPv4 flow
+ */
+ public boolean isIPv4() {
+ return !isIPv6();
+ }
+
+ /**
+ * Returns whether for the specified field type the match is to be considered "any"
+ * Equivalent to say this match does not care about the value of the specified field
+ *
+ * @param type
+ * @return
+ */
+ public boolean isAny(String type) {
+ return !fields.containsKey(type);
+ }
+
+ /**
+ * Returns whether a match for the specified field type is configured
+ *
+ * @param type
+ * @return
+ */
+ public boolean isPresent(String type) {
+ return (fields.get(type) != null);
+ }
+
+ public boolean isEmpty() {
+ return fields.isEmpty();
+ }
+
+ @Override
+ public Match clone() {
+ Match cloned = null;
+ try {
+ cloned = (Match) super.clone();
+ cloned.fields = new HashMap<String, MatchField<?>>();
+ for (Entry<String, MatchField<?>> entry : this.fields.entrySet()) {
+ cloned.fields.put(entry.getKey(), entry.getValue().clone());
+ }
+ } catch (CloneNotSupportedException e) {
+ throw new RuntimeException(e);
+ }
+ return cloned;
+ }
+
+ /**
+ * Returns a reversed version of this match
+ * For example, in the reversed version the network source and destination
+ * addresses will be exchanged. Non symmetric match field will not be
+ * copied over into the reversed match version, like input port.
+ *
+ * @return
+ */
+ public Match reverse() {
+ Match reverse = new Match();
+ for (MatchField<?> field : fields.values()) {
+ reverse.setField(field.hasReverse()? field.getReverse() : field.clone());
+ }
+
+ // Reset asymmetric fields
+ reverse.clearField(InPort.TYPE);
+
+ return reverse;
+ }
+
+ /**
+ * Check whether the current match conflicts with the passed filter match
+ * This match conflicts with the filter if for at least a MatchType defined
+ * in the filter match, the respective MatchFields differ or are not
+ * compatible
+ *
+ * In other words the function returns true if the set of packets described
+ * by one match and the set of packets described by the other match are
+ * disjoint. Equivalently, if the intersection of the two sets of packets
+ * described by the two org.opendaylight.controller.sal.matches is an empty.
+ *
+ * For example, Let's suppose the filter has the following MatchFields:
+ * DL_TYPE = 0x800
+ * NW_DST = 172.20.30.110/24
+ *
+ * while this match has the following MatchFields:
+ * DL_TYPE = 0x800
+ * NW_DST = 172.20.30.45/24
+ * TP_DST = 80
+ *
+ * Then the function would return false as the two Match are not
+ * conflicting.
+ *
+ * Note: the mask value is taken into account only for MatchType.NW_SRC and
+ * MatchType.NW_DST
+ *
+ * @param match
+ * the Match describing the filter
+ * @return true if the set of packets described by one match and the set of
+ * packets described by the other match are disjoint, false
+ * otherwise
+ */
+ public boolean conflictWithFilter(Match filter) {
+ return !this.intersetcs(filter);
+ }
+
+ /**
+ * Merge the current Match fields with the fields of the filter Match. A
+ * check is first run to see if this Match is compatible with the filter
+ * Match. If it is not, the merge is not attempted.
+ *
+ * The result is the match object representing the intersection of the set
+ * of packets described by this match with the set of packets described by
+ * the filter match. If the intersection of the two sets is empty, the
+ * return match will be null.
+ *
+ * @param filter
+ * the match with which attempting the merge
+ * @return a new Match object describing the set of packets represented by
+ * the intersection of this and the filter org.opendaylight.controller.sal.matches. null if the
+ * intersection is empty.
+ */
+ public Match mergeWithFilter(Match filter) {
+ return this.getIntersection(filter);
+ }
+
+ /**
+ * Return the match representing the intersection of the set of packets
+ * described by this match with the set of packets described by the other
+ * match. Such as m.getIntersection(m) == m, m.getIntersection(u) == m and
+ * m.getIntersection(o) == o where u is an empty match (universal set, all
+ * packets) and o is the null match (empty set).
+ *
+ * @param other
+ * the match with which computing the intersection
+ * @return a new Match object representing the intersection of the set of
+ * packets described by this match with the set of packets described
+ * by the other match. null when the intersection is the empty set.
+ */
+ public Match getIntersection(Match other) {
+ // If no intersection, return the empty set
+ if (!this.intersetcs(other)) {
+ return null;
+ }
+ // Check if any of the two is the universal match
+ if (this.isEmpty()) {
+ return other.clone();
+ }
+ if (other.isEmpty()) {
+ return this.clone();
+ }
+ // Get all the match types for both filters
+ Set<String> allTypes = new HashSet<String>(this.fields.keySet());
+ allTypes.addAll(new HashSet<String>(other.fields.keySet()));
+ // Derive the intersection
+ Match intersection = new Match();
+ for (String type : allTypes) {
+ if (this.isAny(type) && other.isAny(type)) {
+ continue;
+ }
+ if (this.isAny(type)) {
+ intersection.setField(other.getField(type).clone());
+ continue;
+ } else if (other.isAny(type)) {
+ intersection.setField(this.getField(type).clone());
+ continue;
+ }
+ // Either they are equal or it is about IP address
+ switch (type) {
+ // When it is about IP address, take the wider prefix address
+ // between the twos
+ case NwSrc.TYPE:
+ case NwDst.TYPE:
+ MatchField<?> thisField = this.getField(type);
+ MatchField<?> otherField = other.getField(type);
+ InetAddress thisAddress = (InetAddress) thisField.getValue();
+ InetAddress otherAddress = (InetAddress) otherField.getValue();
+ InetAddress thisMask = (InetAddress) thisField.getMask();
+ InetAddress otherMask = (InetAddress) otherField.getMask();
+
+ int thisMaskLen = (thisMask == null) ? ((thisAddress instanceof Inet4Address) ? 32 : 128) : NetUtils
+ .getSubnetMaskLength(thisMask);
+ int otherMaskLen = (otherMask == null) ? ((otherAddress instanceof Inet4Address) ? 32 : 128) : NetUtils
+ .getSubnetMaskLength(otherMask);
+
+ InetAddress subnetPrefix = null;
+ InetAddress subnetMask = null;
+ if (thisMaskLen < otherMaskLen) {
+ subnetPrefix = NetUtils.getSubnetPrefix(otherAddress, otherMaskLen);
+ subnetMask = otherMask;
+ } else {
+ subnetPrefix = NetUtils.getSubnetPrefix(thisAddress, thisMaskLen);
+ subnetMask = thisMask;
+ }
+ MatchField<?> field = (type.equals(NwSrc.TYPE)) ? new NwSrc(subnetPrefix, subnetMask) : new NwDst(
+ subnetPrefix, subnetMask);
+ intersection.setField(field);
+ break;
+ default:
+ // this and other match field are equal for this type, pick this
+ // match field
+ intersection.setField(this.getField(type).clone());
+ }
+ }
+ return intersection;
+ }
+
+ /**
+ * Checks whether the intersection of the set of packets described by this
+ * match with the set of packets described by the other match is non empty
+ *
+ * For example, if this match is: DL_SRC = 00:cc:bb:aa:11:22
+ *
+ * and the other match is: DL_TYPE = 0x800 NW_SRC = 1.2.3.4
+ *
+ * then their respective matching packets set intersection is non empty:
+ * DL_SRC = 00:cc:bb:aa:11:22 DL_TYPE = 0x800 NW_SRC = 1.2.3.4
+ *
+ * @param other
+ * the other match with which testing the intersection
+ * @return true if the intersection of the respective matching packets sets
+ * is non empty
+ */
+ public boolean intersetcs(Match other) {
+ // No intersection with the empty set
+ if (other == null) {
+ return false;
+ }
+ // Always intersection with the universal set
+ if (this.isEmpty() || other.isEmpty()) {
+ return true;
+ }
+
+ // Get all the match types for both filters
+ Set<String> allTypes = new HashSet<String>(this.fields.keySet());
+ allTypes.addAll(new HashSet<String>(other.fields.keySet()));
+
+ // Iterate through all the match types defined in the two filters
+ for (String type : allTypes) {
+ if (this.isAny(type) || other.isAny(type)) {
+ continue;
+ }
+
+ MatchField<?> thisField = this.getField(type);
+ MatchField<?> otherField = other.getField(type);
+
+ switch (type) {
+ case DlSrc.TYPE:
+ case DlDst.TYPE:
+ if (!Arrays.equals((byte[]) thisField.getValue(), (byte[]) otherField.getValue())) {
+ return false;
+ }
+ break;
+ case NwSrc.TYPE:
+ case NwDst.TYPE:
+ InetAddress thisAddress = (InetAddress) thisField.getValue();
+ InetAddress otherAddress = (InetAddress) otherField.getValue();
+ // Validity check
+ if (thisAddress instanceof Inet4Address && otherAddress instanceof Inet6Address
+ || thisAddress instanceof Inet6Address && otherAddress instanceof Inet4Address) {
+ return false;
+ }
+ InetAddress thisMask = (InetAddress) thisField.getMask();
+ InetAddress otherMask = (InetAddress) otherField.getMask();
+ if (NetUtils.inetAddressConflict(thisAddress, otherAddress, thisMask, otherMask)
+ && NetUtils.inetAddressConflict(otherAddress, thisAddress, otherMask, thisMask)) {
+ return false;
+ }
+ break;
+ default:
+ if (!thisField.getValue().equals(otherField.getValue())) {
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((fields == null) ? 0 : fields.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null) {
+ return false;
+ }
+ if (!(obj instanceof Match)) {
+ return false;
+ }
+ Match other = (Match) obj;
+ if (fields == null) {
+ if (other.fields != null) {
+ return false;
+ }
+ } else if (!fields.equals(other.fields)) {
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ public String toString() {
+ return "Match[" + fields.values() + "]";
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.controller.sal.match.extensible;
+
+import java.io.Serializable;
+
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+
+/**
+ * Represents the generic matching field object
+ */
+@XmlRootElement
+@XmlAccessorType(XmlAccessType.NONE)
+public abstract class MatchField<T> implements Cloneable, Serializable {
+ private static final long serialVersionUID = 1L;
+ private String type;
+
+ // To satisfy JAXB
+ @SuppressWarnings("unused")
+ private MatchField() {
+ }
+
+ public MatchField(String type) {
+ this.type = type;
+ }
+
+ @XmlElement(name = "type")
+ public String getType() {
+ return type;
+ }
+
+ /**
+ * Returns the value set for this match field
+ *
+ * @return
+ */
+ public abstract T getValue();
+
+ @XmlElement(name = "value")
+ protected abstract String getValueString();
+
+ /**
+ * Returns the mask value set for this field match A null mask means this is
+ * a full match
+ *
+ * @return
+ */
+ public abstract T getMask();
+
+ @XmlElement(name = "mask")
+ protected abstract String getMaskString();
+
+ /**
+ * Returns whether the field match configuration is valid or not
+ *
+ * @return true if valid, false otherwise
+ */
+ public abstract boolean isValid();
+
+ public abstract boolean hasReverse();
+
+ /**
+ * Returns the reverse match field. For example for a MatchField matching on
+ * source ip 1.1.1.1 it will return a MatchField matching on destination IP
+ * 1.1.1.1. For not reversable MatchField, a copy of this MatchField will be
+ * returned
+ *
+ * @return the correspondent reverse MatchField object or a copy of this
+ * object if the field is not reversable
+ */
+ public abstract MatchField<T> getReverse();
+
+ /**
+ * Returns whether the match field is congruent with IPv6 frames
+ *
+ * @return true if congruent with IPv6 frames
+ */
+ public abstract boolean isV6();
+
+ @Override
+ public abstract MatchField<T> clone();
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((type == null) ? 0 : type.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null) {
+ return false;
+ }
+ if (!(obj instanceof MatchField)) {
+ return false;
+ }
+ MatchField<?> other = (MatchField<?>) obj;
+ if (type == null) {
+ if (other.type != null) {
+ return false;
+ }
+ } else if (!type.equals(other.type)) {
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ public String toString() {
+ return (getMask() == null) ? String.format("%s(%s)", getType(), getValueString()) :
+ String.format("%s(%s,%s)", getType(), getValueString(), getMaskString());
+ }
+
+}
--- /dev/null
+package org.opendaylight.controller.sal.match.extensible;
+
+import java.net.Inet4Address;
+import java.net.Inet6Address;
+import java.net.InetAddress;
+
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+
+import org.opendaylight.controller.sal.utils.NetUtils;
+
+@XmlRootElement
+@XmlAccessorType(XmlAccessType.NONE)
+public class NwDst extends MatchField<InetAddress> {
+ private static final long serialVersionUID = 1L;
+ public static final String TYPE = "NW_DST";
+ private InetAddress address;
+ private InetAddress mask;
+
+ /**
+ * Creates a Match field for the network destination address
+ *
+ * @param address
+ * the network address
+ * @param mask
+ * the network mask
+ */
+ public NwDst(InetAddress address, InetAddress mask) {
+ super(TYPE);
+ this.address = address;
+ this.mask = mask;
+ }
+
+ // To satisfy JAXB
+ private NwDst() {
+ super(TYPE);
+ }
+
+ public NwDst(InetAddress address) {
+ super(TYPE);
+ this.address = address;
+ this.mask = null;
+ }
+
+ @Override
+ public InetAddress getValue() {
+ return address;
+ }
+
+ @Override
+ @XmlElement(name = "value")
+ protected String getValueString() {
+ return address.getHostAddress();
+ }
+
+ @Override
+ public InetAddress getMask() {
+ return mask;
+ }
+
+ @Override
+ @XmlElement(name = "mask")
+ protected String getMaskString() {
+ return (mask == null) ? "null" : mask.getHostAddress();
+ }
+
+ @Override
+ public boolean isValid() {
+ if (address != null) {
+ if (mask != null) {
+ return address instanceof Inet4Address && mask instanceof Inet4Address
+ || address instanceof Inet6Address && mask instanceof Inet6Address;
+ }
+ return true;
+ }
+ return false;
+ }
+
+ @Override
+ public boolean hasReverse() {
+ return true;
+ }
+
+ @Override
+ public NwSrc getReverse() {
+ return new NwSrc(address, mask);
+ }
+
+ @Override
+ public NwDst clone() {
+ return new NwDst(address, mask);
+ }
+
+ @Override
+ public boolean isV6() {
+ return address instanceof Inet6Address;
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((address == null) ? 0 : address.hashCode());
+ result = prime * result + ((mask == null) ? 0 : mask.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null) {
+ return false;
+ }
+ if (!(obj instanceof NwDst)) {
+ return false;
+ }
+ NwDst other = (NwDst) obj;
+ // Equality to be checked against prefix addresses
+ int thisMaskLen = (this.mask == null) ? ((this.address instanceof Inet4Address) ? 32 : 128) : NetUtils
+ .getSubnetMaskLength(this.mask);
+ int otherMaskLen = (other.mask == null) ? ((other.address instanceof Inet4Address) ? 32 : 128) : NetUtils
+ .getSubnetMaskLength(other.mask);
+
+ return NetUtils.getSubnetPrefix(address, thisMaskLen).equals(
+ NetUtils.getSubnetPrefix(other.address, otherMaskLen));
+ }
+}
--- /dev/null
+package org.opendaylight.controller.sal.match.extensible;
+
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+
+import org.opendaylight.controller.sal.utils.NetUtils;
+
+@XmlRootElement
+@XmlAccessorType(XmlAccessType.NONE)
+public class NwProtocol extends MatchField<Byte> {
+ private static final long serialVersionUID = 1L;
+ public static final String TYPE = "NW_PROTO";
+ private static final short MAX = 255;
+ private byte protocol;
+
+ /**
+ * Creates a Match field for the network protocol
+ *
+ * @param protocol
+ * the protocol number
+ */
+ public NwProtocol(byte protocol) {
+ super(TYPE);
+ this.protocol = protocol;
+ }
+
+ public NwProtocol(int protocol) {
+ super(TYPE);
+ this.protocol = (byte) protocol;
+ }
+
+ public NwProtocol(short protocol) {
+ super(TYPE);
+ this.protocol = (byte) protocol;
+ }
+
+ // To satisfy JAXB
+ private NwProtocol() {
+ super(TYPE);
+ }
+
+ @Override
+ public Byte getValue() {
+ return protocol;
+ }
+
+ @Override
+ @XmlElement(name = "value")
+ protected String getValueString() {
+ return String.format("0X%s", Integer.toHexString(NetUtils.getUnsignedByte(protocol)));
+ }
+
+ @Override
+ public Byte getMask() {
+ return null;
+ }
+
+ @Override
+ protected String getMaskString() {
+ return null;
+ }
+
+ @Override
+ public boolean isValid() {
+ int intProtocol = NetUtils.getUnsignedByte(protocol);
+ return intProtocol >= 0 && intProtocol <= MAX;
+ }
+
+ @Override
+ public boolean hasReverse() {
+ return false;
+ }
+
+ @Override
+ public NwProtocol getReverse() {
+ return this.clone();
+ }
+
+ @Override
+ public NwProtocol clone() {
+ return new NwProtocol(protocol);
+ }
+
+ @Override
+ public boolean isV6() {
+ return true;
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + protocol;
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null) {
+ return false;
+ }
+ if (!(obj instanceof NwProtocol)) {
+ return false;
+ }
+ NwProtocol other = (NwProtocol) obj;
+ if (protocol != other.protocol) {
+ return false;
+ }
+ return true;
+ }
+}
\ No newline at end of file
--- /dev/null
+package org.opendaylight.controller.sal.match.extensible;
+
+import java.net.Inet4Address;
+import java.net.Inet6Address;
+import java.net.InetAddress;
+
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+
+import org.opendaylight.controller.sal.utils.NetUtils;
+
+@XmlRootElement
+@XmlAccessorType(XmlAccessType.NONE)
+public class NwSrc extends MatchField<InetAddress> {
+ private static final long serialVersionUID = 1L;
+ public static final String TYPE = "NW_SRC";
+ private InetAddress address;
+ private InetAddress mask;
+
+ /**
+ * Creates a Match field for the network source address
+ *
+ * @param address
+ * the network address
+ * @param mask
+ * the network mask
+ */
+ public NwSrc(InetAddress address, InetAddress mask) {
+ super(TYPE);
+ this.address = address;
+ this.mask = mask;
+ }
+
+ // To satisfy JAXB
+ private NwSrc() {
+ super(TYPE);
+ }
+
+ public NwSrc(InetAddress address) {
+ super(TYPE);
+ this.address = address;
+ this.mask = null;
+ }
+
+ @Override
+ public InetAddress getValue() {
+ return address;
+ }
+
+ @Override
+ @XmlElement(name = "value")
+ protected String getValueString() {
+ return address.toString();
+ }
+
+ @Override
+ public InetAddress getMask() {
+ return mask;
+ }
+
+ @Override
+ @XmlElement(name = "mask")
+ protected String getMaskString() {
+ return mask == null ? "null" : mask.toString();
+ }
+
+ @Override
+ public boolean isValid() {
+ if (address != null) {
+ if (mask != null) {
+ return address instanceof Inet4Address && mask instanceof Inet4Address
+ || address instanceof Inet6Address && mask instanceof Inet6Address;
+ }
+ return true;
+ }
+ return false;
+ }
+
+ @Override
+ public boolean hasReverse() {
+ return true;
+ }
+
+ @Override
+ public NwDst getReverse() {
+ return new NwDst(address, mask);
+ }
+
+ @Override
+ public NwSrc clone() {
+ return new NwSrc(address, mask);
+ }
+
+ @Override
+ public boolean isV6() {
+ return address instanceof Inet6Address;
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((address == null) ? 0 : address.hashCode());
+ result = prime * result + ((mask == null) ? 0 : mask.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null) {
+ return false;
+ }
+ if (!(obj instanceof NwSrc)) {
+ return false;
+ }
+ NwSrc other = (NwSrc) obj;
+ // Equality to be checked against prefix addresses
+ int thisMaskLen = (this.mask == null) ? ((this.address instanceof Inet4Address) ? 32 : 128) : NetUtils
+ .getSubnetMaskLength(this.mask);
+ int otherMaskLen = (other.mask == null) ? ((other.address instanceof Inet4Address) ? 32 : 128) : NetUtils
+ .getSubnetMaskLength(other.mask);
+
+ return NetUtils.getSubnetPrefix(address, thisMaskLen).equals(
+ NetUtils.getSubnetPrefix(other.address, otherMaskLen));
+ }
+}
--- /dev/null
+package org.opendaylight.controller.sal.match.extensible;
+
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+
+import org.opendaylight.controller.sal.utils.NetUtils;
+
+@XmlRootElement
+@XmlAccessorType(XmlAccessType.NONE)
+public class NwTos extends MatchField<Byte> {
+ private static final long serialVersionUID = 1L;
+ public static final String TYPE = "NW_TOS";
+ private static final short MAX = 63;
+ private byte tos;
+
+ /**
+ * Creates a Match field for the network TOS
+ *
+ * @param address
+ * the network TOS
+ */
+ public NwTos(byte tos) {
+ super(TYPE);
+ this.tos = tos;
+ }
+
+ public NwTos(int tos) {
+ super(TYPE);
+ this.tos = (byte) tos;
+ }
+
+ public NwTos(short tos) {
+ super(TYPE);
+ this.tos = (byte) tos;
+ }
+
+ // To satisfy JAXB
+ private NwTos() {
+ super(TYPE);
+ }
+
+ @Override
+ public Byte getValue() {
+ return tos;
+ }
+
+ @Override
+ @XmlElement(name = "value")
+ protected String getValueString() {
+ return String.format("0X%s", Integer.toHexString(NetUtils.getUnsignedByte(tos)));
+ }
+
+ @Override
+ public Byte getMask() {
+ return null;
+ }
+
+ @Override
+ protected String getMaskString() {
+ return null;
+ }
+
+ @Override
+ public boolean isValid() {
+ return tos >= 0 && tos <= MAX;
+ }
+
+ @Override
+ public boolean hasReverse() {
+ return false;
+ }
+
+ @Override
+ public NwTos getReverse() {
+ return this.clone();
+ }
+
+ @Override
+ public NwTos clone() {
+ return new NwTos(tos);
+ }
+
+ @Override
+ public boolean isV6() {
+ return true;
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + tos;
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null) {
+ return false;
+ }
+ if (!(obj instanceof NwTos)) {
+ return false;
+ }
+ NwTos other = (NwTos) obj;
+ if (tos != other.tos) {
+ return false;
+ }
+ return true;
+ }
+}
\ No newline at end of file
--- /dev/null
+package org.opendaylight.controller.sal.match.extensible;
+
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+
+import org.opendaylight.controller.sal.utils.NetUtils;
+
+@XmlRootElement
+@XmlAccessorType(XmlAccessType.NONE)
+public class TpDst extends MatchField<Short> {
+ private static final long serialVersionUID = 1L;
+ public static final String TYPE = "TP_DST";
+ private short port;
+
+ /**
+ * Creates a Match field for the transport destination port
+ *
+ * @param port
+ * the transport port
+ */
+ public TpDst(short port) {
+ super(TYPE);
+ this.port = port;
+ }
+
+ // To satisfy JAXB
+ private TpDst() {
+ super(TYPE);
+ }
+
+ @Override
+ public Short getValue() {
+ return port;
+ }
+
+ @Override
+ @XmlElement(name = "value")
+ protected String getValueString() {
+ return String.valueOf(NetUtils.getUnsignedShort(port));
+ }
+
+ @Override
+ public Short getMask() {
+ return null;
+ }
+
+ @Override
+ protected String getMaskString() {
+ return null;
+ }
+
+ @Override
+ public boolean isValid() {
+ return true;
+ }
+
+ @Override
+ public TpSrc getReverse() {
+ return new TpSrc(port);
+ }
+
+ @Override
+ public boolean hasReverse() {
+ return true;
+ }
+
+ @Override
+ public TpDst clone() {
+ return new TpDst(port);
+ }
+
+ @Override
+ public boolean isV6() {
+ return true;
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = super.hashCode();
+ result = prime * result + port;
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (!super.equals(obj)) {
+ return false;
+ }
+ if (!(obj instanceof TpDst)) {
+ return false;
+ }
+ TpDst other = (TpDst) obj;
+ if (port != other.port) {
+ return false;
+ }
+ return true;
+ }
+}
\ No newline at end of file
--- /dev/null
+package org.opendaylight.controller.sal.match.extensible;
+
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+
+import org.opendaylight.controller.sal.utils.NetUtils;
+
+@XmlRootElement
+@XmlAccessorType(XmlAccessType.NONE)
+public class TpSrc extends MatchField<Short> {
+ private static final long serialVersionUID = 1L;
+ public static final String TYPE = "TP_SRC";
+ private short port;
+
+ /**
+ * Creates a Match field for the Transport source port
+ *
+ * @param port
+ * the transport port
+ */
+ public TpSrc(short port) {
+ super(TYPE);
+ this.port = port;
+ }
+
+ // To satisfy JAXB
+ private TpSrc() {
+ super(TYPE);
+ }
+
+ @Override
+ public Short getValue() {
+ return port;
+ }
+
+ @Override
+ @XmlElement(name = "value")
+ protected String getValueString() {
+ return String.valueOf(NetUtils.getUnsignedShort(port));
+ }
+
+ @Override
+ public Short getMask() {
+ return null;
+ }
+
+ @Override
+ protected String getMaskString() {
+ return null;
+ }
+
+ @Override
+ public boolean isValid() {
+ return true;
+ }
+
+ @Override
+ public boolean hasReverse() {
+ return true;
+ }
+
+ @Override
+ public TpDst getReverse() {
+ return new TpDst(port);
+ }
+
+ @Override
+ public TpSrc clone() {
+ return new TpSrc(port);
+ }
+
+ @Override
+ public boolean isV6() {
+ return true;
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = super.hashCode();
+ result = prime * result + port;
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (!super.equals(obj)) {
+ return false;
+ }
+ if (!(obj instanceof TpSrc)) {
+ return false;
+ }
+ TpSrc other = (TpSrc) obj;
+ if (port != other.port) {
+ return false;
+ }
+ return true;
+ }
+}
\ No newline at end of file
--- /dev/null
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.controller.sal.match;
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.opendaylight.controller.sal.core.Node;
+import org.opendaylight.controller.sal.core.NodeConnector;
+import org.opendaylight.controller.sal.match.extensible.DlDst;
+import org.opendaylight.controller.sal.match.extensible.DlSrc;
+import org.opendaylight.controller.sal.match.extensible.DlType;
+import org.opendaylight.controller.sal.match.extensible.DlVlan;
+import org.opendaylight.controller.sal.match.extensible.DlVlanPriority;
+import org.opendaylight.controller.sal.match.extensible.InPort;
+import org.opendaylight.controller.sal.match.extensible.Match;
+import org.opendaylight.controller.sal.match.extensible.MatchField;
+import org.opendaylight.controller.sal.match.extensible.NwDst;
+import org.opendaylight.controller.sal.match.extensible.NwProtocol;
+import org.opendaylight.controller.sal.match.extensible.NwSrc;
+import org.opendaylight.controller.sal.match.extensible.NwTos;
+import org.opendaylight.controller.sal.match.extensible.TpDst;
+import org.opendaylight.controller.sal.match.extensible.TpSrc;
+import org.opendaylight.controller.sal.utils.EtherTypes;
+import org.opendaylight.controller.sal.utils.IPProtocols;
+import org.opendaylight.controller.sal.utils.NodeConnectorCreator;
+import org.opendaylight.controller.sal.utils.NodeCreator;
+
+public class MatchExtensibleTest {
+ @Test
+ public void testMatchCreation() {
+ Node node = NodeCreator.createOFNode(7L);
+ NodeConnector port = NodeConnectorCreator.createOFNodeConnector((short) 6, node);
+ MatchField<?> field = new InPort(port);
+
+ Assert.assertTrue(field != null);
+ Assert.assertEquals(field.getType(), InPort.TYPE);
+ Assert.assertEquals(field.getValue(), port);
+ Assert.assertTrue(field.isValid());
+
+
+ byte mac[] = { (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0xdd, (byte) 11, (byte) 22 };
+ field = null;
+ field = new DlSrc(mac);
+ Assert.assertNotNull(field.getValue());
+
+ field = null;
+ field = new NwTos((byte) 0x22);
+ Assert.assertNotNull(field.getValue());
+ }
+
+ @Test
+ public void testMatchSetGet() {
+ Match x = new Match();
+ short val = 2346;
+ NodeConnector inPort = NodeConnectorCreator.createOFNodeConnector(val, NodeCreator.createOFNode(1L));
+ x.setField(new InPort(inPort));
+ Assert.assertEquals(x.getField(InPort.TYPE).getValue(), inPort);
+ Assert.assertTrue((Short) ((NodeConnector) x.getField(InPort.TYPE).getValue()).getID() == val);
+ }
+
+ @Test
+ public void testMatchSetGetMAC() {
+ Match x = new Match();
+ byte mac[] = { (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0xdd, (byte) 11, (byte) 22 };
+ byte mac2[] = { (byte) 0xaa, (byte) 0xbb, 0, 0, 0, (byte) 0xbb };
+
+ x.setField(new DlSrc(mac));
+ x.setField(new DlDst(mac2));
+ Assert.assertArrayEquals(mac, (byte[]) x.getField(DlSrc.TYPE).getValue());
+ Assert.assertFalse(Arrays.equals((byte[]) x.getField(DlSrc.TYPE).getValue(), (byte[]) x.getField(DlDst.TYPE)
+ .getValue()));
+
+ x.setField(new DlDst(mac.clone()));
+ Assert.assertArrayEquals((byte[]) x.getField(DlSrc.TYPE).getValue(), (byte[]) x.getField(DlDst.TYPE).getValue());
+ }
+
+ @Test
+ public void testMatchSetGetNWAddr() throws UnknownHostException {
+ Match x = new Match();
+ String ip = "172.20.231.23";
+ InetAddress address = InetAddress.getByName(ip);
+ InetAddress mask = InetAddress.getByName("255.255.0.0");
+
+ x.setField(new NwSrc(address, mask));
+ Assert.assertEquals(address, x.getField(NwSrc.TYPE).getValue());
+ Assert.assertEquals(x.getField(NwSrc.TYPE).getMask(), mask);
+ }
+
+ @Test
+ public void testMatchSetGetEtherType() throws UnknownHostException {
+ Match x = new Match();
+
+ x.setField(new DlType(EtherTypes.QINQ.shortValue()));
+ Assert.assertEquals(x.getField(DlType.TYPE).getValue(), EtherTypes.QINQ.shortValue());
+
+ x.setField(new DlType(EtherTypes.LLDP.shortValue()));
+ Assert.assertEquals(x.getField(DlType.TYPE).getValue(), EtherTypes.LLDP.shortValue());
+ Assert.assertFalse(x.getField(DlType.TYPE).equals(EtherTypes.LLDP.intValue()));
+ }
+
+ @Test
+ public void testSetGetNwTos() {
+ Match x = new Match();
+ x.setField(new NwTos((byte) 0xb));
+
+ Byte t = new Byte((byte) 0xb);
+
+ Object o = x.getField(NwTos.TYPE).getValue();
+ Assert.assertEquals(o, t);
+ Assert.assertEquals(o, Byte.valueOf((byte)0xb));
+ }
+
+ @Test
+ public void testSetGetNwProto() {
+ Match x = new Match();
+ Byte proto = (byte) 199;
+ x.setField(new NwProtocol(proto));
+
+ Byte o = (Byte) x.getField(NwProtocol.TYPE).getValue();
+ Assert.assertEquals(o, proto);
+ }
+
+ @Test
+ public void testSetTpSrc() {
+ // Minimum value validation.
+ Match match = new Match();
+ short tp_src = 0;
+ match.setField(new TpSrc(tp_src));
+
+ Object o = match.getField(TpSrc.TYPE).getValue();
+ Assert.assertEquals(o, tp_src);
+
+ // Maximum value validation.
+ match = new Match();
+ tp_src = (short) 0xffff;
+ match.setField(new TpSrc(tp_src));
+
+ o = match.getField(TpSrc.TYPE).getValue();
+ Assert.assertEquals(o, tp_src);
+ }
+
+ @Test
+ public void testSetTpDst() {
+ // Minimum value validation.
+ Match match = new Match();
+ short tp_dst = 0;
+ match.setField(new TpDst(tp_dst));
+
+ Object o = match.getField(TpDst.TYPE).getValue();
+ Assert.assertTrue(o.equals(tp_dst));
+
+ // Maximum value validation.
+ match = new Match();
+ tp_dst = (short) 0xffff;
+ match.setField(new TpDst(tp_dst));
+
+ o = match.getField(TpDst.TYPE).getValue();
+ Assert.assertEquals(o, tp_dst);
+ }
+
+ @Test
+ public void testEquality() throws Exception {
+ Node node = NodeCreator.createOFNode(7L);
+ NodeConnector port = NodeConnectorCreator.createOFNodeConnector((short) 24, node);
+ NodeConnector port2 = NodeConnectorCreator.createOFNodeConnector((short) 24, node);
+ byte srcMac[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc };
+ byte dstMac[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d, (byte) 0x5e, (byte) 0x6f };
+ byte srcMac2[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc };
+ byte dstMac2[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d, (byte) 0x5e, (byte) 0x6f };
+ InetAddress srcIP = InetAddress.getByName("2001:420:281:1004:407a:57f4:4d15:c355");
+ InetAddress dstIP = InetAddress.getByName("2001:420:281:1004:e123:e688:d655:a1b0");
+ InetAddress ipMask = InetAddress.getByName("ffff:ffff:ffff:ffff:0:0:0:0");
+ InetAddress ipMaskd = InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:0");
+ InetAddress ipMask2 = InetAddress.getByName("ffff:ffff:ffff:ffff:0:0:0:0");
+ InetAddress ipMaskd2 = InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:0");
+ short ethertype = EtherTypes.IPv6.shortValue();
+ short ethertype2 = EtherTypes.IPv6.shortValue();
+ short vlan = (short) 27, vlan2 = (short) 27;
+ byte vlanPr = (byte) 3, vlanPr2 = (byte) 3;
+ Byte tos = 4, tos2 = 4;
+ byte proto = IPProtocols.UDP.byteValue(), proto2 = IPProtocols.UDP.byteValue();
+ short src = (short) 5500, src2 = (short) 5500;
+ short dst = 80, dst2 = 80;
+
+ /*
+ * Create a SAL Flow aFlow
+ */
+ Match match1 = new Match();
+ Match match2 = new Match();
+ match1.setField(new InPort(port));
+ match1.setField(new DlSrc(srcMac));
+ match1.setField(new DlDst(dstMac));
+ match1.setField(new DlType(ethertype));
+ match1.setField(new DlVlan(vlan));
+ match1.setField(new DlVlanPriority(vlanPr));
+ match1.setField(new NwSrc(srcIP, ipMask));
+ match1.setField(new NwDst(dstIP, ipMaskd));
+ match1.setField(new NwTos(tos));
+ match1.setField(new NwProtocol(proto));
+ match1.setField(new TpSrc(src));
+ match1.setField(new TpDst(dst));
+
+ match2.setField(new InPort(port2));
+ match2.setField(new DlSrc(srcMac2));
+ match2.setField(new DlDst(dstMac2));
+ match2.setField(new DlType(ethertype2));
+ match2.setField(new DlVlan(vlan2));
+ match2.setField(new DlVlanPriority(vlanPr2));
+ match2.setField(new NwSrc(srcIP, ipMask2));
+ match2.setField(new NwDst(dstIP, ipMaskd2));
+ match2.setField(new NwTos(tos2));
+ match2.setField(new NwProtocol(proto2));
+ match2.setField(new TpSrc(src2));
+ match2.setField(new TpDst(dst2));
+
+ Assert.assertTrue(match1.equals(match2));
+
+ Set<String> allFields = new HashSet<String>(match1.getMatchesList());
+ allFields.addAll(match2.getMatchesList());
+ // Make sure all values are equals
+ for (String type : allFields) {
+ if (match1.isPresent(type)) {
+ Assert.assertEquals(match1.getField(type), match2.getField(type));
+ }
+ }
+
+ // Make none of the fields couples are pointing to the same reference
+ MatchField<?> a = null, b = null;
+ for (String type : allFields) {
+ a = match1.getField(type);
+ b = match2.getField(type);
+ if (a != null && b != null) {
+ Assert.assertFalse(a == b);
+ }
+ }
+ }
+
+ @Test
+ public void testEqualityNetMask() throws Exception {
+
+ InetAddress srcIP = InetAddress.getByName("1.1.1.1");
+ InetAddress ipMask = InetAddress.getByName("255.255.255.255");
+ InetAddress srcIP2 = InetAddress.getByName("1.1.1.1");
+ InetAddress ipMask2 = null;
+ short ethertype = EtherTypes.IPv4.shortValue();
+ short ethertype2 = EtherTypes.IPv4.shortValue();
+
+ /*
+ * Create a SAL Flow aFlow
+ */
+ Match match1 = new Match();
+ Match match2 = new Match();
+
+ match1.setField(new DlType(ethertype));
+ match1.setField(new NwSrc(srcIP, ipMask));
+
+ match2.setField(new DlType(ethertype2));
+ match2.setField(new NwSrc(srcIP2, ipMask2));
+
+ Assert.assertTrue(match1.equals(match2));
+
+ ipMask2 = InetAddress.getByName("255.255.255.255");
+ match2.setField(new NwSrc(srcIP2, ipMask2));
+
+ srcIP = InetAddress.getByName("2001:420:281:1004:407a:57f4:4d15:c355");
+ srcIP2 = InetAddress.getByName("2001:420:281:1004:407a:57f4:4d15:c355");
+ ipMask = null;
+ ipMask2 = InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
+ ethertype = EtherTypes.IPv6.shortValue();
+ ethertype2 = EtherTypes.IPv6.shortValue();
+
+ match1.setField(new DlType(ethertype));
+ match1.setField(new NwSrc(srcIP, ipMask));
+
+ match2.setField(new DlType(ethertype2));
+ match2.setField(new NwSrc(srcIP2, ipMask2));
+
+ Assert.assertEquals(match1, match2);
+ }
+
+ @Test
+ public void testHashCodeWithReverseMatch() throws Exception {
+ InetAddress srcIP1 = InetAddress.getByName("1.1.1.1");
+ InetAddress ipMask1 = InetAddress.getByName("255.255.255.255");
+ InetAddress srcIP2 = InetAddress.getByName("2.2.2.2");
+ InetAddress ipMask2 = InetAddress.getByName("255.255.255.255");
+ MatchField<?> field1 = new NwSrc(srcIP1, ipMask1);
+ MatchField<?> field2 = new NwDst(srcIP2, ipMask2);
+ Match match1 = new Match();
+ match1.setField(field1);
+ match1.setField(field2);
+ Match match2 = match1.reverse();
+ Assert.assertFalse(match1.hashCode() == match2.hashCode());
+ }
+
+ @Test
+ public void testHashCode() throws Exception {
+ byte srcMac1[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc };
+ byte srcMac2[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc };
+ byte dstMac1[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d, (byte) 0x5e, (byte) 0x6f };
+ byte dstMac2[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d, (byte) 0x5e, (byte) 0x6f };
+ short ethertype = EtherTypes.IPv4.shortValue();
+ short ethertype2 = EtherTypes.IPv4.shortValue();
+ InetAddress srcIP1 = InetAddress.getByName("1.1.1.1");
+ InetAddress ipMask1 = InetAddress.getByName("255.255.255.255");
+ InetAddress srcIP2 = InetAddress.getByName("1.1.1.1");
+ InetAddress ipMask2 = InetAddress.getByName("255.255.255.255");
+
+ Match match1 = new Match();
+ Match match2 = new Match();
+
+ MatchField<?> field1 = new DlSrc(srcMac1);
+ MatchField<?> field2 = new DlSrc(srcMac2);
+ Assert.assertTrue(field1.hashCode() == field2.hashCode());
+
+ match1.setField(field1);
+ match2.setField(field2);
+ Assert.assertTrue(match1.hashCode() == match2.hashCode());
+
+ MatchField<?> field3 = new DlDst(dstMac1);
+ MatchField<?> field4 = new DlDst(dstMac2);
+ Assert.assertTrue(field3.hashCode() == field4.hashCode());
+
+ match1.setField(field3);
+ match2.setField(field4);
+ Assert.assertTrue(match1.hashCode() == match2.hashCode());
+
+ MatchField<?> field5 = new DlType(ethertype);
+ MatchField<?> field6 = new DlType(ethertype2);
+ Assert.assertTrue(field5.hashCode() == field6.hashCode());
+
+ match1.setField(field5);
+ match2.setField(field6);
+ Assert.assertTrue(match1.hashCode() == match2 .hashCode());
+
+ MatchField<?> field7 = new NwSrc(srcIP1, ipMask1);
+ MatchField<?> field8 = new NwSrc(srcIP2, ipMask2);
+ Assert.assertTrue(field7.hashCode() == field8.hashCode());
+
+ match1.setField(field7);
+ match2.setField(field8);
+ Assert.assertTrue(match1.hashCode() == match2.hashCode());
+
+ }
+
+ @Test
+ public void testCloning() throws Exception {
+ Node node = NodeCreator.createOFNode(7L);
+ NodeConnector port = NodeConnectorCreator.createOFNodeConnector((short) 24, node);
+ byte srcMac[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc };
+ byte dstMac[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d, (byte) 0x5e, (byte) 0x6f };
+ InetAddress srcIP = InetAddress.getByName("2001:420:281:1004:407a:57f4:4d15:c355");
+ InetAddress dstIP = InetAddress.getByName("2001:420:281:1004:e123:e688:d655:a1b0");
+ InetAddress ipMasks = InetAddress.getByName("ffff:ffff:ffff:ffff:0:0:0:0");
+ InetAddress ipMaskd = InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:0");
+ short ethertype = EtherTypes.IPv6.shortValue();
+ short vlan = (short) 27;
+ byte vlanPr = (byte) 3;
+ Byte tos = 4;
+ byte proto = IPProtocols.UDP.byteValue();
+ short src = (short) 5500;
+ short dst = 80;
+
+ /*
+ * Create a SAL Flow aFlow
+ */
+ Match match = new Match();
+ match.setField(new InPort(port));
+ match.setField(new DlSrc(srcMac));
+ match.setField(new DlDst(dstMac));
+ match.setField(new DlType(ethertype));
+ match.setField(new DlVlan(vlan));
+ match.setField(new DlVlanPriority(vlanPr));
+ match.setField(new NwSrc(srcIP, ipMasks));
+ match.setField(new NwDst(dstIP, ipMaskd));
+ match.setField(new NwTos(tos));
+ match.setField(new NwProtocol(proto));
+ match.setField(new TpSrc(src));
+ match.setField(new TpDst(dst));
+
+ Match cloned = match.clone();
+
+ // Make sure all values are equals
+ for (String type : match.getMatchesList()) {
+ if (match.isPresent(type)) {
+ if (!match.getField(type).equals(cloned.getField(type))) {
+ Assert.assertEquals(match.getField(type), cloned.getField(type));
+ }
+ }
+ }
+
+ // Make sure none of the fields couples are pointing to the same
+ // reference
+ MatchField<?> a = null, b = null;
+ for (String type : match.getMatchesList()) {
+ a = match.getField(type);
+ b = cloned.getField(type);
+ if (a != null && b != null) {
+ Assert.assertFalse(a == b);
+ }
+ }
+
+ Assert.assertTrue(match.equals(cloned));
+
+ Assert.assertEquals(match.getField(DlSrc.TYPE), cloned.getField(DlSrc.TYPE));
+ Assert.assertEquals(match.getField(NwDst.TYPE), cloned.getField(NwDst.TYPE));
+ Assert.assertEquals(match.getField(NwDst.TYPE).getMask(), cloned.getField(NwDst.TYPE).getMask());
+ Assert.assertEquals(match.hashCode(), cloned.hashCode());
+ }
+
+ @Test
+ public void testFlip() throws Exception {
+ Node node = NodeCreator.createOFNode(7L);
+ NodeConnector port = NodeConnectorCreator.createOFNodeConnector((short) 24, node);
+ byte srcMac[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc };
+ byte dstMac[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d, (byte) 0x5e, (byte) 0x6f };
+ InetAddress srcIP = InetAddress.getByName("2001:420:281:1004:407a:57f4:4d15:c355");
+ InetAddress dstIP = InetAddress.getByName("2001:420:281:1004:e123:e688:d655:a1b0");
+ InetAddress ipMasks = InetAddress.getByName("ffff:ffff:ffff:ffff:0:0:0:0");
+ InetAddress ipMaskd = InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:0");
+ short ethertype = EtherTypes.IPv6.shortValue();
+ short vlan = (short) 27;
+ byte vlanPr = (byte) 3;
+ Byte tos = 4;
+ byte proto = IPProtocols.UDP.byteValue();
+ short src = (short) 5500;
+ short dst = 80;
+
+ /*
+ * Create a SAL Flow aFlow
+ */
+ Match match = new Match();
+ match.setField(new InPort(port));
+ match.setField(new DlSrc(srcMac));
+ match.setField(new DlDst(dstMac));
+ match.setField(new DlType(ethertype));
+ match.setField(new DlVlan(vlan));
+ match.setField(new DlVlanPriority(vlanPr));
+ match.setField(new NwSrc(srcIP, ipMasks));
+ match.setField(new NwDst(dstIP, ipMaskd));
+ match.setField(new NwTos(tos));
+ match.setField(new NwProtocol(proto));
+ match.setField(new TpSrc(src));
+ match.setField(new TpDst(dst));
+
+ Match flipped = match.reverse();
+
+ Assert.assertEquals(match.getField(DlType.TYPE), flipped.getField(DlType.TYPE));
+ Assert.assertEquals(match.getField(DlVlan.TYPE), flipped.getField(DlVlan.TYPE));
+
+ Assert.assertArrayEquals((byte[]) match.getField(DlDst.TYPE).getValue(), (byte[]) flipped.getField(DlSrc.TYPE)
+ .getValue());
+
+ Assert.assertEquals(match.getField(NwDst.TYPE).getValue(), flipped.getField(NwSrc.TYPE).getValue());
+
+ Assert.assertEquals(match.getField(TpDst.TYPE).getValue(), flipped.getField(TpSrc.TYPE).getValue());
+
+ Match flipflip = flipped.reverse().reverse();
+ Assert.assertEquals(flipflip, flipped);
+
+ }
+
+ @Test
+ public void testVlanNone() throws Exception {
+ // The value 0 is used to indicate that no VLAN ID is set
+ short vlan = (short) 0;
+ MatchField<?> field = new DlVlan(vlan);
+
+ Assert.assertTrue(field != null);
+ Assert.assertEquals(field.getValue(), new Short(vlan));
+ Assert.assertTrue(field.isValid());
+ }
+
+ @Test
+ public void testIntersection() throws UnknownHostException {
+ Short ethType = Short.valueOf((short)0x800);
+ InetAddress ip1 = InetAddress.getByName("1.1.1.1");
+ InetAddress ip2 = InetAddress.getByName("1.1.1.0");
+ InetAddress ipm2 = InetAddress.getByName("255.255.255.0");
+ InetAddress ip3 = InetAddress.getByName("1.3.0.0");
+ InetAddress ipm3 = InetAddress.getByName("255.255.0.0");
+ InetAddress ip4 = InetAddress.getByName("1.3.4.4");
+ InetAddress ipm4 = InetAddress.getByName("255.255.255.0");
+
+ Match m1 = new Match();
+ m1.setField(new DlType(ethType));
+ m1.setField(new NwSrc(ip1));
+
+ Match m2 = new Match();
+ m2.setField(new DlType(ethType));
+ m2.setField(new NwSrc(ip2, ipm2));
+
+ Match m3 = new Match();
+ m3.setField(new DlType(ethType));
+ m3.setField(new NwSrc(ip3, ipm3));
+ m3.setField(new NwProtocol(IPProtocols.TCP.byteValue()));
+
+ Match m3r = m3.reverse();
+ Assert.assertTrue(m3.intersetcs(m3r));
+
+ Assert.assertTrue(m1.intersetcs(m2));
+ Assert.assertTrue(m2.intersetcs(m1));
+ Assert.assertFalse(m1.intersetcs(m3));
+ Assert.assertTrue(m1.intersetcs(m3r));
+ Assert.assertFalse(m3.intersetcs(m1));
+ Assert.assertTrue(m3.intersetcs(m1.reverse()));
+ Assert.assertFalse(m2.intersetcs(m3));
+ Assert.assertFalse(m3.intersetcs(m2));
+ Assert.assertTrue(m2.intersetcs(m3r));
+
+
+ Match i = m1.getIntersection(m2);
+ Assert.assertTrue(((Short)i.getField(DlType.TYPE).getValue()).equals(ethType));
+ // Verify intersection of IP addresses is correct
+ Assert.assertTrue(((InetAddress)i.getField(NwSrc.TYPE).getValue()).equals(ip1));
+ Assert.assertNull(i.getField(NwSrc.TYPE).getMask());
+
+ // Empty set
+ i = m2.getIntersection(m3);
+ Assert.assertNull(i);
+
+ Match m4 = new Match();
+ m4.setField(new DlType(ethType));
+ m4.setField(new NwProtocol(IPProtocols.TCP.byteValue()));
+ m3.setField(new NwSrc(ip4, ipm4));
+ Assert.assertTrue(m4.intersetcs(m3));
+
+ // Verify intersection of IP and IP mask addresses is correct
+ Match ii = m3.getIntersection(m4);
+ Assert.assertTrue(((InetAddress)ii.getField(NwSrc.TYPE).getValue()).equals(ip4));
+ Assert.assertTrue(((InetAddress)ii.getField(NwSrc.TYPE).getMask()).equals(ipm4));
+
+ Match m5 = new Match();
+ m5.setField(new DlType(ethType));
+ m3.setField(new NwSrc(ip3, ipm3));
+ m5.setField(new NwProtocol(IPProtocols.UDP.byteValue()));
+ Assert.assertFalse(m5.intersetcs(m3));
+ Assert.assertFalse(m5.intersetcs(m4));
+ Assert.assertTrue(m5.intersetcs(m5));
+ Assert.assertFalse(m3.intersetcs(m5));
+ Assert.assertFalse(m4.intersetcs(m5));
+
+
+ Match i2 = m4.getIntersection(m3);
+ Assert.assertFalse(i2.isEmpty());
+ Assert.assertFalse(i2.getMatchesList().isEmpty());
+ Assert.assertTrue(((InetAddress)i2.getField(NwSrc.TYPE).getValue()).equals(ip3));
+ Assert.assertTrue(((InetAddress)i2.getField(NwSrc.TYPE).getMask()).equals(ipm3));
+ Assert.assertTrue(((Byte)i2.getField(NwProtocol.TYPE).getValue()).equals(IPProtocols.TCP.byteValue()));
+
+ byte src[] = {(byte)0, (byte)0xab,(byte)0xbc,(byte)0xcd,(byte)0xde,(byte)0xef};
+ byte dst[] = {(byte)0x10, (byte)0x11,(byte)0x12,(byte)0x13,(byte)0x14,(byte)0x15};
+ Short srcPort = (short)1024;
+ Short dstPort = (short)80;
+
+ // Check identity
+ Match m6 = new Match();
+ m6.setField(new DlSrc(src));
+ m6.setField(new DlDst(dst));
+ m6.setField(new NwSrc(ip2, ipm2));
+ m6.setField(new NwDst(ip3, ipm3));
+ m6.setField(new NwProtocol(IPProtocols.UDP.byteValue()));
+ m6.setField(new TpSrc(srcPort));
+ m6.setField(new TpDst(dstPort));
+ Assert.assertTrue(m6.intersetcs(m6));
+ Assert.assertTrue(m6.getIntersection(m6).equals(m6));
+
+ // Empty match, represents the universal set (all packets)
+ Match u = new Match();
+ Assert.assertEquals(m6.getIntersection(u), m6);
+ Assert.assertEquals(u.getIntersection(m6), m6);
+
+ // No intersection with null match, empty set
+ Assert.assertNull(m6.getIntersection(null));
+ }
+}