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.v3po.translate.v3po.interfaces.ip;
19 import static org.junit.Assert.assertEquals;
20 import static org.mockito.Mockito.mock;
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.base.Optional;
26 import com.google.common.io.BaseEncoding;
27 import io.fd.honeycomb.v3po.translate.MappingContext;
28 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
29 import io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils;
30 import io.fd.honeycomb.v3po.translate.write.WriteContext;
31 import io.fd.honeycomb.v3po.translate.write.WriteFailedException;
32 import java.util.Collections;
33 import java.util.List;
34 import java.util.concurrent.CompletableFuture;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.mockito.ArgumentCaptor;
38 import org.mockito.Mock;
39 import org.mockito.Mockito;
40 import org.mockito.MockitoAnnotations;
41 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.Mapping;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.Interface1;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.Ipv4;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.Ipv4Builder;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.Neighbor;
51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.NeighborBuilder;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
53 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
54 import org.openvpp.jvpp.dto.IpNeighborAddDel;
55 import org.openvpp.jvpp.dto.IpNeighborAddDelReply;
56 import org.openvpp.jvpp.future.FutureJVpp;
58 public class Ipv4NeighbourCustomizerTest {
61 private FutureJVpp jvpp;
64 private WriteContext context;
67 private MappingContext mappingContext;
70 private Mapping mapping;
72 private ArgumentCaptor<IpNeighborAddDel> requestCaptor;
73 private Ipv4NeighbourCustomizer customizer;
74 private NamingContext namingContext;
79 MockitoAnnotations.initMocks(this);
81 namingContext = new NamingContext("prefix", "instance");
82 namingContext.addName(5, "parent", mappingContext);
84 customizer = new Ipv4NeighbourCustomizer(jvpp,namingContext);
86 requestCaptor = ArgumentCaptor.forClass(IpNeighborAddDel.class);
88 CompletableFuture<IpNeighborAddDelReply> future = new CompletableFuture<>();
89 future.complete(new IpNeighborAddDelReply());
91 when(context.getMappingContext()).thenReturn(mappingContext);
92 when(mapping.getIndex()).thenReturn(5);
93 when(mapping.getName()).thenReturn("parent");
94 when(mappingContext.read(Mockito.any())).thenReturn(Optional.fromNullable(mapping));
95 when(jvpp.ipNeighborAddDel(Mockito.any(IpNeighborAddDel.class))).thenReturn(future);
99 public void testWriteCurrentAttributes() throws WriteFailedException {
101 InterfaceKey intfKey = new InterfaceKey("parent");
103 InstanceIdentifier<Neighbor> id = InstanceIdentifier.builder(Interfaces.class).child(Interface.class, intfKey)
104 .augmentation(Interface1.class).child(Ipv4.class).child(Neighbor.class).build();
106 Ipv4AddressNoZone noZoneIp = new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"));
107 PhysAddress mac = new PhysAddress("aa:bb:cc:ee:11:22");
109 Neighbor data = new NeighborBuilder().setIp(noZoneIp).setLinkLayerAddress(mac).build();
111 customizer.writeCurrentAttributes(id, data, context);
113 verify(jvpp, times(1)).ipNeighborAddDel(requestCaptor.capture());
115 IpNeighborAddDel request = requestCaptor.getValue();
117 assertEquals(0, request.isIpv6);
118 assertEquals(1, request.isAdd);
119 assertEquals(1, request.isStatic);
120 assertEquals("1.2.168.192", TranslateUtils.arrayToIpv4AddressNoZone(request.dstAddress).getValue());
121 assertEquals("aabbccee1122", BaseEncoding.base16().lowerCase().encode(request.macAddress));
122 assertEquals(5, request.swIfIndex);
126 public void testDeleteCurrentAttributes() throws WriteFailedException {
127 InterfaceKey intfKey = new InterfaceKey("parent");
129 InstanceIdentifier<Neighbor> id = InstanceIdentifier.builder(Interfaces.class).child(Interface.class, intfKey)
130 .augmentation(Interface1.class).child(Ipv4.class).child(Neighbor.class).build();
132 Ipv4AddressNoZone noZoneIp = new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"));
133 PhysAddress mac = new PhysAddress("aa:bb:cc:ee:11:22");
135 Neighbor data = new NeighborBuilder().setIp(noZoneIp).setLinkLayerAddress(mac).build();
137 customizer.deleteCurrentAttributes(id, data, context);
139 verify(jvpp, times(1)).ipNeighborAddDel(requestCaptor.capture());
141 IpNeighborAddDel request = requestCaptor.getValue();
143 assertEquals(0, request.isIpv6);
144 assertEquals(0, request.isAdd);
145 assertEquals(1, request.isStatic);
146 assertEquals("1.2.168.192", TranslateUtils.arrayToIpv4AddressNoZone(request.dstAddress).getValue());
147 assertEquals("aabbccee1122", BaseEncoding.base16().lowerCase().encode(request.macAddress));
148 assertEquals(5, request.swIfIndex);
152 public void testExtract() {
153 Neighbor data = new NeighborBuilder().build();
154 Ipv4 parentData = new Ipv4Builder().setNeighbor(Collections.singletonList(data)).build();
156 Optional<List<Neighbor>> optionalData = new Ipv4NeighbourCustomizer(mock(FutureJVpp.class), null).extract(null,
158 assertEquals(true, optionalData.isPresent());
159 assertEquals(true, optionalData.get().contains(data));