Bump versions to 0.22.5-SNAPSHOT
[bgpcep.git] / bgp / openconfig-rp-statement / src / main / java / org / opendaylight / protocol / bgp / openconfig / routing / policy / statement / AbstractCommunityHandler.java
1 /*
2  * Copyright (c) 2018 AT&T Intellectual Property. 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.protocol.bgp.openconfig.routing.policy.statement;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.cache.CacheBuilder;
13 import com.google.common.cache.CacheLoader;
14 import com.google.common.cache.LoadingCache;
15 import com.google.common.util.concurrent.FluentFuture;
16 import java.util.List;
17 import java.util.Optional;
18 import java.util.concurrent.ExecutionException;
19 import java.util.stream.Collectors;
20 import org.opendaylight.mdsal.binding.api.DataBroker;
21 import org.opendaylight.mdsal.binding.api.ReadTransaction;
22 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
23 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.policy.rev151009.DefinedSets1;
24 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.policy.rev151009.routing.policy.defined.sets.BgpDefinedSets;
25 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.policy.rev151009.routing.policy.defined.sets.bgp.defined.sets.CommunitySets;
26 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.policy.rev151009.routing.policy.defined.sets.bgp.defined.sets.community.sets.CommunitySet;
27 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.policy.rev151009.routing.policy.defined.sets.bgp.defined.sets.community.sets.CommunitySetKey;
28 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.OpenconfigRoutingPolicyData;
29 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.routing.policy.top.RoutingPolicy;
30 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.routing.policy.top.routing.policy.DefinedSets;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.Communities;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.CommunitiesBuilder;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34
35 public class AbstractCommunityHandler {
36     private static final InstanceIdentifier<CommunitySets> COMMUNITY_SETS_IID =
37         InstanceIdentifier.builderOfInherited(OpenconfigRoutingPolicyData.class, RoutingPolicy.class).build()
38             .child(DefinedSets.class)
39             .augmentation(DefinedSets1.class)
40             .child(BgpDefinedSets.class)
41             .child(CommunitySets.class);
42     protected final LoadingCache<String, List<Communities>> communitySets;
43
44     public AbstractCommunityHandler(final DataBroker dataBroker) {
45         requireNonNull(dataBroker);
46         communitySets = CacheBuilder.newBuilder()
47             .build(new CacheLoader<String, List<Communities>>() {
48                 @Override
49                 public List<Communities> load(final String key) throws ExecutionException, InterruptedException {
50                     final FluentFuture<Optional<CommunitySet>> future;
51                     try (ReadTransaction tr = dataBroker.newReadOnlyTransaction()) {
52                         future = tr.read(LogicalDatastoreType.CONFIGURATION,
53                             COMMUNITY_SETS_IID.child(CommunitySet.class, new CommunitySetKey(key)));
54                     }
55
56                     return future.get().map(set -> set.nonnullCommunities().stream()
57                         .map(ge -> new CommunitiesBuilder()
58                             .setAsNumber(ge.getAsNumber())
59                             .setSemantics(ge.getSemantics())
60                             .build())
61                         .collect(Collectors.toUnmodifiableList()))
62                         .orElse(List.of());
63                 }
64             });
65     }
66 }