6e5abf29ca73fe4103b86e0dba202dd7e0c78faf
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / protocol / bgp / rib / impl / SynchronizationTest.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.impl;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.mockito.Mockito.mock;
12
13 import java.util.Collections;
14
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.opendaylight.protocol.bgp.concepts.BGPAddressFamily;
18 import org.opendaylight.protocol.bgp.concepts.BGPObject;
19 import org.opendaylight.protocol.bgp.concepts.BGPSubsequentAddressFamily;
20 import org.opendaylight.protocol.bgp.concepts.BGPTableType;
21 import org.opendaylight.protocol.bgp.concepts.BaseBGPObjectState;
22 import org.opendaylight.protocol.bgp.parser.BGPLink;
23 import org.opendaylight.protocol.bgp.parser.BGPUpdateMessage;
24 import org.opendaylight.protocol.bgp.parser.impl.BGPUpdateMessageImpl;
25 import org.opendaylight.protocol.bgp.rib.impl.BGPSynchronization;
26 import org.opendaylight.protocol.bgp.rib.impl.BGPUpdateSynchronizedImpl;
27 import org.opendaylight.protocol.bgp.util.BGPIPv4RouteImpl;
28 import org.opendaylight.protocol.bgp.util.BGPIPv6RouteImpl;
29
30 import org.opendaylight.protocol.concepts.IPv4;
31 import org.opendaylight.protocol.concepts.IPv6;
32 import com.google.common.collect.Sets;
33
34 public class SynchronizationTest {
35
36         private BGPSynchronization bs;
37
38         private SimpleSessionListener listener;
39
40         private BGPUpdateMessage ipv4m;
41
42         private BGPUpdateMessage ipv6m;
43
44         private BGPUpdateMessage lsm;
45
46         @Before
47         public void setUp() {
48                 this.listener = new SimpleSessionListener();
49                 final BGPIPv4RouteImpl i4 = new BGPIPv4RouteImpl(IPv4.FAMILY.prefixForString("1.1.1.1/32"), new BaseBGPObjectState(null, null), null);
50                 this.ipv4m = new BGPUpdateMessageImpl(Sets.<BGPObject> newHashSet(i4), Collections.EMPTY_SET);
51                 final BGPIPv6RouteImpl i6 = new BGPIPv6RouteImpl(IPv6.FAMILY.prefixForString("::1/32"), new BaseBGPObjectState(null, null), null);
52                 this.ipv6m = new BGPUpdateMessageImpl(Sets.<BGPObject> newHashSet(i6), Collections.EMPTY_SET);
53                 this.lsm = new BGPUpdateMessageImpl(Sets.<BGPObject> newHashSet(mock(BGPLink.class)), Collections.EMPTY_SET);
54                 this.bs = new BGPSynchronization(this.listener);
55                 this.bs.addTableTypes(Sets.newHashSet(new BGPTableType(BGPAddressFamily.IPv4, BGPSubsequentAddressFamily.Unicast),
56                                 new BGPTableType(BGPAddressFamily.LinkState, BGPSubsequentAddressFamily.Linkstate)));
57         }
58
59         @Test
60         public void testSynchronize() {
61                 // simulate sync
62                 this.bs.updReceived(this.ipv6m);
63
64                 this.bs.updReceived(this.ipv4m);
65                 this.bs.updReceived(this.lsm);
66                 this.bs.kaReceived(); // nothing yet
67                 this.bs.updReceived(this.ipv4m);
68                 this.bs.kaReceived(); // linkstate
69                 assertEquals(1, this.listener.getListMsg().size());
70                 this.bs.kaReceived(); // ipv4 sync
71                 assertEquals(2, this.listener.getListMsg().size());
72                 assertEquals(BGPAddressFamily.IPv4,
73                                 ((BGPUpdateSynchronizedImpl) this.listener.getListMsg().get(1)).getTableType().getAddressFamily());
74         }
75 }