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.Mockito.times;
21 import static org.mockito.Mockito.verify;
22 import static org.mockito.Mockito.when;
24 import com.google.common.base.Optional;
25 import com.google.common.io.BaseEncoding;
26 import io.fd.honeycomb.translate.MappingContext;
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.WriteContext;
30 import io.fd.honeycomb.translate.write.WriteFailedException;
31 import java.util.concurrent.CompletableFuture;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.ArgumentCaptor;
35 import org.mockito.Mock;
36 import org.mockito.Mockito;
37 import org.mockito.MockitoAnnotations;
38 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.Mapping;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.Interface1;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.Ipv4;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.Neighbor;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.NeighborBuilder;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
49 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50 import org.openvpp.jvpp.dto.IpNeighborAddDel;
51 import org.openvpp.jvpp.dto.IpNeighborAddDelReply;
52 import org.openvpp.jvpp.future.FutureJVpp;
54 public class Ipv4NeighbourCustomizerTest {
57 private FutureJVpp jvpp;
60 private WriteContext context;
63 private MappingContext mappingContext;
66 private Mapping mapping;
68 private ArgumentCaptor<IpNeighborAddDel> requestCaptor;
69 private Ipv4NeighbourCustomizer customizer;
70 private NamingContext namingContext;
75 MockitoAnnotations.initMocks(this);
77 namingContext = new NamingContext("prefix", "instance");
78 namingContext.addName(5, "parent", mappingContext);
80 customizer = new Ipv4NeighbourCustomizer(jvpp,namingContext);
82 requestCaptor = ArgumentCaptor.forClass(IpNeighborAddDel.class);
84 CompletableFuture<IpNeighborAddDelReply> future = new CompletableFuture<>();
85 future.complete(new IpNeighborAddDelReply());
87 when(context.getMappingContext()).thenReturn(mappingContext);
88 when(mapping.getIndex()).thenReturn(5);
89 when(mapping.getName()).thenReturn("parent");
90 when(mappingContext.read(Mockito.any())).thenReturn(Optional.fromNullable(mapping));
91 when(jvpp.ipNeighborAddDel(Mockito.any(IpNeighborAddDel.class))).thenReturn(future);
95 public void testWriteCurrentAttributes() throws WriteFailedException {
97 InterfaceKey intfKey = new InterfaceKey("parent");
99 InstanceIdentifier<Neighbor> id = InstanceIdentifier.builder(Interfaces.class).child(Interface.class, intfKey)
100 .augmentation(Interface1.class).child(Ipv4.class).child(Neighbor.class).build();
102 Ipv4AddressNoZone noZoneIp = new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"));
103 PhysAddress mac = new PhysAddress("aa:bb:cc:ee:11:22");
105 Neighbor data = new NeighborBuilder().setIp(noZoneIp).setLinkLayerAddress(mac).build();
107 customizer.writeCurrentAttributes(id, data, context);
109 verify(jvpp, times(1)).ipNeighborAddDel(requestCaptor.capture());
111 IpNeighborAddDel request = requestCaptor.getValue();
113 assertEquals(0, request.isIpv6);
114 assertEquals(1, request.isAdd);
115 assertEquals(1, request.isStatic);
116 assertEquals("1.2.168.192", TranslateUtils.arrayToIpv4AddressNoZone(request.dstAddress).getValue());
117 assertEquals("aabbccee1122", BaseEncoding.base16().lowerCase().encode(request.macAddress));
118 assertEquals(5, request.swIfIndex);
122 public void testDeleteCurrentAttributes() throws WriteFailedException {
123 InterfaceKey intfKey = new InterfaceKey("parent");
125 InstanceIdentifier<Neighbor> id = InstanceIdentifier.builder(Interfaces.class).child(Interface.class, intfKey)
126 .augmentation(Interface1.class).child(Ipv4.class).child(Neighbor.class).build();
128 Ipv4AddressNoZone noZoneIp = new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"));
129 PhysAddress mac = new PhysAddress("aa:bb:cc:ee:11:22");
131 Neighbor data = new NeighborBuilder().setIp(noZoneIp).setLinkLayerAddress(mac).build();
133 customizer.deleteCurrentAttributes(id, data, context);
135 verify(jvpp, times(1)).ipNeighborAddDel(requestCaptor.capture());
137 IpNeighborAddDel request = requestCaptor.getValue();
139 assertEquals(0, request.isIpv6);
140 assertEquals(0, request.isAdd);
141 assertEquals(1, request.isStatic);
142 assertEquals("1.2.168.192", TranslateUtils.arrayToIpv4AddressNoZone(request.dstAddress).getValue());
143 assertEquals("aabbccee1122", BaseEncoding.base16().lowerCase().encode(request.macAddress));
144 assertEquals(5, request.swIfIndex);