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 java.util.Collections;
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.routing.policy.top.RoutingPolicy;
29 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.routing.policy.top.routing.policy.DefinedSets;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.Communities;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.CommunitiesBuilder;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 public class AbstractCommunityHandler {
35 private static final InstanceIdentifier<CommunitySets> COMMUNITY_SETS_IID
36 = InstanceIdentifier.create(RoutingPolicy.class).child(DefinedSets.class)
37 .augmentation(DefinedSets1.class).child(BgpDefinedSets.class)
38 .child(CommunitySets.class);
39 private final DataBroker databroker;
40 protected final LoadingCache<String, List<Communities>> communitySets = CacheBuilder.newBuilder()
41 .build(new CacheLoader<String, List<Communities>>() {
43 public List<Communities> load(final String key) throws ExecutionException, InterruptedException {
44 return loadCommunitySet(key);
48 public AbstractCommunityHandler(final DataBroker dataBroker) {
49 this.databroker = requireNonNull(dataBroker);
52 private List<Communities> loadCommunitySet(final String key) throws ExecutionException, InterruptedException {
53 final ReadTransaction tr = this.databroker.newReadOnlyTransaction();
54 final Optional<CommunitySet> result =
55 tr.read(LogicalDatastoreType.CONFIGURATION, COMMUNITY_SETS_IID
56 .child(CommunitySet.class, new CommunitySetKey(key))).get();
59 if (!result.isPresent()) {
60 return Collections.emptyList();
63 return result.get().getCommunities()
64 .stream().map(ge -> new CommunitiesBuilder().setAsNumber(ge.getAsNumber())
65 .setSemantics(ge.getSemantics()).build()).collect(Collectors.toList());