377d6dce035d193d17b976657e14493bf66dd799
[genius.git] / arputil / arputil-impl / src / test / java / org / opendaylight / genius / arputil / test / ArpUtilTest.java
1 /*
2  * Copyright (c) 2017 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.genius.arputil.test;
9
10 import static org.opendaylight.genius.arputil.test.ArpUtilTestUtil.INTERFACE_NAME;
11
12 import java.util.Collections;
13 import java.util.List;
14 import java.util.concurrent.Future;
15 import javax.inject.Inject;
16 import org.junit.Assert;
17 import org.junit.Rule;
18 import org.junit.Test;
19 import org.junit.rules.MethodRule;
20 import org.opendaylight.genius.arputil.internal.ArpUtilImpl;
21 import org.opendaylight.infrautils.inject.guice.testutils.GuiceRule;
22 import org.opendaylight.infrautils.testutils.LogCaptureRule;
23 import org.opendaylight.infrautils.testutils.LogRule;
24 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractConcurrentDataBrokerTest;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.arputil.rev160406.GetMacInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.arputil.rev160406.GetMacInputBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.arputil.rev160406.GetMacOutput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.arputil.rev160406.SendArpResponseInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.arputil.rev160406.SendArpResponseInputBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.arputil.rev160406.interfaces.InterfaceAddress;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.arputil.rev160406.interfaces.InterfaceAddressBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
36 import org.opendaylight.yangtools.yang.common.RpcResult;
37
38 public class ArpUtilTest extends AbstractConcurrentDataBrokerTest {
39
40     public @Rule LogRule logRule = new LogRule();
41     public @Rule LogCaptureRule logCaptureRule = new LogCaptureRule();
42     public @Rule MethodRule guice = new GuiceRule(new ArpUtilTestModule());
43
44     @Inject ArpUtilImpl arpUtil;
45
46     @Test
47     public void testGetMac() throws Exception {
48         final InterfaceAddress interfaceAddress = new InterfaceAddressBuilder()
49                 .setInterface(INTERFACE_NAME)
50                 .setIpAddress(new IpAddress(Ipv4Address.getDefaultInstance("192.168.0.1")))
51                 .setMacaddress(new PhysAddress("1F:1F:1F:1F:1F:1F")).build();
52
53         final List<InterfaceAddress> itf = Collections.singletonList(interfaceAddress);
54
55         GetMacInput getMacInput = new GetMacInputBuilder()
56                 .setIpaddress(new IpAddress(Ipv4Address.getDefaultInstance("192.168.0.2")))
57                 .setInterfaceAddress(itf).build();
58
59         PacketReceived packetReceived = ArpUtilTestUtil.createPayload(0); //request payload
60
61         Future<RpcResult<GetMacOutput>> output = arpUtil.getMac(getMacInput);
62
63         arpUtil.onPacketReceived(packetReceived);
64
65         Assert.assertEquals("00:01:02:03:04:05", output.get().getResult().getMacaddress().getValue());
66     }
67
68     @Test
69     public void testSendArpResponse() throws Exception {
70         SendArpResponseInput builder = new SendArpResponseInputBuilder().setInterface(INTERFACE_NAME)
71                 .setSrcIpaddress(new IpAddress(Ipv4Address.getDefaultInstance("192.168.0.1")))
72                 .setDstIpaddress(new IpAddress(Ipv4Address.getDefaultInstance("192.168.0.2")))
73                 .setSrcMacaddress(new PhysAddress("1F:1F:1F:1F:1F:1F"))
74                 .setDstMacaddress(new PhysAddress("00:01:02:03:04:05")).build();
75
76         Assert.assertTrue(arpUtil.sendArpResponse(builder).get().isSuccessful());
77     }
78 }