Reconcile packet.yang definition of fields
[l2switch.git] / packethandler / implementation / src / test / java / org / opendaylight / l2switch / packethandler / decoders / IcmpDecoderTest.java
1 /*
2  * Copyright (c) 2016 Hewlett Packard Enterprise, Co. 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.l2switch.packethandler.decoders;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import org.junit.Test;
16 import org.mockito.Mockito;
17 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
18 import org.opendaylight.mdsal.binding.api.NotificationService;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.basepacket.rev140528.packet.chain.grp.PacketChain;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.basepacket.rev140528.packet.chain.grp.PacketChainBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.basepacket.rev140528.packet.chain.grp.packet.chain.packet.RawPacketBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.ethernet.rev140528.ethernet.packet.received.packet.chain.packet.EthernetPacketBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.icmp.rev140528.IcmpPacketReceived;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.icmp.rev140528.icmp.packet.received.packet.chain.packet.IcmpPacket;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.ipv4.rev140528.Ipv4PacketReceivedBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.ipv4.rev140528.ipv4.packet.received.packet.chain.packet.Ipv4PacketBuilder;
27 import org.opendaylight.yangtools.yang.common.Uint32;
28
29 public class IcmpDecoderTest {
30     @Test
31     public void testDecode() throws Exception {
32         byte[] ethPayload = { 0x00, 0x0c, (byte) 0xce, 0x13, (byte) 0xb9, (byte) 0xa0, 0x00, 0x22, 0x5f, 0x3f,
33             (byte) 0x98, (byte) 0x91, 0x08, 0x00, // ethernet
34             0x45, 0x00, 0x00, 0x3c, (byte) 0xc6, 0x3e, 0x00, 0x00, (byte) 0x80, 0x01, (byte) 0xf2, (byte) 0xd7,
35             (byte) 0xc0, (byte) 0xa8, 0x00,
36             0x59, (byte) 0xc0, (byte) 0xa8, 0x00, 0x01, // ipv4
37             0x08, // Type = 8 (Echo request)
38             0x00, // Code = 0
39             0x42, // Checksum (+ next byte)
40             0x5c, //
41             0x02, // Identifier (+ next byte)
42             0x00, //
43             0x09, // Sequence number (+ next byte)
44             0x00, //
45             0, 0, 0, 0 // CRC
46         };
47
48         NotificationPublishService npServiceMock = Mockito.mock(NotificationPublishService.class);
49         NotificationService mock2 = Mockito.mock(NotificationService.class);
50         ArrayList<PacketChain> packetChainList = new ArrayList<>();
51         packetChainList.add(new PacketChainBuilder().setPacket(new RawPacketBuilder().build()).build());
52         packetChainList.add(new PacketChainBuilder().setPacket(new EthernetPacketBuilder().build()).build());
53         packetChainList.add(new PacketChainBuilder()
54             .setPacket(new Ipv4PacketBuilder()
55                     .setPayloadOffset(Uint32.valueOf(34))
56                     .build())
57             .build());
58
59         IcmpPacketReceived notification = new IcmpDecoder(npServiceMock, mock2)
60                 .decode(new Ipv4PacketReceivedBuilder().setPacketChain(packetChainList).setPayload(ethPayload)
61                         .build());
62
63         IcmpPacket icmpPacket = (IcmpPacket) notification.getPacketChain().get(3).getPacket();
64         assertEquals(8, icmpPacket.getType().intValue());
65         assertEquals(0, icmpPacket.getCode().intValue());
66         assertEquals(0x425c, icmpPacket.getCrc().intValue());
67         assertEquals(512, icmpPacket.getIdentifier().intValue());
68         assertEquals(2304, icmpPacket.getSequenceNumber().intValue());
69
70         assertTrue(Arrays.equals(ethPayload, notification.getPayload()));
71     }
72 }