Removed duplicated code from stateful02. Extended existing parsers.
[bgpcep.git] / pcep / ietf-stateful02 / 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
13 import java.io.IOException;
14
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.opendaylight.protocol.pcep.crabbe.initiated00.PCEPOpenObjectParser;
18 import org.opendaylight.protocol.pcep.ietf.stateful02.Stateful02LspaObjectParser;
19 import org.opendaylight.protocol.pcep.spi.ObjectHeaderImpl;
20 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
21 import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
22 import org.opendaylight.protocol.pcep.spi.pojo.ServiceLoaderPCEPExtensionProviderContext;
23 import org.opendaylight.protocol.util.ByteArray;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.Stateful1;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.Stateful1Builder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.Tlvs1;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.Tlvs1Builder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.lsp.cleanup.tlv.LspCleanupBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.Tlvs2;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.Tlvs2Builder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.stateful.capability.tlv.Stateful;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.stateful.capability.tlv.StatefulBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.symbolic.path.name.tlv.SymbolicPathName;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.symbolic.path.name.tlv.SymbolicPathNameBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ProtocolVersion;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lspa.object.Lspa;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lspa.object.LspaBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.AttributeFilter;
40
41 public class PCEPObjectParserTest {
42
43         private TlvHandlerRegistry tlvRegistry;
44
45         @Before
46         public void setUp() throws Exception {
47                 this.tlvRegistry = ServiceLoaderPCEPExtensionProviderContext.create().getTlvHandlerRegistry();
48         }
49
50         @Test
51         public void testOpenObjectWithTLV() throws PCEPDeserializerException, IOException {
52                 final PCEPOpenObjectParser parser = new PCEPOpenObjectParser(this.tlvRegistry);
53                 final byte[] result = ByteArray.fileToBytes("src/test/resources/PCEPOpenObject1.bin");
54
55                 final OpenBuilder builder = new OpenBuilder();
56                 builder.setProcessingRule(false);
57                 builder.setIgnore(false);
58                 builder.setVersion(new ProtocolVersion((short) 1));
59                 builder.setKeepalive((short) 30);
60                 builder.setDeadTimer((short) 120);
61                 builder.setSessionId((short) 1);
62
63                 final Stateful tlv1 = new StatefulBuilder().setLspUpdateCapability(Boolean.TRUE).setIncludeDbVersion(Boolean.FALSE).addAugmentation(
64                                 Stateful1.class, new Stateful1Builder().setInitiation(true).build()).build();
65
66                 final Tlvs2Builder statBuilder = new Tlvs2Builder();
67                 statBuilder.setStateful(tlv1);
68
69                 final Tlvs1Builder cleanupBuilder = new Tlvs1Builder();
70                 cleanupBuilder.setLspCleanup(new LspCleanupBuilder().setTimeout(180L).build());
71
72                 builder.setTlvs(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.TlvsBuilder().addAugmentation(
73                                 Tlvs2.class, statBuilder.build()).addAugmentation(Tlvs1.class, cleanupBuilder.build()).build());
74
75                 assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(false, false), ByteArray.cutBytes(result, 4)));
76                 assertArrayEquals(result, parser.serializeObject(builder.build()));
77         }
78
79         @Test
80         public void testLspaObjectWithTlv() throws IOException, PCEPDeserializerException {
81                 final Stateful02LspaObjectParser parser = new Stateful02LspaObjectParser(this.tlvRegistry);
82                 final byte[] result = ByteArray.fileToBytes("src/test/resources/PCEPLspaObject1LowerBounds.bin");
83
84                 final LspaBuilder builder = new LspaBuilder();
85                 builder.setIgnore(false);
86                 builder.setProcessingRule(false);
87                 builder.setIncludeAny(new AttributeFilter(0l));
88                 builder.setExcludeAny(new AttributeFilter(0l));
89                 builder.setIncludeAll(new AttributeFilter(0l));
90                 builder.setSetupPriority((short) 0);
91                 builder.setHoldPriority((short) 0);
92                 builder.setLocalProtectionDesired(false);
93
94                 final SymbolicPathName tlv = new SymbolicPathNameBuilder().setPathName(
95                                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.SymbolicPathName(new byte[] {
96                                                 (byte) 0x4d, (byte) 0x65, (byte) 0x64, (byte) 0x20, (byte) 0x74, (byte) 0x65, (byte) 0x73, (byte) 0x74,
97                                                 (byte) 0x20, (byte) 0x6f, (byte) 0x66, (byte) 0x20, (byte) 0x73, (byte) 0x79, (byte) 0x6d, (byte) 0x62,
98                                                 (byte) 0x6f, (byte) 0x6c, (byte) 0x69, (byte) 0x63, (byte) 0x20, (byte) 0x6e, (byte) 0x61, (byte) 0x6d, (byte) 0x65 })).build();
99
100                 builder.setTlvs(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lspa.object.lspa.TlvsBuilder().addAugmentation(
101                                 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.Tlvs2.class,
102                                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.Tlvs2Builder().setSymbolicPathName(
103                                                 tlv).build()).build());
104                 // Tlvs container does not contain toString
105                 final Object o = parser.parseObject(new ObjectHeaderImpl(true, true), ByteArray.cutBytes(result, 4));
106                 assertEquals(
107                                 tlv,
108                                 ((Lspa) o).getTlvs().getAugmentation(
109                                                 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.Tlvs2.class).getSymbolicPathName());
110                 // assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), ByteArray.cutBytes(result,
111                 // 4)));
112                 assertArrayEquals(result, parser.serializeObject(builder.build()));
113         }
114
115         @Test
116         public void testLspObjectWithTLV() throws IOException, PCEPDeserializerException {
117                 // final PCEPLspObjectParser parser = new PCEPLspObjectParser(this.tlvRegistry);
118                 // final byte[] result = ByteArray.fileToBytes("src/test/resources/PCEPLspObject1WithTLV.bin");
119                 //
120                 // final LspBuilder builder = new LspBuilder();
121                 // builder.setProcessingRule(true);
122                 // builder.setIgnore(true);
123                 // builder.setDelegate(false);
124                 // builder.setRemove(true);
125                 // builder.setSync(false);
126                 //
127                 // final LspErrorCode tlv1 = new LspErrorCodeBuilder().setErrorCode(627610883L).build();
128                 // final SymbolicPathName tlv2 = new SymbolicPathNameBuilder().setPathName(
129                 // new
130                 // org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.SymbolicPathName("Med".getBytes())).build();
131                 // builder.setTlvs(new
132                 // org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.lsp.TlvsBuilder().setLspErrorCode(
133                 // tlv1).setSymbolicPathName(tlv2).build());
134                 // assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), ByteArray.cutBytes(result,
135                 // 4)));
136                 // assertArrayEquals(result, parser.serializeObject(builder.build()));
137         }
138 }