BUG-3225 : fix NPE
[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.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * Tracks peers for adj-rib-out writeout.
33  */
34 final class ExportPolicyPeerTracker extends AbstractPeerRoleTracker {
35     private static final Logger LOG = LoggerFactory.getLogger(ExportPolicyPeerTracker.class);
36     private static final Function<YangInstanceIdentifier, Entry<PeerId, YangInstanceIdentifier>> GENERATE_PEERID = new Function<YangInstanceIdentifier, Entry<PeerId, YangInstanceIdentifier>>() {
37         @Override
38         public Entry<PeerId, YangInstanceIdentifier> apply(final YangInstanceIdentifier input) {
39             final PeerId peerId = IdentifierUtils.peerId((NodeIdentifierWithPredicates) input.getLastPathArgument());
40             return new AbstractMap.SimpleImmutableEntry<>(peerId, input);
41         }
42     };
43
44     private final Map<YangInstanceIdentifier, PeerRole> peerRoles = new HashMap<>();
45     private volatile Map<PeerRole, PeerExportGroup> groups = Collections.emptyMap();
46     private final PolicyDatabase policyDatabase;
47
48     ExportPolicyPeerTracker(final PolicyDatabase policyDatabase) {
49         this.policyDatabase = Preconditions.checkNotNull(policyDatabase);
50     }
51
52     private 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 (final 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> allPeerRoles = ImmutableMap.copyOf(idToRole);
67
68         final Map<PeerRole, PeerExportGroup> ret = new EnumMap<>(PeerRole.class);
69         for (final Entry<PeerRole, Collection<YangInstanceIdentifier>> e : roleToIds.asMap().entrySet()) {
70             final AbstractExportPolicy policy = this.policyDatabase.exportPolicyForRole(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, allPeerRoles, 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 = this.peerRoles.put(peerPath, role);
88         } else {
89             oldRole = this.peerRoles.remove(peerPath);
90         }
91
92         if (role != oldRole) {
93             LOG.debug("Peer {} changed role from {} to {}", peerPath, oldRole, role);
94             this.groups = createGroups(this.peerRoles);
95         }
96     }
97
98     PeerExportGroup getPeerGroup(final PeerRole role) {
99         return this.groups.get(Preconditions.checkNotNull(role));
100     }
101 }