29f0ae6b9e0cc997903d090039366dd100b82a7d
[bgpcep.git] / pcep / ietf-stateful07 / src / test / java / org / opendaylight / protocol / pcep / ietf / PCEPObjectParserTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. 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.protocol.pcep.ietf;
9
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;
14
15 import java.io.IOException;
16
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;
52
53 public class PCEPObjectParserTest {
54
55     private TlvRegistry tlvRegistry;
56
57     @Before
58     public void setUp() throws Exception {
59         this.tlvRegistry = ServiceLoaderPCEPExtensionProviderContext.create().getTlvHandlerRegistry();
60     }
61
62     @Test
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"));
66
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);
74
75         final Stateful tlv1 = new StatefulBuilder().setLspUpdateCapability(Boolean.TRUE).build();
76
77         final Tlvs1Builder statBuilder = new Tlvs1Builder();
78         statBuilder.setStateful(tlv1);
79
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());
82
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));
87     }
88
89     @Test
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"));
93
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));
104
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));
114     }
115
116     @Test
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"));
121
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();
127
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());
138
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,
143         // 4)));
144         ByteBuf buf = Unpooled.buffer();
145         parser.serializeObject(builder.build(), buf);
146         assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
147     }
148
149     @Test
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 });
154
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());
160
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));
165     }
166 }