2 * Copyright (c) 2016 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.groupbasedpolicy.sxp.ep.provider.impl.dao;
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Function;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Predicate;
14 import com.google.common.base.Predicates;
15 import com.google.common.util.concurrent.CheckedFuture;
16 import com.google.common.util.concurrent.Futures;
17 import com.google.common.util.concurrent.ListenableFuture;
18 import com.google.common.util.concurrent.MoreExecutors;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import javax.annotation.Nonnull;
23 import javax.annotation.Nullable;
24 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
25 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
26 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
27 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
28 import org.opendaylight.groupbasedpolicy.sxp.ep.provider.impl.DSAsyncDao;
29 import org.opendaylight.groupbasedpolicy.sxp.ep.provider.impl.EPTemplateListener;
30 import org.opendaylight.groupbasedpolicy.sxp.ep.provider.impl.ReadableByKey;
31 import org.opendaylight.groupbasedpolicy.sxp.ep.provider.impl.SimpleCachedDao;
32 import org.opendaylight.groupbasedpolicy.sxp.ep.provider.impl.util.SxpListenerUtil;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.integration.sxp.ep.provider.model.rev160302.sxp.ep.mapper.EndpointPolicyTemplateBySgt;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.integration.sxp.ep.provider.model.rev160302.sxp.ep.mapper.EndpointPolicyTemplateBySgtKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.sxp.database.rev160308.Sgt;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 * Purpose: general dao for EndPoint templates
42 public class EPPolicyTemplateDaoImpl implements DSAsyncDao<Sgt, EndpointPolicyTemplateBySgt>,
43 ReadableByKey<EpPolicyTemplateValueKey, EndpointPolicyTemplateBySgt> {
45 private static final ListenableFuture<Optional<EndpointPolicyTemplateBySgt>> READ_FUTURE_ABSENT = Futures.immediateFuture(Optional.absent());
47 private final DataBroker dataBroker;
48 private final SimpleCachedDao<Sgt, EndpointPolicyTemplateBySgt> cachedDao;
49 private final EpPolicyTemplateValueKeyFactory keyFactory;
51 public EPPolicyTemplateDaoImpl(final DataBroker dataBroker,
52 final SimpleCachedDao<Sgt, EndpointPolicyTemplateBySgt> cachedDao,
53 final EpPolicyTemplateValueKeyFactory keyFactory) {
54 this.dataBroker = dataBroker;
55 this.cachedDao = cachedDao;
56 this.keyFactory = keyFactory;
60 public ListenableFuture<Optional<EndpointPolicyTemplateBySgt>> read(@Nonnull final Sgt key) {
61 final Optional<EndpointPolicyTemplateBySgt> cachedEndpointPolicyTemplateBySgtValue = lookup(cachedDao, key);
62 if (cachedEndpointPolicyTemplateBySgtValue.isPresent()) {
63 return Futures.immediateFuture(cachedEndpointPolicyTemplateBySgtValue);
64 } else if (!cachedDao.isEmpty()) {
65 return READ_FUTURE_ABSENT;
67 final ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction();
68 final CheckedFuture<Optional<EndpointPolicyTemplateBySgt>, ReadFailedException> read =
69 rTx.read(LogicalDatastoreType.CONFIGURATION, buildReadPath(key));
71 Futures.addCallback(read, SxpListenerUtil.createTxCloseCallback(rTx), MoreExecutors.directExecutor());
73 return Futures.transform(read, new Function<Optional<EndpointPolicyTemplateBySgt>, Optional<EndpointPolicyTemplateBySgt>>() {
76 public Optional<EndpointPolicyTemplateBySgt> apply(@Nullable final Optional<EndpointPolicyTemplateBySgt> input) {
77 if (input.isPresent()) {
78 cachedDao.update(key, keyFactory.sortValueKeyLists(input.get()));
82 }, MoreExecutors.directExecutor());
87 protected InstanceIdentifier<EndpointPolicyTemplateBySgt> buildReadPath(final Sgt key) {
88 return EPTemplateListener.SXP_MAPPER_TEMPLATE_PARENT_PATH
89 .child(EndpointPolicyTemplateBySgt.class, new EndpointPolicyTemplateBySgtKey(key));
92 private Optional<EndpointPolicyTemplateBySgt> lookup(final SimpleCachedDao<Sgt, EndpointPolicyTemplateBySgt> cachedDao, final Sgt key) {
93 return cachedDao.find(key);
97 public Collection<EndpointPolicyTemplateBySgt> readBy(@Nonnull final EpPolicyTemplateValueKey specialKey) {
98 final Predicate<EpPolicyTemplateValueKey> templateValuePredicate = Predicates.equalTo(specialKey);
99 final Collection<EndpointPolicyTemplateBySgt> foundTemplates = new ArrayList<>();
101 for (EndpointPolicyTemplateBySgt epPolicyTemplate : cachedDao.values()) {
102 final EpPolicyTemplateValueKey templateValueKey = keyFactory.createKeyWithDefaultOrdering(epPolicyTemplate);
103 if (templateValuePredicate.apply(templateValueKey)) {
104 foundTemplates.add(epPolicyTemplate);
108 return foundTemplates;