dcde0b43d0edd1db76aae72267cdf31bef32cc93
[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.honeycomb.translate.v3po.interfaces.ip;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.mockito.Mockito.times;
21 import static org.mockito.Mockito.verify;
22 import static org.mockito.Mockito.when;
23
24 import com.google.common.io.BaseEncoding;
25 import io.fd.honeycomb.translate.v3po.test.ContextTestUtils;
26 import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest;
27 import io.fd.honeycomb.translate.v3po.util.NamingContext;
28 import io.fd.honeycomb.translate.v3po.util.TranslateUtils;
29 import io.fd.honeycomb.translate.write.WriteFailedException;
30 import java.util.concurrent.CompletableFuture;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.mockito.ArgumentCaptor;
34 import org.mockito.Mockito;
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.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.Interface1;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.Ipv4;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.Neighbor;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.NeighborBuilder;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import org.openvpp.jvpp.core.dto.IpNeighborAddDel;
47 import org.openvpp.jvpp.core.dto.IpNeighborAddDelReply;
48
49 public class Ipv4NeighbourCustomizerTest extends WriterCustomizerTest {
50
51     private static final String IFC_CTX_NAME = "ifc-test-instance";
52     private static final String IFACE_NAME = "parent";
53     private static final int IFACE_ID = 5;
54
55     private ArgumentCaptor<IpNeighborAddDel> requestCaptor;
56     private Ipv4NeighbourCustomizer customizer;
57
58     @Before
59     public void init() {
60         ContextTestUtils.mockMapping(mappingContext, IFACE_NAME, IFACE_ID, IFC_CTX_NAME);
61         customizer = new Ipv4NeighbourCustomizer(api, new NamingContext("prefix", IFC_CTX_NAME));
62
63         requestCaptor = ArgumentCaptor.forClass(IpNeighborAddDel.class);
64         CompletableFuture<IpNeighborAddDelReply> future = new CompletableFuture<>();
65         future.complete(new IpNeighborAddDelReply());
66         when(api.ipNeighborAddDel(Mockito.any(IpNeighborAddDel.class))).thenReturn(future);
67     }
68
69     @Test
70     public void testWriteCurrentAttributes() throws WriteFailedException {
71
72         InterfaceKey intfKey = new InterfaceKey(IFACE_NAME);
73
74         InstanceIdentifier<Neighbor> id = InstanceIdentifier.builder(Interfaces.class).child(Interface.class, intfKey)
75                 .augmentation(Interface1.class).child(Ipv4.class).child(Neighbor.class).build();
76
77         Ipv4AddressNoZone noZoneIp = new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"));
78         PhysAddress mac = new PhysAddress("aa:bb:cc:ee:11:22");
79
80         Neighbor data = new NeighborBuilder().setIp(noZoneIp).setLinkLayerAddress(mac).build();
81
82         customizer.writeCurrentAttributes(id, data, writeContext);
83
84         verify(api, times(1)).ipNeighborAddDel(requestCaptor.capture());
85
86         IpNeighborAddDel request = requestCaptor.getValue();
87
88         assertEquals(0, request.isIpv6);
89         assertEquals(1, request.isAdd);
90         assertEquals(1, request.isStatic);
91         assertEquals("1.2.168.192", TranslateUtils.arrayToIpv4AddressNoZone(request.dstAddress).getValue());
92         assertEquals("aabbccee1122", BaseEncoding.base16().lowerCase().encode(request.macAddress));
93         assertEquals(5, request.swIfIndex);
94     }
95
96     @Test
97     public void testDeleteCurrentAttributes() throws WriteFailedException {
98         InterfaceKey intfKey = new InterfaceKey(IFACE_NAME);
99
100         InstanceIdentifier<Neighbor> id = InstanceIdentifier.builder(Interfaces.class).child(Interface.class, intfKey)
101                 .augmentation(Interface1.class).child(Ipv4.class).child(Neighbor.class).build();
102
103         Ipv4AddressNoZone noZoneIp = new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"));
104         PhysAddress mac = new PhysAddress("aa:bb:cc:ee:11:22");
105
106         Neighbor data = new NeighborBuilder().setIp(noZoneIp).setLinkLayerAddress(mac).build();
107
108         customizer.deleteCurrentAttributes(id, data, writeContext);
109
110         verify(api, times(1)).ipNeighborAddDel(requestCaptor.capture());
111
112         IpNeighborAddDel request = requestCaptor.getValue();
113
114         assertEquals(0, request.isIpv6);
115         assertEquals(0, request.isAdd);
116         assertEquals(1, request.isStatic);
117         assertEquals("1.2.168.192", TranslateUtils.arrayToIpv4AddressNoZone(request.dstAddress).getValue());
118         assertEquals("aabbccee1122", BaseEncoding.base16().lowerCase().encode(request.macAddress));
119         assertEquals(5, request.swIfIndex);
120     }
121
122 }