Bug-6562: Support add-path in base BGP NLRI
[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.rev171207.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().setErrorCode(BGPError.CEASE.getCode()).build();
38
39     @GuardedBy("this")
40     private final List<byte[]> allPreviousByteMessages;
41     private final List<Notification> allPreviousBGPMessages;
42     private final EventBus eventBus;
43
44     @GuardedBy("this")
45     private final List<EventBusRegistration> openRegistrations = new ArrayList<>();
46
47     public BGPMock(final EventBus eventBus, final MessageRegistry registry, final List<byte[]> bgpMessages) {
48         this.allPreviousByteMessages = new ArrayList<>(bgpMessages);
49         this.eventBus = eventBus;
50         this.allPreviousBGPMessages = parsePrevious(registry, this.allPreviousByteMessages);
51     }
52
53     private static List<Notification> parsePrevious(final MessageRegistry registry, final List<byte[]> msgs) {
54         final List<Notification> messages = new ArrayList<>();
55         try {
56             for (final byte[] b : msgs) {
57
58                 final byte[] body = ByteArray.cutBytes(b, 1);
59
60                 messages.add(registry.parseMessage(Unpooled.copiedBuffer(body), null));
61             }
62         } catch (final BGPDocumentedException | BGPParsingException e) {
63             LOG.warn("Failed to parse message {}", e.getMessage(), e);
64         }
65         return messages;
66     }
67
68     @Override
69     public synchronized void close() {
70         // unregister all EventBusRegistration instances
71         for (final EventBusRegistration registration : this.openRegistrations) {
72             registration.close();
73         }
74         this.openRegistrations.clear();
75     }
76
77     public ListenerRegistration<BGPSessionListener> registerUpdateListener(final BGPSessionListener listener) {
78         return EventBusRegistration.createAndRegister(this.eventBus, listener, this.allPreviousBGPMessages);
79     }
80 }