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.ExtCommunitySets;
26 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.policy.rev151009.routing.policy.defined.sets.bgp.defined.sets.ext.community.sets.ExtCommunitySet;
27 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.policy.rev151009.routing.policy.defined.sets.bgp.defined.sets.ext.community.sets.ExtCommunitySetKey;
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.ExtendedCommunities;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.ExtendedCommunitiesBuilder;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 public class AbstractExtCommunityHandler {
35 private static final InstanceIdentifier<ExtCommunitySets> EXT_COMMUNITY_SETS_IID
36 = InstanceIdentifier.create(RoutingPolicy.class).child(DefinedSets.class)
37 .augmentation(DefinedSets1.class).child(BgpDefinedSets.class)
38 .child(ExtCommunitySets.class);
39 private final DataBroker databroker;
40 protected final LoadingCache<String, List<ExtendedCommunities>> extCommunitySets = CacheBuilder.newBuilder()
41 .build(new CacheLoader<String, List<ExtendedCommunities>>() {
43 public List<ExtendedCommunities> load(final String key)
44 throws ExecutionException, InterruptedException {
45 return loadCommunitySet(key);
49 public AbstractExtCommunityHandler(final DataBroker databroker) {
50 this.databroker = requireNonNull(databroker);
53 private List<ExtendedCommunities> loadCommunitySet(final String key)
54 throws ExecutionException, InterruptedException {
55 final ReadTransaction tr = this.databroker.newReadOnlyTransaction();
56 final Optional<ExtCommunitySet> result =
57 tr.read(LogicalDatastoreType.CONFIGURATION, EXT_COMMUNITY_SETS_IID
58 .child(ExtCommunitySet.class, new ExtCommunitySetKey(key))).get();
59 if (!result.isPresent()) {
60 return Collections.emptyList();
62 return result.get().getExtCommunityMember()
63 .stream().map(ge -> new ExtendedCommunitiesBuilder().setExtendedCommunity(ge.getExtendedCommunity())
64 .setTransitive(ge.isTransitive()).build()).collect(Collectors.toList());