2 * Copyright (c) 2013 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.ietf;
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
15 import java.io.IOException;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.opendaylight.protocol.pcep.ietf.initiated00.CInitiated00LspObjectParser;
20 import org.opendaylight.protocol.pcep.ietf.initiated00.CInitiated00SrpObjectParser;
21 import org.opendaylight.protocol.pcep.ietf.stateful07.Stateful07LspaObjectParser;
22 import org.opendaylight.protocol.pcep.ietf.stateful07.Stateful07OpenObjectParser;
23 import org.opendaylight.protocol.pcep.spi.ObjectHeaderImpl;
24 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
25 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
26 import org.opendaylight.protocol.pcep.spi.pojo.ServiceLoaderPCEPExtensionProviderContext;
27 import org.opendaylight.protocol.util.ByteArray;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev131126.Lsp1;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev131126.Lsp1Builder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev131126.Srp1;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev131126.Srp1Builder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.OperationalStatus;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.PlspId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.SrpIdNumber;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Tlvs1;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Tlvs1Builder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Tlvs2;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Tlvs2Builder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.error.code.tlv.LspErrorCode;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.error.code.tlv.LspErrorCodeBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.LspBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.SrpBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.stateful.capability.tlv.Stateful;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.stateful.capability.tlv.StatefulBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.symbolic.path.name.tlv.SymbolicPathName;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.symbolic.path.name.tlv.SymbolicPathNameBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ProtocolVersion;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lspa.object.Lspa;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lspa.object.LspaBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.AttributeFilter;
53 public class PCEPObjectParserTest {
55 private TlvRegistry tlvRegistry;
58 public void setUp() throws Exception {
59 this.tlvRegistry = ServiceLoaderPCEPExtensionProviderContext.create().getTlvHandlerRegistry();
63 public void testOpenObjectWithTLV() throws PCEPDeserializerException, IOException {
64 final Stateful07OpenObjectParser parser = new Stateful07OpenObjectParser(this.tlvRegistry);
65 final ByteBuf result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPOpenObject1.bin"));
67 final OpenBuilder builder = new OpenBuilder();
68 builder.setProcessingRule(false);
69 builder.setIgnore(false);
70 builder.setVersion(new ProtocolVersion((short) 1));
71 builder.setKeepalive((short) 30);
72 builder.setDeadTimer((short) 120);
73 builder.setSessionId((short) 1);
75 final Stateful tlv1 = new StatefulBuilder().setLspUpdateCapability(Boolean.TRUE).build();
77 final Tlvs1Builder statBuilder = new Tlvs1Builder();
78 statBuilder.setStateful(tlv1);
80 builder.setTlvs(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.TlvsBuilder().addAugmentation(
81 Tlvs1.class, statBuilder.build()).build());
83 assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
84 ByteBuf buf = Unpooled.buffer();
85 parser.serializeObject(builder.build(), buf);
86 assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
90 public void testLspObjectWithTLV() throws IOException, PCEPDeserializerException {
91 final CInitiated00LspObjectParser parser = new CInitiated00LspObjectParser(this.tlvRegistry);
92 final ByteBuf result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPLspObject1WithTLV.bin"));
94 final LspBuilder builder = new LspBuilder();
95 builder.setProcessingRule(true);
96 builder.setIgnore(true);
97 builder.setAdministrative(true);
98 builder.setDelegate(false);
99 builder.setRemove(true);
100 builder.setSync(false);
101 builder.addAugmentation(Lsp1.class, new Lsp1Builder().setCreate(false).build());
102 builder.setOperational(OperationalStatus.GoingDown);
103 builder.setPlspId(new PlspId(0x12345L));
105 final LspErrorCode tlv1 = new LspErrorCodeBuilder().setErrorCode(627610883L).build();
106 final SymbolicPathName tlv2 = new SymbolicPathNameBuilder().setPathName(
107 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.SymbolicPathName("Med".getBytes())).build();
108 builder.setTlvs(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.lsp.TlvsBuilder().setLspErrorCode(
109 tlv1).setSymbolicPathName(tlv2).build());
110 assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
111 ByteBuf buf = Unpooled.buffer();
112 parser.serializeObject(builder.build(), buf);
113 assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
117 public void testLspaObject() throws IOException, PCEPDeserializerException {
118 final Stateful07LspaObjectParser parser = new Stateful07LspaObjectParser(this.tlvRegistry);
119 final LspaBuilder builder = new LspaBuilder();
120 final ByteBuf result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPLspaObject3RandVals.bin"));
122 final SymbolicPathName tlv = new SymbolicPathNameBuilder().setPathName(
123 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.SymbolicPathName(new byte[] {
124 (byte) 0x4d, (byte) 0x65, (byte) 0x64, (byte) 0x20, (byte) 0x74, (byte) 0x65, (byte) 0x73, (byte) 0x74, (byte) 0x20,
125 (byte) 0x6f, (byte) 0x66, (byte) 0x20, (byte) 0x73, (byte) 0x79, (byte) 0x6d, (byte) 0x62, (byte) 0x6f, (byte) 0x6c,
126 (byte) 0x69, (byte) 0x63, (byte) 0x20, (byte) 0x6e, (byte) 0x61, (byte) 0x6d, (byte) 0x65 })).build();
128 builder.setIgnore(false);
129 builder.setProcessingRule(false);
130 builder.setExcludeAny(new AttributeFilter(0x20A1FEE3L));
131 builder.setIncludeAny(new AttributeFilter(0x1A025CC7L));
132 builder.setIncludeAll(new AttributeFilter(0x2BB66532L));
133 builder.setHoldPriority((short) 0x02);
134 builder.setSetupPriority((short) 0x03);
135 builder.setLocalProtectionDesired(true);
136 builder.setTlvs(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lspa.object.lspa.TlvsBuilder().addAugmentation(
137 Tlvs2.class, new Tlvs2Builder().setSymbolicPathName(tlv).build()).build());
139 // Tlvs container does not contain toString
140 final Object o = parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4));
141 assertEquals(tlv, ((Lspa) o).getTlvs().getAugmentation(Tlvs2.class).getSymbolicPathName());
142 // assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), ByteArray.cutBytes(result,
144 ByteBuf buf = Unpooled.buffer();
145 parser.serializeObject(builder.build(), buf);
146 assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
150 public void testSrpObject() throws IOException, PCEPDeserializerException {
151 final CInitiated00SrpObjectParser parser = new CInitiated00SrpObjectParser(this.tlvRegistry);
152 final ByteBuf result = Unpooled.wrappedBuffer(new byte[] { (byte) 0x21, (byte) 0x10, (byte) 0x00, (byte) 0x0c, 0, 0, 0,
153 (byte) 0x01, 0, 0, 0, (byte) 0x01 });
155 final SrpBuilder builder = new SrpBuilder();
156 builder.setProcessingRule(false);
157 builder.setIgnore(false);
158 builder.setOperationId(new SrpIdNumber(1L));
159 builder.addAugmentation(Srp1.class, new Srp1Builder().setRemove(true).build());
161 assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
162 ByteBuf buf = Unpooled.buffer();
163 parser.serializeObject(builder.build(), buf);
164 assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));