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.base.Function;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Predicate;
13 import com.google.common.base.Predicates;
14 import com.google.common.util.concurrent.CheckedFuture;
15 import com.google.common.util.concurrent.Futures;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import javax.annotation.Nonnull;
20 import javax.annotation.Nullable;
21 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
22 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
23 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
24 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
25 import org.opendaylight.groupbasedpolicy.sxp.ep.provider.impl.DSAsyncDao;
26 import org.opendaylight.groupbasedpolicy.sxp.ep.provider.impl.EPTemplateListener;
27 import org.opendaylight.groupbasedpolicy.sxp.ep.provider.impl.ReadableByKey;
28 import org.opendaylight.groupbasedpolicy.sxp.ep.provider.impl.SimpleCachedDao;
29 import org.opendaylight.groupbasedpolicy.sxp.ep.provider.impl.util.SxpListenerUtil;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.ep.provider.model.rev160302.sxp.ep.mapper.EndpointPolicyTemplateBySgt;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.ep.provider.model.rev160302.sxp.ep.mapper.EndpointPolicyTemplateBySgtKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.sxp.database.rev160308.Sgt;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 * Purpose: general dao for EndPoint templates
39 public class EPPolicyTemplateDaoImpl implements DSAsyncDao<Sgt, EndpointPolicyTemplateBySgt>,
40 ReadableByKey<EpPolicyTemplateValueKey, EndpointPolicyTemplateBySgt> {
42 private static final ListenableFuture<Optional<EndpointPolicyTemplateBySgt>> READ_FUTURE_ABSENT = Futures.immediateFuture(Optional.absent());
44 private final DataBroker dataBroker;
45 private final SimpleCachedDao<Sgt, EndpointPolicyTemplateBySgt> cachedDao;
46 private final EpPolicyTemplateValueKeyFactory keyFactory;
48 public EPPolicyTemplateDaoImpl(final DataBroker dataBroker,
49 final SimpleCachedDao<Sgt, EndpointPolicyTemplateBySgt> cachedDao,
50 final EpPolicyTemplateValueKeyFactory keyFactory) {
51 this.dataBroker = dataBroker;
52 this.cachedDao = cachedDao;
53 this.keyFactory = keyFactory;
57 public ListenableFuture<Optional<EndpointPolicyTemplateBySgt>> read(@Nonnull final Sgt key) {
58 final Optional<EndpointPolicyTemplateBySgt> cachedEndpointPolicyTemplateBySgtalue = lookup(cachedDao, key);
59 if (cachedEndpointPolicyTemplateBySgtalue.isPresent()) {
60 return Futures.immediateFuture(cachedEndpointPolicyTemplateBySgtalue);
61 } else if (!cachedDao.isEmpty()) {
62 return READ_FUTURE_ABSENT;
64 final ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction();
65 final CheckedFuture<Optional<EndpointPolicyTemplateBySgt>, ReadFailedException> read =
66 rTx.read(LogicalDatastoreType.CONFIGURATION, buildReadPath(key));
68 Futures.addCallback(read, SxpListenerUtil.createTxCloseCallback(rTx));
70 return Futures.transform(read, new Function<Optional<EndpointPolicyTemplateBySgt>, Optional<EndpointPolicyTemplateBySgt>>() {
73 public Optional<EndpointPolicyTemplateBySgt> apply(@Nullable final Optional<EndpointPolicyTemplateBySgt> input) {
74 if (input.isPresent()) {
75 cachedDao.update(key, keyFactory.sortValueKeyLists(input.get()));
83 protected InstanceIdentifier<EndpointPolicyTemplateBySgt> buildReadPath(final Sgt key) {
84 return EPTemplateListener.SXP_MAPPER_TEMPLATE_PARENT_PATH
85 .child(EndpointPolicyTemplateBySgt.class, new EndpointPolicyTemplateBySgtKey(key));
88 private Optional<EndpointPolicyTemplateBySgt> lookup(final SimpleCachedDao<Sgt, EndpointPolicyTemplateBySgt> cachedDao, final Sgt key) {
89 return cachedDao.find(key);
93 public Collection<EndpointPolicyTemplateBySgt> readBy(@Nonnull final EpPolicyTemplateValueKey specialKey) {
94 final Predicate<EpPolicyTemplateValueKey> templateValuePredicate = Predicates.equalTo(specialKey);
95 final Collection<EndpointPolicyTemplateBySgt> foundTemplates = new ArrayList<>();
97 for (EndpointPolicyTemplateBySgt epPolicyTemplate : cachedDao.values()) {
98 final EpPolicyTemplateValueKey templateValueKey = keyFactory.createKeyWithDefaultOrdering(epPolicyTemplate);
99 if (templateValuePredicate.apply(templateValueKey)) {
100 foundTemplates.add(epPolicyTemplate);
104 return foundTemplates;