Switched BGPMessage concept to yangtools.binding.Notification.
[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.BGPError;
19 import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
20 import org.opendaylight.protocol.bgp.parser.impl.BGPMessageFactoryImpl;
21 import org.opendaylight.protocol.bgp.parser.message.BGPNotificationMessage;
22 import org.opendaylight.protocol.bgp.rib.impl.BGP;
23 import org.opendaylight.protocol.concepts.ListenerRegistration;
24 import org.opendaylight.protocol.framework.DeserializerException;
25 import org.opendaylight.protocol.framework.DocumentedException;
26 import org.opendaylight.protocol.framework.ProtocolMessageFactory;
27 import org.opendaylight.protocol.framework.ReconnectStrategy;
28 import org.opendaylight.protocol.util.ByteArray;
29 import org.opendaylight.yangtools.yang.binding.Notification;
30
31 import com.google.common.collect.Lists;
32 import com.google.common.eventbus.EventBus;
33
34 /**
35  * 
36  * Mock implementation of {@link BGP}.
37  * 
38  */
39 @ThreadSafe
40 public final class BGPMock implements BGP, Closeable {
41         static final Notification connectionLostMagicMessage = new BGPNotificationMessage(BGPError.CEASE);
42
43         @GuardedBy("this")
44         private final List<byte[]> allPreviousByteMessages;
45         private final List<Notification> allPreviousBGPMessages;
46         private final EventBus eventBus;
47         @GuardedBy("this")
48         private final List<EventBusRegistration> openRegistrations = Lists.newLinkedList();
49
50         public BGPMock(final EventBus eventBus, final List<byte[]> bgpMessages) {
51                 this.allPreviousByteMessages = Lists.newLinkedList(bgpMessages);
52                 this.eventBus = eventBus;
53                 this.allPreviousBGPMessages = this.parsePrevious(this.allPreviousByteMessages);
54         }
55
56         private List<Notification> parsePrevious(final List<byte[]> msgs) {
57                 final List<Notification> messages = Lists.newArrayList();
58                 final ProtocolMessageFactory<Notification> parser = new BGPMessageFactoryImpl();
59                 try {
60                         for (final byte[] b : msgs) {
61
62                                 final byte[] body = ByteArray.cutBytes(b, 1);
63
64                                 messages.addAll(parser.parse(body));
65                         }
66                 } catch (final DeserializerException e) {
67                         e.printStackTrace();
68                 } catch (final DocumentedException e) {
69                         e.printStackTrace();
70                 }
71                 return messages;
72         }
73
74         /**
75          * @param listener BGPListener
76          * @return ListenerRegistration
77          */
78         @Override
79         public synchronized ListenerRegistration<BGPSessionListener> registerUpdateListener(final BGPSessionListener listener,
80                         final ReconnectStrategy strategy) {
81                 return EventBusRegistration.createAndRegister(this.eventBus, listener, this.allPreviousBGPMessages);
82         }
83
84         public synchronized void insertConnectionLostEvent() {
85                 this.insertMessage(connectionLostMagicMessage);
86         }
87
88         public synchronized void insertMessages(final List<Notification> messages) {
89                 for (final Notification message : messages) {
90                         this.insertMessage(message);
91                 }
92         }
93
94         private synchronized void insertMessage(final Notification message) {
95                 this.allPreviousBGPMessages.add(message);
96                 this.eventBus.post(message);
97         }
98
99         @Override
100         public synchronized void close() {
101                 // unregister all EventBusRegistration instances
102                 for (final EventBusRegistration registration : this.openRegistrations) {
103                         registration.close();
104                 }
105                 this.openRegistrations.clear();
106         }
107
108         public boolean isMessageListSame(final List<byte[]> newMessages) {
109                 if (this.allPreviousBGPMessages.size() != newMessages.size()) {
110                         return false;
111                 }
112                 final Iterator<byte[]> i1 = this.allPreviousByteMessages.iterator();
113                 final Iterator<byte[]> i2 = this.allPreviousByteMessages.iterator();
114                 for (int i = 0; i < this.allPreviousBGPMessages.size(); i++) {
115                         if (!Arrays.equals(i1.next(), i2.next())) {
116                                 return false;
117                         }
118                 }
119                 return true;
120         }
121
122         public EventBus getEventBus() {
123                 return this.eventBus;
124         }
125 }