Move releaseConnection from ReusableBGPPeer to BGPSessionListener
[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.MoreObjects;
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.spi.BGPSessionListener;
9 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
10 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Open;
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 this.global.getPeerPreferences(ip);
49         }
50
51         @Override
52         public BGPSessionListener getPeer(final IpAddress ip, final Ipv4Address sourceId, final Ipv4Address remoteId, final Open open)
53                 throws BGPDocumentedException {
54             return this.global.getPeer(ip, sourceId, remoteId, open);
55         }
56
57         @Override
58         public boolean isPeerConfigured(final IpAddress ip) {
59             return this.global.isPeerConfigured(ip);
60         }
61
62         @Override
63         public void removePeer(final IpAddress ip) {
64             this.global.removePeer(ip);
65         }
66
67         @Override
68         public void removePeerSession(final IpAddress ip) {
69             this.global.removePeerSession(ip);
70         }
71
72         @Override
73         public void addPeer(final IpAddress ip, final BGPSessionListener peer, final BGPSessionPreferences preferences) {
74             this.global.addPeer(ip, peer, preferences);
75         }
76
77         @Override
78         public void close() {
79             // DO nothing, do not close the global instance
80         }
81
82         @Override
83         public String toString() {
84             return MoreObjects.toStringHelper(this)
85                     .add("peers", this.global)
86                     .toString();
87         }
88     }
89 }