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 java.util.ArrayList;
19 import java.util.Collection;
20 import javax.annotation.Nonnull;
21 import javax.annotation.Nullable;
22 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
23 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
26 import org.opendaylight.groupbasedpolicy.sxp.ep.provider.impl.DSAsyncDao;
27 import org.opendaylight.groupbasedpolicy.sxp.ep.provider.impl.EPTemplateListener;
28 import org.opendaylight.groupbasedpolicy.sxp.ep.provider.impl.ReadableByKey;
29 import org.opendaylight.groupbasedpolicy.sxp.ep.provider.impl.SimpleCachedDao;
30 import org.opendaylight.groupbasedpolicy.sxp.ep.provider.impl.util.SxpListenerUtil;
31 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;
32 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;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.sxp.database.rev160308.Sgt;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 * Purpose: general dao for EndPoint templates
40 public class EPPolicyTemplateDaoImpl implements DSAsyncDao<Sgt, EndpointPolicyTemplateBySgt>,
41 ReadableByKey<EpPolicyTemplateValueKey, EndpointPolicyTemplateBySgt> {
43 private static final ListenableFuture<Optional<EndpointPolicyTemplateBySgt>> READ_FUTURE_ABSENT = Futures.immediateFuture(Optional.absent());
45 private final DataBroker dataBroker;
46 private final SimpleCachedDao<Sgt, EndpointPolicyTemplateBySgt> cachedDao;
47 private final EpPolicyTemplateValueKeyFactory keyFactory;
49 public EPPolicyTemplateDaoImpl(final DataBroker dataBroker,
50 final SimpleCachedDao<Sgt, EndpointPolicyTemplateBySgt> cachedDao,
51 final EpPolicyTemplateValueKeyFactory keyFactory) {
52 this.dataBroker = dataBroker;
53 this.cachedDao = cachedDao;
54 this.keyFactory = keyFactory;
58 public ListenableFuture<Optional<EndpointPolicyTemplateBySgt>> read(@Nonnull final Sgt key) {
59 final Optional<EndpointPolicyTemplateBySgt> cachedEndpointPolicyTemplateBySgtValue = lookup(cachedDao, key);
60 if (cachedEndpointPolicyTemplateBySgtValue.isPresent()) {
61 return Futures.immediateFuture(cachedEndpointPolicyTemplateBySgtValue);
62 } else if (!cachedDao.isEmpty()) {
63 return READ_FUTURE_ABSENT;
65 final ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction();
66 final CheckedFuture<Optional<EndpointPolicyTemplateBySgt>, ReadFailedException> read =
67 rTx.read(LogicalDatastoreType.CONFIGURATION, buildReadPath(key));
69 Futures.addCallback(read, SxpListenerUtil.createTxCloseCallback(rTx));
71 return Futures.transform(read, new Function<Optional<EndpointPolicyTemplateBySgt>, Optional<EndpointPolicyTemplateBySgt>>() {
74 public Optional<EndpointPolicyTemplateBySgt> apply(@Nullable final Optional<EndpointPolicyTemplateBySgt> input) {
75 if (input.isPresent()) {
76 cachedDao.update(key, keyFactory.sortValueKeyLists(input.get()));
85 protected InstanceIdentifier<EndpointPolicyTemplateBySgt> buildReadPath(final Sgt key) {
86 return EPTemplateListener.SXP_MAPPER_TEMPLATE_PARENT_PATH
87 .child(EndpointPolicyTemplateBySgt.class, new EndpointPolicyTemplateBySgtKey(key));
90 private Optional<EndpointPolicyTemplateBySgt> lookup(final SimpleCachedDao<Sgt, EndpointPolicyTemplateBySgt> cachedDao, final Sgt key) {
91 return cachedDao.find(key);
95 public Collection<EndpointPolicyTemplateBySgt> readBy(@Nonnull final EpPolicyTemplateValueKey specialKey) {
96 final Predicate<EpPolicyTemplateValueKey> templateValuePredicate = Predicates.equalTo(specialKey);
97 final Collection<EndpointPolicyTemplateBySgt> foundTemplates = new ArrayList<>();
99 for (EndpointPolicyTemplateBySgt epPolicyTemplate : cachedDao.values()) {
100 final EpPolicyTemplateValueKey templateValueKey = keyFactory.createKeyWithDefaultOrdering(epPolicyTemplate);
101 if (templateValuePredicate.apply(templateValueKey)) {
102 foundTemplates.add(epPolicyTemplate);
106 return foundTemplates;