Bug-2229: RFC6286 - AS-wide Unique BGP Identifier
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / controller / config / yang / bgp / rib / impl / StrictBgpPeerRegistryModule.java
1 package org.opendaylight.controller.config.yang.bgp.rib.impl;
2
3 import com.google.common.base.Objects;
4 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
5 import org.opendaylight.protocol.bgp.rib.impl.StrictBGPPeerRegistry;
6 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
7 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
8 import org.opendaylight.protocol.bgp.rib.impl.spi.ReusableBGPPeer;
9 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
10 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
11 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
12 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
13
14 /**
15 * Registry of BGP peers that allows only one connection per 2 peers
16 */
17 public class StrictBgpPeerRegistryModule extends org.opendaylight.controller.config.yang.bgp.rib.impl.AbstractStrictBgpPeerRegistryModule {
18     public StrictBgpPeerRegistryModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier, final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
19         super(identifier, dependencyResolver);
20     }
21
22     public StrictBgpPeerRegistryModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier, final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver, final org.opendaylight.controller.config.yang.bgp.rib.impl.StrictBgpPeerRegistryModule oldModule, final java.lang.AutoCloseable oldInstance) {
23         super(identifier, dependencyResolver, oldModule, oldInstance);
24     }
25
26     @Override
27     public void customValidation() {
28         // add custom validation form module attributes here.
29     }
30
31     @Override
32     public java.lang.AutoCloseable createInstance() {
33         return new GlobalBGPPeerRegistryWrapper(StrictBGPPeerRegistry.GLOBAL);
34     }
35
36     // TODO backwards compatibility, peer-registry has to be mandatory attribute for peers
37     /**
38      * Wrapper for BGPPeerRegistry that prevents from executing close method
39       */
40     private static final class GlobalBGPPeerRegistryWrapper implements BGPPeerRegistry, AutoCloseable {
41         private final StrictBGPPeerRegistry global;
42
43         public GlobalBGPPeerRegistryWrapper(final StrictBGPPeerRegistry global) {
44             this.global = global;
45         }
46
47         @Override
48         public BGPSessionPreferences getPeerPreferences(final IpAddress ip) {
49             return this.global.getPeerPreferences(ip);
50         }
51
52         @Override
53         public BGPSessionListener getPeer(final IpAddress ip, final Ipv4Address sourceId, final Ipv4Address remoteId, final AsNumber asNumber)
54                 throws BGPDocumentedException {
55             return this.global.getPeer(ip, sourceId, remoteId, asNumber);
56         }
57
58         @Override
59         public boolean isPeerConfigured(final IpAddress ip) {
60             return this.global.isPeerConfigured(ip);
61         }
62
63         @Override
64         public void removePeer(final IpAddress ip) {
65             this.global.removePeer(ip);
66         }
67
68         @Override
69         public void removePeerSession(final IpAddress ip) {
70             this.global.removePeerSession(ip);
71         }
72
73         @Override
74         public void addPeer(final IpAddress ip, final ReusableBGPPeer peer, final BGPSessionPreferences preferences) {
75             this.global.addPeer(ip, peer, preferences);
76         }
77
78         @Override
79         public void close() {
80             // DO nothing, do not close the global instance
81         }
82
83         @Override
84         public String toString() {
85             return Objects.toStringHelper(this)
86                     .add("peers", this.global)
87                     .toString();
88         }
89     }
90 }