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.eventbus.EventBus;
13 import io.netty.buffer.Unpooled;
15 import java.io.Closeable;
16 import java.util.List;
18 import javax.annotation.concurrent.GuardedBy;
19 import javax.annotation.concurrent.ThreadSafe;
21 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
22 import org.opendaylight.protocol.bgp.parser.BGPError;
23 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
24 import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
25 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
26 import org.opendaylight.protocol.util.ByteArray;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.NotifyBuilder;
28 import org.opendaylight.yangtools.concepts.ListenerRegistration;
29 import org.opendaylight.yangtools.yang.binding.Notification;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * Mock BGP session. It provides a way how to route a set of messages to BGPSessionListener.
37 public final class BGPMock implements Closeable {
39 private static final Logger LOG = LoggerFactory.getLogger(BGPMock.class);
41 static final Notification CONNECTION_LOST_MAGIC_MSG = new NotifyBuilder().setErrorCode(BGPError.CEASE.getCode()).build();
44 private final List<byte[]> allPreviousByteMessages;
45 private final List<Notification> allPreviousBGPMessages;
46 private final EventBus eventBus;
49 private final List<EventBusRegistration> openRegistrations = Lists.newLinkedList();
51 public BGPMock(final EventBus eventBus, final MessageRegistry registry, final List<byte[]> bgpMessages) {
52 this.allPreviousByteMessages = Lists.newLinkedList(bgpMessages);
53 this.eventBus = eventBus;
54 this.allPreviousBGPMessages = this.parsePrevious(registry, this.allPreviousByteMessages);
57 private List<Notification> parsePrevious(final MessageRegistry registry, final List<byte[]> msgs) {
58 final List<Notification> messages = Lists.newArrayList();
60 for (final byte[] b : msgs) {
62 final byte[] body = ByteArray.cutBytes(b, 1);
64 messages.add(registry.parseMessage(Unpooled.copiedBuffer(body)));
66 } catch (final BGPDocumentedException | BGPParsingException e) {
67 LOG.warn("Failed to parse message {}", e.getMessage(), e);
73 public synchronized void close() {
74 // unregister all EventBusRegistration instances
75 for (final EventBusRegistration registration : this.openRegistrations) {
78 this.openRegistrations.clear();
81 public ListenerRegistration<BGPSessionListener> registerUpdateListener(final BGPSessionListener listener) {
82 return EventBusRegistration.createAndRegister(this.eventBus, listener, this.allPreviousBGPMessages);