f96942be1d1f2ff75c705b565e2c049c8c70a97f
[hc2vpp.git] /
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.hc2vpp.lisp.gpe.translate.read;
18
19 import static io.fd.hc2vpp.lisp.gpe.translate.read.NativeForwardPathsTableCustomizerTest.TABLE_0_IDX;
20 import static io.fd.hc2vpp.lisp.gpe.translate.read.NativeForwardPathsTableCustomizerTest.TABLE_1_IDX;
21 import static io.fd.hc2vpp.lisp.gpe.translate.read.NativeForwardPathsTableCustomizerTest.TABLE_2_IDX;
22 import static org.junit.Assert.assertEquals;
23 import static org.mockito.Mockito.when;
24
25 import io.fd.hc2vpp.common.test.read.InitializingListReaderCustomizerTest;
26 import io.fd.hc2vpp.common.translate.util.NamingContext;
27 import io.fd.honeycomb.translate.read.ReadFailedException;
28 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
29 import io.fd.vpp.jvpp.core.dto.GpeNativeFwdRpathsGet;
30 import io.fd.vpp.jvpp.core.dto.GpeNativeFwdRpathsGetReply;
31 import io.fd.vpp.jvpp.core.types.GpeNativeFwdRpath;
32 import java.util.List;
33 import org.junit.Test;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.gpe.rev170801.NativeForwardPathsTablesState;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.gpe.rev170801._native.forward.paths.tables.state.NativeForwardPathsTable;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.gpe.rev170801._native.forward.paths.tables.state.NativeForwardPathsTableBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.gpe.rev170801._native.forward.paths.tables.state.NativeForwardPathsTableKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.gpe.rev170801._native.forward.paths.tables.state._native.forward.paths.table.NativeForwardPath;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.gpe.rev170801._native.forward.paths.tables.state._native.forward.paths.table.NativeForwardPathBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.gpe.rev170801._native.forward.paths.tables.state._native.forward.paths.table.NativeForwardPathKey;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
45
46 public class NativeForwardPathCustomizerTest
47         extends
48         InitializingListReaderCustomizerTest<NativeForwardPath, NativeForwardPathKey, NativeForwardPathBuilder> {
49
50     private static final String IFC_CTX = "ifc-ctx";
51     private static final String ETH_0 = "eth-0";
52     private static final String ETH_1 = "eth-1";
53     private static final int ETH_0_IDX = 2;
54     private static final int ETH_1_IDX = 7;
55
56     private NamingContext interfaceContext;
57     private KeyedInstanceIdentifier<NativeForwardPath, NativeForwardPathKey> validId;
58     private KeyedInstanceIdentifier<NativeForwardPath, NativeForwardPathKey> defaultTableId;
59
60     public NativeForwardPathCustomizerTest() {
61         super(NativeForwardPath.class, NativeForwardPathsTableBuilder.class);
62     }
63
64     @Override
65     protected void setUp() throws Exception {
66         interfaceContext = new NamingContext("iface", IFC_CTX);
67         final GpeNativeFwdRpathsGet requestV4 = new GpeNativeFwdRpathsGet();
68         requestV4.isIp4 = 1;
69         final GpeNativeFwdRpathsGet requestV6 = new GpeNativeFwdRpathsGet();
70         requestV6.isIp4 = 0;
71         when(api.gpeNativeFwdRpathsGet(requestV4)).thenReturn(future(getReplyV4()));
72         when(api.gpeNativeFwdRpathsGet(requestV6)).thenReturn(future(getReplyV6()));
73         validId = InstanceIdentifier.create(NativeForwardPathsTablesState.class)
74                 .child(NativeForwardPathsTable.class, new NativeForwardPathsTableKey((long) TABLE_0_IDX))
75                 .child(NativeForwardPath.class,
76                         new NativeForwardPathKey(new IpAddress(new Ipv4Address("192.168.2.1"))));
77         defaultTableId = InstanceIdentifier.create(NativeForwardPathsTablesState.class)
78                 .child(NativeForwardPathsTable.class, new NativeForwardPathsTableKey((long) 0))
79                 .child(NativeForwardPath.class,
80                         new NativeForwardPathKey(new IpAddress(new Ipv4Address("192.168.2.7"))));
81         defineMapping(mappingContext, ETH_0, ETH_0_IDX, IFC_CTX);
82         defineMapping(mappingContext, ETH_1, ETH_1_IDX, IFC_CTX);
83     }
84
85     @Test
86     public void testGetAll() throws ReadFailedException {
87         final List<NativeForwardPathKey> allIds = getCustomizer().getAllIds(validId, ctx);
88         assertEquals(2, allIds.size());
89         final String firstAddress = allIds.get(0).getNextHopAddress().getIpv6Address().getValue();
90         final String secondAddress = allIds.get(1).getNextHopAddress().getIpv4Address().getValue();
91         assertEquals("2001:db8:a0b:12f0::1", firstAddress);
92         assertEquals("192.168.2.1", secondAddress);
93     }
94
95     @Test
96     public void testGetAllDefaultTable() throws ReadFailedException {
97         final List<NativeForwardPathKey> allIds = getCustomizer().getAllIds(defaultTableId, ctx);
98         assertEquals(1, allIds.size());
99         final String firstAddress = allIds.get(0).getNextHopAddress().getIpv4Address().getValue();
100         assertEquals("192.168.3.7", firstAddress);
101     }
102
103     @Test
104     public void testReadCurrent() throws ReadFailedException {
105         final NativeForwardPathBuilder builder = new NativeForwardPathBuilder();
106         getCustomizer().readCurrentAttributes(validId, builder, ctx);
107
108         assertEquals("192.168.2.1", builder.getNextHopAddress().getIpv4Address().getValue());
109         assertEquals(ETH_1, builder.getNextHopInterface());
110     }
111
112     private GpeNativeFwdRpathsGetReply getReplyV4() {
113         GpeNativeFwdRpathsGetReply reply = new GpeNativeFwdRpathsGetReply();
114         GpeNativeFwdRpath table0Path1 = new GpeNativeFwdRpath();
115         table0Path1.fibIndex = TABLE_0_IDX;
116         table0Path1.nhAddr = new byte[]{-64, -88, 2, 1};
117         table0Path1.isIp4 = 1;
118         table0Path1.nhSwIfIndex = ETH_1_IDX;
119         GpeNativeFwdRpath table2Path1 = new GpeNativeFwdRpath();
120         table2Path1.fibIndex = TABLE_2_IDX;
121         table2Path1.nhAddr = new byte[]{-64, -88, 3, 2};
122         table2Path1.isIp4 = 1;
123         GpeNativeFwdRpath tableDefaultPath1 = new GpeNativeFwdRpath();
124         tableDefaultPath1.fibIndex = ~0;
125         tableDefaultPath1.nhAddr = new byte[]{-64, -88, 3, 7};
126         tableDefaultPath1.isIp4 = 1;
127
128         reply.entries = new GpeNativeFwdRpath[]{table0Path1, table2Path1, tableDefaultPath1};
129         return reply;
130     }
131
132     private GpeNativeFwdRpathsGetReply getReplyV6() {
133         GpeNativeFwdRpathsGetReply reply = new GpeNativeFwdRpathsGetReply();
134         GpeNativeFwdRpath table0Path2 = new GpeNativeFwdRpath();
135         table0Path2.fibIndex = TABLE_0_IDX;
136         table0Path2.nhAddr = new byte[]{32, 1, 13, -72, 10, 11, 18, -16, 0, 0, 0, 0, 0, 0, 0, 1};
137         table0Path2.isIp4 = 0;
138         GpeNativeFwdRpath table1Path1 = new GpeNativeFwdRpath();
139         table1Path1.fibIndex = TABLE_1_IDX;
140         table1Path1.nhAddr = new byte[]{32, 1, 13, -72, 10, 11, 18, -16, 0, 0, 0, 0, 0, 0, 3, 2};
141         table1Path1.isIp4 = 0;
142         table1Path1.nhSwIfIndex = ETH_0_IDX;
143         reply.entries = new GpeNativeFwdRpath[]{table0Path2, table1Path1};
144         return reply;
145     }
146
147     @Override
148     protected ReaderCustomizer<NativeForwardPath, NativeForwardPathBuilder> initCustomizer() {
149         return new NativeForwardPathCustomizer(api, interfaceContext);
150     }
151 }