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 org.checkerframework.checker.lock.qual.GuardedBy;
16 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
17 import org.opendaylight.protocol.bgp.parser.BGPError;
18 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
19 import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
20 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
21 import org.opendaylight.protocol.util.ByteArray;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Notify;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.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. This class is thread-safe.
32 public final class BGPMock implements Closeable {
34 private static final Logger LOG = LoggerFactory.getLogger(BGPMock.class);
36 static final Notify CONNECTION_LOST_MAGIC_MSG = new NotifyBuilder().setErrorCode(BGPError.CEASE.getCode()).build();
39 private final List<byte[]> allPreviousByteMessages;
40 private final List<Notification<?>> allPreviousBGPMessages;
41 private final EventBus eventBus;
44 private final List<EventBusRegistration> openRegistrations = new ArrayList<>();
46 public BGPMock(final EventBus eventBus, final MessageRegistry registry, final List<byte[]> bgpMessages) {
47 allPreviousByteMessages = new ArrayList<>(bgpMessages);
48 this.eventBus = eventBus;
49 allPreviousBGPMessages = parsePrevious(registry, allPreviousByteMessages);
52 private static List<Notification<?>> parsePrevious(final MessageRegistry registry, final List<byte[]> msgs) {
53 final List<Notification<?>> messages = new ArrayList<>();
55 for (final byte[] b : msgs) {
57 final byte[] body = ByteArray.cutBytes(b, 1);
59 messages.add(registry.parseMessage(Unpooled.copiedBuffer(body), null));
61 } catch (final BGPDocumentedException | BGPParsingException e) {
62 LOG.warn("Failed to parse message", e);
68 public synchronized void close() {
69 // unregister all EventBusRegistration instances
70 for (final EventBusRegistration registration : openRegistrations) {
73 openRegistrations.clear();
76 public ListenerRegistration<BGPSessionListener> registerUpdateListener(final BGPSessionListener listener) {
77 return EventBusRegistration.createAndRegister(eventBus, listener, allPreviousBGPMessages);