8ddb7eaf6ad7785b39477eb535b0b6a93221c3ea
[bgpcep.git] / bgp / rib-mock / src / main / java / org / opendaylight / protocol / bgp / rib / mock / EventBusRegistration.java
1 /*
2  * Copyright (c) 2013 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.mock;
9
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;
17 import java.util.Set;
18 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
19 import org.opendaylight.protocol.bgp.parser.BGPError;
20 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
21 import org.opendaylight.protocol.bgp.rib.spi.BGPSession;
22 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
23 import org.opendaylight.protocol.bgp.rib.spi.BGPTerminationReason;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.Keepalive;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.Open;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.open.message.BgpParameters;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.open.message.bgp.parameters.OptionalCapabilities;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.open.message.bgp.parameters.optional.capabilities.CParameters;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.BgpTableType;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.CParameters1;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.mp.capabilities.AddPathCapability;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.mp.capabilities.MultiprotocolCapability;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.mp.capabilities.add.path.capability.AddressFamilies;
36 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
37 import org.opendaylight.yangtools.yang.binding.Notification;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * This class has @Subscribe annotated methods which receive events from {@link EventBus} . Events are produced by
43  * {@link BGPMock}, and each instance notifies exactly one {@link BGPSessionListener}.
44  */
45 final class EventBusRegistration extends AbstractListenerRegistration<BGPSessionListener> {
46
47     private static final Logger LOG = LoggerFactory.getLogger(EventBusRegistration.class);
48
49     private final EventBus eventBus;
50
51     public static EventBusRegistration createAndRegister(final EventBus eventBus, final BGPSessionListener listener,
52             final List<Notification> allPreviousMessages) {
53         final EventBusRegistration instance = new EventBusRegistration(eventBus, listener, allPreviousMessages);
54         eventBus.register(instance);
55         return instance;
56     }
57
58     private EventBusRegistration(final EventBus eventBus, final BGPSessionListener listener,
59             final List<Notification> allPreviousMessages) {
60         super(listener);
61         this.eventBus = eventBus;
62         for (final Notification message : allPreviousMessages) {
63             sendMessage(listener, message);
64         }
65     }
66
67     @Subscribe
68     public void onMessage(final Notification message) {
69         sendMessage(this.getInstance(), message);
70     }
71
72     @Override
73     public synchronized void removeRegistration() {
74         this.eventBus.unregister(this);
75     }
76
77     private static void sendMessage(final BGPSessionListener listener, final Notification message) {
78         if (BGPMock.CONNECTION_LOST_MAGIC_MSG.equals(message)) {
79             listener.onSessionTerminated(null, new BGPTerminationReason(BGPError.CEASE));
80         } else if (message instanceof Open) {
81             final Set<BgpTableType> tts = Sets.newHashSet();
82             final List<AddressFamilies> addPathCapabilitiesList = Lists.newArrayList();
83             for (final BgpParameters param : ((Open) message).getBgpParameters()) {
84                 for (final OptionalCapabilities capa : param.getOptionalCapabilities()) {
85                     final CParameters cParam = capa.getCParameters();
86                     if (cParam.getAugmentation(CParameters1.class) == null) {
87                         continue;
88                     }
89                     if (cParam.getAugmentation(CParameters1.class).getMultiprotocolCapability() != null) {
90                         final MultiprotocolCapability p = cParam.getAugmentation(CParameters1.class)
91                                 .getMultiprotocolCapability();
92                         LOG.debug("Adding open parameter {}", p);
93                         final BgpTableType type = new BgpTableTypeImpl(p.getAfi(), p.getSafi());
94                         tts.add(type);
95                     } else if (cParam.getAugmentation(CParameters1.class).getAddPathCapability() != null) {
96                         final AddPathCapability addPathCap = cParam.getAugmentation(CParameters1.class)
97                                 .getAddPathCapability();
98                         addPathCapabilitiesList.addAll(addPathCap.getAddressFamilies());
99                     }
100                 }
101             }
102             listener.onSessionUp(new MockBGPSession(tts));
103         } else if (!(message instanceof Keepalive)) {
104             try {
105                 listener.onMessage(new MockBGPSession(), message);
106             } catch (BGPDocumentedException e) {
107                 LOG.warn("Exception encountered while handling message", e);
108             }
109         }
110     }
111
112     private static class MockBGPSession implements BGPSession {
113         private static final long AS = 30L;
114         private final Set<BgpTableType> tts;
115
116         MockBGPSession(final Set<BgpTableType> tts) {
117             this.tts = tts;
118         }
119
120         MockBGPSession() {
121             this.tts = Collections.emptySet();
122         }
123
124         @Override
125         public void channelRegistered(final ChannelHandlerContext channelHandlerContext) throws Exception {
126         }
127
128         @Override
129         public void channelUnregistered(final ChannelHandlerContext channelHandlerContext) throws Exception {
130         }
131
132         @Override
133         public void channelActive(final ChannelHandlerContext channelHandlerContext) throws Exception {
134         }
135
136         @Override
137         public void channelInactive(final ChannelHandlerContext channelHandlerContext) throws Exception {
138         }
139
140         @Override
141         public void channelRead(final ChannelHandlerContext channelHandlerContext, final Object obj)
142                 throws Exception {
143         }
144
145         @Override
146         public void channelReadComplete(final ChannelHandlerContext channelHandlerContext) throws Exception {
147         }
148
149         @Override
150         public void userEventTriggered(final ChannelHandlerContext channelHandlerContext, final Object obj)
151                 throws Exception {
152         }
153
154         @Override
155         public void channelWritabilityChanged(final ChannelHandlerContext channelHandlerContext) throws Exception {
156         }
157
158         @Override
159         public void handlerAdded(final ChannelHandlerContext channelHandlerContext) throws Exception {
160         }
161
162         @Override
163         public void handlerRemoved(final ChannelHandlerContext channelHandlerContext) throws Exception {
164         }
165
166         @Override
167         public void exceptionCaught(final ChannelHandlerContext channelHandlerContext, final Throwable throwable)
168                 throws Exception {
169         }
170
171         @Override
172         public void close() {
173             LOG.debug("Session {} closed", this);
174         }
175
176         @Override
177         public Set<BgpTableType> getAdvertisedTableTypes() {
178             return tts;
179         }
180
181         @Override
182         public Ipv4Address getBgpId() {
183             return new Ipv4Address("127.0.0.1");
184         }
185
186         @Override
187         public AsNumber getAsNumber() {
188             return new AsNumber(AS);
189         }
190
191         @Override
192         public List<AddressFamilies> getAdvertisedAddPathTableTypes() {
193             return Collections.emptyList();
194         }
195
196         @Override
197         public List<BgpTableType> getAdvertisedGracefulRestartTableTypes() {
198             return Collections.emptyList();
199         }
200
201     }
202 }