Migrate pcep-spi to OSGi DS
[bgpcep.git] / pcep / spi / src / test / java / org / opendaylight / protocol / pcep / spi / RegistryTest.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.spi;
9
10 import static org.mockito.ArgumentMatchers.any;
11 import static org.mockito.ArgumentMatchers.anyBoolean;
12 import static org.mockito.Mockito.doNothing;
13 import static org.mockito.Mockito.doReturn;
14
15 import io.netty.buffer.ByteBuf;
16 import io.netty.buffer.Unpooled;
17 import java.util.ArrayList;
18 import java.util.Collections;
19 import java.util.List;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.mockito.Mock;
24 import org.mockito.junit.MockitoJUnitRunner;
25 import org.opendaylight.protocol.pcep.spi.pojo.SimplePCEPExtensionProviderContext;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.iana.rev130816.EnterpriseNumber;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.Keepalive;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.KeepaliveBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Message;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Tlv;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.exclude.route.object.xro.Subobject;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.exclude.route.object.xro.SubobjectBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.of.list.tlv.OfList;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.of.list.tlv.OfListBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.open.object.OpenBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.rp.object.Rp;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.EnterpriseSpecificInformation;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.objects.VendorInformationObject;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.tlvs.VendorInformationTlv;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820._record.route.subobjects.subobject.type.IpPrefixCaseBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820._record.route.subobjects.subobject.type.LabelCase;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.AsNumberCase;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.AsNumberCaseBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.IpPrefixCase;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.LabelType;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.Type1LabelCase;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.Type1LabelCaseBuilder;
50 import org.opendaylight.yangtools.concepts.Registration;
51 import org.opendaylight.yangtools.yang.common.Uint32;
52
53 @RunWith(MockitoJUnitRunner.StrictStubs.class)
54 public class RegistryTest {
55
56     @Mock
57     XROSubobjectParser xroParser;
58     @Mock
59     XROSubobjectSerializer xroSerializer;
60
61     @Mock
62     RROSubobjectParser rroParser;
63     @Mock
64     RROSubobjectSerializer rroSerializer;
65
66     @Mock
67     EROSubobjectParser eroParser;
68     @Mock
69     EROSubobjectSerializer eroSerializer;
70
71     @Mock
72     TlvParser tlvParser;
73     @Mock
74     TlvSerializer tlvSerializer;
75
76     @Mock
77     ObjectParser objectParser;
78
79     @Mock
80     ObjectSerializer objectSerializer;
81
82     @Mock
83     MessageParser msgParser;
84     @Mock
85     MessageSerializer msgSerializer;
86
87     @Mock
88     LabelParser labelParser;
89     @Mock
90     LabelSerializer labelSerializer;
91
92     @Mock
93     VendorInformationObject esi;
94
95     @Mock
96     EnterpriseSpecificInformation es;
97
98     @Mock
99     VendorInformationTlv viTlv;
100
101     public final List<Registration> regs = new ArrayList<>();
102
103     final PCEPExtensionProviderContext ctx = new SimplePCEPExtensionProviderContext();
104
105     @SuppressWarnings("unchecked")
106     @Before
107     public void setUp() throws PCEPDeserializerException {
108         doReturn(null).when(this.xroParser).parseSubobject(any(ByteBuf.class), anyBoolean());
109         doNothing().when(this.xroSerializer).serializeSubobject(any(Subobject.class), any(ByteBuf.class));
110
111         doReturn(this.viTlv).when(this.tlvParser).parseTlv(any(ByteBuf.class));
112         doNothing().when(this.tlvSerializer).serializeTlv(any(Tlv.class), any(ByteBuf.class));
113
114         doReturn(5).when(this.objectParser).getObjectClass();
115         doReturn(1).when(this.objectParser).getObjectType();
116         doReturn(new OpenBuilder().build()).when(this.objectParser).parseObject(any(ObjectHeader.class),
117             any(ByteBuf.class));
118         doNothing().when(this.objectSerializer).serializeObject(any(Object.class), any(ByteBuf.class));
119
120         doReturn(null).when(this.msgParser).parseMessage(any(ByteBuf.class), any(List.class));
121         doNothing().when(this.msgSerializer).serializeMessage(any(Message.class), any(ByteBuf.class));
122
123         doNothing().when(this.labelSerializer).serializeLabel(anyBoolean(), anyBoolean(), any(LabelType.class),
124             any(ByteBuf.class));
125
126         doReturn(this.es).when(this.esi).getEnterpriseSpecificInformation();
127
128         doReturn(this.es).when(this.viTlv).getEnterpriseSpecificInformation();
129
130         doReturn(EnterpriseSpecificInformation.class).when(this.es).implementedInterface();
131     }
132
133     @Test
134     public void testRegistry() throws PCEPDeserializerException {
135         this.regs.add(this.ctx.registerXROSubobjectParser(2, this.xroParser));
136         this.regs.add(this.ctx.registerXROSubobjectSerializer(AsNumberCase.class, this.xroSerializer));
137
138         this.regs.add(this.ctx.registerRROSubobjectParser(3, this.rroParser));
139         this.regs.add(this.ctx.registerRROSubobjectSerializer(LabelCase.class, this.rroSerializer));
140
141         this.regs.add(this.ctx.registerEROSubobjectParser(4, this.eroParser));
142         this.regs.add(this.ctx.registerEROSubobjectSerializer(IpPrefixCase.class, this.eroSerializer));
143
144         this.regs.add(this.ctx.registerTlvParser(1, this.tlvParser));
145         this.regs.add(this.ctx.registerTlvSerializer(OfList.class, this.tlvSerializer));
146
147         this.regs.add(this.ctx.registerObjectParser(this.objectParser));
148         this.regs.add(this.ctx.registerObjectSerializer(Rp.class, this.objectSerializer));
149
150         this.regs.add(this.ctx.registerMessageParser(6, this.msgParser));
151         this.regs.add(this.ctx.registerMessageSerializer(Keepalive.class, this.msgSerializer));
152
153         this.regs.add(this.ctx.registerLabelParser(7, this.labelParser));
154         this.regs.add(this.ctx.registerLabelSerializer(Type1LabelCase.class, this.labelSerializer));
155
156         this.regs.add(this.ctx.registerVendorInformationObjectParser(new EnterpriseNumber(Uint32.TEN),
157             this.objectParser));
158         this.regs.add(this.ctx.registerVendorInformationObjectSerializer(EnterpriseSpecificInformation.class,
159             this.objectSerializer));
160
161         this.regs.add(this.ctx.registerVendorInformationTlvParser(new EnterpriseNumber(Uint32.valueOf(12)),
162             this.tlvParser));
163         this.regs.add(this.ctx.registerVendorInformationTlvSerializer(EnterpriseSpecificInformation.class,
164             this.tlvSerializer));
165
166         final ByteBuf buffer = Unpooled.buffer();
167         this.ctx.getXROSubobjectHandlerRegistry().parseSubobject(2, buffer, false);
168         this.ctx.getXROSubobjectHandlerRegistry().serializeSubobject(new SubobjectBuilder()
169             .setSubobjectType(new AsNumberCaseBuilder().build()).build(), buffer);
170
171         this.ctx.getEROSubobjectHandlerRegistry().parseSubobject(3, buffer, true);
172         this.ctx.getEROSubobjectHandlerRegistry().serializeSubobject(new org.opendaylight.yang.gen.v1.urn.opendaylight
173             .params.xml.ns.yang.pcep.types.rev181109.explicit.route.object.ero.SubobjectBuilder()
174             .setSubobjectType(new AsNumberCaseBuilder().build()).build(), buffer);
175
176         this.ctx.getRROSubobjectHandlerRegistry().parseSubobject(4, buffer);
177         this.ctx.getRROSubobjectHandlerRegistry().serializeSubobject(new org.opendaylight.yang.gen.v1.urn.opendaylight
178             .params.xml.ns.yang.pcep.types.rev181109.reported.route.object.rro.SubobjectBuilder()
179             .setSubobjectType(new IpPrefixCaseBuilder().build()).build(), buffer);
180
181         this.ctx.getLabelHandlerRegistry().parseLabel(1, buffer);
182         this.ctx.getLabelHandlerRegistry().serializeLabel(true, false, new Type1LabelCaseBuilder().build(), buffer);
183
184         this.ctx.getTlvHandlerRegistry().parseTlv(2, buffer);
185         this.ctx.getTlvHandlerRegistry().serializeTlv(new OfListBuilder().build(), buffer);
186
187         this.ctx.getObjectHandlerRegistry().parseObject(4, 1, new ObjectHeaderImpl(true, false), buffer);
188         this.ctx.getObjectHandlerRegistry().serializeObject(new OpenBuilder().build(), buffer);
189
190         this.ctx.getMessageHandlerRegistry().parseMessage(6, buffer, Collections.emptyList());
191         this.ctx.getMessageHandlerRegistry().serializeMessage(new KeepaliveBuilder().build(), buffer);
192
193         this.ctx.getVendorInformationObjectRegistry().parseVendorInformationObject(
194             new EnterpriseNumber(Uint32.TEN), new ObjectHeaderImpl(true, false), buffer);
195         this.ctx.getVendorInformationObjectRegistry().serializeVendorInformationObject(this.esi, buffer);
196
197         this.ctx.getVendorInformationTlvRegistry().parseVendorInformationTlv(
198             new EnterpriseNumber(Uint32.valueOf(12)), buffer);
199         this.ctx.getVendorInformationTlvRegistry().serializeVendorInformationTlv(this.viTlv, buffer);
200     }
201 }