04e624276a4251249082ffb984557f583dc3f3df
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / ExportPolicyPeerTracker.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  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 package org.opendaylight.protocol.bgp.rib.impl;
9
10 import com.google.common.base.Function;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.ArrayListMultimap;
13 import com.google.common.collect.Collections2;
14 import com.google.common.collect.ImmutableList;
15 import com.google.common.collect.ImmutableMap;
16 import com.google.common.collect.Multimap;
17 import java.util.AbstractMap;
18 import java.util.Collection;
19 import java.util.Collections;
20 import java.util.EnumMap;
21 import java.util.HashMap;
22 import java.util.Map;
23 import java.util.Map.Entry;
24 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * Tracks peers for adj-rib-out writeout.
34  */
35 final class ExportPolicyPeerTracker extends AbstractPeerRoleTracker {
36     private static final Logger LOG = LoggerFactory.getLogger(ExportPolicyPeerTracker.class);
37     private static final Function<YangInstanceIdentifier, Entry<PeerId, YangInstanceIdentifier>> GENERATE_PEERID = new Function<YangInstanceIdentifier, Entry<PeerId, YangInstanceIdentifier>>() {
38         @Override
39         public Entry<PeerId, YangInstanceIdentifier> apply(final YangInstanceIdentifier input) {
40             final PeerId peerId = IdentifierUtils.peerId((NodeIdentifierWithPredicates) input.getLastPathArgument());
41             return new AbstractMap.SimpleImmutableEntry<>(peerId, input);
42         }
43     };
44
45     private final Map<YangInstanceIdentifier, PeerRole> peerRoles = new HashMap<>();
46     private volatile Map<PeerRole, PeerExportGroup> groups = Collections.emptyMap();
47     private final PolicyDatabase policyDatabase;
48
49     ExportPolicyPeerTracker(final DOMDataTreeChangeService service, final YangInstanceIdentifier ribId, final PolicyDatabase policyDatabase) {
50         super(service, ribId);
51         this.policyDatabase = Preconditions.checkNotNull(policyDatabase);
52     }
53
54     private Map<PeerRole, PeerExportGroup> createGroups(final Map<YangInstanceIdentifier, PeerRole> peerPathRoles) {
55         if (peerPathRoles.isEmpty()) {
56             return Collections.emptyMap();
57         }
58
59         // Index things nicely for easy access
60         final Multimap<PeerRole, YangInstanceIdentifier> roleToIds = ArrayListMultimap.create(PeerRole.values().length, 2);
61         final Map<PeerId, PeerRole> idToRole = new HashMap<>();
62         for (Entry<YangInstanceIdentifier, PeerRole> e : peerPathRoles.entrySet()) {
63             roleToIds.put(e.getValue(), e.getKey());
64             idToRole.put(IdentifierUtils.peerId((NodeIdentifierWithPredicates) e.getKey().getLastPathArgument()), e.getValue());
65         }
66
67         // Optimized immutable copy, reused for all PeerGroups
68         final Map<PeerId, PeerRole> allPeerRoles = ImmutableMap.copyOf(idToRole);
69
70         final Map<PeerRole, PeerExportGroup> ret = new EnumMap<>(PeerRole.class);
71         for (Entry<PeerRole, Collection<YangInstanceIdentifier>> e : roleToIds.asMap().entrySet()) {
72             final AbstractExportPolicy policy = policyDatabase.exportPolicyForRole(e.getKey());
73             final Collection<Entry<PeerId, YangInstanceIdentifier>> peers = ImmutableList.copyOf(Collections2.transform(e.getValue(), GENERATE_PEERID));
74
75             ret.put(e.getKey(), new PeerExportGroup(peers, allPeerRoles, policy));
76         }
77
78         return ret;
79     }
80
81     @Override
82     protected void peerRoleChanged(final YangInstanceIdentifier peerPath, final PeerRole role) {
83         /*
84          * This is a sledgehammer approach to the problem: modify the role map first,
85          * then construct the group map from scratch.
86          */
87         final PeerRole oldRole;
88         if (role != null) {
89             oldRole = peerRoles.put(peerPath, role);
90         } else {
91             oldRole = peerRoles.remove(peerPath);
92         }
93
94         if (role != oldRole) {
95             LOG.debug("Peer {} changed role from {} to {}", peerPath, oldRole, role);
96             groups = createGroups(peerRoles);
97         }
98     }
99
100     PeerExportGroup getPeerGroup(final PeerRole role) {
101         return groups.get(Preconditions.checkNotNull(role));
102     }
103 }