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