Bump ODL dependencies to Fluorine (HONEYCOMB-433)
[honeycomb.git] / infra / bgp-translate-impl / src / main / test / java / io / fd / honeycomb / bgp / translate / impl / LocRibWriterTest.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.bgp.translate.impl;
18
19 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.OPERATIONAL;
20
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;
51
52 public class LocRibWriterTest {
53
54     private static final InstanceIdentifier<Tables> TABLES = InstanceIdentifier.create(BgpRib.class).child(Rib.class)
55         .child(LocRib.class).child(Tables.class);
56
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"));
64
65     @SuppressWarnings("unchecked")
66     private static final InstanceIdentifier<Ipv4Route> IP4_ROUTE_ID =
67         TABLES.child((Class) Ipv4Routes.class).child(Ipv4Route.class);
68
69     @SuppressWarnings("unchecked")
70     private static final InstanceIdentifier<LabeledUnicastRoute> LABELED_IP4_ID =
71         TABLES.child((Class) LabeledUnicastRoutes.class).child(LabeledUnicastRoute.class);
72
73     @Mock
74     private DataBroker bgpDataBroker;
75     private LocRibWriter ribWriter;
76
77     @Before
78     public void setUp() {
79         MockitoAnnotations.initMocks(this);
80         ribWriter = new LocRibWriter(bgpDataBroker);
81     }
82
83     @Test(expected = IllegalArgumentException.class)
84     public void testRegisterFailsForNonRoute() {
85         ribWriter.register(new NoopWriter(IP4_ROUTE_ID.child(Attributes.class)));
86     }
87
88     @Test(expected = IllegalArgumentException.class)
89     public void testRegisterFailsForNonLocRibRoute() {
90         ribWriter.register(new NoopWriter(InstanceIdentifier.create(Ipv4Route.class)));
91     }
92
93     @Test(expected = IllegalArgumentException.class)
94     public void testRegisterFailsForSpecificLocRibRoute() {
95         ribWriter.register(new NoopWriter(SPECIFIC_IP4_ROUTE_ID));
96     }
97
98     @Test
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());
103     }
104
105     @Test
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());
110     }
111
112     private static final class NoopWriter implements RouteWriter {
113         private final InstanceIdentifier<? extends Route> id;
114
115         private NoopWriter(@Nonnull final InstanceIdentifier id) {
116             this.id = id;
117         }
118
119         @SuppressWarnings("unchecked")
120         @Nonnull
121         @Override
122         public InstanceIdentifier getManagedDataObjectType() {
123             return id;
124         }
125
126         @Override
127         public void create(@Nonnull final InstanceIdentifier id, @Nonnull final Route dataAfter)
128             throws WriteFailedException.CreateFailedException {
129         }
130
131         @Override
132         public void delete(@Nonnull final InstanceIdentifier id, @Nonnull final Route dataBefore)
133             throws WriteFailedException.DeleteFailedException {
134         }
135
136         @Override
137         public void update(@Nonnull final InstanceIdentifier id, @Nonnull final Route dataBefore,
138                            @Nonnull final Route dataAfter) throws WriteFailedException.UpdateFailedException {
139         }
140     }
141 }