6c764e9ba28af23d284f3a2953193b09e12d651a
[bgpcep.git] / bgp / openconfig-rp-statement / src / main / java / org / opendaylight / protocol / bgp / openconfig / routing / policy / statement / AbstractExtCommunityHandler.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
9 package org.opendaylight.protocol.bgp.openconfig.routing.policy.statement;
10
11 import static java.util.Objects.requireNonNull;
12
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.ExtCommunitySets;
27 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.policy.rev151009.routing.policy.defined.sets.bgp.defined.sets.ext.community.sets.ExtCommunitySet;
28 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.policy.rev151009.routing.policy.defined.sets.bgp.defined.sets.ext.community.sets.ExtCommunitySetKey;
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.ExtendedCommunities;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.ExtendedCommunitiesBuilder;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34
35 public class AbstractExtCommunityHandler {
36     private static final InstanceIdentifier<ExtCommunitySets> EXT_COMMUNITY_SETS_IID
37             = InstanceIdentifier.create(RoutingPolicy.class).child(DefinedSets.class)
38             .augmentation(DefinedSets1.class).child(BgpDefinedSets.class)
39             .child(ExtCommunitySets.class);
40     private final DataBroker databroker;
41     protected final LoadingCache<String, List<ExtendedCommunities>> extCommunitySets = CacheBuilder.newBuilder()
42             .build(new CacheLoader<String, List<ExtendedCommunities>>() {
43                 @Override
44                 public List<ExtendedCommunities> load(final String key)
45                         throws ExecutionException, InterruptedException {
46                     return loadCommunitySet(key);
47                 }
48             });
49
50     public AbstractExtCommunityHandler(final DataBroker databroker) {
51         this.databroker = requireNonNull(databroker);
52     }
53
54     private List<ExtendedCommunities> loadCommunitySet(final String key)
55             throws ExecutionException, InterruptedException {
56         final ReadOnlyTransaction tr = this.databroker.newReadOnlyTransaction();
57         final Optional<ExtCommunitySet> result =
58                 tr.read(LogicalDatastoreType.CONFIGURATION, EXT_COMMUNITY_SETS_IID
59                         .child(ExtCommunitySet.class, new ExtCommunitySetKey(key))).get();
60         if (!result.isPresent()) {
61             return Collections.emptyList();
62         }
63         return result.get().getExtCommunityMember()
64                 .stream().map(ge -> new ExtendedCommunitiesBuilder().setExtendedCommunity(ge.getExtendedCommunity())
65                         .setTransitive(ge.isTransitive()).build()).collect(Collectors.toList());
66     }
67 }