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.BgpTableTypeImpl;
19 import org.opendaylight.protocol.bgp.rib.spi.BGPSession;
20 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Keepalive;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Open;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParameters;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.OptionalCapabilities;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.CParameters;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.CParameters1;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.open.bgp.parameters.optional.capabilities.c.parameters.AddPathCapability;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.open.bgp.parameters.optional.capabilities.c.parameters.MultiprotocolCapability;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.open.bgp.parameters.optional.capabilities.c.parameters.add.path.capability.AddressFamilies;
33 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
34 import org.opendaylight.yangtools.yang.binding.Notification;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * This class has @Subscribe annotated methods which receive events from {@link EventBus} . Events are produced by
40 * {@link BGPMock}, and each instance notifies exactly one {@link BGPSessionListener}.
42 final class EventBusRegistration extends AbstractListenerRegistration<BGPSessionListener> {
44 private static final Logger LOG = LoggerFactory.getLogger(EventBusRegistration.class);
46 private final EventBus eventBus;
48 public static EventBusRegistration createAndRegister(final EventBus eventBus, final BGPSessionListener listener,
49 final List<Notification> allPreviousMessages) {
50 final EventBusRegistration instance = new EventBusRegistration(eventBus, listener, allPreviousMessages);
51 eventBus.register(instance);
55 private EventBusRegistration(final EventBus eventBus, final BGPSessionListener listener, final List<Notification> allPreviousMessages) {
57 this.eventBus = eventBus;
58 for (final Notification message : allPreviousMessages) {
59 sendMessage(listener, message);
64 public void onMessage(final Notification message) {
65 sendMessage(this.getInstance(), message);
69 public synchronized void removeRegistration() {
70 this.eventBus.unregister(this);
73 private static void sendMessage(final BGPSessionListener listener, final Notification message) {
74 if (BGPMock.CONNECTION_LOST_MAGIC_MSG.equals(message)) {
75 listener.onSessionTerminated(null, null);
76 } else if (message instanceof Open) {
77 final Set<BgpTableType> tts = Sets.newHashSet();
78 final List<AddressFamilies> addPathCapabilitiesList = Lists.newArrayList();
79 for (final BgpParameters param : ((Open) message).getBgpParameters()) {
80 for (final OptionalCapabilities capa : param.getOptionalCapabilities()) {
81 final CParameters cParam = capa.getCParameters();
82 if (cParam.getAugmentation(CParameters1.class) == null) {
85 if (cParam.getAugmentation(CParameters1.class).getMultiprotocolCapability() != null) {
86 final MultiprotocolCapability p = cParam.getAugmentation(CParameters1.class).getMultiprotocolCapability();
87 LOG.debug("Adding open parameter {}", p);
88 final BgpTableType type = new BgpTableTypeImpl(p.getAfi(), p.getSafi());
90 } else if (cParam.getAugmentation(CParameters1.class).getAddPathCapability() != null) {
91 final AddPathCapability addPathCap = cParam.getAugmentation(CParameters1.class).getAddPathCapability();
92 addPathCapabilitiesList.addAll(addPathCap.getAddressFamilies());
96 activateSession(listener, tts);
97 } else if (!(message instanceof Keepalive)) {
98 listener.onMessage(null, message);
102 private static void activateSession(final BGPSessionListener listener, final Set<BgpTableType> tts) {
103 listener.onSessionUp(new BGPSession() {
104 private static final long AS = 30L;
107 public void channelRegistered(final ChannelHandlerContext channelHandlerContext) throws Exception {
111 public void channelUnregistered(final ChannelHandlerContext channelHandlerContext) throws Exception {
115 public void channelActive(final ChannelHandlerContext channelHandlerContext) throws Exception {
119 public void channelInactive(final ChannelHandlerContext channelHandlerContext) throws Exception {
123 public void channelRead(final ChannelHandlerContext channelHandlerContext, final Object o) throws Exception {
127 public void channelReadComplete(final ChannelHandlerContext channelHandlerContext) throws Exception {
131 public void userEventTriggered(final ChannelHandlerContext channelHandlerContext, final Object o) throws Exception {
135 public void channelWritabilityChanged(final ChannelHandlerContext channelHandlerContext) throws Exception {
139 public void handlerAdded(final ChannelHandlerContext channelHandlerContext) throws Exception {
143 public void handlerRemoved(final ChannelHandlerContext channelHandlerContext) throws Exception {
147 public void exceptionCaught(final ChannelHandlerContext channelHandlerContext, final Throwable throwable) throws Exception {
151 public void close() {
152 LOG.debug("Session {} closed", this);
156 public Set<BgpTableType> getAdvertisedTableTypes() {
161 public Ipv4Address getBgpId() {
162 return new Ipv4Address("127.0.0.1");
166 public AsNumber getAsNumber() {
167 return new AsNumber(AS);
171 public List<AddressFamilies> getAdvertisedAddPathTableTypes() {
172 return Collections.emptyList();