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