BUG-6650: ep-ip/sgt, implement and wire template provider
[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 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;
35
36
37 /**
38  * Purpose: general dao for EndPoint templates
39  */
40 public class EPPolicyTemplateDaoImpl implements DSAsyncDao<Sgt, EndpointPolicyTemplateBySgt>,
41         ReadableByKey<EpPolicyTemplateValueKey, EndpointPolicyTemplateBySgt> {
42
43     private static final ListenableFuture<Optional<EndpointPolicyTemplateBySgt>> READ_FUTURE_ABSENT = Futures.immediateFuture(Optional.absent());
44
45     private final DataBroker dataBroker;
46     private final SimpleCachedDao<Sgt, EndpointPolicyTemplateBySgt> cachedDao;
47     private final EpPolicyTemplateValueKeyFactory keyFactory;
48
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;
55     }
56
57     @Override
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;
64         } else {
65             final ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction();
66             final CheckedFuture<Optional<EndpointPolicyTemplateBySgt>, ReadFailedException> read =
67                     rTx.read(LogicalDatastoreType.CONFIGURATION, buildReadPath(key));
68
69             Futures.addCallback(read, SxpListenerUtil.createTxCloseCallback(rTx));
70
71             return Futures.transform(read, new Function<Optional<EndpointPolicyTemplateBySgt>, Optional<EndpointPolicyTemplateBySgt>>() {
72                 @Nullable
73                 @Override
74                 public Optional<EndpointPolicyTemplateBySgt> apply(@Nullable final Optional<EndpointPolicyTemplateBySgt> input) {
75                     if (input.isPresent()) {
76                         cachedDao.update(key, keyFactory.sortValueKeyLists(input.get()));
77                     }
78                     return input;
79                 }
80             });
81         }
82     }
83
84     @VisibleForTesting
85     protected InstanceIdentifier<EndpointPolicyTemplateBySgt> buildReadPath(final Sgt key) {
86         return EPTemplateListener.SXP_MAPPER_TEMPLATE_PARENT_PATH
87                 .child(EndpointPolicyTemplateBySgt.class, new EndpointPolicyTemplateBySgtKey(key));
88     }
89
90     private Optional<EndpointPolicyTemplateBySgt> lookup(final SimpleCachedDao<Sgt, EndpointPolicyTemplateBySgt> cachedDao, final Sgt key) {
91         return cachedDao.find(key);
92     }
93
94     @Override
95     public Collection<EndpointPolicyTemplateBySgt> readBy(@Nonnull final EpPolicyTemplateValueKey specialKey) {
96         final Predicate<EpPolicyTemplateValueKey> templateValuePredicate = Predicates.equalTo(specialKey);
97         final Collection<EndpointPolicyTemplateBySgt> foundTemplates = new ArrayList<>();
98
99         for (EndpointPolicyTemplateBySgt epPolicyTemplate : cachedDao.values()) {
100             final EpPolicyTemplateValueKey templateValueKey = keyFactory.createKeyWithDefaultOrdering(epPolicyTemplate);
101             if (templateValuePredicate.apply(templateValueKey)) {
102                 foundTemplates.add(epPolicyTemplate);
103             }
104         }
105
106         return foundTemplates;
107     }
108 }