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
9 package org.opendaylight.protocol.bgp.openconfig.routing.policy.statement;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.base.Optional;
14 import com.google.common.cache.CacheBuilder;
15 import com.google.common.cache.CacheLoader;
16 import com.google.common.cache.LoadingCache;
17 import java.util.Collections;
18 import java.util.List;
19 import java.util.concurrent.ExecutionException;
20 import java.util.stream.Collectors;
21 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
22 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
23 import org.opendaylight.controller.md.sal.common.api.data.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.rev171207.path.attributes.attributes.Communities;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.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 private List<Communities> loadCommunitySet(final String key) throws ExecutionException, InterruptedException {
54 final ReadOnlyTransaction tr = this.databroker.newReadOnlyTransaction();
55 final Optional<CommunitySet> result =
56 tr.read(LogicalDatastoreType.CONFIGURATION, COMMUNITY_SETS_IID
57 .child(CommunitySet.class, new CommunitySetKey(key))).get();
60 if (!result.isPresent()) {
61 return Collections.emptyList();
64 return result.get().getCommunities()
65 .stream().map(ge -> new CommunitiesBuilder().setAsNumber(ge.getAsNumber())
66 .setSemantics(ge.getSemantics()).build()).collect(Collectors.toList());