update deprecated transform and addCallback methods
[groupbasedpolicy.git] / sxp-integration / sxp-ep-provider / src / main / java / org / opendaylight / groupbasedpolicy / sxp / ep / provider / impl / dao / EPPolicyTemplateDaoImpl.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.groupbasedpolicy.sxp.ep.provider.impl.dao;
9
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;
19
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;
37
38
39 /**
40  * Purpose: general dao for EndPoint templates
41  */
42 public class EPPolicyTemplateDaoImpl implements DSAsyncDao<Sgt, EndpointPolicyTemplateBySgt>,
43         ReadableByKey<EpPolicyTemplateValueKey, EndpointPolicyTemplateBySgt> {
44
45     private static final ListenableFuture<Optional<EndpointPolicyTemplateBySgt>> READ_FUTURE_ABSENT = Futures.immediateFuture(Optional.absent());
46
47     private final DataBroker dataBroker;
48     private final SimpleCachedDao<Sgt, EndpointPolicyTemplateBySgt> cachedDao;
49     private final EpPolicyTemplateValueKeyFactory keyFactory;
50
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;
57     }
58
59     @Override
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;
66         } else {
67             final ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction();
68             final CheckedFuture<Optional<EndpointPolicyTemplateBySgt>, ReadFailedException> read =
69                     rTx.read(LogicalDatastoreType.CONFIGURATION, buildReadPath(key));
70
71             Futures.addCallback(read, SxpListenerUtil.createTxCloseCallback(rTx), MoreExecutors.directExecutor());
72
73             return Futures.transform(read, new Function<Optional<EndpointPolicyTemplateBySgt>, Optional<EndpointPolicyTemplateBySgt>>() {
74                 @Nullable
75                 @Override
76                 public Optional<EndpointPolicyTemplateBySgt> apply(@Nullable final Optional<EndpointPolicyTemplateBySgt> input) {
77                     if (input.isPresent()) {
78                         cachedDao.update(key, keyFactory.sortValueKeyLists(input.get()));
79                     }
80                     return input;
81                 }
82             }, MoreExecutors.directExecutor());
83         }
84     }
85
86     @VisibleForTesting
87     protected InstanceIdentifier<EndpointPolicyTemplateBySgt> buildReadPath(final Sgt key) {
88         return EPTemplateListener.SXP_MAPPER_TEMPLATE_PARENT_PATH
89                 .child(EndpointPolicyTemplateBySgt.class, new EndpointPolicyTemplateBySgtKey(key));
90     }
91
92     private Optional<EndpointPolicyTemplateBySgt> lookup(final SimpleCachedDao<Sgt, EndpointPolicyTemplateBySgt> cachedDao, final Sgt key) {
93         return cachedDao.find(key);
94     }
95
96     @Override
97     public Collection<EndpointPolicyTemplateBySgt> readBy(@Nonnull final EpPolicyTemplateValueKey specialKey) {
98         final Predicate<EpPolicyTemplateValueKey> templateValuePredicate = Predicates.equalTo(specialKey);
99         final Collection<EndpointPolicyTemplateBySgt> foundTemplates = new ArrayList<>();
100
101         for (EndpointPolicyTemplateBySgt epPolicyTemplate : cachedDao.values()) {
102             final EpPolicyTemplateValueKey templateValueKey = keyFactory.createKeyWithDefaultOrdering(epPolicyTemplate);
103             if (templateValuePredicate.apply(templateValueKey)) {
104                 foundTemplates.add(epPolicyTemplate);
105             }
106         }
107
108         return foundTemplates;
109     }
110 }