ee4b1c861da5cd675f60adc3a94cc950ef944e9f
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / RIBImpl.java
1 /*
2  * Copyright (c) 2013 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 java.util.HashMap;
11 import java.util.Map;
12 import java.util.Set;
13
14 import javax.annotation.concurrent.ThreadSafe;
15
16 import org.opendaylight.protocol.bgp.concepts.BGPObject;
17 import org.opendaylight.protocol.bgp.concepts.BGPTableType;
18 import org.opendaylight.protocol.bgp.linkstate.LinkIdentifier;
19 import org.opendaylight.protocol.bgp.linkstate.NodeIdentifier;
20 import org.opendaylight.protocol.bgp.linkstate.PrefixIdentifier;
21 import org.opendaylight.protocol.bgp.parser.BGPLink;
22 import org.opendaylight.protocol.bgp.parser.BGPLinkState;
23 import org.opendaylight.protocol.bgp.parser.BGPNode;
24 import org.opendaylight.protocol.bgp.parser.BGPNodeState;
25 import org.opendaylight.protocol.bgp.parser.BGPPrefix;
26 import org.opendaylight.protocol.bgp.parser.BGPPrefixState;
27 import org.opendaylight.protocol.bgp.parser.BGPRoute;
28 import org.opendaylight.protocol.bgp.parser.BGPRouteState;
29 import org.opendaylight.protocol.concepts.Prefix;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev130918.LinkstateAddressFamily;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv6AddressFamily;
33
34 import com.google.common.base.Objects;
35 import com.google.common.base.Objects.ToStringHelper;
36 import com.google.common.base.Preconditions;
37
38 @ThreadSafe
39 public final class RIBImpl {
40         private final RIBTable<LinkIdentifier, BGPLinkState> links = new RIBTable<>();
41         private final RIBTable<NodeIdentifier, BGPNodeState> nodes = new RIBTable<>();
42         private final RIBTable<PrefixIdentifier<?>, BGPPrefixState> prefixes = new RIBTable<>();
43         private final RIBTable<Prefix<?>, BGPRouteState> routes = new RIBTable<>();
44         private final String name;
45
46         public RIBImpl(final String name) {
47                 this.name = Preconditions.checkNotNull(name);
48         }
49
50         synchronized void updateTables(final BGPPeer peer, final Set<BGPObject> addedObjects, final Set<?> removedObjects) {
51                 final Map<LinkIdentifier, BGPLinkState> l = new HashMap<>();
52                 final Map<NodeIdentifier, BGPNodeState> n = new HashMap<>();
53                 final Map<PrefixIdentifier<?>, BGPPrefixState> p = new HashMap<>();
54                 final Map<Prefix<?>, BGPRouteState> r = new HashMap<>();
55
56                 for (final Object id : removedObjects) {
57                         if (id instanceof Prefix<?>) {
58                                 this.routes.remove(r, peer, (Prefix<?>) id);
59                         } else if (id instanceof LinkIdentifier) {
60                                 this.links.remove(l, peer, (LinkIdentifier) id);
61                         } else if (id instanceof NodeIdentifier) {
62                                 this.nodes.remove(n, peer, (NodeIdentifier) id);
63                         } else if (id instanceof PrefixIdentifier<?>) {
64                                 this.prefixes.remove(p, peer, (PrefixIdentifier<?>) id);
65                         } else {
66                                 throw new IllegalArgumentException("Unsupported identifier " + id.getClass());
67                         }
68                 }
69
70                 for (final BGPObject o : addedObjects) {
71                         if (o instanceof BGPLink) {
72                                 final BGPLink link = (BGPLink) o;
73                                 this.links.add(l, peer, link.getLinkIdentifier(), link.currentState());
74                         } else if (o instanceof BGPNode) {
75                                 final BGPNode node = (BGPNode) o;
76                                 this.nodes.add(n, peer, node.getNodeIdentifier(), node.currentState());
77                         } else if (o instanceof BGPPrefix<?>) {
78                                 final BGPPrefix<?> prefix = (BGPPrefix<?>) o;
79                                 this.prefixes.add(p, peer, prefix.getPrefixIdentifier(), prefix.currentState());
80                         } else if (o instanceof BGPRoute) {
81                                 final BGPRoute route = (BGPRoute) o;
82                                 this.routes.add(r, peer, route.getName(), route.currentState());
83                         } else {
84                                 throw new IllegalArgumentException("Unsupported identifier " + o.getClass());
85                         }
86                 }
87
88                 // FIXME: push into MD SAL
89         }
90
91         synchronized void clearTable(final BGPPeer peer, final BGPTableType t) {
92                 if (Ipv4AddressFamily.class == t.getAddressFamily() || Ipv6AddressFamily.class == t.getAddressFamily()) {
93                         this.routes.clear(peer);
94                 } else if (LinkstateAddressFamily.class == t.getAddressFamily()) {
95                         this.links.clear(peer);
96                         this.nodes.clear(peer);
97                         this.prefixes.clear(peer);
98                 }
99         }
100
101         @Override
102         public final String toString() {
103                 return addToStringAttributes(Objects.toStringHelper(this)).toString();
104         }
105
106         protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
107                 toStringHelper.add("name", this.name);
108                 return toStringHelper;
109         }
110 }