65daec2108fad2c0772c075eca4498e785126498
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / protocol / bgp / rib / impl / config / RibImplTest.java
1 /*
2  * Copyright (c) 2016 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
9 package org.opendaylight.protocol.bgp.rib.impl.config;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.mockito.Matchers.any;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.verify;
16
17 import com.google.common.collect.ImmutableSet;
18 import com.google.common.primitives.Shorts;
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.List;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.mockito.Mock;
25 import org.mockito.Mockito;
26 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
27 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService;
28 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTreeFactory;
29 import org.opendaylight.mdsal.binding.generator.impl.GeneratedClassLoadingStrategy;
30 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
31 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
32 import org.opendaylight.protocol.bgp.rib.impl.RIBImpl;
33 import org.opendaylight.protocol.bgp.rib.spi.RIBExtensionConsumerContext;
34 import org.opendaylight.protocol.bgp.rib.spi.RIBSupport;
35 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.multiprotocol.rev151009.bgp.common.afi.safi.list.AfiSafi;
36 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.multiprotocol.rev151009.bgp.common.afi.safi.list.AfiSafiBuilder;
37 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.global.base.AfiSafisBuilder;
38 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.bgp.Global;
39 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.bgp.GlobalBuilder;
40 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009.IPV4UNICAST;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.BgpTableType;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev171207.AfiSafi2;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev171207.AfiSafi2Builder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.rib.TablesKey;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpId;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
49 import org.opendaylight.yangtools.concepts.ListenerRegistration;
50 import org.opendaylight.yangtools.yang.common.QName;
51 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
52 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
53 import org.osgi.framework.ServiceRegistration;
54
55 public class RibImplTest extends AbstractConfig {
56     private static final List<AfiSafi> AFISAFIS = new ArrayList<>();
57     private static final Long ALL_PATHS = 0L;
58     private static final BgpTableType TABLE_TYPE = new BgpTableTypeImpl(Ipv4AddressFamily.class,
59             UnicastSubsequentAddressFamily.class);
60     private static final Ipv4Address BGP_ID = new BgpId(new Ipv4Address("127.0.0.1"));
61
62     static {
63         AFISAFIS.add(new AfiSafiBuilder().setAfiSafiName(IPV4UNICAST.class)
64                 .addAugmentation(AfiSafi2.class, new AfiSafi2Builder().setReceive(true)
65                         .setSendMax(Shorts.checkedCast(ALL_PATHS)).build()).build());
66     }
67
68     @Mock
69     private RIBExtensionConsumerContext extension;
70     @Mock
71     private BindingCodecTreeFactory bindingCodecTreeFactory;
72     @Mock
73     private DOMDataBroker domDataBroker;
74     @Mock
75     private DOMSchemaService domSchemaService;
76     @Mock
77     private ListenerRegistration<?> dataTreeRegistration;
78     @Mock
79     private RIBSupport ribSupport;
80     @Mock
81     private ServiceRegistration<?> serviceRegistration;
82
83     @Override
84     @Before
85     public void setUp() throws Exception {
86         super.setUp();
87
88         Mockito.doReturn(mock(GeneratedClassLoadingStrategy.class)).when(this.extension).getClassLoadingStrategy();
89         Mockito.doReturn(this.ribSupport).when(this.extension).getRIBSupport(any(TablesKey.class));
90         final NodeIdentifier nii = new NodeIdentifier(QName.create("test").intern());
91         Mockito.doReturn(nii).when(this.ribSupport).routeAttributesIdentifier();
92         Mockito.doReturn(ImmutableSet.of()).when(this.ribSupport).cacheableAttributeObjects();
93         final ChoiceNode choiceNode = mock(ChoiceNode.class);
94         Mockito.doReturn(choiceNode).when(this.ribSupport).emptyRoutes();
95         Mockito.doReturn(nii).when(choiceNode).getIdentifier();
96         Mockito.doReturn(QName.create("test").intern()).when(choiceNode).getNodeType();
97         Mockito.doReturn(this.domTx).when(this.domDataBroker).createTransactionChain(any());
98         final DOMDataTreeChangeService dOMDataTreeChangeService = mock(DOMDataTreeChangeService.class);
99         Mockito.doReturn(Collections.singletonMap(DOMDataTreeChangeService.class, dOMDataTreeChangeService))
100                 .when(this.domDataBroker).getSupportedExtensions();
101         Mockito.doReturn(this.dataTreeRegistration).when(this.domSchemaService).registerSchemaContextListener(any());
102         Mockito.doNothing().when(this.dataTreeRegistration).close();
103         Mockito.doReturn(mock(ListenerRegistration.class)).when(dOMDataTreeChangeService)
104                 .registerDataTreeChangeListener(any(), any());
105         Mockito.doNothing().when(this.serviceRegistration).unregister();
106     }
107
108     @Test
109     public void testRibImpl() throws Exception {
110         final RibImpl ribImpl = new RibImpl(this.extension, this.dispatcher,
111                 this.bindingCodecTreeFactory, this.domDataBroker, this.domSchemaService);
112         ribImpl.setServiceRegistration(this.serviceRegistration);
113         ribImpl.start(createGlobal(), "rib-test", this.tableTypeRegistry);
114         verify(this.extension).getClassLoadingStrategy();
115         verify(this.domDataBroker).getSupportedExtensions();
116         verify(this.domSchemaService).registerSchemaContextListener(any(RIBImpl.class));
117         assertEquals("RIBImpl{}", ribImpl.toString());
118         assertEquals(Collections.singleton(new TablesKey(Ipv4AddressFamily.class,
119                 UnicastSubsequentAddressFamily.class)), ribImpl.getLocalTablesKeys());
120         assertNotNull(ribImpl.getImportPolicyPeerTracker());
121         assertNotNull(ribImpl.getService());
122         assertNotNull(ribImpl.getInstanceIdentifier());
123         assertEquals(AS, ribImpl.getLocalAs());
124         assertEquals(BGP_ID, ribImpl.getBgpIdentifier());
125         assertEquals(Collections.singleton(TABLE_TYPE), ribImpl.getLocalTables());
126         assertEquals(this.dispatcher, ribImpl.getDispatcher());
127         assertEquals(this.extension, ribImpl.getRibExtensions());
128         assertNotNull(ribImpl.getRibSupportContext());
129         assertNotNull(ribImpl.getCodecsRegistry());
130
131         ribImpl.close();
132         verify(this.dataTreeRegistration).close();
133         verify(this.dataTreeRegistration).close();
134         verify(this.serviceRegistration).unregister();
135     }
136
137     private static Global createGlobal() {
138         return new GlobalBuilder()
139                 .setAfiSafis(new AfiSafisBuilder().setAfiSafi(AFISAFIS).build())
140                 .setConfig(new org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.global.base
141                         .ConfigBuilder().setAs(AS).setRouterId(BGP_ID).build()).build();
142     }
143 }