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