Bump ODL dependencies to Fluorine (HONEYCOMB-433)
[honeycomb.git] / infra / northbound / bgp / src / test / java / io / fd / honeycomb / infra / bgp / neighbors / NeighborCustomizerTest.java
1 /*
2  * Copyright (c) 2017 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package io.fd.honeycomb.infra.bgp.neighbors;
18
19 import static org.junit.Assert.assertFalse;
20 import static org.junit.Assert.assertTrue;
21 import static org.mockito.ArgumentMatchers.any;
22 import static org.mockito.Mockito.mock;
23 import static org.mockito.Mockito.verify;
24 import static org.mockito.Mockito.when;
25 import static org.mockito.MockitoAnnotations.initMocks;
26
27 import com.google.common.util.concurrent.CheckedFuture;
28 import com.google.common.util.concurrent.FluentFuture;
29 import com.google.common.util.concurrent.Futures;
30 import io.fd.honeycomb.infra.bgp.BgpConfiguration;
31 import io.fd.honeycomb.translate.write.WriteContext;
32 import io.fd.honeycomb.translate.write.WriteFailedException;
33 import java.util.Collections;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.mockito.Mock;
37 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
38 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
39 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
40 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService;
41 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
42 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
43 import org.opendaylight.protocol.bgp.openconfig.spi.BGPTableTypeRegistryConsumer;
44 import org.opendaylight.protocol.bgp.rib.impl.config.PeerBean;
45 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher;
46 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
47 import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
48 import org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContextRegistry;
49 import org.opendaylight.protocol.bgp.rib.spi.BGPPeerTracker;
50 import org.opendaylight.protocol.bgp.rib.spi.policy.BGPRibRoutingPolicy;
51 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.multiprotocol.rev151009.bgp.common.afi.safi.list.AfiSafiBuilder;
52 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.AfiSafisBuilder;
53 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.ConfigBuilder;
54 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbors.Neighbor;
55 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbors.NeighborBuilder;
56 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbors.NeighborKey;
57 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.bgp.Neighbors;
58 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009.IPV4UNICAST;
59 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009.PeerType;
60 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
61 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
62 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.NeighborPeerGroupConfig;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.NeighborPeerGroupConfigBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.BgpRib;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.RibId;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.Rib;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.RibKey;
69 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
70 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
71 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
72
73 public class NeighborCustomizerTest {
74     private static final IpAddress IP = new IpAddress(new Ipv4Address("10.25.1.9"));
75     private static final InstanceIdentifier<Neighbor> ID =
76         InstanceIdentifier.create(Neighbors.class).child(Neighbor.class, new NeighborKey(IP));
77     private static final KeyedInstanceIdentifier<Rib, RibKey> RIB_IID =
78         InstanceIdentifier.create(BgpRib.class).child(Rib.class, new RibKey(new RibId("test-rib-id")));
79
80     @Mock
81     private RIB globalRib;
82     @Mock
83     private BGPPeerRegistry peerRegistry;
84     @Mock
85     private BGPTableTypeRegistryConsumer tableTypeRegistry;
86     @Mock
87     private WriteContext ctx;
88     @Mock
89     private BindingTransactionChain chain;
90     @Mock
91     private DOMTransactionChain domChain;
92     @Mock
93     private DOMDataWriteTransaction domTx;
94     @Mock
95     private WriteTransaction tx;
96     @Mock
97     private BGPDispatcher dispatcher;
98
99     private NeighborCustomizer customizer;
100     @Mock
101     private DataBroker dataBroker;
102     @Mock
103     private BgpConfiguration configuration;
104
105     @Before
106     public void setUp() {
107         initMocks(this);
108         when(globalRib.getYangRibId()).thenReturn(YangInstanceIdentifier.EMPTY);
109         when(globalRib.getInstanceIdentifier()).thenReturn(RIB_IID);
110         when(globalRib.getRibPolicies()).thenReturn(mock(BGPRibRoutingPolicy.class));
111
112         when(globalRib.createPeerDOMChain(any())).thenReturn(domChain);
113         when(domChain.newWriteOnlyTransaction()).thenReturn(domTx);
114         when(domTx.commit()).thenReturn(FluentFuture.from(Futures.immediateFuture(null)));
115
116         when(globalRib.createPeerChain(any())).thenReturn(chain);
117         when(chain.newWriteOnlyTransaction()).thenReturn(tx);
118         when(tx.submit()).thenReturn(mock(CheckedFuture.class));
119         when(globalRib.getRibSupportContext()).thenReturn(mock(RIBSupportContextRegistry.class));
120         when(globalRib.getService()).thenReturn(mock(DOMDataTreeChangeService.class));
121         when(globalRib.getDispatcher()).thenReturn(dispatcher);
122         when(globalRib.getLocalAs()).thenReturn(new AsNumber(65000L));
123         when(globalRib.getDataBroker()).thenReturn(dataBroker);
124         when(globalRib.getPeerTracker()).thenReturn(mock(BGPPeerTracker.class));
125         when(dispatcher.getBGPPeerRegistry()).thenReturn(peerRegistry);
126         when(dataBroker.newWriteOnlyTransaction()).thenReturn(tx);
127         when(tx.commit()).thenReturn(FluentFuture.from(Futures.immediateFuture(null)));
128
129         customizer = new NeighborCustomizer(globalRib, peerRegistry, tableTypeRegistry, configuration);
130     }
131
132     @Test
133     public void testAddAppPeer() throws WriteFailedException {
134         final Neighbor neighbor = new NeighborBuilder()
135                 .setNeighborAddress(IP)
136                 .setConfig(
137                         new ConfigBuilder()
138                                 .addAugmentation(
139                                         NeighborPeerGroupConfig.class,
140                                         new NeighborPeerGroupConfigBuilder().setPeerGroup("application-peers").build())
141                                 .build())
142                 .build();
143         customizer.writeCurrentAttributes(ID, neighbor, ctx);
144         assertTrue(customizer.isPeerConfigured(ID));
145     }
146
147     @Test
148     public void testAddInternalPeer() throws WriteFailedException {
149         final Neighbor neighbor = new NeighborBuilder()
150             .setNeighborAddress(IP)
151             .setAfiSafis(new AfiSafisBuilder()
152                 .setAfiSafi(Collections.singletonList(
153                     new AfiSafiBuilder().setAfiSafiName(IPV4UNICAST.class).build()
154                 )).build())
155             .setConfig(
156                 new ConfigBuilder()
157                     .setPeerType(PeerType.INTERNAL)
158                     .build())
159             .build();
160         customizer.writeCurrentAttributes(ID, neighbor, ctx);
161         assertTrue(customizer.isPeerConfigured(ID));
162     }
163
164     @Test
165     public void testUpdate() throws WriteFailedException {
166         final PeerBean peer = mock(PeerBean.class);
167         customizer.addPeer(ID, peer);
168         final Neighbor before = mock(Neighbor.class);
169         final Neighbor after = mock(Neighbor.class);
170         customizer.updateCurrentAttributes(ID, before, after, ctx);
171         verify(peer).closeServiceInstance();
172         verify(peer).close();
173         verify(peer).start(globalRib, after, NeighborCustomizer.bgpIid, configuration, tableTypeRegistry);
174     }
175
176     @Test
177     public void testDelete() throws WriteFailedException {
178         final PeerBean peer = mock(PeerBean.class);
179         customizer.addPeer(ID, peer);
180         final Neighbor before = mock(Neighbor.class);
181         customizer.deleteCurrentAttributes(ID, before, ctx);
182         verify(peer).closeServiceInstance();
183         verify(peer).close();
184         assertFalse(customizer.isPeerConfigured(ID));
185     }
186 }