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.DataNormalizationOperation;
25 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadTransaction;
26 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
27 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
28 import org.opendaylight.yangtools.concepts.Delegator;
29 import org.opendaylight.yangtools.yang.binding.DataObject;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.opendaylight.yangtools.yang.common.RpcResult;
32 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
34 import org.opendaylight.yangtools.yang.data.impl.codec.DeserializationException;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 import com.google.common.base.Function;
39 import com.google.common.base.Optional;
40 import com.google.common.cache.Cache;
41 import com.google.common.cache.CacheBuilder;
42 import com.google.common.util.concurrent.Futures;
43 import com.google.common.util.concurrent.ListenableFuture;
45 public class AbstractForwardedTransaction<T extends AsyncTransaction<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, NormalizedNode<?, ?>>>
46 implements Delegator<T> {
48 private static final Logger LOG = LoggerFactory.getLogger(AbstractForwardedTransaction.class);
49 private final T delegate;
50 private final static CacheBuilder<Object, Object> CACHE_BUILDER = CacheBuilder.newBuilder()
51 .expireAfterWrite(10, TimeUnit.MILLISECONDS).maximumSize(100);
52 private final BindingToNormalizedNodeCodec codec;
53 private final EnumMap<LogicalDatastoreType, Cache<InstanceIdentifier<?>, DataObject>> cacheMap;
55 protected AbstractForwardedTransaction(final T delegate, final BindingToNormalizedNodeCodec codec) {
57 this.delegate = delegate;
60 this.cacheMap = new EnumMap<>(LogicalDatastoreType.class);
61 cacheMap.put(LogicalDatastoreType.OPERATIONAL, CACHE_BUILDER.<InstanceIdentifier<?>, DataObject> build());
62 cacheMap.put(LogicalDatastoreType.CONFIGURATION, CACHE_BUILDER.<InstanceIdentifier<?>, DataObject> build());
67 public T getDelegate() {
71 protected final BindingToNormalizedNodeCodec getCodec() {
75 protected ListenableFuture<Optional<DataObject>> transformFuture(final LogicalDatastoreType store,
76 final InstanceIdentifier<?> path, final ListenableFuture<Optional<NormalizedNode<?, ?>>> future) {
77 return Futures.transform(future, new Function<Optional<NormalizedNode<?, ?>>, Optional<DataObject>>() {
80 public Optional<DataObject> apply(@Nullable final Optional<NormalizedNode<?, ?>> normalizedNode) {
82 final DataObject dataObject = normalizedNode.isPresent() ? codec.toBinding(path,
83 normalizedNode.get()) : null;
84 if(dataObject != null) {
85 updateCache(store, path, dataObject);
87 return Optional.fromNullable(dataObject);
88 } catch (DeserializationException e) {
89 Exceptions.sneakyThrow(e);
96 protected void doPut(final DOMDataWriteTransaction writeTransaction, final LogicalDatastoreType store,
97 final InstanceIdentifier<?> path, final DataObject data) {
98 invalidateCache(store, path);
99 final Entry<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, NormalizedNode<?, ?>> normalized = codec
100 .toNormalizedNode(path, data);
101 writeTransaction.put(store, normalized.getKey(), normalized.getValue());
104 protected void doPutWithEnsureParents(final DOMDataReadWriteTransaction writeTransaction,
105 final LogicalDatastoreType store, final InstanceIdentifier<?> path, final DataObject data) {
106 invalidateCache(store, path);
107 final Entry<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, NormalizedNode<?, ?>> normalized = codec
108 .toNormalizedNode(path, data);
110 org.opendaylight.yangtools.yang.data.api.InstanceIdentifier normalizedPath = normalized.getKey();
111 List<PathArgument> currentArguments = new ArrayList<>();
112 DataNormalizationOperation<?> currentOp = codec.getDataNormalizer().getRootOperation();
113 Iterator<PathArgument> iterator = normalizedPath.getPath().iterator();
114 while (iterator.hasNext()) {
115 PathArgument currentArg = iterator.next();
116 currentOp = currentOp.getChild(currentArg);
117 currentArguments.add(currentArg);
118 org.opendaylight.yangtools.yang.data.api.InstanceIdentifier currentPath = new org.opendaylight.yangtools.yang.data.api.InstanceIdentifier(
121 final Optional<NormalizedNode<?, ?>> d;
123 d = writeTransaction.read(store, currentPath).get();
124 } catch (InterruptedException | ExecutionException e) {
125 LOG.error("Failed to read pre-existing data from store {} path {}", store, currentPath, e);
126 throw new IllegalStateException("Failed to read pre-existing data", e);
129 if (!d.isPresent() && iterator.hasNext()) {
130 writeTransaction.put(store, currentPath, currentOp.createDefault(currentArg));
133 //LOG .info("Tx: {} : Putting data {}",getDelegate().getIdentifier(),normalized.getKey());
134 writeTransaction.put(store, normalized.getKey(), normalized.getValue());
137 protected void doMerge(final DOMDataWriteTransaction writeTransaction, final LogicalDatastoreType store,
138 final InstanceIdentifier<?> path, final DataObject data) {
139 invalidateCache(store, path);
140 final Entry<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, NormalizedNode<?, ?>> normalized = codec
141 .toNormalizedNode(path, data);
142 writeTransaction.merge(store, normalized.getKey(), normalized.getValue());
145 protected void doDelete(final DOMDataWriteTransaction writeTransaction, final LogicalDatastoreType store,
146 final InstanceIdentifier<?> path) {
147 invalidateCache(store, path);
148 final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier normalized = codec.toNormalized(path);
149 writeTransaction.delete(store, normalized);
152 protected ListenableFuture<RpcResult<TransactionStatus>> doCommit(final DOMDataWriteTransaction writeTransaction) {
153 return writeTransaction.commit();
156 protected void doCancel(final DOMDataWriteTransaction writeTransaction) {
157 writeTransaction.cancel();
160 protected ListenableFuture<Optional<DataObject>> doRead(final DOMDataReadTransaction readTransaction,
161 final LogicalDatastoreType store, final InstanceIdentifier<?> path) {
162 final DataObject dataObject = getFromCache(store, path);
163 if (dataObject == null) {
164 final ListenableFuture<Optional<NormalizedNode<?, ?>>> future = readTransaction.read(store,
165 codec.toNormalized(path));
166 return transformFuture(store, path, future);
168 return Futures.immediateFuture(Optional.of(dataObject));
172 private DataObject getFromCache(final LogicalDatastoreType store, final InstanceIdentifier<?> path) {
173 Cache<InstanceIdentifier<?>, DataObject> cache = cacheMap.get(store);
175 return cache.getIfPresent(path);
180 private void updateCache(final LogicalDatastoreType store, final InstanceIdentifier<?> path,
181 final DataObject dataObject) {
182 // Check if cache exists. If not create one.
183 Cache<InstanceIdentifier<?>, DataObject> cache = cacheMap.get(store);
185 cache = CacheBuilder.newBuilder().maximumSize(1000).expireAfterWrite(1, TimeUnit.MINUTES).build();
189 cache.put(path, dataObject);
192 private void invalidateCache(final LogicalDatastoreType store, final InstanceIdentifier<?> path) {
193 // FIXME: Optimization: invalidate only parents and children of path
194 Cache<InstanceIdentifier<?>, DataObject> cache = cacheMap.get(store);
195 cache.invalidateAll();
196 LOG.trace("Cache invalidated");