2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.md.sal.binding.impl;
10 import java.util.ArrayList;
11 import java.util.EnumMap;
12 import java.util.Iterator;
13 import java.util.List;
14 import java.util.Map.Entry;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.TimeUnit;
18 import javax.annotation.Nullable;
20 import org.eclipse.xtext.xbase.lib.Exceptions;
21 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
22 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
23 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
24 import org.opendaylight.controller.md.sal.common.impl.util.compat.DataNormalizationException;
25 import org.opendaylight.controller.md.sal.common.impl.util.compat.DataNormalizationOperation;
26 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadTransaction;
27 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
28 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
29 import org.opendaylight.yangtools.concepts.Delegator;
30 import org.opendaylight.yangtools.yang.binding.DataObject;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.opendaylight.yangtools.yang.common.RpcResult;
33 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
34 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
35 import org.opendaylight.yangtools.yang.data.impl.codec.DeserializationException;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 import com.google.common.base.Function;
40 import com.google.common.base.Optional;
41 import com.google.common.cache.Cache;
42 import com.google.common.cache.CacheBuilder;
43 import com.google.common.util.concurrent.Futures;
44 import com.google.common.util.concurrent.ListenableFuture;
46 public class AbstractForwardedTransaction<T extends AsyncTransaction<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, NormalizedNode<?, ?>>>
47 implements Delegator<T> {
49 private static final Logger LOG = LoggerFactory.getLogger(AbstractForwardedTransaction.class);
50 private final T delegate;
51 private final static CacheBuilder<Object, Object> CACHE_BUILDER = CacheBuilder.newBuilder()
52 .expireAfterWrite(10, TimeUnit.MILLISECONDS).maximumSize(100);
53 private final BindingToNormalizedNodeCodec codec;
54 private final EnumMap<LogicalDatastoreType, Cache<InstanceIdentifier<?>, DataObject>> cacheMap;
56 protected AbstractForwardedTransaction(final T delegate, final BindingToNormalizedNodeCodec codec) {
58 this.delegate = delegate;
61 this.cacheMap = new EnumMap<>(LogicalDatastoreType.class);
62 cacheMap.put(LogicalDatastoreType.OPERATIONAL, CACHE_BUILDER.<InstanceIdentifier<?>, DataObject> build());
63 cacheMap.put(LogicalDatastoreType.CONFIGURATION, CACHE_BUILDER.<InstanceIdentifier<?>, DataObject> build());
68 public T getDelegate() {
72 protected final BindingToNormalizedNodeCodec getCodec() {
76 protected ListenableFuture<Optional<DataObject>> transformFuture(final LogicalDatastoreType store,
77 final InstanceIdentifier<?> path, final ListenableFuture<Optional<NormalizedNode<?, ?>>> future) {
78 return Futures.transform(future, new Function<Optional<NormalizedNode<?, ?>>, Optional<DataObject>>() {
81 public Optional<DataObject> apply(@Nullable final Optional<NormalizedNode<?, ?>> normalizedNode) {
83 final DataObject dataObject = normalizedNode.isPresent() ? codec.toBinding(path,
84 normalizedNode.get()) : null;
85 if (dataObject != null) {
86 updateCache(store, path, dataObject);
88 return Optional.fromNullable(dataObject);
89 } catch (DeserializationException e) {
90 Exceptions.sneakyThrow(e);
97 protected void doPut(final DOMDataWriteTransaction writeTransaction, final LogicalDatastoreType store,
98 final InstanceIdentifier<?> path, final DataObject data) {
99 invalidateCache(store, path);
100 final Entry<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, NormalizedNode<?, ?>> normalized = codec
101 .toNormalizedNode(path, data);
102 writeTransaction.put(store, normalized.getKey(), normalized.getValue());
105 protected void doPutWithEnsureParents(final DOMDataReadWriteTransaction writeTransaction,
106 final LogicalDatastoreType store, final InstanceIdentifier<?> path, final DataObject data) {
107 invalidateCache(store, path);
108 final Entry<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, NormalizedNode<?, ?>> normalized = codec
109 .toNormalizedNode(path, data);
111 org.opendaylight.yangtools.yang.data.api.InstanceIdentifier normalizedPath = normalized.getKey();
112 ensureParentsByMerge(writeTransaction, store, normalized.getKey(), path);
113 LOG.debug("Tx: {} : Putting data {}",getDelegate().getIdentifier(),normalized.getKey());
114 writeTransaction.put(store, normalized.getKey(), normalized.getValue());
117 protected void doMergeWithEnsureParents(final DOMDataReadWriteTransaction writeTransaction,
118 final LogicalDatastoreType store, final InstanceIdentifier<?> path, final DataObject data) {
119 invalidateCache(store, path);
120 final Entry<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, NormalizedNode<?, ?>> normalized = codec
121 .toNormalizedNode(path, data);
123 org.opendaylight.yangtools.yang.data.api.InstanceIdentifier normalizedPath = normalized.getKey();
124 ensureParentsByMerge(writeTransaction, store, normalized.getKey(), path);
125 LOG.debug("Tx: {} : Merge data {}",getDelegate().getIdentifier(),normalized.getKey());
126 writeTransaction.merge(store, normalized.getKey(), normalized.getValue());
129 private void ensureParentsByMerge(final DOMDataReadWriteTransaction writeTransaction,
130 final LogicalDatastoreType store,
131 final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier normalizedPath,
132 final InstanceIdentifier<?> path) {
133 List<PathArgument> currentArguments = new ArrayList<>();
134 DataNormalizationOperation<?> currentOp = codec.getDataNormalizer().getRootOperation();
135 Iterator<PathArgument> iterator = normalizedPath.getPath().iterator();
136 while (iterator.hasNext()) {
137 PathArgument currentArg = iterator.next();
139 currentOp = currentOp.getChild(currentArg);
140 } catch (DataNormalizationException e) {
141 throw new IllegalArgumentException(String.format("Invalid child encountered in path %s", path), e);
143 currentArguments.add(currentArg);
144 org.opendaylight.yangtools.yang.data.api.InstanceIdentifier currentPath = new org.opendaylight.yangtools.yang.data.api.InstanceIdentifier(
147 final Optional<NormalizedNode<?, ?>> d;
149 d = writeTransaction.read(store, currentPath).get();
150 } catch (InterruptedException | ExecutionException e) {
151 LOG.error("Failed to read pre-existing data from store {} path {}", store, currentPath, e);
152 throw new IllegalStateException("Failed to read pre-existing data", e);
155 if (!d.isPresent() && iterator.hasNext()) {
156 writeTransaction.merge(store, currentPath, currentOp.createDefault(currentArg));
161 protected void doMerge(final DOMDataWriteTransaction writeTransaction, final LogicalDatastoreType store,
162 final InstanceIdentifier<?> path, final DataObject data) {
163 invalidateCache(store, path);
164 final Entry<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, NormalizedNode<?, ?>> normalized = codec
165 .toNormalizedNode(path, data);
166 writeTransaction.merge(store, normalized.getKey(), normalized.getValue());
169 protected void doDelete(final DOMDataWriteTransaction writeTransaction, final LogicalDatastoreType store,
170 final InstanceIdentifier<?> path) {
171 invalidateCache(store, path);
172 final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier normalized = codec.toNormalized(path);
173 writeTransaction.delete(store, normalized);
176 protected ListenableFuture<RpcResult<TransactionStatus>> doCommit(final DOMDataWriteTransaction writeTransaction) {
177 return writeTransaction.commit();
180 protected void doCancel(final DOMDataWriteTransaction writeTransaction) {
181 writeTransaction.cancel();
184 protected ListenableFuture<Optional<DataObject>> doRead(final DOMDataReadTransaction readTransaction,
185 final LogicalDatastoreType store, final InstanceIdentifier<?> path) {
186 final DataObject dataObject = getFromCache(store, path);
187 if (dataObject == null) {
188 final ListenableFuture<Optional<NormalizedNode<?, ?>>> future = readTransaction.read(store,
189 codec.toNormalized(path));
190 return transformFuture(store, path, future);
192 return Futures.immediateFuture(Optional.of(dataObject));
196 private DataObject getFromCache(final LogicalDatastoreType store, final InstanceIdentifier<?> path) {
197 Cache<InstanceIdentifier<?>, DataObject> cache = cacheMap.get(store);
199 return cache.getIfPresent(path);
204 private void updateCache(final LogicalDatastoreType store, final InstanceIdentifier<?> path,
205 final DataObject dataObject) {
206 // Check if cache exists. If not create one.
207 Cache<InstanceIdentifier<?>, DataObject> cache = cacheMap.get(store);
209 cache = CacheBuilder.newBuilder().maximumSize(1000).expireAfterWrite(1, TimeUnit.MINUTES).build();
213 cache.put(path, dataObject);
216 private void invalidateCache(final LogicalDatastoreType store, final InstanceIdentifier<?> path) {
217 // FIXME: Optimization: invalidate only parents and children of path
218 Cache<InstanceIdentifier<?>, DataObject> cache = cacheMap.get(store);
219 cache.invalidateAll();
220 LOG.trace("Cache invalidated");