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