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