2 * Copyright (c) 2017 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.bgp.translate.impl;
19 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.OPERATIONAL;
21 import io.fd.honeycomb.translate.bgp.RouteWriter;
22 import io.fd.honeycomb.translate.write.WriteFailedException;
23 import javax.annotation.Nonnull;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.ArgumentMatchers;
27 import org.mockito.Mock;
28 import org.mockito.Mockito;
29 import org.mockito.MockitoAnnotations;
30 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
31 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.routes.Ipv4Routes;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.routes.ipv4.routes.Ipv4Route;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.routes.ipv4.routes.Ipv4RouteKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.routes.LabeledUnicastRoutes;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.routes.list.LabeledUnicastRoute;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.PathId;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.BgpRib;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.RibId;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.Route;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.Rib;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.RibKey;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.rib.LocRib;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.Tables;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv4AddressFamily;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.UnicastSubsequentAddressFamily;
49 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
52 public class LocRibWriterTest {
54 private static final InstanceIdentifier<Tables> TABLES = InstanceIdentifier.create(BgpRib.class).child(Rib.class)
55 .child(LocRib.class).child(Tables.class);
57 @SuppressWarnings("unchecked")
58 private static final KeyedInstanceIdentifier<Ipv4Route, Ipv4RouteKey>
59 SPECIFIC_IP4_ROUTE_ID =
60 InstanceIdentifier.create(BgpRib.class).child(Rib.class, new RibKey(new RibId("some-rib"))).child(LocRib.class)
61 .child(Tables.class, new TablesKey(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class))
62 .child((Class) Ipv4Routes.class)
63 .child(Ipv4Route.class, new Ipv4RouteKey(new PathId(1L), "1.2.3.4/24"));
65 @SuppressWarnings("unchecked")
66 private static final InstanceIdentifier<Ipv4Route> IP4_ROUTE_ID =
67 TABLES.child((Class) Ipv4Routes.class).child(Ipv4Route.class);
69 @SuppressWarnings("unchecked")
70 private static final InstanceIdentifier<LabeledUnicastRoute> LABELED_IP4_ID =
71 TABLES.child((Class) LabeledUnicastRoutes.class).child(LabeledUnicastRoute.class);
74 private DataBroker bgpDataBroker;
75 private LocRibWriter ribWriter;
79 MockitoAnnotations.initMocks(this);
80 ribWriter = new LocRibWriter(bgpDataBroker);
83 @Test(expected = IllegalArgumentException.class)
84 public void testRegisterFailsForNonRoute() {
85 ribWriter.register(new NoopWriter(IP4_ROUTE_ID.child(Attributes.class)));
88 @Test(expected = IllegalArgumentException.class)
89 public void testRegisterFailsForNonLocRibRoute() {
90 ribWriter.register(new NoopWriter(InstanceIdentifier.create(Ipv4Route.class)));
93 @Test(expected = IllegalArgumentException.class)
94 public void testRegisterFailsForSpecificLocRibRoute() {
95 ribWriter.register(new NoopWriter(SPECIFIC_IP4_ROUTE_ID));
99 public void testRegisterIpv4RouteWriter() {
100 ribWriter.register(new NoopWriter(IP4_ROUTE_ID));
101 Mockito.verify(bgpDataBroker).registerDataTreeChangeListener(
102 ArgumentMatchers.eq(new DataTreeIdentifier<>(OPERATIONAL, IP4_ROUTE_ID)), ArgumentMatchers.any());
106 public void testRegisterLabeledIpv4RouteWriter() {
107 ribWriter.register(new NoopWriter(LABELED_IP4_ID));
108 Mockito.verify(bgpDataBroker).registerDataTreeChangeListener(
109 ArgumentMatchers.eq(new DataTreeIdentifier<>(OPERATIONAL, LABELED_IP4_ID)), ArgumentMatchers.any());
112 private static final class NoopWriter implements RouteWriter {
113 private final InstanceIdentifier<? extends Route> id;
115 private NoopWriter(@Nonnull final InstanceIdentifier id) {
119 @SuppressWarnings("unchecked")
122 public InstanceIdentifier getManagedDataObjectType() {
127 public void create(@Nonnull final InstanceIdentifier id, @Nonnull final Route dataAfter)
128 throws WriteFailedException.CreateFailedException {
132 public void delete(@Nonnull final InstanceIdentifier id, @Nonnull final Route dataBefore)
133 throws WriteFailedException.DeleteFailedException {
137 public void update(@Nonnull final InstanceIdentifier id, @Nonnull final Route dataBefore,
138 @Nonnull final Route dataAfter) throws WriteFailedException.UpdateFailedException {