2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.rib.impl;
10 import java.util.HashMap;
14 import javax.annotation.concurrent.ThreadSafe;
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;
34 import com.google.common.base.Objects;
35 import com.google.common.base.Objects.ToStringHelper;
36 import com.google.common.base.Preconditions;
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;
46 public RIBImpl(final String name) {
47 this.name = Preconditions.checkNotNull(name);
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<>();
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);
66 throw new IllegalArgumentException("Unsupported identifier " + id.getClass());
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());
84 throw new IllegalArgumentException("Unsupported identifier " + o.getClass());
88 // FIXME: push into MD SAL
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);
102 public final String toString() {
103 return addToStringAttributes(Objects.toStringHelper(this)).toString();
106 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
107 toStringHelper.add("name", this.name);
108 return toStringHelper;