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.util.concurrent.CheckedFuture;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import java.util.List;
16 import javax.annotation.Nonnull;
17 import javax.annotation.Nullable;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
22 import org.opendaylight.groupbasedpolicy.sxp.ep.provider.impl.DSAsyncDao;
23 import org.opendaylight.groupbasedpolicy.sxp.ep.provider.impl.EPTemplateListener;
24 import org.opendaylight.groupbasedpolicy.sxp.ep.provider.impl.SimpleCachedDao;
25 import org.opendaylight.groupbasedpolicy.sxp.ep.provider.impl.util.SxpListenerUtil;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.integration.sxp.ep.provider.model.rev160302.SxpEpMapper;
28 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.EndpointForwardingTemplateBySubnet;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 * Purpose: general dao for EndPoint templates
34 public class EPForwardingTemplateDaoImpl implements DSAsyncDao<IpPrefix, EndpointForwardingTemplateBySubnet> {
36 private static final ListenableFuture<Optional<EndpointForwardingTemplateBySubnet>> READ_FUTURE_ABSENT = Futures.immediateFuture(Optional.absent());
37 private final DataBroker dataBroker;
38 private final SimpleCachedDao<IpPrefix, EndpointForwardingTemplateBySubnet> cachedDao;
40 public EPForwardingTemplateDaoImpl(final DataBroker dataBroker,
41 final SimpleCachedDao<IpPrefix, EndpointForwardingTemplateBySubnet> cachedDao) {
42 this.dataBroker = dataBroker;
43 this.cachedDao = cachedDao;
47 public ListenableFuture<Optional<EndpointForwardingTemplateBySubnet>> read(@Nonnull final IpPrefix key) {
48 final Optional<EndpointForwardingTemplateBySubnet> value = lookup(cachedDao, key);
49 final ListenableFuture<Optional<EndpointForwardingTemplateBySubnet>> readResult;
50 if (value.isPresent()) {
51 readResult = Futures.immediateFuture(value);
52 } else if (!cachedDao.isEmpty()) {
53 return READ_FUTURE_ABSENT;
55 final ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction();
56 final CheckedFuture<Optional<SxpEpMapper>, ReadFailedException> read =
57 rTx.read(LogicalDatastoreType.CONFIGURATION, buildReadPath(key));
59 Futures.addCallback(read, SxpListenerUtil.createTxCloseCallback(rTx));
61 readResult = Futures.transform(read, new Function<Optional<SxpEpMapper>, Optional<EndpointForwardingTemplateBySubnet>>() {
64 public Optional<EndpointForwardingTemplateBySubnet> apply(@Nullable final Optional<SxpEpMapper> input) {
65 if (input.isPresent()) {
67 cachedDao.invalidateCache();
69 // iterate through all template entries and update cachedDao
70 final List<EndpointForwardingTemplateBySubnet> templateLot = input.get().getEndpointForwardingTemplateBySubnet();
71 if (templateLot != null) {
72 for (EndpointForwardingTemplateBySubnet template : templateLot) {
73 cachedDao.update(template.getIpPrefix(), template);
76 return lookup(cachedDao, key);
78 return Optional.absent();
86 protected InstanceIdentifier<SxpEpMapper> buildReadPath(final IpPrefix key) {
87 return EPTemplateListener.SXP_MAPPER_TEMPLATE_PARENT_PATH;
90 private Optional<EndpointForwardingTemplateBySubnet> lookup(final SimpleCachedDao<IpPrefix, EndpointForwardingTemplateBySubnet> cachedDao, final IpPrefix key) {
91 return cachedDao.find(key);