26010ba17c7bee37acc503bc72ea38cf32821adb
[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.write;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.mockito.ArgumentMatchers.any;
21 import static org.mockito.Mockito.times;
22 import static org.mockito.Mockito.verify;
23 import static org.mockito.Mockito.when;
24
25 import io.fd.hc2vpp.common.test.write.WriterCustomizerTest;
26 import io.fd.hc2vpp.common.translate.util.NamingContext;
27 import io.fd.honeycomb.translate.write.WriteFailedException;
28 import io.fd.vpp.jvpp.core.dto.GpeAddDelNativeFwdRpath;
29 import io.fd.vpp.jvpp.core.dto.GpeAddDelNativeFwdRpathReply;
30 import org.junit.Test;
31 import org.mockito.ArgumentCaptor;
32 import org.mockito.Captor;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.gpe.rev170801.NativeForwardPathsTables;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.gpe.rev170801._native.forward.paths.tables.NativeForwardPathsTable;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.gpe.rev170801._native.forward.paths.tables.NativeForwardPathsTableKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.gpe.rev170801._native.forward.paths.tables._native.forward.paths.table.NativeForwardPath;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.gpe.rev170801._native.forward.paths.tables._native.forward.paths.table.NativeForwardPathBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.gpe.rev170801._native.forward.paths.tables._native.forward.paths.table.NativeForwardPathKey;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43
44 public class NativeForwardPathCustomizerTest extends WriterCustomizerTest {
45
46     private static final long TABLE_ID = 1L;
47     private static final String IFC_CTX = "ifc-ctx";
48     private static final String ETH_0 = "eth-0";
49     private static final String ETH_1 = "eth-1";
50     private static final int ETH_0_IDX = 2;
51     private static final int ETH_1_IDX = 7;
52     private static final byte[] V4_WITH_IF_ADDR = {-64, -88, 2, 1};
53     private static final byte[] V4_WITHOUT_IF_ADDR = {-64, -88, 2, 3};
54     private static final byte[] V6_WITH_IF_ADDR = {32, 1, 13, -72, 10, 11, 18, -16, 0, 0, 0, 0, 0, 0, 0, 1};
55
56     private NamingContext ifcCtx;
57     private NativeForwardPathCustomizer customizer;
58     private InstanceIdentifier<NativeForwardPath> validId;
59
60     @Captor
61     private ArgumentCaptor<GpeAddDelNativeFwdRpath> requestCaptor;
62
63     @Override
64     protected void setUpTest() throws Exception {
65         ifcCtx = new NamingContext("iface", IFC_CTX);
66         defineMapping(mappingContext, ETH_0, ETH_0_IDX, IFC_CTX);
67         defineMapping(mappingContext, ETH_1, ETH_1_IDX, IFC_CTX);
68         customizer = new NativeForwardPathCustomizer(api, ifcCtx);
69         validId = InstanceIdentifier.create(NativeForwardPathsTables.class)
70                 .child(NativeForwardPathsTable.class, new NativeForwardPathsTableKey(TABLE_ID))
71                 .child(NativeForwardPath.class,
72                         new NativeForwardPathKey(new IpAddress(new Ipv4Address("192.168.2.1"))));
73         when(api.gpeAddDelNativeFwdRpath(any())).thenReturn(future(new GpeAddDelNativeFwdRpathReply()));
74     }
75
76     @Test
77     public void testWriteV4WithIfc() throws WriteFailedException {
78         customizer.writeCurrentAttributes(validId, v4WithIfc(), writeContext);
79         verify(api, times(1)).gpeAddDelNativeFwdRpath(requestCaptor.capture());
80         final GpeAddDelNativeFwdRpath request = requestCaptor.getValue();
81         assertEquals(desiredRequest(1, 1, V4_WITH_IF_ADDR, ETH_0_IDX, (int) TABLE_ID), request);
82     }
83
84     @Test
85     public void testWriteV4WithoutIfc() throws WriteFailedException {
86         customizer.writeCurrentAttributes(validId, v4WithoutIfc(), writeContext);
87         verify(api, times(1)).gpeAddDelNativeFwdRpath(requestCaptor.capture());
88         final GpeAddDelNativeFwdRpath request = requestCaptor.getValue();
89         assertEquals(desiredRequest(1, 1, V4_WITHOUT_IF_ADDR, ~0, (int) TABLE_ID), request);
90     }
91
92     @Test
93     public void testWriteV6() throws WriteFailedException {
94         customizer.writeCurrentAttributes(validId, v6WithIfc(), writeContext);
95         verify(api, times(1)).gpeAddDelNativeFwdRpath(requestCaptor.capture());
96         final GpeAddDelNativeFwdRpath request = requestCaptor.getValue();
97         assertEquals(desiredRequest(1, 0, V6_WITH_IF_ADDR, ETH_1_IDX, (int) TABLE_ID), request);
98     }
99
100     private static GpeAddDelNativeFwdRpath desiredRequest(final int add, final int isV4,
101                                                           final byte[] addr, final int swIfIndex,
102                                                           final int tableId) {
103         GpeAddDelNativeFwdRpath request = new GpeAddDelNativeFwdRpath();
104         request.isAdd = (byte) add;
105         request.isIp4 = (byte) isV4;
106         request.nhAddr = addr;
107         request.nhSwIfIndex = swIfIndex;
108         request.tableId = tableId;
109
110         return request;
111     }
112
113     private static NativeForwardPath v6WithIfc() {
114         return new NativeForwardPathBuilder()
115                 .setNextHopAddress(new IpAddress(new Ipv6Address("2001:0db8:0a0b:12f0:0000:0000:0000:0001")))
116                 .setNextHopInterface(ETH_1)
117                 .build();
118     }
119
120     private static NativeForwardPath v4WithoutIfc() {
121         return new NativeForwardPathBuilder()
122                 .setNextHopAddress(new IpAddress(new Ipv4Address("192.168.2.3")))
123                 .build();
124     }
125
126     private static NativeForwardPath v4WithIfc() {
127         return new NativeForwardPathBuilder()
128                 .setNextHopAddress(new IpAddress(new Ipv4Address("192.168.2.1")))
129                 .setNextHopInterface(ETH_0)
130                 .build();
131     }
132 }