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.util.Ipv4Translator;
27 import io.fd.honeycomb.translate.v3po.util.NamingContext;
28 import io.fd.honeycomb.translate.write.WriteFailedException;
29 import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.mockito.ArgumentCaptor;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.Interface1;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.Ipv4;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.Neighbor;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.NeighborBuilder;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import org.openvpp.jvpp.core.dto.IpNeighborAddDel;
45 import org.openvpp.jvpp.core.dto.IpNeighborAddDelReply;
47 public class Ipv4NeighbourCustomizerTest extends WriterCustomizerTest implements Ipv4Translator {
49 private static final String IFC_CTX_NAME = "ifc-test-instance";
50 private static final String IFACE_NAME = "parent";
51 private static final int IFACE_ID = 5;
53 private ArgumentCaptor<IpNeighborAddDel> requestCaptor;
54 private Ipv4NeighbourCustomizer customizer;
58 defineMapping(mappingContext, IFACE_NAME, IFACE_ID, IFC_CTX_NAME);
59 customizer = new Ipv4NeighbourCustomizer(api, new NamingContext("prefix", IFC_CTX_NAME));
61 requestCaptor = ArgumentCaptor.forClass(IpNeighborAddDel.class);
62 when(api.ipNeighborAddDel(any())).thenReturn(future(new IpNeighborAddDelReply()));
66 public void testWriteCurrentAttributes() throws WriteFailedException {
68 InterfaceKey intfKey = new InterfaceKey(IFACE_NAME);
70 InstanceIdentifier<Neighbor> id = InstanceIdentifier.builder(Interfaces.class).child(Interface.class, intfKey)
71 .augmentation(Interface1.class).child(Ipv4.class).child(Neighbor.class).build();
73 Ipv4AddressNoZone noZoneIp = new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"));
74 PhysAddress mac = new PhysAddress("aa:bb:cc:ee:11:22");
76 Neighbor data = new NeighborBuilder().setIp(noZoneIp).setLinkLayerAddress(mac).build();
78 customizer.writeCurrentAttributes(id, data, writeContext);
80 verify(api, times(1)).ipNeighborAddDel(requestCaptor.capture());
82 IpNeighborAddDel request = requestCaptor.getValue();
84 assertEquals(0, request.isIpv6);
85 assertEquals(1, request.isAdd);
86 assertEquals(1, request.isStatic);
87 assertEquals("1.2.168.192", arrayToIpv4AddressNoZone(request.dstAddress).getValue());
88 assertEquals("aabbccee1122", BaseEncoding.base16().lowerCase().encode(request.macAddress));
89 assertEquals(5, request.swIfIndex);
93 public void testDeleteCurrentAttributes() throws WriteFailedException {
94 InterfaceKey intfKey = new InterfaceKey(IFACE_NAME);
96 InstanceIdentifier<Neighbor> id = InstanceIdentifier.builder(Interfaces.class).child(Interface.class, intfKey)
97 .augmentation(Interface1.class).child(Ipv4.class).child(Neighbor.class).build();
99 Ipv4AddressNoZone noZoneIp = new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"));
100 PhysAddress mac = new PhysAddress("aa:bb:cc:ee:11:22");
102 Neighbor data = new NeighborBuilder().setIp(noZoneIp).setLinkLayerAddress(mac).build();
104 customizer.deleteCurrentAttributes(id, data, writeContext);
106 verify(api, times(1)).ipNeighborAddDel(requestCaptor.capture());
108 IpNeighborAddDel request = requestCaptor.getValue();
110 assertEquals(0, request.isIpv6);
111 assertEquals(0, request.isAdd);
112 assertEquals(1, request.isStatic);
113 assertEquals("1.2.168.192", arrayToIpv4AddressNoZone(request.dstAddress).getValue());
114 assertEquals("aabbccee1122", BaseEncoding.base16().lowerCase().encode(request.macAddress));
115 assertEquals(5, request.swIfIndex);