2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.protocol.bgp.parser.spi.pojo;
11 import static org.mockito.ArgumentMatchers.any;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.mock;
15 import com.google.common.collect.Lists;
16 import io.netty.buffer.ByteBuf;
17 import java.util.List;
18 import org.junit.Assert;
19 import org.mockito.Mock;
20 import org.mockito.Mockito;
21 import org.mockito.MockitoAnnotations;
22 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
23 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
24 import org.opendaylight.protocol.bgp.parser.BGPTreatAsWithdrawException;
25 import org.opendaylight.protocol.bgp.parser.spi.AbstractBGPExtensionProviderActivator;
26 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
27 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
28 import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext;
29 import org.opendaylight.protocol.bgp.parser.spi.BgpPrefixSidTlvParser;
30 import org.opendaylight.protocol.bgp.parser.spi.BgpPrefixSidTlvSerializer;
31 import org.opendaylight.protocol.bgp.parser.spi.CapabilityParser;
32 import org.opendaylight.protocol.bgp.parser.spi.CapabilitySerializer;
33 import org.opendaylight.protocol.bgp.parser.spi.MessageParser;
34 import org.opendaylight.protocol.bgp.parser.spi.MessageSerializer;
35 import org.opendaylight.protocol.bgp.parser.spi.NextHopParserSerializer;
36 import org.opendaylight.protocol.bgp.parser.spi.NlriParser;
37 import org.opendaylight.protocol.bgp.parser.spi.NlriSerializer;
38 import org.opendaylight.protocol.bgp.parser.spi.ParameterParser;
39 import org.opendaylight.protocol.bgp.parser.spi.ParameterSerializer;
40 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
41 import org.opendaylight.protocol.bgp.parser.spi.RevisedErrorHandling;
42 import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunityParser;
43 import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunitySerializer;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.BgpParameters;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.bgp.parameters.optional.capabilities.CParameters;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.bgp.prefix.sid.bgp.prefix.sid.tlvs.BgpPrefixSidTlv;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlriBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpUnreachNlriBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv4AddressFamily;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv6AddressFamily;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.UnicastSubsequentAddressFamily;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.extended.community.RouteTargetIpv4Case;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.CNextHop;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.Ipv4NextHopCase;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.Ipv4NextHopCaseBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.Ipv6NextHopCase;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.ipv4.next.hop._case.Ipv4NextHopBuilder;
61 import org.opendaylight.yangtools.yang.binding.DataObject;
62 import org.opendaylight.yangtools.yang.binding.Notification;
64 public class BgpTestActivator extends AbstractBGPExtensionProviderActivator {
66 protected static final int TYPE = 0;
67 private static final String EMPTY = "";
70 protected AttributeSerializer attrSerializer;
72 protected AttributeParser attrParser;
75 protected ParameterParser paramParser;
77 protected ParameterSerializer paramSerializer;
80 protected CapabilityParser capaParser;
82 protected CapabilitySerializer capaSerializer;
85 protected MessageParser msgParser;
87 protected MessageSerializer msgSerializer;
90 protected NlriParser nlriParser;
92 protected NlriSerializer nlriSerializer;
95 protected ExtendedCommunityParser exParser;
97 protected ExtendedCommunitySerializer exSerializer;
99 protected NextHopParserSerializer nextHopParserSerializer;
102 protected BgpPrefixSidTlvParser sidTlvParser;
104 protected BgpPrefixSidTlvSerializer sidTlvSerializer;
107 protected List<AutoCloseable> startImpl(final BGPExtensionProviderContext context) {
109 final List<AutoCloseable> regs = Lists.newArrayList();
110 regs.add(context.registerAttributeParser(TYPE, this.attrParser));
111 regs.add(context.registerAttributeSerializer(DataObject.class, this.attrSerializer));
113 regs.add(context.registerParameterParser(TYPE, this.paramParser));
114 regs.add(context.registerParameterSerializer(BgpParameters.class, this.paramSerializer));
116 regs.add(context.registerCapabilityParser(TYPE, this.capaParser));
117 regs.add(context.registerCapabilitySerializer(CParameters.class, this.capaSerializer));
119 regs.add(context.registerBgpPrefixSidTlvParser(TYPE, this.sidTlvParser));
120 regs.add(context.registerBgpPrefixSidTlvSerializer(BgpPrefixSidTlv.class, this.sidTlvSerializer));
122 regs.add(context.registerMessageParser(TYPE, this.msgParser));
123 regs.add(context.registerMessageSerializer(Notification.class, this.msgSerializer));
125 regs.add(context.registerAddressFamily(Ipv4AddressFamily.class, 1));
126 regs.add(context.registerAddressFamily(Ipv6AddressFamily.class, 2));
127 regs.add(context.registerSubsequentAddressFamily(UnicastSubsequentAddressFamily.class, 1));
129 this.nextHopParserSerializer = new NextHopParserSerializer() {
131 public CNextHop parseNextHop(final ByteBuf buffer) throws BGPParsingException {
132 return new Ipv4NextHopCaseBuilder().setIpv4NextHop(new Ipv4NextHopBuilder()
133 .setGlobal(new Ipv4Address("127.0.0.1")).build()).build();
137 public void serializeNextHop(final CNextHop cnextHop, final ByteBuf byteAggregator) {
138 final byte[] mpReachBytes = {
139 0x7f, 0x00, 0x00, 0x01
141 byteAggregator.writeBytes(mpReachBytes);
145 regs.add(context.registerNlriParser(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class, this
146 .nlriParser, this.nextHopParserSerializer, Ipv4NextHopCase.class));
147 regs.add(context.registerNlriParser(Ipv6AddressFamily.class, UnicastSubsequentAddressFamily.class, this
148 .nlriParser, this.nextHopParserSerializer, Ipv6NextHopCase.class));
149 regs.add(context.registerNlriSerializer(DataObject.class, this.nlriSerializer));
151 regs.add(context.registerExtendedCommunityParser(0, 0, this.exParser));
152 regs.add(context.registerExtendedCommunitySerializer(RouteTargetIpv4Case.class, this.exSerializer));
157 private void initMock() {
158 MockitoAnnotations.initMocks(this);
160 Mockito.doNothing().when(this.attrParser).parseAttribute(any(ByteBuf.class), any(AttributesBuilder.class),
161 any(RevisedErrorHandling.class), any(PeerSpecificParserConstraint.class));
162 doReturn(EMPTY).when(this.attrParser).toString();
163 Mockito.doNothing().when(this.attrSerializer).serializeAttribute(any(Attributes.class), any(ByteBuf.class));
164 doReturn(EMPTY).when(this.attrSerializer).toString();
166 doReturn(null).when(this.paramParser).parseParameter(any(ByteBuf.class));
167 doReturn(EMPTY).when(this.paramParser).toString();
168 Mockito.doNothing().when(this.paramSerializer).serializeParameter(any(BgpParameters.class),
170 doReturn(EMPTY).when(this.paramSerializer).toString();
172 doReturn(null).when(this.capaParser).parseCapability(any(ByteBuf.class));
173 doReturn(EMPTY).when(this.capaParser).toString();
174 Mockito.doNothing().when(this.capaSerializer).serializeCapability(any(CParameters.class),
176 doReturn(EMPTY).when(this.capaSerializer).toString();
178 doReturn(null).when(this.sidTlvParser).parseBgpPrefixSidTlv(any(ByteBuf.class));
179 doReturn(EMPTY).when(this.sidTlvParser).toString();
180 Mockito.doNothing().when(this.sidTlvSerializer).serializeBgpPrefixSidTlv(any(BgpPrefixSidTlv.class),
182 doReturn(EMPTY).when(this.sidTlvSerializer).toString();
183 doReturn(0).when(this.sidTlvSerializer).getType();
185 doReturn(mock(Notification.class)).when(this.msgParser).parseMessageBody(any(ByteBuf.class),
186 Mockito.anyInt(), any(PeerSpecificParserConstraint.class));
187 doReturn(EMPTY).when(this.msgParser).toString();
188 Mockito.doNothing().when(this.msgSerializer).serializeMessage(any(Notification.class),
190 doReturn(EMPTY).when(this.msgSerializer).toString();
192 Mockito.doNothing().when(this.nlriParser).parseNlri(any(ByteBuf.class), any(MpUnreachNlriBuilder.class),
194 Mockito.doNothing().when(this.nlriParser).parseNlri(any(ByteBuf.class), any(MpReachNlriBuilder.class),
196 doReturn(EMPTY).when(this.nlriParser).toString();
198 } catch (BGPDocumentedException | BGPParsingException | BGPTreatAsWithdrawException e) {