2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.pcep.impl;
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import java.util.ArrayList;
17 import java.util.List;
18 import org.junit.Test;
19 import org.opendaylight.protocol.pcep.MessageRegistry;
20 import org.opendaylight.protocol.pcep.spi.pojo.DefaultPCEPExtensionConsumerContext;
21 import org.opendaylight.protocol.util.ByteArray;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.Close;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.Keepalive;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.KeepaliveBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.Pcerr;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.keepalive.message.KeepaliveMessageBuilder;
28 public class PCEPParserTest {
30 private final MessageRegistry registry = new DefaultPCEPExtensionConsumerContext().getMessageHandlerRegistry();
33 public void testMessageToByteEncoding() {
34 PCEPMessageToByteEncoder encoder = new PCEPMessageToByteEncoder(this.registry);
35 ByteBuf out = Unpooled.buffer();
37 null, new KeepaliveBuilder().setKeepaliveMessage(new KeepaliveMessageBuilder().build()).build(), out);
38 assertArrayEquals(new byte[] { 32, 2, 0, 4 }, ByteArray.readAllBytes(out));
42 public void testByteToMessageEncoding() {
43 PCEPByteToMessageDecoder decoder = new PCEPByteToMessageDecoder(this.registry);
44 List<Object> out = new ArrayList<>();
46 Unpooled.wrappedBuffer(new byte[] {
50 assertTrue(out.get(0) instanceof Keepalive);
52 Unpooled.wrappedBuffer(new byte[] {
53 0x20, 0x07, 0, 0x0C, 0x0F, 0x10, 0, 8,
57 assertTrue(out.get(1) instanceof Close);
59 Unpooled.wrappedBuffer(new byte[] {
60 0x20, 06, 00, 0x18, 0x21, 0x10, 00, 0x0c,
61 00, 00, 00, 00, 00, 00, 00, 01,
62 0x0d, 0x10, 00, 0x08, 00, 00, 0x18, 02
65 assertTrue(out.get(2) instanceof Pcerr);
69 public void testHandlerFactory() {
70 PCEPHandlerFactory handlers = new PCEPHandlerFactory(this.registry);
71 assertEquals(1, handlers.getEncoders().length);
72 assertTrue(handlers.getEncoders()[0] instanceof PCEPMessageToByteEncoder);
73 assertEquals(2, handlers.getDecoders().length);
74 assertTrue(handlers.getDecoders()[0] instanceof PCEPMessageHeaderDecoder);
75 assertTrue(handlers.getDecoders()[1] instanceof PCEPByteToMessageDecoder);