e9acdbe88339ff96f11f16b571fd401492e06baa
[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.Matchers.any;
21 import static org.mockito.Mockito.times;
22 import static org.mockito.Mockito.verify;
23 import static org.mockito.Mockito.when;
24
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;
46
47 public class Ipv4NeighbourCustomizerTest extends WriterCustomizerTest implements Ipv4Translator {
48
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;
52
53     private ArgumentCaptor<IpNeighborAddDel> requestCaptor;
54     private Ipv4NeighbourCustomizer customizer;
55
56     @Before
57     public void init() {
58         defineMapping(mappingContext, IFACE_NAME, IFACE_ID, IFC_CTX_NAME);
59         customizer = new Ipv4NeighbourCustomizer(api, new NamingContext("prefix", IFC_CTX_NAME));
60
61         requestCaptor = ArgumentCaptor.forClass(IpNeighborAddDel.class);
62         when(api.ipNeighborAddDel(any())).thenReturn(future(new IpNeighborAddDelReply()));
63     }
64
65     @Test
66     public void testWriteCurrentAttributes() throws WriteFailedException {
67
68         InterfaceKey intfKey = new InterfaceKey(IFACE_NAME);
69
70         InstanceIdentifier<Neighbor> id = InstanceIdentifier.builder(Interfaces.class).child(Interface.class, intfKey)
71                 .augmentation(Interface1.class).child(Ipv4.class).child(Neighbor.class).build();
72
73         Ipv4AddressNoZone noZoneIp = new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"));
74         PhysAddress mac = new PhysAddress("aa:bb:cc:ee:11:22");
75
76         Neighbor data = new NeighborBuilder().setIp(noZoneIp).setLinkLayerAddress(mac).build();
77
78         customizer.writeCurrentAttributes(id, data, writeContext);
79
80         verify(api, times(1)).ipNeighborAddDel(requestCaptor.capture());
81
82         IpNeighborAddDel request = requestCaptor.getValue();
83
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);
90     }
91
92     @Test
93     public void testDeleteCurrentAttributes() throws WriteFailedException {
94         InterfaceKey intfKey = new InterfaceKey(IFACE_NAME);
95
96         InstanceIdentifier<Neighbor> id = InstanceIdentifier.builder(Interfaces.class).child(Interface.class, intfKey)
97                 .augmentation(Interface1.class).child(Ipv4.class).child(Neighbor.class).build();
98
99         Ipv4AddressNoZone noZoneIp = new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"));
100         PhysAddress mac = new PhysAddress("aa:bb:cc:ee:11:22");
101
102         Neighbor data = new NeighborBuilder().setIp(noZoneIp).setLinkLayerAddress(mac).build();
103
104         customizer.deleteCurrentAttributes(id, data, writeContext);
105
106         verify(api, times(1)).ipNeighborAddDel(requestCaptor.capture());
107
108         IpNeighborAddDel request = requestCaptor.getValue();
109
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);
116     }
117
118 }