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.eventbus.EventBus;
11 import io.netty.buffer.Unpooled;
12 import java.io.Closeable;
13 import java.util.ArrayList;
14 import java.util.List;
15 import javax.annotation.concurrent.GuardedBy;
16 import javax.annotation.concurrent.ThreadSafe;
17 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
18 import org.opendaylight.protocol.bgp.parser.BGPError;
19 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
20 import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
21 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
22 import org.opendaylight.protocol.util.ByteArray;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.NotifyBuilder;
24 import org.opendaylight.yangtools.concepts.ListenerRegistration;
25 import org.opendaylight.yangtools.yang.binding.Notification;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * Mock BGP session. It provides a way how to route a set of messages to BGPSessionListener.
33 public final class BGPMock implements Closeable {
35 private static final Logger LOG = LoggerFactory.getLogger(BGPMock.class);
37 static final Notification CONNECTION_LOST_MAGIC_MSG = new NotifyBuilder()
38 .setErrorCode(BGPError.CEASE.getCode()).build();
41 private final List<byte[]> allPreviousByteMessages;
42 private final List<Notification> allPreviousBGPMessages;
43 private final EventBus eventBus;
46 private final List<EventBusRegistration> openRegistrations = new ArrayList<>();
48 public BGPMock(final EventBus eventBus, final MessageRegistry registry, final List<byte[]> bgpMessages) {
49 this.allPreviousByteMessages = new ArrayList<>(bgpMessages);
50 this.eventBus = eventBus;
51 this.allPreviousBGPMessages = parsePrevious(registry, this.allPreviousByteMessages);
54 private static List<Notification> parsePrevious(final MessageRegistry registry, final List<byte[]> msgs) {
55 final List<Notification> messages = new ArrayList<>();
57 for (final byte[] b : msgs) {
59 final byte[] body = ByteArray.cutBytes(b, 1);
61 messages.add(registry.parseMessage(Unpooled.copiedBuffer(body), null));
63 } catch (final BGPDocumentedException | BGPParsingException e) {
64 LOG.warn("Failed to parse message {}", e.getMessage(), e);
70 public synchronized void close() {
71 // unregister all EventBusRegistration instances
72 for (final EventBusRegistration registration : this.openRegistrations) {
75 this.openRegistrations.clear();
78 public ListenerRegistration<BGPSessionListener> registerUpdateListener(final BGPSessionListener listener) {
79 return EventBusRegistration.createAndRegister(this.eventBus, listener, this.allPreviousBGPMessages);