Fix model names and imports
[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 java.io.Closeable;
11 import java.util.Arrays;
12 import java.util.Iterator;
13 import java.util.List;
14
15 import javax.annotation.concurrent.GuardedBy;
16 import javax.annotation.concurrent.ThreadSafe;
17
18 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
19 import org.opendaylight.protocol.bgp.parser.BGPError;
20 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
21 import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
22 import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
23 import org.opendaylight.protocol.bgp.rib.impl.BGP;
24 import org.opendaylight.protocol.concepts.ListenerRegistration;
25 import org.opendaylight.protocol.framework.ReconnectStrategy;
26 import org.opendaylight.protocol.framework.ReconnectStrategyFactory;
27 import org.opendaylight.protocol.util.ByteArray;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.NotifyBuilder;
29 import org.opendaylight.yangtools.yang.binding.Notification;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import com.google.common.collect.Lists;
34 import com.google.common.eventbus.EventBus;
35
36 /**
37  * 
38  * Mock implementation of {@link BGP}.
39  * 
40  */
41 @ThreadSafe
42 public final class BGPMock implements BGP, Closeable {
43
44         private static final Logger LOG = LoggerFactory.getLogger(BGPMock.class);
45
46         static final Notification CONNECTION_LOST_MAGIC_MSG = new NotifyBuilder().setErrorCode(BGPError.CEASE.getCode()).build();
47
48         @GuardedBy("this")
49         private final List<byte[]> allPreviousByteMessages;
50         private final List<Notification> allPreviousBGPMessages;
51         private final EventBus eventBus;
52
53         @GuardedBy("this")
54         private final List<EventBusRegistration> openRegistrations = Lists.newLinkedList();
55
56         public BGPMock(final EventBus eventBus, final MessageRegistry registry, final List<byte[]> bgpMessages) {
57                 this.allPreviousByteMessages = Lists.newLinkedList(bgpMessages);
58                 this.eventBus = eventBus;
59                 this.allPreviousBGPMessages = this.parsePrevious(registry, this.allPreviousByteMessages);
60         }
61
62         private List<Notification> parsePrevious(final MessageRegistry registry, final List<byte[]> msgs) {
63                 final List<Notification> messages = Lists.newArrayList();
64                 try {
65                         for (final byte[] b : msgs) {
66
67                                 final byte[] body = ByteArray.cutBytes(b, 1);
68
69                                 messages.add(registry.parseMessage(body));
70                         }
71                 } catch (final BGPDocumentedException | BGPParsingException e) {
72                         LOG.warn("Failed to parse message {}", e);
73                 }
74                 return messages;
75         }
76
77         public synchronized void insertConnectionLostEvent() {
78                 this.insertMessage(CONNECTION_LOST_MAGIC_MSG);
79         }
80
81         public synchronized void insertMessages(final List<Notification> messages) {
82                 for (final Notification message : messages) {
83                         this.insertMessage(message);
84                 }
85         }
86
87         private synchronized void insertMessage(final Notification message) {
88                 this.allPreviousBGPMessages.add(message);
89                 this.eventBus.post(message);
90         }
91
92         @Override
93         public synchronized void close() {
94                 // unregister all EventBusRegistration instances
95                 for (final EventBusRegistration registration : this.openRegistrations) {
96                         registration.close();
97                 }
98                 this.openRegistrations.clear();
99         }
100
101         public boolean isMessageListSame(final List<byte[]> newMessages) {
102                 if (this.allPreviousBGPMessages.size() != newMessages.size()) {
103                         return false;
104                 }
105                 final Iterator<byte[]> i1 = this.allPreviousByteMessages.iterator();
106                 final Iterator<byte[]> i2 = this.allPreviousByteMessages.iterator();
107                 for (int i = 0; i < this.allPreviousBGPMessages.size(); i++) {
108                         if (!Arrays.equals(i1.next(), i2.next())) {
109                                 return false;
110                         }
111                 }
112                 return true;
113         }
114
115         public EventBus getEventBus() {
116                 return this.eventBus;
117         }
118
119         @Override
120         public ListenerRegistration<BGPSessionListener> registerUpdateListener(
121                         final BGPSessionListener listener,
122                         final ReconnectStrategyFactory tcpStrategyFactory,
123                         final ReconnectStrategy sessionStrategy) {
124                 return EventBusRegistration.createAndRegister(this.eventBus, listener, this.allPreviousBGPMessages);
125         }
126 }