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.Sets;
11 import com.google.common.eventbus.EventBus;
12 import com.google.common.eventbus.Subscribe;
13 import io.netty.channel.ChannelHandlerContext;
14 import java.util.List;
16 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
17 import org.opendaylight.protocol.bgp.rib.spi.BGPSession;
18 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Keepalive;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Open;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParameters;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.OptionalCapabilities;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.CParameters;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.CParameters1;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.open.bgp.parameters.optional.capabilities.c.parameters.MultiprotocolCapability;
29 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
30 import org.opendaylight.yangtools.yang.binding.Notification;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * This class has @Subscribe annotated methods which receive events from {@link EventBus} . Events are produced by
36 * {@link BGPMock}, and each instance notifies exactly one {@link BGPSessionListener}.
38 final class EventBusRegistration extends AbstractListenerRegistration<BGPSessionListener> {
40 private static final Logger LOG = LoggerFactory.getLogger(EventBusRegistration.class);
42 private final EventBus eventBus;
44 public static EventBusRegistration createAndRegister(final EventBus eventBus, final BGPSessionListener listener,
45 final List<Notification> allPreviousMessages) {
46 final EventBusRegistration instance = new EventBusRegistration(eventBus, listener, allPreviousMessages);
47 eventBus.register(instance);
51 private EventBusRegistration(final EventBus eventBus, final BGPSessionListener listener, final List<Notification> allPreviousMessages) {
53 this.eventBus = eventBus;
54 for (final Notification message : allPreviousMessages) {
55 sendMessage(listener, message);
60 public void onMessage(final Notification message) {
61 sendMessage(this.getInstance(), message);
65 public synchronized void removeRegistration() {
66 this.eventBus.unregister(this);
69 private static void sendMessage(final BGPSessionListener listener, final Notification message) {
70 if (BGPMock.CONNECTION_LOST_MAGIC_MSG.equals(message)) {
71 listener.onSessionTerminated(null, null);
72 } else if (message instanceof Open) {
73 final Set<BgpTableType> tts = Sets.newHashSet();
74 for (final BgpParameters param : ((Open) message).getBgpParameters()) {
75 for (final OptionalCapabilities capa : param.getOptionalCapabilities()) {
76 final CParameters cParam = capa.getCParameters();
77 if (cParam.getAugmentation(CParameters1.class) != null && cParam.getAugmentation(CParameters1.class)
78 .getMultiprotocolCapability() != null) {
79 final MultiprotocolCapability p = cParam.getAugmentation(CParameters1.class).getMultiprotocolCapability();
80 LOG.debug("Adding open parameter {}", p);
81 final BgpTableType type = new BgpTableTypeImpl(p.getAfi(), p.getSafi());
86 activateSession(listener, tts);
87 } else if (!(message instanceof Keepalive)) {
88 listener.onMessage(null, message);
92 private static void activateSession(final BGPSessionListener listener, final Set<BgpTableType> tts) {
93 listener.onSessionUp(new BGPSession() {
94 private static final long AS = 30L;
97 public void channelRegistered(final ChannelHandlerContext channelHandlerContext) throws Exception {
101 public void channelUnregistered(final ChannelHandlerContext channelHandlerContext) throws Exception {
105 public void channelActive(final ChannelHandlerContext channelHandlerContext) throws Exception {
109 public void channelInactive(final ChannelHandlerContext channelHandlerContext) throws Exception {
113 public void channelRead(final ChannelHandlerContext channelHandlerContext, final Object o) throws Exception {
117 public void channelReadComplete(final ChannelHandlerContext channelHandlerContext) throws Exception {
121 public void userEventTriggered(final ChannelHandlerContext channelHandlerContext, final Object o) throws Exception {
125 public void channelWritabilityChanged(final ChannelHandlerContext channelHandlerContext) throws Exception {
129 public void handlerAdded(final ChannelHandlerContext channelHandlerContext) throws Exception {
133 public void handlerRemoved(final ChannelHandlerContext channelHandlerContext) throws Exception {
137 public void exceptionCaught(final ChannelHandlerContext channelHandlerContext, final Throwable throwable) throws Exception {
141 public void close() {
142 LOG.debug("Session {} closed", this);
146 public Set<BgpTableType> getAdvertisedTableTypes() {
151 public Ipv4Address getBgpId() {
152 return new Ipv4Address("127.0.0.1");
156 public AsNumber getAsNumber() {
157 return new AsNumber(AS);