Update MRI projects for Aluminium
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / state / BGPSessionStateImpl.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.protocol.bgp.rib.impl.state;
9
10 import com.google.common.base.Stopwatch;
11 import java.net.InetSocketAddress;
12 import java.net.SocketAddress;
13 import java.util.List;
14 import java.util.Set;
15 import java.util.concurrent.TimeUnit;
16 import org.checkerframework.checker.lock.qual.GuardedBy;
17 import org.opendaylight.protocol.bgp.rib.impl.StrictBGPPeerRegistry;
18 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPMessagesListener;
19 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionStateListener;
20 import org.opendaylight.protocol.bgp.rib.spi.State;
21 import org.opendaylight.protocol.bgp.rib.spi.state.BGPSessionState;
22 import org.opendaylight.protocol.bgp.rib.spi.state.BGPTimersState;
23 import org.opendaylight.protocol.bgp.rib.spi.state.BGPTransportState;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressNoZone;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.open.message.BgpParameters;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.open.message.bgp.parameters.OptionalCapabilities;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.open.message.bgp.parameters.optional.capabilities.CParameters;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.BgpTableType;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.CParameters1;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.mp.capabilities.MultiprotocolCapability;
32 import org.opendaylight.yangtools.yang.binding.Notification;
33 import org.opendaylight.yangtools.yang.common.Uint16;
34
35 // This class is thread-safe
36 public final class BGPSessionStateImpl implements BGPSessionState, BGPTimersState, BGPTransportState,
37     BGPSessionStateListener {
38     private static final PortNumber NON_DEFINED_PORT = new PortNumber(Uint16.ZERO);
39     private final Stopwatch sessionStopwatch;
40     private int holdTimerValue;
41     private IpAddressNoZone remoteAddress;
42     private PortNumber remotePort = NON_DEFINED_PORT;
43     private PortNumber localPort = NON_DEFINED_PORT;
44     @GuardedBy("this")
45     private boolean addPathCapability;
46     @GuardedBy("this")
47     private boolean asn32Capability;
48     @GuardedBy("this")
49     private boolean gracefulRestartCapability;
50     @GuardedBy("this")
51     private boolean multiProtocolCapability;
52     @GuardedBy("this")
53     private boolean routerRefreshCapability;
54     @GuardedBy("this")
55     private State sessionState;
56     @GuardedBy("this")
57     private BGPMessagesListener messagesListenerCounter;
58
59     public BGPSessionStateImpl() {
60         this.sessionState = State.OPEN_CONFIRM;
61         this.sessionStopwatch = Stopwatch.createUnstarted();
62     }
63
64     @Override
65     public synchronized void messageSent(final Notification msg) {
66         if (this.messagesListenerCounter != null) {
67             this.messagesListenerCounter.messageSent(msg);
68         }
69     }
70
71     @Override
72     public synchronized void messageReceived(final Notification msg) {
73         if (this.messagesListenerCounter != null) {
74             this.messagesListenerCounter.messageReceived(msg);
75         }
76     }
77
78     @Override
79     public synchronized void advertizeCapabilities(final int newHoldTimerValue, final SocketAddress newRemoteAddress,
80         final SocketAddress localAddress, final Set<BgpTableType> tableTypes, final List<BgpParameters> bgpParameters) {
81         if (bgpParameters != null) {
82             for (final BgpParameters parameters : bgpParameters) {
83                 for (final OptionalCapabilities optionalCapabilities : parameters.nonnullOptionalCapabilities()) {
84                     final CParameters cParam = optionalCapabilities.getCParameters();
85                     final CParameters1 capabilities = cParam.augmentation(CParameters1.class);
86                     if (capabilities != null) {
87                         final MultiprotocolCapability mc = capabilities.getMultiprotocolCapability();
88                         if (mc != null) {
89                             this.multiProtocolCapability = true;
90                         }
91                         if (capabilities.getGracefulRestartCapability() != null) {
92                             this.gracefulRestartCapability = true;
93                         }
94                         if (capabilities.getAddPathCapability() != null) {
95                             this.addPathCapability = true;
96                         }
97                         if (capabilities.getRouteRefreshCapability() != null) {
98                             this.routerRefreshCapability = true;
99                         }
100                     }
101                     if (cParam.getAs4BytesCapability() != null) {
102                         this.asn32Capability = true;
103                     }
104                 }
105             }
106         }
107
108         this.holdTimerValue = newHoldTimerValue;
109         this.remoteAddress = StrictBGPPeerRegistry.getIpAddress(newRemoteAddress);
110         this.remotePort = new PortNumber(Uint16.valueOf(((InetSocketAddress) newRemoteAddress).getPort()));
111         this.localPort = new PortNumber(Uint16.valueOf(((InetSocketAddress) localAddress).getPort()));
112     }
113
114     @Override
115     public synchronized State getSessionState() {
116         return this.sessionState;
117     }
118
119     @Override
120     public synchronized void setSessionState(final State state) {
121         if (state == State.IDLE) {
122             this.sessionStopwatch.reset();
123         } else if (state == State.UP) {
124             this.sessionStopwatch.start();
125         }
126         this.sessionState = state;
127     }
128
129     @Override
130     public synchronized boolean isAddPathCapabilitySupported() {
131         return this.addPathCapability;
132     }
133
134     @Override
135     public synchronized boolean isAsn32CapabilitySupported() {
136         return this.asn32Capability;
137     }
138
139     @Override
140     public synchronized boolean isGracefulRestartCapabilitySupported() {
141         return this.gracefulRestartCapability;
142     }
143
144     @Override
145     public synchronized boolean isMultiProtocolCapabilitySupported() {
146         return this.multiProtocolCapability;
147     }
148
149     @Override
150     public synchronized boolean isRouterRefreshCapabilitySupported() {
151         return this.routerRefreshCapability;
152     }
153
154     @Override
155     public synchronized PortNumber getLocalPort() {
156         return this.localPort;
157     }
158
159     @Override
160     public synchronized IpAddressNoZone getRemoteAddress() {
161         return this.remoteAddress;
162     }
163
164     @Override
165     public synchronized PortNumber getRemotePort() {
166         return this.remotePort;
167     }
168
169     @Override
170     public synchronized long getNegotiatedHoldTime() {
171         return this.holdTimerValue;
172     }
173
174     @Override
175     public synchronized long getUpTime() {
176         return this.sessionStopwatch.elapsed(TimeUnit.MILLISECONDS);
177     }
178
179     public synchronized void registerMessagesCounter(final BGPMessagesListener bgpMessagesListener) {
180         this.messagesListenerCounter = bgpMessagesListener;
181     }
182 }