Bug 2938: register serializers for all augmentations
[bgpcep.git] / pcep / segment-routing / src / test / java / org / opendaylight / protocol / pcep / segment / routing / SrObjectParserTest.java
1 /*
2  * Copyright (c) 2014 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
9 package org.opendaylight.protocol.pcep.segment.routing;
10
11 import static org.junit.Assert.assertArrayEquals;
12 import static org.junit.Assert.assertEquals;
13
14 import com.google.common.collect.Lists;
15 import io.netty.buffer.ByteBuf;
16 import io.netty.buffer.Unpooled;
17 import java.util.List;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.opendaylight.protocol.pcep.impl.object.PCEPExplicitRouteObjectParser;
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.SimplePCEPExtensionProviderContext;
26 import org.opendaylight.protocol.util.ByteArray;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev150112.SidType;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev150112.Tlvs1;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev150112.Tlvs1Builder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev150112.add.lsp.input.arguments.ero.subobject.subobject.type.SrEroTypeBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev150112.sr.pce.capability.tlv.SrPceCapabilityBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev150112.sr.subobject.nai.IpNodeIdBuilder;
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.explicit.route.object.EroBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.TlvsBuilder;
41
42 public class SrObjectParserTest {
43
44     private static final byte[] openObjectBytes = {
45         0x01,0x10,0x00,0x10,
46         0x20,0x1e,0x78,0x01,
47         /* sr-capability-tlv */
48         0x00,0x1a,0x00,0x04,
49         0x00,0x00,0x00,0x01};
50
51     private static final byte[] srEroObjectBytes = {
52         0x07,0x10,0x00,0x10,
53         /* ero-subobject */
54         0x05,0x0c,(byte) 0x10,0x00,
55         0x00,0x01,(byte)0xe2,0x40,
56         0x4A,0x7D,0x2b,0x63,
57     };
58
59     private TlvRegistry tlvRegistry;
60     private VendorInformationTlvRegistry viTlvRegistry;
61
62     private SimplePCEPExtensionProviderContext ctx;
63     private SegmentRoutingActivator act;
64
65     @Before
66     public void setUp() {
67         this.ctx = new SimplePCEPExtensionProviderContext();
68         this.act = new SegmentRoutingActivator();
69         this.act.start(this.ctx);
70         this.tlvRegistry = this.ctx.getTlvHandlerRegistry();
71         this.viTlvRegistry = this.ctx.getVendorInformationTlvRegistry();
72     }
73
74     @Test
75     public void testOpenObjectWithSpcTlv() throws PCEPDeserializerException {
76         final PcepOpenObjectWithSpcTlvParser parser = new PcepOpenObjectWithSpcTlvParser(this.tlvRegistry, this.viTlvRegistry);
77
78         final OpenBuilder builder = new OpenBuilder();
79         builder.setProcessingRule(false);
80         builder.setIgnore(false);
81         builder.setVersion(new ProtocolVersion((short) 1));
82         builder.setKeepalive((short) 30);
83         builder.setDeadTimer((short) 120);
84         builder.setSessionId((short) 1);
85
86         final Tlvs1 tlv = new Tlvs1Builder().setSrPceCapability(new SrPceCapabilityBuilder().setMsd((short) 1).build())
87                 .build();
88         builder.setTlvs(new TlvsBuilder().addAugmentation(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Tlvs1.class, new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Tlvs1Builder().build()).addAugmentation(Tlvs1.class, tlv).build());
89
90         final ByteBuf result = Unpooled.wrappedBuffer(openObjectBytes);
91         assertEquals(builder.build(),
92                 parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
93         final ByteBuf buffer = Unpooled.buffer();
94         parser.serializeObject(builder.build(), buffer);
95         assertArrayEquals(openObjectBytes, ByteArray.getAllBytes(buffer));
96     }
97
98     @Test
99     public void testSrEroObjectWithSubobjects() throws PCEPDeserializerException {
100         final PCEPExplicitRouteObjectParser parser = new PCEPExplicitRouteObjectParser(this.ctx.getEROSubobjectHandlerRegistry());
101
102         final EroBuilder builder = new EroBuilder();
103         builder.setProcessingRule(false);
104         builder.setIgnore(false);
105         final List<Subobject> subobjects = Lists.newArrayList();
106
107         final SrEroTypeBuilder srEroSubBuilder = new SrEroTypeBuilder();
108         srEroSubBuilder.setCFlag(false);
109         srEroSubBuilder.setMFlag(false);
110         srEroSubBuilder.setSidType(SidType.Ipv4NodeId);
111         srEroSubBuilder.setSid(123456L);
112         srEroSubBuilder.setNai(new IpNodeIdBuilder().setIpAddress(new IpAddress(new Ipv4Address("74.125.43.99"))).build());
113         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(srEroSubBuilder.build()).setLoose(false);
114         subobjects.add(subobjBuilder.build());
115
116         builder.setSubobject(subobjects);
117
118         final ByteBuf result = Unpooled.wrappedBuffer(srEroObjectBytes);
119         assertEquals(builder.build(),
120                 parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
121         final ByteBuf buffer = Unpooled.buffer();
122         parser.serializeObject(builder.build(), buffer);
123         assertArrayEquals(srEroObjectBytes, ByteArray.getAllBytes(buffer));
124     }
125
126     @Test
127     public void testSrEroSerializerWithUpdateLspAugmentation() throws PCEPDeserializerException {
128         final PCEPExplicitRouteObjectParser parser = new PCEPExplicitRouteObjectParser(this.ctx.getEROSubobjectHandlerRegistry());
129
130         final EroBuilder builder = new EroBuilder();
131         builder.setProcessingRule(false);
132         builder.setIgnore(false);
133
134         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev150112.update.lsp.input.arguments.ero.subobject.subobject.type.SrEroTypeBuilder srEroSubBuilder =
135                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev150112.update.lsp.input.arguments.ero.subobject.subobject.type.SrEroTypeBuilder();
136         srEroSubBuilder.setCFlag(false);
137         srEroSubBuilder.setMFlag(false);
138         srEroSubBuilder.setSidType(SidType.Ipv4NodeId);
139         srEroSubBuilder.setSid(123456L);
140         srEroSubBuilder.setNai(new IpNodeIdBuilder().setIpAddress(new IpAddress(new Ipv4Address("74.125.43.99"))).build());
141         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(srEroSubBuilder.build()).setLoose(false);
142         builder.setSubobject(Lists.newArrayList(subobjBuilder.build()));
143
144         final ByteBuf buffer = Unpooled.buffer();
145         parser.serializeObject(builder.build(), buffer);
146         assertArrayEquals(srEroObjectBytes, ByteArray.getAllBytes(buffer));
147     }
148
149 }