8d5e9e669a390dedb171f89a2d82fc2b17f8c24a
[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
48     protected ExportPolicyPeerTracker(final DOMDataTreeChangeService service, final YangInstanceIdentifier ribId) {
49         super(service, ribId);
50     }
51
52     private static Map<PeerRole, PeerExportGroup> createGroups(final Map<YangInstanceIdentifier, PeerRole> peerPathRoles) {
53         if (peerPathRoles.isEmpty()) {
54             return Collections.emptyMap();
55         }
56
57         // Index things nicely for easy access
58         final Multimap<PeerRole, YangInstanceIdentifier> roleToIds = ArrayListMultimap.create(PeerRole.values().length, 2);
59         final Map<PeerId, PeerRole> idToRole = new HashMap<>();
60         for (Entry<YangInstanceIdentifier, PeerRole> e : peerPathRoles.entrySet()) {
61             roleToIds.put(e.getValue(), e.getKey());
62             idToRole.put(IdentifierUtils.peerId((NodeIdentifierWithPredicates) e.getKey().getLastPathArgument()), e.getValue());
63         }
64
65         // Optimized immutable copy, reused for all PeerGroups
66         final Map<PeerId, PeerRole> peerRoles = ImmutableMap.copyOf(idToRole);
67
68         final Map<PeerRole, PeerExportGroup> ret = new EnumMap<>(PeerRole.class);
69         for (Entry<PeerRole, Collection<YangInstanceIdentifier>> e : roleToIds.asMap().entrySet()) {
70             final AbstractExportPolicy policy = AbstractExportPolicy.forRole(e.getKey());
71             final Collection<Entry<PeerId, YangInstanceIdentifier>> peers = ImmutableList.copyOf(Collections2.transform(e.getValue(), GENERATE_PEERID));
72
73             ret.put(e.getKey(), new PeerExportGroup(peers, peerRoles, policy));
74         }
75
76         return ret;
77     }
78
79     @Override
80     protected void peerRoleChanged(final YangInstanceIdentifier peerPath, final PeerRole role) {
81         /*
82          * This is a sledgehammer approach to the problem: modify the role map first,
83          * then construct the group map from scratch.
84          */
85         final PeerRole oldRole;
86         if (role != null) {
87             oldRole = peerRoles.put(peerPath, role);
88         } else {
89             oldRole = peerRoles.remove(peerPath);
90         }
91
92         if (role != oldRole) {
93             LOG.debug("Peer {} changed role from {} to {}", peerPath, oldRole, role);
94             groups = createGroups(peerRoles);
95         }
96     }
97
98     PeerExportGroup getPeerGroup(final PeerRole role) {
99         return groups.get(Preconditions.checkNotNull(role));
100     }
101 }