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