Fix findbugs warnings
[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.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;
28
29 /**
30  * Mock BGP session. It provides a way how to route a set of messages to BGPSessionListener.
31  */
32 @ThreadSafe
33 public final class BGPMock implements Closeable {
34
35     private static final Logger LOG = LoggerFactory.getLogger(BGPMock.class);
36
37     static final Notification CONNECTION_LOST_MAGIC_MSG = new NotifyBuilder()
38             .setErrorCode(BGPError.CEASE.getCode()).build();
39
40     @GuardedBy("this")
41     private final List<byte[]> allPreviousByteMessages;
42     private final List<Notification> allPreviousBGPMessages;
43     private final EventBus eventBus;
44
45     @GuardedBy("this")
46     private final List<EventBusRegistration> openRegistrations = new ArrayList<>();
47
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);
52     }
53
54     private static List<Notification> parsePrevious(final MessageRegistry registry, final List<byte[]> msgs) {
55         final List<Notification> messages = new ArrayList<>();
56         try {
57             for (final byte[] b : msgs) {
58
59                 final byte[] body = ByteArray.cutBytes(b, 1);
60
61                 messages.add(registry.parseMessage(Unpooled.copiedBuffer(body), null));
62             }
63         } catch (final BGPDocumentedException | BGPParsingException e) {
64             LOG.warn("Failed to parse message", e);
65         }
66         return messages;
67     }
68
69     @Override
70     public synchronized void close() {
71         // unregister all EventBusRegistration instances
72         for (final EventBusRegistration registration : this.openRegistrations) {
73             registration.close();
74         }
75         this.openRegistrations.clear();
76     }
77
78     public ListenerRegistration<BGPSessionListener> registerUpdateListener(final BGPSessionListener listener) {
79         return EventBusRegistration.createAndRegister(this.eventBus, listener, this.allPreviousBGPMessages);
80     }
81 }