35cd57bc274cc75e9d6b734814c7b3676a0d1dc3
[bgpcep.git] / pcep / auto-bandwidth-extension / src / test / java / org / opendaylight / protocol / pcep / auto / bandwidth / extension / PcRptMessageCodecTest.java
1 /*
2  * Copyright (c) 2016 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.auto.bandwidth.extension;
10
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13
14 import com.google.common.collect.Lists;
15 import io.netty.buffer.ByteBuf;
16 import io.netty.buffer.Unpooled;
17 import java.util.Collections;
18 import java.util.List;
19 import javax.xml.bind.DatatypeConverter;
20 import org.junit.After;
21 import org.junit.Assert;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.protocol.pcep.ietf.stateful07.StatefulActivator;
25 import org.opendaylight.protocol.pcep.impl.Activator;
26 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
27 import org.opendaylight.protocol.pcep.spi.pojo.SimplePCEPExtensionProviderContext;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.auto.bandwidth.rev160109.Bandwidth1;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.auto.bandwidth.rev160109.Bandwidth1Builder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.auto.bandwidth.rev160109.bandwidth.usage.object.BandwidthUsage;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.auto.bandwidth.rev160109.bandwidth.usage.object.BandwidthUsageBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Pcrpt;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.Lsp;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.LspBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.pcrpt.message.pcrpt.message.Reports;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.bandwidth.object.BandwidthBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.Ero;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.EroBuilder;
42
43 public class PcRptMessageCodecTest {
44
45     private SimplePCEPExtensionProviderContext ctx;
46     private Activator act;
47     private StatefulActivator statefulAct;
48     private org.opendaylight.protocol.pcep.auto.bandwidth.extension.Activator autoBwActivator;
49
50     @Before
51     public void setUp() {
52         this.ctx = new SimplePCEPExtensionProviderContext();
53         this.act = new Activator();
54         this.act.start(this.ctx);
55         this.statefulAct = new StatefulActivator();
56         this.statefulAct.start(this.ctx);
57         this.autoBwActivator = new org.opendaylight.protocol.pcep.auto.bandwidth.extension.Activator(5);
58         this.autoBwActivator.start(this.ctx);
59     }
60
61     @After
62     public void tearDown() {
63         this.act.stop();
64         this.statefulAct.stop();
65         this.autoBwActivator.stop();
66     }
67
68     @Test
69     public void testGetValidReportsPositive() {
70         final PcRptMessageCodec codec = new PcRptMessageCodec(this.ctx.getObjectHandlerRegistry());
71         final BandwidthUsage bw = new BandwidthUsageBuilder().setBwSample(Lists.newArrayList(new Bandwidth(new byte[] {0, 0, 0, 1}))).build();
72         final Lsp lsp = new LspBuilder().build();
73         final Ero ero = new EroBuilder().build();
74         final List<Object> objects = Lists.<Object>newArrayList(lsp, ero, bw);
75         final Reports validReports = codec.getValidReports(objects, Collections.<Message>emptyList());
76         assertNotNull(validReports.getPath().getBandwidth().getAugmentation(Bandwidth1.class));
77         assertTrue(objects.isEmpty());
78     }
79
80     @Test
81     public void testserializeObject() {
82         final PcRptMessageCodec codec = new PcRptMessageCodec(this.ctx.getObjectHandlerRegistry());
83         final BandwidthBuilder bwBuilder = new BandwidthBuilder();
84         bwBuilder.addAugmentation(Bandwidth1.class, new Bandwidth1Builder().setBwSample(
85                 Lists.newArrayList(new Bandwidth(new byte[] {0, 0, 0, 1}))).build());
86         final ByteBuf buffer = Unpooled.buffer();
87         codec.serializeObject(bwBuilder.build(), buffer);
88         Assert.assertTrue(buffer.readableBytes() > 0);
89     }
90
91     @Test
92     public void testReportMsgWithRro() throws PCEPDeserializerException {
93         final byte[] parseHexBinary = DatatypeConverter.parseHexBinary("2010003c0084a019001100106e79636e7932316372735f7432313231001200100a0000d2004008490a0000d40a0000d4001f0006000005dd700000000710001401080a000706200001080a0000d420000910001400000000000000000000000005050100051000084998968005500008513a43b70810002401080a0000d42020030801010000000001080a00070620000308010100000000");
94         final Pcrpt msg = (Pcrpt) this.ctx.getMessageHandlerRegistry().parseMessage(10, Unpooled.wrappedBuffer(parseHexBinary), Collections.<Message>emptyList());
95         Assert.assertNotNull(msg.getPcrptMessage().getReports().get(0).getPath().getBandwidth().getAugmentation(Bandwidth1.class));
96     }
97
98 }