2 * Copyright (c) 2018 AT&T Intellectual Property. 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.protocol.bgp.openconfig.routing.policy.statement;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.cache.CacheBuilder;
13 import com.google.common.cache.CacheLoader;
14 import com.google.common.cache.LoadingCache;
15 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
16 import java.util.Collections;
17 import java.util.List;
18 import java.util.Optional;
19 import java.util.concurrent.ExecutionException;
20 import java.util.stream.Collectors;
21 import org.opendaylight.mdsal.binding.api.DataBroker;
22 import org.opendaylight.mdsal.binding.api.ReadTransaction;
23 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
24 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.policy.rev151009.DefinedSets1;
25 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.policy.rev151009.routing.policy.defined.sets.BgpDefinedSets;
26 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.policy.rev151009.routing.policy.defined.sets.bgp.defined.sets.CommunitySets;
27 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.policy.rev151009.routing.policy.defined.sets.bgp.defined.sets.community.sets.CommunitySet;
28 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.policy.rev151009.routing.policy.defined.sets.bgp.defined.sets.community.sets.CommunitySetKey;
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;
35 public class AbstractCommunityHandler {
36 private static final InstanceIdentifier<CommunitySets> COMMUNITY_SETS_IID
37 = InstanceIdentifier.create(RoutingPolicy.class).child(DefinedSets.class)
38 .augmentation(DefinedSets1.class).child(BgpDefinedSets.class)
39 .child(CommunitySets.class);
40 private final DataBroker databroker;
41 protected final LoadingCache<String, List<Communities>> communitySets = CacheBuilder.newBuilder()
42 .build(new CacheLoader<String, List<Communities>>() {
44 public List<Communities> load(final String key) throws ExecutionException, InterruptedException {
45 return loadCommunitySet(key);
49 public AbstractCommunityHandler(final DataBroker dataBroker) {
50 this.databroker = requireNonNull(dataBroker);
53 @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
54 justification = "https://github.com/spotbugs/spotbugs/issues/811")
55 private List<Communities> loadCommunitySet(final String key) throws ExecutionException, InterruptedException {
56 final ReadTransaction tr = this.databroker.newReadOnlyTransaction();
57 final Optional<CommunitySet> result =
58 tr.read(LogicalDatastoreType.CONFIGURATION, COMMUNITY_SETS_IID
59 .child(CommunitySet.class, new CommunitySetKey(key))).get();
62 if (!result.isPresent()) {
63 return Collections.emptyList();
66 return result.get().getCommunities()
67 .stream().map(ge -> new CommunitiesBuilder().setAsNumber(ge.getAsNumber())
68 .setSemantics(ge.getSemantics()).build()).collect(Collectors.toList());