928243a388502cd6ba31c78906149c4862053df3
[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.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 this.global.getPeerPreferences(ip);
49         }
50
51         @Override
52         public BGPSessionListener getPeer(final IpAddress ip, final Ipv4Address sourceId, final Ipv4Address remoteId) throws BGPDocumentedException {
53             return this.global.getPeer(ip, sourceId, remoteId);
54         }
55
56         @Override
57         public boolean isPeerConfigured(final IpAddress ip) {
58             return this.global.isPeerConfigured(ip);
59         }
60
61         @Override
62         public void removePeer(final IpAddress ip) {
63             this.global.removePeer(ip);
64         }
65
66         @Override
67         public void removePeerSession(final IpAddress ip) {
68             this.global.removePeerSession(ip);
69         }
70
71         @Override
72         public void addPeer(final IpAddress ip, final ReusableBGPPeer peer, final BGPSessionPreferences preferences) {
73             this.global.addPeer(ip, peer, preferences);
74         }
75
76         @Override
77         public void close() {
78             // DO nothing, do not close the global instance
79         }
80
81         @Override
82         public String toString() {
83             return Objects.toStringHelper(this)
84                     .add("peers", this.global)
85                     .toString();
86         }
87     }
88 }