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