2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.translate.v3po.interfaces.ip;
19 import static org.junit.Assert.assertEquals;
20 import static org.mockito.Matchers.any;
21 import static org.mockito.Mockito.times;
22 import static org.mockito.Mockito.verify;
23 import static org.mockito.Mockito.when;
25 import com.google.common.io.BaseEncoding;
26 import io.fd.honeycomb.translate.v3po.test.ContextTestUtils;
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 io.fd.honeycomb.vpp.test.write.WriterCustomizerTest;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.mockito.ArgumentCaptor;
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.Ipv4AddressNoZone;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.Interface1;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.Ipv4;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.Neighbor;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.NeighborBuilder;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 import org.openvpp.jvpp.core.dto.IpNeighborAddDel;
46 import org.openvpp.jvpp.core.dto.IpNeighborAddDelReply;
48 public class Ipv4NeighbourCustomizerTest extends WriterCustomizerTest {
50 private static final String IFC_CTX_NAME = "ifc-test-instance";
51 private static final String IFACE_NAME = "parent";
52 private static final int IFACE_ID = 5;
54 private ArgumentCaptor<IpNeighborAddDel> requestCaptor;
55 private Ipv4NeighbourCustomizer customizer;
59 ContextTestUtils.mockMapping(mappingContext, IFACE_NAME, IFACE_ID, IFC_CTX_NAME);
60 customizer = new Ipv4NeighbourCustomizer(api, new NamingContext("prefix", IFC_CTX_NAME));
62 requestCaptor = ArgumentCaptor.forClass(IpNeighborAddDel.class);
63 when(api.ipNeighborAddDel(any())).thenReturn(future(new IpNeighborAddDelReply()));
67 public void testWriteCurrentAttributes() throws WriteFailedException {
69 InterfaceKey intfKey = new InterfaceKey(IFACE_NAME);
71 InstanceIdentifier<Neighbor> id = InstanceIdentifier.builder(Interfaces.class).child(Interface.class, intfKey)
72 .augmentation(Interface1.class).child(Ipv4.class).child(Neighbor.class).build();
74 Ipv4AddressNoZone noZoneIp = new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"));
75 PhysAddress mac = new PhysAddress("aa:bb:cc:ee:11:22");
77 Neighbor data = new NeighborBuilder().setIp(noZoneIp).setLinkLayerAddress(mac).build();
79 customizer.writeCurrentAttributes(id, data, writeContext);
81 verify(api, times(1)).ipNeighborAddDel(requestCaptor.capture());
83 IpNeighborAddDel request = requestCaptor.getValue();
85 assertEquals(0, request.isIpv6);
86 assertEquals(1, request.isAdd);
87 assertEquals(1, request.isStatic);
88 assertEquals("1.2.168.192", TranslateUtils.arrayToIpv4AddressNoZone(request.dstAddress).getValue());
89 assertEquals("aabbccee1122", BaseEncoding.base16().lowerCase().encode(request.macAddress));
90 assertEquals(5, request.swIfIndex);
94 public void testDeleteCurrentAttributes() throws WriteFailedException {
95 InterfaceKey intfKey = new InterfaceKey(IFACE_NAME);
97 InstanceIdentifier<Neighbor> id = InstanceIdentifier.builder(Interfaces.class).child(Interface.class, intfKey)
98 .augmentation(Interface1.class).child(Ipv4.class).child(Neighbor.class).build();
100 Ipv4AddressNoZone noZoneIp = new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"));
101 PhysAddress mac = new PhysAddress("aa:bb:cc:ee:11:22");
103 Neighbor data = new NeighborBuilder().setIp(noZoneIp).setLinkLayerAddress(mac).build();
105 customizer.deleteCurrentAttributes(id, data, writeContext);
107 verify(api, times(1)).ipNeighborAddDel(requestCaptor.capture());
109 IpNeighborAddDel request = requestCaptor.getValue();
111 assertEquals(0, request.isIpv6);
112 assertEquals(0, request.isAdd);
113 assertEquals(1, request.isStatic);
114 assertEquals("1.2.168.192", TranslateUtils.arrayToIpv4AddressNoZone(request.dstAddress).getValue());
115 assertEquals("aabbccee1122", BaseEncoding.base16().lowerCase().encode(request.macAddress));
116 assertEquals(5, request.swIfIndex);