2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.rib.impl.state;
10 import com.google.common.base.Stopwatch;
11 import java.net.InetSocketAddress;
12 import java.net.SocketAddress;
13 import java.util.List;
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.IpAddress;
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.rev180329.open.message.BgpParameters;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.bgp.parameters.OptionalCapabilities;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.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;
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 IpAddress remoteAddress;
42 private PortNumber remotePort = NON_DEFINED_PORT;
43 private PortNumber localPort = NON_DEFINED_PORT;
45 private boolean addPathCapability;
47 private boolean asn32Capability;
49 private boolean gracefulRestartCapability;
51 private boolean multiProtocolCapability;
53 private boolean routerRefreshCapability;
55 private State sessionState;
57 private BGPMessagesListener messagesListenerCounter;
59 public BGPSessionStateImpl() {
60 this.sessionState = State.OPEN_CONFIRM;
61 this.sessionStopwatch = Stopwatch.createUnstarted();
65 public synchronized void messageSent(final Notification msg) {
66 if (this.messagesListenerCounter != null) {
67 this.messagesListenerCounter.messageSent(msg);
72 public synchronized void messageReceived(final Notification msg) {
73 if (this.messagesListenerCounter != null) {
74 this.messagesListenerCounter.messageReceived(msg);
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 && !bgpParameters.isEmpty()) {
82 for (final BgpParameters parameters : bgpParameters) {
83 for (final OptionalCapabilities optionalCapabilities : parameters.getOptionalCapabilities()) {
84 final CParameters cParam = optionalCapabilities.getCParameters();
85 final CParameters1 capabilities = cParam.augmentation(CParameters1.class);
86 if (capabilities != null) {
87 final MultiprotocolCapability mc = capabilities.getMultiprotocolCapability();
89 this.multiProtocolCapability = true;
91 if (capabilities.getGracefulRestartCapability() != null) {
92 this.gracefulRestartCapability = true;
94 if (capabilities.getAddPathCapability() != null) {
95 this.addPathCapability = true;
97 if (capabilities.getRouteRefreshCapability() != null) {
98 this.routerRefreshCapability = true;
101 if (cParam.getAs4BytesCapability() != null) {
102 this.asn32Capability = true;
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()));
115 public synchronized State getSessionState() {
116 return this.sessionState;
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();
126 this.sessionState = state;
130 public synchronized boolean isAddPathCapabilitySupported() {
131 return this.addPathCapability;
135 public synchronized boolean isAsn32CapabilitySupported() {
136 return this.asn32Capability;
140 public synchronized boolean isGracefulRestartCapabilitySupported() {
141 return this.gracefulRestartCapability;
145 public synchronized boolean isMultiProtocolCapabilitySupported() {
146 return this.multiProtocolCapability;
150 public synchronized boolean isRouterRefreshCapabilitySupported() {
151 return this.routerRefreshCapability;
155 public synchronized PortNumber getLocalPort() {
156 return this.localPort;
160 public synchronized IpAddress getRemoteAddress() {
161 return this.remoteAddress;
165 public synchronized PortNumber getRemotePort() {
166 return this.remotePort;
170 public synchronized long getNegotiatedHoldTime() {
171 return this.holdTimerValue;
175 public synchronized long getUpTime() {
176 return this.sessionStopwatch.elapsed(TimeUnit.MILLISECONDS);
179 public synchronized void registerMessagesCounter(final BGPMessagesListener bgpMessagesListener) {
180 this.messagesListenerCounter = bgpMessagesListener;