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 java.io.Closeable;
11 import java.util.Arrays;
12 import java.util.Iterator;
13 import java.util.List;
15 import javax.annotation.concurrent.GuardedBy;
16 import javax.annotation.concurrent.ThreadSafe;
18 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
19 import org.opendaylight.protocol.bgp.parser.BGPError;
20 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
21 import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
22 import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
23 import org.opendaylight.protocol.bgp.rib.impl.BGP;
24 import org.opendaylight.protocol.concepts.ListenerRegistration;
25 import org.opendaylight.protocol.framework.ReconnectStrategy;
26 import org.opendaylight.protocol.framework.ReconnectStrategyFactory;
27 import org.opendaylight.protocol.util.ByteArray;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.NotifyBuilder;
29 import org.opendaylight.yangtools.yang.binding.Notification;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 import com.google.common.collect.Lists;
34 import com.google.common.eventbus.EventBus;
38 * Mock implementation of {@link BGP}.
42 public final class BGPMock implements BGP, Closeable {
44 private static final Logger LOG = LoggerFactory.getLogger(BGPMock.class);
46 static final Notification CONNECTION_LOST_MAGIC_MSG = new NotifyBuilder().setErrorCode(BGPError.CEASE.getCode()).build();
49 private final List<byte[]> allPreviousByteMessages;
50 private final List<Notification> allPreviousBGPMessages;
51 private final EventBus eventBus;
54 private final List<EventBusRegistration> openRegistrations = Lists.newLinkedList();
56 public BGPMock(final EventBus eventBus, final MessageRegistry registry, final List<byte[]> bgpMessages) {
57 this.allPreviousByteMessages = Lists.newLinkedList(bgpMessages);
58 this.eventBus = eventBus;
59 this.allPreviousBGPMessages = this.parsePrevious(registry, this.allPreviousByteMessages);
62 private List<Notification> parsePrevious(final MessageRegistry registry, final List<byte[]> msgs) {
63 final List<Notification> messages = Lists.newArrayList();
65 for (final byte[] b : msgs) {
67 final byte[] body = ByteArray.cutBytes(b, 1);
69 messages.add(registry.parseMessage(body));
71 } catch (final BGPDocumentedException | BGPParsingException e) {
72 LOG.warn("Failed to parse message {}", e);
77 public synchronized void insertConnectionLostEvent() {
78 this.insertMessage(CONNECTION_LOST_MAGIC_MSG);
81 public synchronized void insertMessages(final List<Notification> messages) {
82 for (final Notification message : messages) {
83 this.insertMessage(message);
87 private synchronized void insertMessage(final Notification message) {
88 this.allPreviousBGPMessages.add(message);
89 this.eventBus.post(message);
93 public synchronized void close() {
94 // unregister all EventBusRegistration instances
95 for (final EventBusRegistration registration : this.openRegistrations) {
98 this.openRegistrations.clear();
101 public boolean isMessageListSame(final List<byte[]> newMessages) {
102 if (this.allPreviousBGPMessages.size() != newMessages.size()) {
105 final Iterator<byte[]> i1 = this.allPreviousByteMessages.iterator();
106 final Iterator<byte[]> i2 = this.allPreviousByteMessages.iterator();
107 for (int i = 0; i < this.allPreviousBGPMessages.size(); i++) {
108 if (!Arrays.equals(i1.next(), i2.next())) {
115 public EventBus getEventBus() {
116 return this.eventBus;
120 public ListenerRegistration<BGPSessionListener> registerUpdateListener(
121 final BGPSessionListener listener,
122 final ReconnectStrategyFactory tcpStrategyFactory,
123 final ReconnectStrategy sessionStrategy) {
124 return EventBusRegistration.createAndRegister(this.eventBus, listener, this.allPreviousBGPMessages);