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