2 * Copyright (c) 2013 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.mock;
10 import com.google.common.collect.Lists;
11 import com.google.common.collect.Sets;
12 import com.google.common.eventbus.EventBus;
13 import com.google.common.eventbus.Subscribe;
14 import io.netty.channel.ChannelHandlerContext;
15 import java.util.Collections;
16 import java.util.List;
18 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
19 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
20 import org.opendaylight.protocol.bgp.rib.spi.BGPSession;
21 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Keepalive;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Open;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParameters;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.OptionalCapabilities;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.CParameters;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.CParameters1;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.mp.capabilities.AddPathCapability;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.mp.capabilities.MultiprotocolCapability;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.mp.capabilities.add.path.capability.AddressFamilies;
34 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
35 import org.opendaylight.yangtools.yang.binding.Notification;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * This class has @Subscribe annotated methods which receive events from {@link EventBus} . Events are produced by
41 * {@link BGPMock}, and each instance notifies exactly one {@link BGPSessionListener}.
43 final class EventBusRegistration extends AbstractListenerRegistration<BGPSessionListener> {
45 private static final Logger LOG = LoggerFactory.getLogger(EventBusRegistration.class);
47 private final EventBus eventBus;
49 public static EventBusRegistration createAndRegister(final EventBus eventBus, final BGPSessionListener listener,
50 final List<Notification> allPreviousMessages) {
51 final EventBusRegistration instance = new EventBusRegistration(eventBus, listener, allPreviousMessages);
52 eventBus.register(instance);
56 private EventBusRegistration(final EventBus eventBus, final BGPSessionListener listener, final List<Notification> allPreviousMessages) {
58 this.eventBus = eventBus;
59 for (final Notification message : allPreviousMessages) {
60 sendMessage(listener, message);
65 public void onMessage(final Notification message) {
66 sendMessage(this.getInstance(), message);
70 public synchronized void removeRegistration() {
71 this.eventBus.unregister(this);
74 private static void sendMessage(final BGPSessionListener listener, final Notification message) {
75 if (BGPMock.CONNECTION_LOST_MAGIC_MSG.equals(message)) {
76 listener.onSessionTerminated(null, null);
77 } else if (message instanceof Open) {
78 final Set<BgpTableType> tts = Sets.newHashSet();
79 final List<AddressFamilies> addPathCapabilitiesList = Lists.newArrayList();
80 for (final BgpParameters param : ((Open) message).getBgpParameters()) {
81 for (final OptionalCapabilities capa : param.getOptionalCapabilities()) {
82 final CParameters cParam = capa.getCParameters();
83 if (cParam.getAugmentation(CParameters1.class) == null) {
86 if (cParam.getAugmentation(CParameters1.class).getMultiprotocolCapability() != null) {
87 final MultiprotocolCapability p = cParam.getAugmentation(CParameters1.class).getMultiprotocolCapability();
88 LOG.debug("Adding open parameter {}", p);
89 final BgpTableType type = new BgpTableTypeImpl(p.getAfi(), p.getSafi());
91 } else if (cParam.getAugmentation(CParameters1.class).getAddPathCapability() != null) {
92 final AddPathCapability addPathCap = cParam.getAugmentation(CParameters1.class).getAddPathCapability();
93 addPathCapabilitiesList.addAll(addPathCap.getAddressFamilies());
97 activateSession(listener, tts);
98 } else if (!(message instanceof Keepalive)) {
100 listener.onMessage(null, message);
101 } catch (BGPDocumentedException e) {
102 LOG.warn("Exception encountered while handling message", e);
107 private static void activateSession(final BGPSessionListener listener, final Set<BgpTableType> tts) {
108 listener.onSessionUp(new BGPSession() {
109 private static final long AS = 30L;
112 public void channelRegistered(final ChannelHandlerContext channelHandlerContext) throws Exception {
116 public void channelUnregistered(final ChannelHandlerContext channelHandlerContext) throws Exception {
120 public void channelActive(final ChannelHandlerContext channelHandlerContext) throws Exception {
124 public void channelInactive(final ChannelHandlerContext channelHandlerContext) throws Exception {
128 public void channelRead(final ChannelHandlerContext channelHandlerContext, final Object o) throws Exception {
132 public void channelReadComplete(final ChannelHandlerContext channelHandlerContext) throws Exception {
136 public void userEventTriggered(final ChannelHandlerContext channelHandlerContext, final Object o) throws Exception {
140 public void channelWritabilityChanged(final ChannelHandlerContext channelHandlerContext) throws Exception {
144 public void handlerAdded(final ChannelHandlerContext channelHandlerContext) throws Exception {
148 public void handlerRemoved(final ChannelHandlerContext channelHandlerContext) throws Exception {
152 public void exceptionCaught(final ChannelHandlerContext channelHandlerContext, final Throwable throwable) throws Exception {
156 public void close() {
157 LOG.debug("Session {} closed", this);
161 public Set<BgpTableType> getAdvertisedTableTypes() {
166 public Ipv4Address getBgpId() {
167 return new Ipv4Address("127.0.0.1");
171 public AsNumber getAsNumber() {
172 return new AsNumber(AS);
176 public List<AddressFamilies> getAdvertisedAddPathTableTypes() {
177 return Collections.emptyList();