BUG-54 : switched channel pipeline to be protocol specific.
[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.BGPMessageFactoryImpl;
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.framework.ReconnectStrategy;
29 import org.opendaylight.protocol.util.ByteArray;
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 BGPMessage connectionLostMagicMessage = new BGPNotificationMessage(BGPError.CEASE);
42
43         @GuardedBy("this")
44         private final List<byte[]> allPreviousByteMessages;
45         private final List<BGPMessage> 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<BGPMessage> parsePrevious(final List<byte[]> msgs) {
57                 final List<BGPMessage> messages = Lists.newArrayList();
58                 final ProtocolMessageFactory<BGPMessage> 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, final ReconnectStrategy strategy) {
80                 return EventBusRegistration.createAndRegister(this.eventBus, listener, this.allPreviousBGPMessages);
81         }
82
83         public synchronized void insertConnectionLostEvent() {
84                 this.insertMessage(connectionLostMagicMessage);
85         }
86
87         public synchronized void insertMessages(final List<BGPMessage> messages) {
88                 for (final BGPMessage message : messages) {
89                         this.insertMessage(message);
90                 }
91         }
92
93         private synchronized void insertMessage(final BGPMessage message) {
94                 this.allPreviousBGPMessages.add(message);
95                 this.eventBus.post(message);
96         }
97
98         @Override
99         public synchronized void close() {
100                 // unregister all EventBusRegistration instances
101                 for (final EventBusRegistration registration : this.openRegistrations) {
102                         registration.close();
103                 }
104                 this.openRegistrations.clear();
105         }
106
107         public boolean isMessageListSame(final List<byte[]> newMessages) {
108                 if (this.allPreviousBGPMessages.size() != newMessages.size()) {
109                         return false;
110                 }
111                 final Iterator<byte[]> i1 = this.allPreviousByteMessages.iterator();
112                 final Iterator<byte[]> i2 = this.allPreviousByteMessages.iterator();
113                 for (int i = 0; i < this.allPreviousBGPMessages.size(); i++) {
114                         if (!Arrays.equals(i1.next(), i2.next())) {
115                                 return false;
116                         }
117                 }
118                 return true;
119         }
120
121         public EventBus getEventBus() {
122                 return this.eventBus;
123         }
124 }