Promote MessageRegistry to pcep-api
[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 package org.opendaylight.protocol.pcep.segment.routing;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12
13 import com.google.common.collect.Lists;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import java.util.ArrayList;
17 import java.util.List;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
21 import org.opendaylight.protocol.pcep.parser.object.PCEPExplicitRouteObjectParser;
22 import org.opendaylight.protocol.pcep.spi.ObjectHeaderImpl;
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.IpAddressNoZone;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev200720.Tlvs3Builder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev200720.NaiType;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev200720.Tlvs1Builder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev200720.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.rev200720.sr.pce.capability.tlv.SrPceCapabilityBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev200720.sr.subobject.nai.IpNodeIdBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ProtocolVersion;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit.route.object.EroBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit.route.object.ero.Subobject;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit.route.object.ero.SubobjectBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.open.object.OpenBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.open.object.open.TlvsBuilder;
41 import org.opendaylight.yangtools.yang.common.Uint32;
42 import org.opendaylight.yangtools.yang.common.Uint8;
43
44 public class SrObjectParserTest {
45
46     private static final byte[] OPEN_OBJECT_BYTES = {
47         0x01,0x10,0x00,0x10,
48         0x20,0x1e,0x78,0x01,
49         /* sr-capability-tlv */
50         0x00,0x1a,0x00,0x04,
51         0x00,0x00,0x03,0x01};
52
53     private static final byte[] SR_ERO_OBJECT_BYTES = {
54         0x07,0x10,0x00,0x10,
55         /* ero-subobject */
56         0x24,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         ctx = new SimplePCEPExtensionProviderContext();
70         act = new SegmentRoutingActivator();
71         act.start(ctx);
72         tlvRegistry = ctx.getTlvHandlerRegistry();
73         viTlvRegistry = ctx.getVendorInformationTlvRegistry();
74     }
75
76     @Test
77     public void testOpenObjectWithSpcTlv() throws PCEPDeserializerException {
78         final PcepOpenObjectWithSpcTlvParser parser = new PcepOpenObjectWithSpcTlvParser(tlvRegistry,
79             viTlvRegistry);
80
81         final OpenBuilder builder = new OpenBuilder()
82                 .setProcessingRule(false)
83                 .setIgnore(false)
84                 .setVersion(new ProtocolVersion(Uint8.ONE))
85                 .setKeepalive(Uint8.valueOf(30))
86                 .setDeadTimer(Uint8.valueOf(120))
87                 .setSessionId(Uint8.ONE);
88
89         builder.setTlvs(new TlvsBuilder()
90                 .addAugmentation(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful
91                     .rev200720.Tlvs1Builder().build())
92                 .addAugmentation(new Tlvs1Builder()
93                     .setSrPceCapability(new SrPceCapabilityBuilder().setNFlag(Boolean.TRUE).setXFlag(Boolean.TRUE)
94                         .setMsd(Uint8.ONE).build())
95                     .build())
96                 .addAugmentation(new Tlvs3Builder().build()).build());
97
98         final ByteBuf result = Unpooled.wrappedBuffer(OPEN_OBJECT_BYTES);
99         assertEquals(builder.build(),
100                 parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
101         final ByteBuf buffer = Unpooled.buffer();
102         parser.serializeObject(builder.build(), buffer);
103         parser.serializeTlvs(null, Unpooled.EMPTY_BUFFER);
104         parser.serializeTlvs(new TlvsBuilder().build(), Unpooled.EMPTY_BUFFER);
105         assertArrayEquals(OPEN_OBJECT_BYTES, ByteArray.getAllBytes(buffer));
106     }
107
108     @Test
109     public void testSrEroObjectWithSubobjects() throws PCEPDeserializerException {
110         final PCEPExplicitRouteObjectParser parser = new PCEPExplicitRouteObjectParser(
111             ctx.getEROSubobjectHandlerRegistry());
112
113         final EroBuilder builder = new EroBuilder();
114         builder.setProcessingRule(false);
115         builder.setIgnore(false);
116         final List<Subobject> subobjects = new ArrayList<>();
117
118         final SrEroTypeBuilder srEroSubBuilder = new SrEroTypeBuilder()
119                 .setCFlag(false)
120                 .setMFlag(false)
121                 .setNaiType(NaiType.Ipv4NodeId)
122                 .setSid(Uint32.valueOf(123456))
123                 .setNai(new IpNodeIdBuilder().setIpAddress(new IpAddressNoZone(
124                     new Ipv4AddressNoZone("74.125.43.99"))).build());
125         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(srEroSubBuilder.build())
126                 .setLoose(false);
127         subobjects.add(subobjBuilder.build());
128
129         builder.setSubobject(subobjects);
130
131         final ByteBuf result = Unpooled.wrappedBuffer(SR_ERO_OBJECT_BYTES);
132         assertEquals(builder.build(),
133                 parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
134         final ByteBuf buffer = Unpooled.buffer();
135         parser.serializeObject(builder.build(), buffer);
136         assertArrayEquals(SR_ERO_OBJECT_BYTES, ByteArray.getAllBytes(buffer));
137     }
138
139     @Test
140     public void testSrEroSerializerWithUpdateLspAugmentation() throws PCEPDeserializerException {
141         final PCEPExplicitRouteObjectParser parser = new PCEPExplicitRouteObjectParser(
142             ctx.getEROSubobjectHandlerRegistry());
143
144         final EroBuilder builder = new EroBuilder();
145         builder.setProcessingRule(false);
146         builder.setIgnore(false);
147
148         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev200720.update.lsp
149             .input.arguments.ero.subobject.subobject.type.SrEroTypeBuilder srEroSubBuilder =
150                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev200720
151                     .update.lsp.input.arguments.ero.subobject.subobject.type.SrEroTypeBuilder()
152                     .setCFlag(false)
153                     .setMFlag(false)
154                     .setNaiType(NaiType.Ipv4NodeId)
155                     .setSid(Uint32.valueOf(123456))
156                     .setNai(new IpNodeIdBuilder().setIpAddress(new IpAddressNoZone(
157                         new Ipv4AddressNoZone("74.125.43.99"))).build());
158         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(srEroSubBuilder.build())
159                 .setLoose(false);
160         builder.setSubobject(Lists.newArrayList(subobjBuilder.build()));
161
162         final ByteBuf buffer = Unpooled.buffer();
163         parser.serializeObject(builder.build(), buffer);
164         assertArrayEquals(SR_ERO_OBJECT_BYTES, ByteArray.getAllBytes(buffer));
165     }
166 }