HONEYCOMB-58 - Routing Api
[honeycomb.git] / v3po / v3po2vpp / src / test / java / io / fd / honeycomb / translate / v3po / interfaces / ip / Ipv4NeighbourCustomizerTest.java
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 junit.framework.TestCase.assertTrue;
20 import static org.junit.Assert.fail;
21 import static org.mockito.Matchers.any;
22 import static org.mockito.Mockito.verify;
23 import static org.mockito.Mockito.when;
24 import io.fd.honeycomb.translate.vpp.util.Ipv4Translator;
25 import io.fd.honeycomb.translate.vpp.util.NamingContext;
26 import io.fd.honeycomb.translate.write.WriteFailedException;
27 import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest;
28 import org.junit.Test;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.Interface1;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.Ipv4;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.Neighbor;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.NeighborBuilder;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import io.fd.vpp.jvpp.VppBaseCallException;
41 import io.fd.vpp.jvpp.core.dto.IpNeighborAddDel;
42 import io.fd.vpp.jvpp.core.dto.IpNeighborAddDelReply;
43
44 public class Ipv4NeighbourCustomizerTest extends WriterCustomizerTest implements Ipv4Translator {
45
46     private static final String IFC_CTX_NAME = "ifc-test-instance";
47     private static final String IFACE_NAME = "parent";
48     private static final int IFACE_ID = 5;
49     private static final InstanceIdentifier<Neighbor> IID =
50         InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(IFACE_NAME))
51             .augmentation(Interface1.class).child(Ipv4.class).child(Neighbor.class);
52
53     private Ipv4NeighbourCustomizer customizer;
54
55     @Override
56     public void setUp() {
57         defineMapping(mappingContext, IFACE_NAME, IFACE_ID, IFC_CTX_NAME);
58         customizer = new Ipv4NeighbourCustomizer(api, new NamingContext("prefix", IFC_CTX_NAME));
59     }
60
61     @Test
62     public void testWriteCurrentAttributes() throws WriteFailedException {
63         when(api.ipNeighborAddDel(any())).thenReturn(future(new IpNeighborAddDelReply()));
64         customizer.writeCurrentAttributes(IID, getData(), writeContext);
65         verify(api).ipNeighborAddDel(getExpectedRequest(true));
66     }
67
68     @Test
69     public void testWriteCurrentAttributesFailed() {
70         when(api.ipNeighborAddDel(any())).thenReturn(failedFuture());
71         try {
72             customizer.writeCurrentAttributes(IID, getData(), writeContext);
73         } catch (WriteFailedException e) {
74             assertTrue(e.getCause() instanceof VppBaseCallException);
75             verify(api).ipNeighborAddDel(getExpectedRequest(true));
76             return;
77         }
78         fail("WriteFailedException expected");
79     }
80     @Test(expected = UnsupportedOperationException.class)
81     public void testUpdateCurrentAttributes() throws WriteFailedException {
82         customizer.updateCurrentAttributes(IID, getData(), getData(), writeContext);
83     }
84
85     @Test
86     public void testDeleteCurrentAttributes() throws WriteFailedException {
87         when(api.ipNeighborAddDel(any())).thenReturn(future(new IpNeighborAddDelReply()));
88         customizer.deleteCurrentAttributes(IID, getData(), writeContext);
89         verify(api).ipNeighborAddDel(getExpectedRequest(false));
90     }
91
92     @Test
93     public void testDeleteCurrentAttributesFailed() {
94         when(api.ipNeighborAddDel(any())).thenReturn(failedFuture());
95         try {
96             customizer.deleteCurrentAttributes(IID, getData(), writeContext);
97         } catch (WriteFailedException e) {
98             assertTrue(e.getCause() instanceof VppBaseCallException);
99             verify(api).ipNeighborAddDel(getExpectedRequest(false));
100             return;
101         }
102         fail("WriteFailedException expected");
103     }
104
105     private Neighbor getData() {
106         final Ipv4AddressNoZone noZoneIp = new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"));
107         final PhysAddress mac = new PhysAddress("aa:bb:cc:ee:11:22");
108         return new NeighborBuilder().setIp(noZoneIp).setLinkLayerAddress(mac).build();
109     }
110     private IpNeighborAddDel getExpectedRequest(final boolean isAdd) {
111         final IpNeighborAddDel request = new IpNeighborAddDel();
112         request.isIpv6 = 0;
113         request.isAdd = booleanToByte(isAdd);
114         request.isStatic = 1;
115         request.dstAddress = new byte[] {(byte) 192, (byte) 168, 2, 1};
116         request.macAddress = new byte[] {(byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0xee, 0x11, 0x22};
117         request.swIfIndex = IFACE_ID;
118         return request;
119     }
120 }