c5e1aff7124da3e9f292528bace4a97512a87ee3
[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.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;
15 import java.util.Set;
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;
33
34 /**
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}.
37  */
38 final class EventBusRegistration extends AbstractListenerRegistration<BGPSessionListener> {
39
40     private static final Logger LOG = LoggerFactory.getLogger(EventBusRegistration.class);
41
42     private final EventBus eventBus;
43
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);
48         return instance;
49     }
50
51     private EventBusRegistration(final EventBus eventBus, final BGPSessionListener listener, final List<Notification> allPreviousMessages) {
52         super(listener);
53         this.eventBus = eventBus;
54         for (final Notification message : allPreviousMessages) {
55             sendMessage(listener, message);
56         }
57     }
58
59     @Subscribe
60     public void onMessage(final Notification message) {
61         sendMessage(this.getInstance(), message);
62     }
63
64     @Override
65     public synchronized void removeRegistration() {
66         this.eventBus.unregister(this);
67     }
68
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());
82                         tts.add(type);
83                     }
84                 }
85             }
86
87             listener.onSessionUp(new BGPSession() {
88
89                 private static final long AS = 30L;
90
91                 @Override
92                 public void channelRegistered(final ChannelHandlerContext channelHandlerContext) throws Exception {
93
94                 }
95
96                 @Override
97                 public void channelUnregistered(final ChannelHandlerContext channelHandlerContext) throws Exception {
98
99                 }
100
101                 @Override
102                 public void channelActive(final ChannelHandlerContext channelHandlerContext) throws Exception {
103
104                 }
105
106                 @Override
107                 public void channelInactive(final ChannelHandlerContext channelHandlerContext) throws Exception {
108
109                 }
110
111                 @Override
112                 public void channelRead(final ChannelHandlerContext channelHandlerContext, final Object o) throws Exception {
113
114                 }
115
116                 @Override
117                 public void channelReadComplete(final ChannelHandlerContext channelHandlerContext) throws Exception {
118
119                 }
120
121                 @Override
122                 public void userEventTriggered(final ChannelHandlerContext channelHandlerContext, final Object o) throws Exception {
123
124                 }
125
126                 @Override
127                 public void channelWritabilityChanged(final ChannelHandlerContext channelHandlerContext) throws Exception {
128
129                 }
130
131                 @Override
132                 public void handlerAdded(final ChannelHandlerContext channelHandlerContext) throws Exception {
133
134                 }
135
136                 @Override
137                 public void handlerRemoved(final ChannelHandlerContext channelHandlerContext) throws Exception {
138
139                 }
140
141                 @Override
142                 public void exceptionCaught(final ChannelHandlerContext channelHandlerContext, final Throwable throwable) throws Exception {
143
144                 }
145
146                 @Override
147                 public void close() {
148                     LOG.debug("Session {} closed", this);
149                 }
150
151                 @Override
152                 public Set<BgpTableType> getAdvertisedTableTypes() {
153                     return tts;
154                 }
155
156                 @Override
157                 public Ipv4Address getBgpId() {
158                     return new Ipv4Address("127.0.0.1");
159                 }
160
161                 @Override
162                 public AsNumber getAsNumber() {
163                     return new AsNumber(AS);
164                 }
165             });
166         } else if (!(message instanceof Keepalive)) {
167             listener.onMessage(null, message);
168         }
169     }
170 }