43ec73bd1639f0bf625c8a570a5d021c4529a022
[hc2vpp.git] /
1 /*
2  * Copyright (c) 2016 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.hc2vpp.routing.read;
18
19 import static io.fd.hc2vpp.routing.helpers.RoutingRequestTestHelper.ROUTE_PROTOCOL_NAME;
20 import static org.hamcrest.MatcherAssert.assertThat;
21 import static org.hamcrest.Matchers.hasSize;
22 import static org.hamcrest.core.IsCollectionContaining.hasItems;
23 import static org.junit.Assert.assertEquals;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.Mockito.when;
26
27 import io.fd.hc2vpp.common.test.read.ListReaderCustomizerTest;
28 import io.fd.hc2vpp.common.translate.util.ByteDataTranslator;
29 import io.fd.hc2vpp.common.translate.util.NamingContext;
30 import io.fd.hc2vpp.routing.Ipv4RouteData;
31 import io.fd.hc2vpp.routing.Ipv6RouteData;
32 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
33 import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager;
34 import io.fd.honeycomb.translate.util.read.cache.EntityDumpExecutor;
35 import io.fd.vpp.jvpp.core.dto.Ip6FibDetails;
36 import io.fd.vpp.jvpp.core.dto.Ip6FibDetailsReplyDump;
37 import io.fd.vpp.jvpp.core.dto.IpFibDetails;
38 import io.fd.vpp.jvpp.core.dto.IpFibDetailsReplyDump;
39 import io.fd.vpp.jvpp.core.types.FibPath;
40 import java.util.Arrays;
41 import java.util.List;
42 import org.junit.Test;
43 import org.mockito.Mock;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.routing.rev180313.Static;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.routing.rev180313.routing.ControlPlaneProtocols;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.routing.rev180313.routing.ControlPlaneProtocolsBuilder;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.routing.rev180313.routing.control.plane.protocols.ControlPlaneProtocol;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.routing.rev180313.routing.control.plane.protocols.ControlPlaneProtocolBuilder;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.routing.rev180313.routing.control.plane.protocols.ControlPlaneProtocolKey;
50 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
51
52 public class ControlPlaneProtocolCustomizerTest
53         extends ListReaderCustomizerTest<ControlPlaneProtocol, ControlPlaneProtocolKey, ControlPlaneProtocolBuilder>
54         implements ByteDataTranslator {
55
56     private static final String VPP_PROTOCOL_PREFIX = "vpp-protocol";
57
58     @Mock
59     private EntityDumpExecutor<IpFibDetailsReplyDump, Void> ipv4Executor;
60
61     @Mock
62     private EntityDumpExecutor<Ip6FibDetailsReplyDump, Void> ipv6Executor;
63     private DumpCacheManager<IpFibDetailsReplyDump, Void> ipv4RoutesDumpManager;
64     private DumpCacheManager<Ip6FibDetailsReplyDump, Void> ipv6RoutesDumpManager;
65
66     private NamingContext routingProtocolContext;
67
68     public ControlPlaneProtocolCustomizerTest() { super(ControlPlaneProtocol.class, ControlPlaneProtocolsBuilder.class);
69     }
70
71     @Override
72     protected void setUp() throws Exception {
73         when(ipv4Executor.executeDump(any(), any())).thenReturn(replyDumpIpv4());
74         when(ipv6Executor.executeDump(any(), any())).thenReturn(replyDumpIpv6());
75         when(ctx.getModificationCache()).thenReturn(cache);
76
77         ipv4RoutesDumpManager = new DumpCacheManager.DumpCacheManagerBuilder<IpFibDetailsReplyDump, Void>()
78                 .withExecutor(ipv4Executor)
79                 .acceptOnly(IpFibDetailsReplyDump.class)
80                 .build();
81
82         ipv6RoutesDumpManager = new DumpCacheManager.DumpCacheManagerBuilder<Ip6FibDetailsReplyDump, Void>()
83                 .withExecutor(ipv6Executor)
84                 .acceptOnly(Ip6FibDetailsReplyDump.class)
85                 .build();
86
87         routingProtocolContext = new NamingContext("routing-protocol", "routing-protocol-context");
88         defineMapping(mappingContext, ROUTE_PROTOCOL_NAME, 1, "routing-protocol-context");
89         defineMapping(mappingContext, "tst-protocol-2", 2, "routing-protocol-context");
90         defineMapping(mappingContext, "tst-protocol-3", 3, "routing-protocol-context");
91     }
92
93     @Test
94     public void getAllIds() throws Exception {
95         final List<ControlPlaneProtocolKey> keys =
96                 getCustomizer().getAllIds(InstanceIdentifier.create(ControlPlaneProtocol.class), ctx);
97
98         assertThat(keys, hasSize(3));
99         assertThat(keys, hasItems(new ControlPlaneProtocolKey(ROUTE_PROTOCOL_NAME, Static.class),
100                                   new ControlPlaneProtocolKey("tst-protocol-2", Static.class),
101                                   new ControlPlaneProtocolKey("tst-protocol-3", Static.class)));
102     }
103
104     @Test
105     public void readCurrentAttributes() throws Exception {
106         final InstanceIdentifier<ControlPlaneProtocol> identifier =
107             InstanceIdentifier.create(ControlPlaneProtocols.class)
108                 .child(ControlPlaneProtocol.class, new ControlPlaneProtocolKey(ROUTE_PROTOCOL_NAME, Static.class));
109
110         final ControlPlaneProtocolBuilder builder = new ControlPlaneProtocolBuilder();
111         getCustomizer().readCurrentAttributes(identifier, builder, ctx);
112
113         assertEquals(ROUTE_PROTOCOL_NAME, builder.getName());
114         assertEquals(ROUTE_PROTOCOL_NAME, builder.getKey().getName());
115         assertEquals(Static.class, builder.getType());
116     }
117
118     @Override
119     protected ReaderCustomizer<ControlPlaneProtocol, ControlPlaneProtocolBuilder> initCustomizer() {
120         return new ControlPlaneProtocolCustomizer(routingProtocolContext, ipv4RoutesDumpManager, ipv6RoutesDumpManager);
121     }
122
123     private Ip6FibDetailsReplyDump replyDumpIpv6() {
124         Ip6FibDetailsReplyDump replyDump = new Ip6FibDetailsReplyDump();
125
126         // first is special
127         Ip6FibDetails detail1 = new Ip6FibDetails();
128         detail1.tableId = 1;
129         detail1.address = Ipv6RouteData.FIRST_ADDRESS_AS_ARRAY;
130         detail1.addressLength = 24;
131
132         FibPath path1 = new FibPath();
133         path1.isLocal = 1;
134         detail1.path = new FibPath[]{path1};
135
136
137         //second is simple
138         Ip6FibDetails detail2 = new Ip6FibDetails();
139         detail2.tableId = 2;
140         detail2.address = Ipv6RouteData.SECOND_ADDRESS_AS_ARRAY;
141         detail2.addressLength = 22;
142         detail2.path = new FibPath[]{};
143
144         FibPath path2 = new FibPath();
145         path2.weight = 3;
146         path2.nextHop = Ipv6RouteData.FIRST_ADDRESS_AS_ARRAY;
147         path2.afi = 0;
148         path2.swIfIndex = 1;
149         detail2.path = new FibPath[]{path2};
150
151         // third is list
152         Ip6FibDetails detail3 = new Ip6FibDetails();
153         detail3.tableId = 1;
154         detail3.address = Ipv6RouteData.SECOND_ADDRESS_AS_ARRAY;
155         detail3.addressLength = 16;
156
157         FibPath path3 = new FibPath();
158         path3.swIfIndex = 1;
159         path3.weight = 1;
160         path3.nextHop = Ipv6RouteData.FIRST_ADDRESS_AS_ARRAY;
161         path3.afi = 0;
162
163         FibPath path4 = new FibPath();
164         path4.swIfIndex = 1;
165         path4.weight = 2;
166         path4.nextHop = Ipv6RouteData.SECOND_ADDRESS_AS_ARRAY;
167         path4.afi = 0;
168
169         detail3.path = new FibPath[]{path3, path4};
170
171         replyDump.ip6FibDetails = Arrays.asList(detail1, detail2, detail3);
172         return replyDump;
173     }
174
175     private IpFibDetailsReplyDump replyDumpIpv4() {
176         IpFibDetailsReplyDump replyDump = new IpFibDetailsReplyDump();
177
178         // first is special
179         IpFibDetails detail1 = new IpFibDetails();
180         detail1.tableId = 1;
181         detail1.address = Ipv4RouteData.FIRST_ADDRESS_AS_ARRAY;
182         detail1.addressLength = 24;
183
184         FibPath path1 = new FibPath();
185         path1.isLocal = 1;
186         detail1.path = new FibPath[]{path1};
187
188
189         //second is simple
190         IpFibDetails detail2 = new IpFibDetails();
191         detail2.tableId = 3;
192         detail2.address = Ipv4RouteData.SECOND_ADDRESS_AS_ARRAY;
193         detail2.addressLength = 22;
194         detail2.path = new FibPath[]{};
195
196         FibPath path2 = new FibPath();
197         path2.weight = 3;
198         path2.nextHop = Ipv4RouteData.FIRST_ADDRESS_AS_ARRAY;
199         path2.afi = 0;
200         path2.swIfIndex = 1;
201         detail2.path = new FibPath[]{path2};
202
203         // third is list
204         IpFibDetails detail3 = new IpFibDetails();
205         detail3.tableId = 1;
206         detail3.address = Ipv4RouteData.SECOND_ADDRESS_AS_ARRAY;
207         detail3.addressLength = 16;
208
209         FibPath path3 = new FibPath();
210         path3.swIfIndex = 1;
211         path3.weight = 1;
212         path3.nextHop = Ipv4RouteData.FIRST_ADDRESS_AS_ARRAY;
213         path3.afi = 0;
214
215         FibPath path4 = new FibPath();
216         path4.swIfIndex = 1;
217         path4.weight = 2;
218         path4.nextHop = Ipv4RouteData.SECOND_ADDRESS_AS_ARRAY;
219         path4.afi = 0;
220
221         detail3.path = new FibPath[]{path3, path4};
222
223         replyDump.ipFibDetails = Arrays.asList(detail1, detail2, detail3);
224         return replyDump;
225     }
226 }