X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=bgp%2Frib-mock%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fprotocol%2Fbgp%2Frib%2Fmock%2FBGPMock.java;h=d07f5fdf994e8a32e643ae07f0f944722d02da5d;hb=eb0c8ac9e83beb2731e1ce94222529382524baa0;hp=afe7ad008f48cf7e5be8a40be733c7a661a0acc4;hpb=ab025f573547e9cd0cf8be24a2b2e8cefc26c6f6;p=bgpcep.git diff --git a/bgp/rib-mock/src/main/java/org/opendaylight/protocol/bgp/rib/mock/BGPMock.java b/bgp/rib-mock/src/main/java/org/opendaylight/protocol/bgp/rib/mock/BGPMock.java index afe7ad008f..d07f5fdf99 100644 --- a/bgp/rib-mock/src/main/java/org/opendaylight/protocol/bgp/rib/mock/BGPMock.java +++ b/bgp/rib-mock/src/main/java/org/opendaylight/protocol/bgp/rib/mock/BGPMock.java @@ -15,18 +15,20 @@ import java.util.List; import javax.annotation.concurrent.GuardedBy; import javax.annotation.concurrent.ThreadSafe; +import org.opendaylight.protocol.bgp.parser.BGPDocumentedException; import org.opendaylight.protocol.bgp.parser.BGPError; +import org.opendaylight.protocol.bgp.parser.BGPParsingException; import org.opendaylight.protocol.bgp.parser.BGPSessionListener; -import org.opendaylight.protocol.bgp.parser.impl.BGPMessageFactoryImpl; -import org.opendaylight.protocol.bgp.parser.message.BGPNotificationMessage; +import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry; import org.opendaylight.protocol.bgp.rib.impl.BGP; import org.opendaylight.protocol.concepts.ListenerRegistration; -import org.opendaylight.protocol.framework.DeserializerException; -import org.opendaylight.protocol.framework.DocumentedException; -import org.opendaylight.protocol.framework.ProtocolMessageFactory; import org.opendaylight.protocol.framework.ReconnectStrategy; +import org.opendaylight.protocol.framework.ReconnectStrategyFactory; import org.opendaylight.protocol.util.ByteArray; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.NotifyBuilder; import org.opendaylight.yangtools.yang.binding.Notification; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import com.google.common.collect.Lists; import com.google.common.eventbus.EventBus; @@ -38,51 +40,42 @@ import com.google.common.eventbus.EventBus; */ @ThreadSafe public final class BGPMock implements BGP, Closeable { - static final Notification connectionLostMagicMessage = new BGPNotificationMessage(BGPError.CEASE); + + private static final Logger LOG = LoggerFactory.getLogger(BGPMock.class); + + static final Notification CONNECTION_LOST_MAGIC_MSG = new NotifyBuilder().setErrorCode(BGPError.CEASE.getCode()).build(); @GuardedBy("this") private final List allPreviousByteMessages; private final List allPreviousBGPMessages; private final EventBus eventBus; + @GuardedBy("this") private final List openRegistrations = Lists.newLinkedList(); - public BGPMock(final EventBus eventBus, final List bgpMessages) { + public BGPMock(final EventBus eventBus, final MessageRegistry registry, final List bgpMessages) { this.allPreviousByteMessages = Lists.newLinkedList(bgpMessages); this.eventBus = eventBus; - this.allPreviousBGPMessages = this.parsePrevious(this.allPreviousByteMessages); + this.allPreviousBGPMessages = this.parsePrevious(registry, this.allPreviousByteMessages); } - private List parsePrevious(final List msgs) { + private List parsePrevious(final MessageRegistry registry, final List msgs) { final List messages = Lists.newArrayList(); - final ProtocolMessageFactory parser = new BGPMessageFactoryImpl(); try { for (final byte[] b : msgs) { final byte[] body = ByteArray.cutBytes(b, 1); - messages.addAll(parser.parse(body)); + messages.add(registry.parseMessage(body)); } - } catch (final DeserializerException e) { - e.printStackTrace(); - } catch (final DocumentedException e) { - e.printStackTrace(); + } catch (final BGPDocumentedException | BGPParsingException e) { + LOG.warn("Failed to parse message {}", e); } return messages; } - /** - * @param listener BGPListener - * @return ListenerRegistration - */ - @Override - public synchronized ListenerRegistration registerUpdateListener(final BGPSessionListener listener, - final ReconnectStrategy strategy) { - return EventBusRegistration.createAndRegister(this.eventBus, listener, this.allPreviousBGPMessages); - } - public synchronized void insertConnectionLostEvent() { - this.insertMessage(connectionLostMagicMessage); + this.insertMessage(CONNECTION_LOST_MAGIC_MSG); } public synchronized void insertMessages(final List messages) { @@ -122,4 +115,12 @@ public final class BGPMock implements BGP, Closeable { public EventBus getEventBus() { return this.eventBus; } + + @Override + public ListenerRegistration registerUpdateListener( + final BGPSessionListener listener, + final ReconnectStrategyFactory tcpStrategyFactory, + final ReconnectStrategy sessionStrategy) { + return EventBusRegistration.createAndRegister(this.eventBus, listener, this.allPreviousBGPMessages); + } }