BUG-6647 Increase code coverage and clean up II
[bgpcep.git] / bgp / rib-mock / src / main / java / org / opendaylight / protocol / bgp / rib / mock / BGPMock.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.bgp.rib.mock;
9
10 import com.google.common.collect.Lists;
11 import com.google.common.eventbus.EventBus;
12
13 import io.netty.buffer.Unpooled;
14
15 import java.io.Closeable;
16 import java.util.List;
17
18 import javax.annotation.concurrent.GuardedBy;
19 import javax.annotation.concurrent.ThreadSafe;
20
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;
32
33 /**
34  * Mock BGP session. It provides a way how to route a set of messages to BGPSessionListener.
35  */
36 @ThreadSafe
37 public final class BGPMock implements Closeable {
38
39     private static final Logger LOG = LoggerFactory.getLogger(BGPMock.class);
40
41     static final Notification CONNECTION_LOST_MAGIC_MSG = new NotifyBuilder().setErrorCode(BGPError.CEASE.getCode()).build();
42
43     @GuardedBy("this")
44     private final List<byte[]> allPreviousByteMessages;
45     private final List<Notification> allPreviousBGPMessages;
46     private final EventBus eventBus;
47
48     @GuardedBy("this")
49     private final List<EventBusRegistration> openRegistrations = Lists.newLinkedList();
50
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);
55     }
56
57     private List<Notification> parsePrevious(final MessageRegistry registry, final List<byte[]> msgs) {
58         final List<Notification> messages = Lists.newArrayList();
59         try {
60             for (final byte[] b : msgs) {
61
62                 final byte[] body = ByteArray.cutBytes(b, 1);
63
64                 messages.add(registry.parseMessage(Unpooled.copiedBuffer(body)));
65             }
66         } catch (final BGPDocumentedException | BGPParsingException e) {
67             LOG.warn("Failed to parse message {}", e.getMessage(), e);
68         }
69         return messages;
70     }
71
72     @Override
73     public synchronized void close() {
74         // unregister all EventBusRegistration instances
75         for (final EventBusRegistration registration : this.openRegistrations) {
76             registration.close();
77         }
78         this.openRegistrations.clear();
79     }
80
81     public ListenerRegistration<BGPSessionListener> registerUpdateListener(final BGPSessionListener listener) {
82         return EventBusRegistration.createAndRegister(this.eventBus, listener, this.allPreviousBGPMessages);
83     }
84 }